ethers-multicall项目解读,站在代码角度看问题。
今天给网友推荐了合约multicall调用方式,然后另外一个网友抛出了cavanmflynn/ethers-multicall: Make multiple Ethereum network requests in a single HTTP query. ethcall for ethers v5. (github.com) 并且叫开始那个网友看readme,说这个是一个不一样的东西,所以我稍微做一下代码解读。
ethers-multicall的文档
const ethcallProvider = new Provider(provider);
await ethcallProvider.init(); // Only required when `chainId` is not provided in the `Provider` constructor
const daiContract = new Contract(daiAddress, erc20Abi);
const uniswapDaiPool = '0x2a1530c4c41db0b0b2bb646cb5eb1a67b7158667';
const ethBalanceCall = ethcallProvider.getEthBalance(uniswapDaiPool);
const daiBalanceCall = daiContract.balanceOf(uniswapDaiPool);
const [ethBalance, daiBalance] = await ethcallProvider.all([ethBalanceCall, daiBalanceCall]);
console.log('ETH Balance:', ethBalance.toString());
console.log('DAI Balance:', daiBalance.toString());
主要通过all函数来实现 multicall。 可是all函数是怎么调用的呢,代码在 provider.ts
public async all<T extends any[] = any[]>(calls: ContractCall[]) {
if (!this._provider) {
throw new Error('Provider should be initialized before use.');
}
return all<T>(calls, this._multicallAddress, this._provider);
}
const multicallAddresses = {
1: '0xeefba1e63905ef1d7acba5a8513c70307c1ce441',
3: '0xF24b01476a55d635118ca848fbc7Dab69d403be3',
4: '0x42ad527de7d4e9d9d011ac45b31d8551f8fe9821',
5: '0x77dca2c955b15e9de4dbbcf1246b4b85b651e50e',
42: '0x2cc8688c5f75e365aaeeb4ea8d6a480405a48d2a',
56: '0x1Ee38d535d541c55C9dae27B12edf090C608E6Fb',
66: '0x94fEadE0D3D832E4A05d459eBeA9350c6cDd3bCa',
97: '0x3A09ad1B8535F25b48e6Fa0CFd07dB6B017b31B2',
100: '0xb5b692a88bdfc81ca69dcb1d924f59f0413a602a',
128: '0x2C55D51804CF5b436BA5AF37bD7b8E5DB70EBf29',
137: '0x11ce4B23bD875D7F5C6a31084f55fDe1e9A87507',
250: '0x0118EF741097D0d3cc88e46233Da1e407d9ac139',
1337: '0x77dca2c955b15e9de4dbbcf1246b4b85b651e50e',
42161: '0x813715eF627B01f4931d8C6F8D2459F26E19137E',
43114: '0x7f3aC7C283d7E6662D886F494f7bc6F1993cDacf',
80001: '0x08411ADd0b5AA8ee47563b146743C13b3556c9Cc',
};
做个简单假设,multicallAddresses 中的1,是链的id ethscan 搜索 0xeefba1e63905ef1d7acba5a8513c70307c1ce441
Multicall | 0xeefba1e63905ef1d7acba5a8513c70307c1ce441 (etherscan.io)
我想你会看到的。 作者在不同的链上都部署了multicall合约。 不信你可以多搜索几个试试! multicall合约代码,我就不给大家解读了,前面有文章提到过。一个蜜罐合约的解析(二)调用隐藏 | 登链社区 | 深入浅出区块链技术 (learnblockchain.cn) 欢迎大家讨论。
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!