블록체인

[ethereum] input data의 methodId와 event signature 만드는 방법

멍개. 2022. 8. 28. 07:19

● methodId

etherscan을 보다보면 input Data가 다음과 같이 출력되는 모습을 볼 수 있습니다.

원래의 input data로 변환하면 다음과 같습니다.

여기서 methodID는 어떤 원리로 만들어 지는지 알아보겠습니다.

· solidity

 
contract Test {
    function setA() public pure returns (bytes memory) {
       // return bytes4(keccak256('balanceOf(address)')); 해당 방법도 가능
       return abi.encodeWithSignature("transfer(address,uint256)");
    }
}

· web3

const Web3 = require('web3');
const web3 = new Web3()

console.log(web3.eth.abi.encodeFunctionSignature("transfer(address,uint256)"))
 
0xa9059cbb

abi를 object 형태로 전달하는 방법도 있습니다.

const Web3 = require('web3');
const web3 = new Web3()

web3.eth.abi.encodeFunctionSignature({
  name: 'transfer',
  type: 'function',
  inputs: [{
      type: 'address',
      name: 'to'
  },{
      type: 'uint256',
      name: 'value'
  }]
})
0xa9059cbb
 

● event signature

· web3js

web3.eth.abi.encodeEventSignature("Transfer(address,address,uint256)")

web3.eth.abi.encodeEventSignature({
  name: 'Transfer',
  type: 'event',
  inputs: [{
    indexed: true,
    type: 'address',
    name: 'from'
  }, {
    indexed: true,
    type: 'address',
    name: 'to'
  },{
    type: 'uint256',
    name: 'value'
  }]
})
 
0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef
0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef