@indexed-finance/multicall – @指数金融/多通话区块链毕设代写

区块链毕设代写本文提供国外最新区块链项目源码下载,包括solidity,eth,fabric等blockchain区块链,@indexed-finance/multicall – @指数金融/多通话区块链毕设代写 是一篇很好的国外资料

@indexed-finance/multicall

Solidity contract and typescript library for static multi-calls.

The MultiCall.sol contract executes multiple calls inside its constructor and returns the result as an ABI encoded bytes array. When the initialization code is sent to the 0 address as an eth_call operation, the code is not deployed and the returndata that would have been deployed if it was sent with eth_sendTransaction is returned. This enables simple multi-call functionality for on-chain queries without needing to deploy an aggregator contract.

The class in the typescript library takes an ethers Interface object or a JSON ABI array as inputs along with the address, function names and arguments to call. The class then decodes the returndata and returns the results as an array.

Install

npm install –save @indexed-finance/multicall

API

MultiCall

To create a MultiCall object, give an ethers or web3 Provider in the constructor, e.g. new MultiCall(provider)

MultiCall.multiCall

The MultiCall class has a single multiCall function which takes an array of CallInput objects.

Each CallInput has:

  • interface – ABI array or ethers Interface object
  • target – address of the contract to call
  • function – name of the function to call, or function signature if the function is overloaded
  • args – array of parameters to send in the call

Optionally, the interface can be provided as the first parameter and left out of the call input objects if all the calls target the same contract.

The multi-call will then bundle all of these calls into the constructor arguments for the MultiCall contract, which will call each of the functions and return an array with each call’s returndata.

Strict mode

If a call reverts, the default behavior for the contract is to return an empty bytes for that call. However, you may provide a strict boolean field as an input parameter to multiCall which will instead revert the entire call.

Parameter options

multiCall has two function signatures:

multiCall(_interface: Interface | JsonFragment[], inputs: CallInput[], strict?: boolean): Promise<any[]>;  multiCall(inputs: CallInput[], strict?: boolean): Promise<any[]>;

Response

The result will be a promise which resolves to an array with the decoded return data from each call. The data will be decoded by the ethers interface using whatever return types are defined in the ABI. If strict: true is not given, the result of any call that reverted will be null.

Example

If we have this example contract:

contract TokenMap {   struct TokenData {     uint256 balance;     uint8 decimals;   }    mapping(address => TokenData) internal _tokenDatas;    function getTokenData(address token) external view returns (TokenData memory) {     return _tokenDatas[token];   } } 

We can execute a multi-call with this javascript code:

const MultiCall = require('@indexed-finance/multicall'); const { abi } = require('./artifacts/TokenMap.json');  async function getMultiCallResults(provider, tokenMapAddress, tokens) {   const multi = new MultiCall(provider);   const inputs = [];   for (let token of tokens) {     inputs.push({ target: tokenMapAddress, function: 'getTokenData', args: [token] });   }   const tokenDatas = await multi.multiCall(abi, inputs);   return tokenDatas; } // Result: Array<{ balance: BigNumber, decimals: number }>

We use optional third-party analytics cookies to understand how you use GitHub.com so we can build better products. Learn more.


示例要调用的协定的

用于静态多调用的Solidity契约和typescript库。

多通话.sol协定在其构造函数内执行多个调用,并以ABI编码的字节数组的形式返回结果。当初始化代码作为eth_call操作发送到0地址时,不会部署该代码,并且返回如果使用eth_sendTransaction发送的话本应部署的returndata。这就为链上查询提供了简单的多调用功能,而无需部署聚合器契约。

typescript库中的类接受ethers接口对象或JSON ABI数组作为输入,以及要调用的地址、函数名和参数。然后,该类对返回的数据进行解码并以数组形式返回结果。

Install

npm install–save@indexed finance/multicall

API

MultiCall

要创建多调用对象,请在构造函数中提供一个以太网或web3提供程序,例如新的multicall(Provider)

MultiCall.multiCall

multicall类有一个multicall函数,它接受一个CallInput对象数组。

每个CallInput都有:

  • interface – ABI array or ethers Interface object
  • target – address of the contract to call
  • function – name of the function to call, or function signature if the function is overloaded
  • args – array of parameters to send in the call

可选地,如果所有调用的目标都是同一个协定,则可以将接口作为第一个参数提供,并将其排除在调用输入对象之外。

然后,multi调用将把所有这些调用绑定到MultiCall协定的构造函数参数中,后者将调用每个函数并返回一个数组,其中包含每个调用的returndata。

严格模式

如果调用恢复,契约的默认行为是为该调用返回空字节。但是,您可以提供一个严格的布尔字段作为multiCall的输入参数,而multiCall将恢复整个调用。

参数选项

多调用有两个函数签名:

multiCall(_interface: Interface | JsonFragment[], inputs: CallInput[], strict?: boolean): Promise<any[]>;  multiCall(inputs: CallInput[], strict?: boolean): Promise<any[]>;

响应

结果将是一个承诺,它解析为一个数组,其中包含每次调用的解码返回数据。数据将由ethers接口使用ABI中定义的任何返回类型进行解码。如果未给定strict:true,则任何还原的调用的结果都将为null。

Example

如果我们有这个示例合同:

contract TokenMap {   struct TokenData {     uint256 balance;     uint8 decimals;   }    mapping(address => TokenData) internal _tokenDatas;    function getTokenData(address token) external view returns (TokenData memory) {     return _tokenDatas[token];   } } 

我们可以使用这个javascript代码执行多个调用:

const MultiCall = require('@indexed-finance/multicall'); const { abi } = require('./artifacts/TokenMap.json');  async function getMultiCallResults(provider, tokenMapAddress, tokens) {   const multi = new MultiCall(provider);   const inputs = [];   for (let token of tokens) {     inputs.push({ target: tokenMapAddress, function: 'getTokenData', args: [token] });   }   const tokenDatas = await multi.multiCall(abi, inputs);   return tokenDatas; } // Result: Array<{ balance: BigNumber, decimals: number }>

我们使用可选的第三方分析cookie来了解您如何使用GitHub.com网站所以我们可以制造更好的产品。了解更多。

部分转自网络,侵权联系删除区块链源码网

www.interchains.cc

https://www.interchains.cc/18309.html

区块链毕设网(www.interchains.cc)全网最靠谱的原创区块链毕设代做网站 部分资料来自网络,侵权联系删除! 最全最大的区块链源码站 !
区块链知识分享网, 以太坊dapp资源网, 区块链教程, fabric教程下载, 区块链书籍下载, 区块链资料下载, 区块链视频教程下载, 区块链基础教程, 区块链入门教程, 区块链资源 » @indexed-finance/multicall – @指数金融/多通话区块链毕设代写

提供最优质的资源集合

立即查看 了解详情