Solidity
使用函式簽名對字節數組進行編碼:“TypeError:無法編碼此類型。”
我在嘗試呼叫的智能合約中有一個函式
call
。function doStuff(bytes[] calldata data) external payable {
要呼叫它,我正在執行:
bytes[] memory dataArray = new bytes[](1); (bool success, bytes memory returnData) = CONTRACT_ADDRESS.call( abi.encodeWithSignature("doStuff(bytes[])", dataArray) );
但我不斷得到:
TypeError: This type cannot be encoded. abi.encodeWithSignature("doStuff(bytes[])", dataArray)
在編譯時。我怎樣才能解決這個問題?
您使用的是什麼 Solidity 版本?
我正在使用 Solidity 版本
0.8.16
,它對我來說工作正常。查看:
pragma solidity ^0.8.16; contract DoStuff { event DidStuff(uint256 timestamp); function doStuff(bytes[] memory _data) public { // Doing stuff.. emit DidStuff(block.timestamp); } } contract Contract { address CONTRACT_ADDRESS; constructor(address doStuffAddress) { CONTRACT_ADDRESS = doStuffAddress; } function doIt() public { bytes[] memory dataArray = new bytes[](1); (bool success, bytes memory returnData) = CONTRACT_ADDRESS.call( abi.encodeWithSignature("doStuff(bytes[])", dataArray) ); } }
從 Remix 測試(我不確定您使用的是什麼):
[block:6 txIndex:0]from: 0xE81...4429Eto: Contract.doIt() 0xD8C...97aA9value: 0 weidata: 0xb29...f0835logs: 1hash: 0xb07...8bdeb status true Transaction mined and execution succeed transaction hash 0x1ed0efca8ae04b6023af938acb6f2ba87dc066708d30bbe45d1884e18470d34d from 0xE81903819F26C76C33f6f35967A56ca545f4429E to Contract.doIt() 0xD8C3fA537fc8FBe442E007759207cdd3AA997aA9 gas 27547 gas transaction cost 27547 gas input 0xb29...f0835 decoded input {} decoded output - logs [ { "from": "0xB5c6870320fEDcE225116588aD29dc4cCd988770", "topic": "0x492037fdabb4ac5a9b4e72edb0de0d83013611c4a53b457573d0d085f9222296", "event": "DidStuff", "args": { "0": "1665669544", "timestamp": "1665669544" } } ] val 0 wei