@gao623/multicall-js
v2.0.2
Published
multi-call for blockchain
Downloads
8
Readme
multicall-js
multicall for multicall contract, support complex type such as struct,bytes and so on.
step 1: create web3 client
const Web3 = require('web3');
function getEthClient(nodeUrl) {
const web3 = new Web3(new Web3.providers.HttpProvider(nodeUrl));
return web3;
}
step 2: multiCall function
const { MultiCall } = require("@gao623/multicall-js");
async function multiCall(client, config, multiCallType) {
console.log("multiCallType", multiCallType)
const {multiCallAddr, targetInfo} = config;
const multiRequestResult = await MultiCall[multiCallType](client, multiCallAddr, targetInfo);
const result = await MultiCall.decodeEthMultiCall(targetInfo, multiRequestResult);
return result;
}
step 3: targetInfo
const targetInfo = [
{
address:process.env.testnetWanStoremanAdminAddr
,funcName:"getStoremanGroupInfo"
,inputTypes:["bytes32"]
,outputTypes:[{"components":[{"name":"groupId","type":"bytes32"},{"name":"status","type":"uint8"},{"name":"deposit","type":"uint256"},{"name":"depositWeight","type":"uint256"},{"name":"selectedCount","type":"uint256"},{"name":"memberCount","type":"uint256"},{"name":"whiteCount","type":"uint256"},{"name":"whiteCountAll","type":"uint256"},{"name":"startTime","type":"uint256"},{"name":"endTime","type":"uint256"},{"name":"registerTime","type":"uint256"},{"name":"registerDuration","type":"uint256"},{"name":"memberCountDesign","type":"uint256"},{"name":"threshold","type":"uint256"},{"name":"chain1","type":"uint256"},{"name":"chain2","type":"uint256"},{"name":"curve1","type":"uint256"},{"name":"curve2","type":"uint256"},{"name":"tickedCount","type":"uint256"},{"name":"minStakeIn","type":"uint256"},{"name":"minDelegateIn","type":"uint256"},{"name":"minPartIn","type":"uint256"},{"name":"crossIncoming","type":"uint256"},{"name":"gpk1","type":"bytes"},{"name":"gpk2","type":"bytes"},{"name":"delegateFee","type":"uint256"}],"name":"info","type":"tuple"}]
,outputNames:["storemanGroupInfo"]
,args:["0x000000000000000000000000000000000000000000000000006465765f303833"]
}
,{
address:process.env.testnetWanMultiCallAddr
,funcName:"getEthBalance"
,inputTypes:["address"]
,outputTypes:["uint256"]
,outputNames:["balance"]
,args:["0x14095a721Dddb892D6350a777c75396D634A7d97"]
}
];
step 4: test function
async function test() {
const client = getEthClient(process.env.testnetWanRpc);
const result = await multiCall(client, {multiCallAddr: process.env.testnetWanMultiCallAddr, targetInfo}, "ethMultiCall");
console.log("final result:", JSON.stringify(result));
return result;
}
test();
step 5: test result
{
blockNumber: '21634472',
result: [
{
storemanGroupInfo: [
'0x000000000000000000000000000000000000000000000000006465765f303833',
'7',
'1422099999999999999934000',
'1568549999999999999901000',
'29',
'29',
'1',
'10',
'1658895420',
'1659500220',
'1658809035',
'1799',
'29',
'21',
'2153201998',
'2147483708',
'1',
'0',
'0',
'10000000000000000000000',
'100000000000000000000',
'10000000000000000000000',
'0',
'0x18bb7db1cfb22a18f7d49bcbc05388cdaf83519d63b8ebd3f3352d9255c85b442c2b2c4ccc4518429d2a4bc8eb65c2efcc538616b89195a624082dd84a5d5c41',
'0x931c58032cce3939ede8914307b7e50b303281bc239b1b02e4daff3511e77a374094fb4c16729f29bb6f4fd38a77d0f47b0ee23a091e9e167158abc394713905',
'1000',
groupId: '0x000000000000000000000000000000000000000000000000006465765f303833',
status: '7',
deposit: '1422099999999999999934000',
depositWeight: '1568549999999999999901000',
selectedCount: '29',
memberCount: '29',
whiteCount: '1',
whiteCountAll: '10',
startTime: '1658895420',
endTime: '1659500220',
registerTime: '1658809035',
registerDuration: '1799',
memberCountDesign: '29',
threshold: '21',
chain1: '2153201998',
chain2: '2147483708',
curve1: '1',
curve2: '0',
tickedCount: '0',
minStakeIn: '10000000000000000000000',
minDelegateIn: '100000000000000000000',
minPartIn: '10000000000000000000000',
crossIncoming: '0',
gpk1: '0x18bb7db1cfb22a18f7d49bcbc05388cdaf83519d63b8ebd3f3352d9255c85b442c2b2c4ccc4518429d2a4bc8eb65c2efcc538616b89195a624082dd84a5d5c41',
gpk2: '0x931c58032cce3939ede8914307b7e50b303281bc239b1b02e4daff3511e77a374094fb4c16729f29bb6f4fd38a77d0f47b0ee23a091e9e167158abc394713905',
delegateFee: '1000'
]
},
{ balance: '0' }
]
}