Web3js
事務在混音中工作,但沒有從 dapp 出錯
我有一個部署其他契約實例的主契約。
pragma solidity ^0.5.0; pragma experimental ABIEncoderV2; contract DataEntityContract { DataEntity DE; constructor(string memory _name,string[] memory _relatedEntities, string memory _date, string memory _fileName, string memory _annotations) public { DE = DataEntity(_name, 'URN', _fileName, new string[](0), _relatedEntities, 'references', _annotations, msg.sender, _date); } struct DataEntity { //Event notifications; string dataEntityName; string rawFileURN; string fileName; string[] nameChangeHistory; string[] sources; string references; string annotations; address createdUpdateBy; string when; } function editDE() public { DE.annotations = "edited"; } function getAnnotations() public view returns(string memory) { return (DE.annotations); } } contract FlattenedDEC { address owner; string public name; mapping (string => address) dataEntities; string[] public dataEntityNames; uint count = 0; uint public DataEntitiesCount = 0; mapping(uint => Lineage) public lineageInfo; mapping(uint => DE) public DataEntities; event DataEntityEdited( string name, string comments, address editedBy ); constructor() public { name = "DataEntityCatalogue Master Contract"; owner = msg.sender; } function createNewDataEntity(string memory _name, string[] memory _dataEntities, string memory _date, string memory _fileName, string memory _annotations) public returns(address _new){ DataEntityContract instance = new DataEntityContract(_name,_dataEntities, _date, _fileName, _annotations); return address(instance); } function editDE(string memory _name) public{ DataEntityContract de = DataEntityContract(dataEntities[_name]); de.editDE(); emit DataEntityEdited(_name, "edited", msg.sender); }
現在,當我嘗試從我的 dapp 呼叫 FlattenedDEC 甚至 DataEntityContract 中的 editDE 方法時,我得到了一個 jsonrpc 錯誤,但是當我從 remix 呼叫相同的方法時,一切正常。
這是我的反應js程式碼:
async editDataEntity(name){ const web3 = window.web3; const networkId = await web3.eth.net.getId(); const networkData = await FlattenedDEC.networks[networkId]; const contract = web3.eth.Contract(FlattenedDEC.abi, networkData.address); contract.methods.editDE().send({from: this.state.account}) .on('error',(error) => { console.log("error: ", error); }) .on('transactionHash', () => { console.log("edited"); }); }
這是我得到的錯誤:
MetaMask - RPC Error: Error: [ethjs-rpc] rpc error with payload {"id":6402969023659,"jsonrpc":"2.0","params": ["0xf9010e8203dc85746a528800836170d494ca9a565e46004c9e5f9b2ac2b4ca83d61814c20180b 8a4108770750000000000000060000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000064544495 445440000000000000000000000000000000000000000000000000000822d45a0186aa938713cc78d fe0089d896b0890ffa693ad77fe521d4d3e5e454d2fbe600a045413f6b3d94d1df175ba42e7790e99 9fe3a97a44c91451632abb02789e10612"], "method":"eth_sendRawTransaction"}
所以,
事實證明,每當發生此錯誤時,都是因為我們正在向合約發送一些奇怪的數據。因此,就我而言,我沒有發送正確的參數,而是發送了一個空名稱。這需要大量調試才能得出這個結論。這實際上為我解決了更多問題。
因此,資訊是每當有人收到此錯誤時,請檢查傳遞給合約方法的值。