Solidity

Web3 JS 中 Solidity 的 abi.encode() 函式的等價物是什麼?

  • April 12, 2021

我在 Solidity 中有一個函式,我想使用 Typescript 和 Web3 進行複制

   function mint(
       address to,
       uint256[] memory ids,
       uint256 indexToMint
   ) public {
       require(
           registeredHashes[to][keccak256(abi.encode(to, ids))],
           "Hash not registered"
       );
   ...

什麼是相當於行的 Web3js + Typescript

abi.encode(to, ids)

您可以使用web3.eth.abi.encodeParameters

const web3 = new Web3;
web3.eth.abi.encodeParameters(
 ['address', 'uint256[]'],
 ['0x6b175474e89094c44da98b954eedeac495271d0f', [1, 2, 3]]
);

結果是:

0x0000000000000000000000006b175474e89094c44da98b954eedeac495271d0f00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003

文件

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