|
1 |
| -# Multicall |
2 |
| - |
| 1 | +# Multicall |
3 | 2 | Multicall aggregates results from multiple contract **constant function calls**.
|
4 |
| - |
5 | 3 | This reduces the number of separate JSON RPC requests that need to be sent (especially useful if using remote nodes like Infura), while also providing the guarantee that all values returned are from the same block (like an atomic read) and returning the block number the values are from (giving them important context so that results from old blocks can be ignored if they're from an out-of-date node).
|
6 | 4 |
|
7 |
| -This smart contract is intended to be used with **Multicall.js** in front-end dapps. |
| 5 | +This smart contract is intended to be used with **Multicall.js** in front-end dapps |
8 | 6 |
|
9 |
| -index finance 的 multicall 实现更为简单。 |
10 | 7 |
|
11 | 8 | ## 合约调用方法
|
| 9 | +合约之间的调用有 2 种方式: 底层的 call 方式和 new 合约的方式 |
12 | 10 |
|
13 |
| -合约之间的调用有 2 种方式: 底层的 call 方式和 new 合约的方式 |
14 |
| - |
15 |
| -call:通过合约 ContractAddres.call(编码后的方法名和参数),返回调用是否成功,以及返回值 data |
| 11 | +call:通过合约 ContractAddres.call(编码后的方法名和参数),返回调用是否成功,以及返回值 data |
16 | 12 |
|
17 |
| -delegatecall :设计是为了调用其它合约的 API 用的,类似于 Copy 了 API 合约的 API 函数到**本地合约**执行,会修改调用者合约的状态变量。 |
| 13 | +delegatecall :设计是为了调用其它合约的 API 用的,类似于 Copy 了 API 合约的 API 函数到**本地合约**执行,会修改调用者合约的状态变量。 |
18 | 14 |
|
19 | 15 | staticcall: Since byzantium staticcall can be used as well. This is basically the same as call, but will revert if the called function modifies the state in any way
|
20 | 16 | eth.call 方法可以在本地节点执行方法;
|
21 | 17 |
|
22 | 18 | ## Multicall.js
|
23 |
| - |
24 |
| -project url: https://github.com/makerdao/multicall.js |
25 |
| - |
26 |
| -- Get the return value(s) of multiple smart contract function calls in a single call |
27 |
| -- Guarantee that all values are from the same block |
28 |
| -- Use watchers to poll for multiple blockchain state variables/functions |
29 |
| -- Get updates when a watcher detects state has changed |
30 |
| -- Results from out of sync nodes are automatically ignored |
31 |
| -- Get new block updates |
32 |
| - |
33 |
| -## indexed-finance-multicall.js |
34 |
| - |
35 |
| -indexed-finance multicall: https://github.com/indexed-finance/multicall |
36 |
| - |
37 |
| -与常规 Muticall 合约调用不同,indexed-finance multicall 不用依赖链上已经部署成功的 multicall 合约,而是将 muticall 请求放到了待部署合约的 constructor 中,通过假部署的方式,拿到链上查询的结果。 |
38 |
| - |
39 |
| -indexed-finance multicall 的“骚操作”: |
40 |
| - |
| 19 | +Multicall.js是一个轻量级的 JavaScript 库, 用于与多个智能合约进行交互. |
| 20 | +Multicall.js 库具有以下这些特点: |
| 21 | +- 在一次调用中获取多个智能合约函数调用的返回值 |
| 22 | +- 保证所有值都来自同一个块 |
| 23 | +- 使用观察者轮询多个区块链状态变量/函数 |
| 24 | +- 当观察者检测到状态发生变化时获取更新 |
| 25 | +- 不同步节点的结果会被自动忽略 |
| 26 | +- 获取新的区块更新 |
| 27 | + |
| 28 | +## indexed-finance-multicall.js |
| 29 | +与常规 Muticall 合约调用不同,indexed-finance multicall 不用依赖链上已经部署成功的 multicall 合约,而是将 muticall 请求放到了待部署合约的 constructor 中,通过假部署的方式,拿到链上查询的结果。 |
| 30 | +indexed-finance multicall 的“骚操作”: |
41 | 31 | 1. constructor中进行 muticall 请求
|
42 | 32 | 2. 利用 `assembly` 修改evm的返回数据,将本来为 revert 的信息,替换为muticall请求结果
|
43 | 33 |
|
@@ -68,9 +58,36 @@ contract MultiCall {
|
68 | 58 | }
|
69 | 59 | ```
|
70 | 60 |
|
71 |
| -## 参考链接 |
| 61 | +## 测试步骤 |
| 62 | +- 安装依赖 |
| 63 | +```shell |
| 64 | +yarn |
| 65 | +``` |
| 66 | + |
| 67 | +- 配置环境变量 |
| 68 | +```shell |
| 69 | +cp .env.example .env |
| 70 | + |
| 71 | +## 配置 .evn 文件中的 INFURA_ID |
| 72 | +``` |
| 73 | + |
| 74 | +- 测试 makerdao-multicall.js |
| 75 | +```shell |
| 76 | +npx hardhat run scripts/makerdao-multicall.js |
| 77 | +``` |
| 78 | + |
| 79 | +- 测试 indexed-finance-multicall.js |
| 80 | +```shell |
| 81 | +npx hardhat run scripts/indexed-finance-multicall.js |
| 82 | +``` |
| 83 | + |
| 84 | +- 单元测试 |
| 85 | +```shell |
| 86 | +npx hardhat test --network mainnet |
| 87 | +``` |
72 | 88 |
|
73 |
| -- github 仓库地址: https://github.com/ETHLend/Microstaking/blob/master/contracts/StakingContract.sol |
74 |
| -- index finance: https://github.com/indexed-finance/dividends/tree/master/contracts |
| 89 | +## 参考链接 |
| 90 | +- Multicall.js github 地址: https://github.com/makerdao/multicall.js |
| 91 | +- index finance github 地址: https://github.com/indexed-finance/dividends/tree/master/contracts |
75 | 92 | - Solidity Call 函数: https://www.jianshu.com/p/a5c97d0d7cae
|
76 |
| -- https://zhuanlan.zhihu.com/p/35292014 |
| 93 | +- call/delegatecall/staticcall 介绍: https://zhuanlan.zhihu.com/p/35292014 |
0 commit comments