Solidity

Solidity 函式的參數數量無效(嘗試檢索映射)

  • July 4, 2018

solidity 合約定義了以下映射

struct recordStruct {
   uint256 price;
   uint64 time;
}

mapping(address => recordStruct[]) public records;

**問題:**我們如何使用 Web3.js 來檢索整個映射?

如果這不可能,我們如何傳入address並接收儲存在recordStruct數組中的對象?

嘗試使用以下 web3 程式碼時:

var record = await MyContractInstance.records.call('0xDbB7d1Ed07F8D1D0C5D5bB9b9e427E997f240F7d')

我收到以下錯誤

(節點:2452)UnhandledPromiseRejectionWarning:錯誤:Solidity 函式的參數數量無效

我正在使用 Web3.js 1.0.0-beta.34

您需要指定數組的地址和索引,因為 ABI 將具有以下結構:

[
   {
       "constant": true,
       "inputs": [
           {
               "name": "",
               "type": "address"
           },
           {
               "name": "",
               "type": "uint256"
           }
       ],
       "name": "records",
       "outputs": [
           {
               "name": "price",
               "type": "uint256"
           },
           {
               "name": "time",
               "type": "uint64"
           }
       ],
       "payable": false,
       "stateMutability": "view",
       "type": "function"
   }
]

並考慮到 javascript 程式碼也不完全正確:

var record = await MyContractInstance.records('0xDbB7d1Ed07F8D1D0C5D5bB9b9e427E997f240F7d', 0).call({ from: ... })

或者

var record = await MyContractInstance.methods.records('0xDbB7d1Ed07F8D1D0C5D5bB9b9e427E997f240F7d', 0).call({ from: ... })

應該管用。

引用自:https://ethereum.stackexchange.com/questions/52611