Created
July 14, 2023 15:31
-
-
Save antonydenyer/9f58b81f6fe2ede4baa1c02d1c71d741 to your computer and use it in GitHub Desktop.
Multicall web3js tuple encoding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { Web3 } = require("web3"); | |
async function multiCall() { | |
const abi = | |
{ | |
"stateMutability": "payable", | |
"type": "function", | |
"name": "aggregate3", | |
"inputs": [ | |
{ | |
"components": [ | |
{ | |
"internalType": "address", | |
"name": "target", | |
"type": "address" | |
}, | |
{ | |
"internalType": "bool", | |
"name": "allowFailure", | |
"type": "bool" | |
}, | |
{ | |
"internalType": "bytes", | |
"name": "callData", | |
"type": "bytes" | |
}, | |
], | |
"internalType": "struct Multicall3.Call3[]", | |
"name": "calls", | |
"type": "tuple[]" | |
} | |
], | |
"outputs": [ | |
{ | |
"components": [ | |
{ | |
"internalType": "bool", | |
"name": "success", | |
"type": "bool" | |
}, | |
{ | |
"internalType": "bytes", | |
"name": "returnData", | |
"type": "bytes" | |
} | |
], | |
"internalType": "struct Multicall3.Result[]", | |
"name": "returnData", | |
"type": "tuple[]" | |
} | |
], | |
} | |
const web3 = new Web3(`https://goerli.infura.io/v3/${key}`); | |
const hello = web3.eth.abi.encodeFunctionCall({ | |
name: 'setGreeting', | |
type: 'function', | |
inputs: [{ | |
type: 'string', | |
name: '_greeting' | |
}] | |
}, ['bonjour']) | |
const greet = web3.eth.abi.encodeFunctionCall({ | |
name: 'greet', | |
type: 'function' | |
}) | |
const goodbye = web3.eth.abi.encodeFunctionCall({ | |
name: 'setGreeting', | |
type: 'function', | |
inputs: [{ | |
type: 'string', | |
name: '_greeting' | |
}] | |
}, ['au revoir']) | |
// | |
const multiCall = web3.eth.abi.encodeFunctionCall(abi, [[ | |
{ 'target': '0x8d9fBC02598e32C8d594bbEF4257846653ff4732', 'allowFailure': false, "callData": hello }, | |
{ 'target': '0x8d9fBC02598e32C8d594bbEF4257846653ff4732', 'allowFailure': false, "callData": greet }, | |
{ 'target': '0x8d9fBC02598e32C8d594bbEF4257846653ff4732', 'allowFailure': false, "callData": goodbye }, | |
{ 'target': '0x8d9fBC02598e32C8d594bbEF4257846653ff4732', 'allowFailure': false, "callData": greet }, | |
]]) | |
const hexString = await web3.eth.call({ | |
from: '0x673D2EBe4B6BAA946345C7b1F8d3Cc2FfB3429Bf', | |
to: '0xcA11bde05977b3631167028862bE2a173976CA11', | |
data: multiCall, | |
}) | |
const multiCallResult = web3.eth.abi.decodeParameters(abi.outputs, hexString).returnData | |
console.log(web3.eth.abi.decodeParameters([{"internalType":"string","name":"response","type":"string"}], multiCallResult[1].returnData).response) | |
console.log(web3.eth.abi.decodeParameters([{"internalType":"string","name":"response","type":"string"}], multiCallResult[3].returnData).response) | |
} | |
async function estimateGas() { | |
const web3 = new Web3(`https://goerli.infura.io/v3/${key}`); | |
const hexString = await web3.eth.estimateGas({ | |
"from": "0x673D2EBe4B6BAA946345C7b1F8d3Cc2FfB3429Bf", | |
"to": "0xebF43e1E2AA01651705764848D057AeC92a041FA", | |
"value": 0, | |
"data": "0xa41368620000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000441484f5900000000000000000000000000000000000000000000000000000000", | |
"nonce": 77 | |
}) | |
console.log(hexString); | |
} | |
// Call start | |
(async () => { | |
multiCall() | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment