ethereum-multicall
Advanced tools
Comparing version 1.0.5 to 2.0.0
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ExecutionType = void 0; | ||
var ExecutionType; | ||
@@ -4,0 +5,0 @@ (function (ExecutionType) { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Networks = exports.ExecutionType = void 0; | ||
var execution_type_1 = require("./execution-type"); | ||
exports.ExecutionType = execution_type_1.ExecutionType; | ||
Object.defineProperty(exports, "ExecutionType", { enumerable: true, get: function () { return execution_type_1.ExecutionType; } }); | ||
var networks_1 = require("./networks"); | ||
exports.Networks = networks_1.Networks; | ||
Object.defineProperty(exports, "Networks", { enumerable: true, get: function () { return networks_1.Networks; } }); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Networks = void 0; | ||
var Networks; | ||
@@ -4,0 +5,0 @@ (function (Networks) { |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Multicall = void 0; | ||
__exportStar(require("./models/multicall-options"), exports); | ||
var multicall_1 = require("./multicall"); | ||
exports.Multicall = multicall_1.Multicall; | ||
Object.defineProperty(exports, "Multicall", { enumerable: true, get: function () { return multicall_1.Multicall; } }); |
import { CallContext } from './call-context'; | ||
export interface ContractCallContext { | ||
export interface ContractCallContext<TContext = any> { | ||
/** | ||
@@ -19,2 +19,7 @@ * Reference to this contract call context | ||
calls: CallContext[]; | ||
/** | ||
* Store any context or state in here so you don't need | ||
* to look back over arrays once you got the result back. | ||
*/ | ||
context?: TContext | undefined; | ||
} |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./multicall-options"), exports); |
@@ -13,2 +13,7 @@ import { ContractCallContext, ContractCallResults, MulticallOptionsCustomJsonRpcProvider, MulticallOptionsEthers, MulticallOptionsWeb3 } from './models'; | ||
/** | ||
* Format return values so its always an array | ||
* @param decodedReturnValues The decoded return values | ||
*/ | ||
private formatReturnValues; | ||
/** | ||
* Build aggregate call context | ||
@@ -15,0 +20,0 @@ * @param contractCallContexts The contract call contexts |
@@ -39,2 +39,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Multicall = void 0; | ||
var ethers_1 = require("ethers"); | ||
@@ -92,3 +93,3 @@ var utils_1 = require("ethers/lib/utils"); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var aggregateResponse, returnObject, response, contractCallsResults, originalContractCallContext, returnObjectResult, method, methodContext, originalContractCallMethodContext, outputTypes, decodedReturnValue; | ||
var aggregateResponse, returnObject, response, contractCallsResults, originalContractCallContext, returnObjectResult, method, methodContext, originalContractCallMethodContext, outputTypes, decodedReturnValues; | ||
return __generator(this, function (_a) { | ||
@@ -119,8 +120,7 @@ switch (_a.label) { | ||
if (outputTypes && outputTypes.length > 0) { | ||
decodedReturnValue = utils_1.defaultAbiCoder.decode( | ||
decodedReturnValues = utils_1.defaultAbiCoder.decode( | ||
// tslint:disable-next-line: no-any | ||
outputTypes, methodContext.returnData); | ||
returnObjectResult.callsReturnContext.push(utils_2.Utils.deepClone({ | ||
// ethers put the result of the decode in an array | ||
returnValues: decodedReturnValue[0], | ||
returnValues: this.formatReturnValues(decodedReturnValues), | ||
decoded: true, | ||
@@ -150,2 +150,15 @@ reference: originalContractCallMethodContext.reference, | ||
/** | ||
* Format return values so its always an array | ||
* @param decodedReturnValues The decoded return values | ||
*/ | ||
// tslint:disable-next-line: no-any | ||
Multicall.prototype.formatReturnValues = function (decodedReturnValues) { | ||
// ethers put the result of the decode in an array sometimes. | ||
var decodedReturnResults = decodedReturnValues[0]; | ||
if (Array.isArray(decodedReturnResults)) { | ||
return decodedReturnResults; | ||
} | ||
return [decodedReturnResults]; | ||
}; | ||
/** | ||
* Build aggregate call context | ||
@@ -152,0 +165,0 @@ * @param contractCallContexts The contract call contexts |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Utils = void 0; | ||
var Utils = /** @class */ (function () { | ||
@@ -4,0 +5,0 @@ function Utils() { |
{ | ||
"name": "ethereum-multicall", | ||
"version": "1.0.5", | ||
"version": "2.0.0", | ||
"description": "Multicall allows multiple smart contract constant function calls to be grouped into a single call and the results aggregated into a single result", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
103
README.md
@@ -69,3 +69,3 @@ [](https://badge.fury.io/js/ethereum-multicall) | ||
const contractCallContext: ContractCallContext[] = [ | ||
const contractCallContext: ContractCallContext<{extraContext: string, foo4: boolean}>[] = [ | ||
{ | ||
@@ -75,3 +75,7 @@ reference: 'testContract', | ||
abi: [ { name: 'foo', type: 'function', inputs: [ { name: 'example', type: 'uint256' } ], outputs: [ { name: 'amounts', type: 'uint256' }] } ], | ||
calls: [{ reference: 'fooCall', methodName: 'foo', methodParameters: [42] }] | ||
calls: [{ reference: 'fooCall', methodName: 'foo', methodParameters: [42] }], | ||
context: { | ||
extraContext: 'extraContext', | ||
foo4: true | ||
} | ||
}, | ||
@@ -197,2 +201,97 @@ { | ||
### passing extra context to the call | ||
If you want store any context or state so you don't need to look back over arrays once you got the result back. it can be stored in `context` within `ContractCallContext`. | ||
```ts | ||
import { | ||
Multicall, | ||
ContractCallResults, | ||
ContractCallContext, | ||
} from 'ethereum-multicall'; | ||
import { ethers } from 'ethers'; | ||
let provider = ethers.getDefaultProvider(); | ||
// you can use any ethers provider context here this example is | ||
// just shows passing in a default provider, ethers hold providers in | ||
// other context like wallet, signer etc all can be passed in as well. | ||
const multicall = new Multicall({ ethersProvider: wallet.provider }); | ||
// this is showing you having the same context for all `ContractCallContext` but you can also make this have | ||
// different context for each `ContractCallContext`, as `ContractCallContext<TContext>` takes generic `TContext`. | ||
const contractCallContext: ContractCallContext<{extraContext: string, foo4: boolean}>[] = [ | ||
{ | ||
reference: 'testContract', | ||
contractAddress: '0x6795b15f3b16Cf8fB3E56499bbC07F6261e9b0C3', | ||
abi: [ { name: 'foo', type: 'function', inputs: [ { name: 'example', type: 'uint256' } ], outputs: [ { name: 'amounts', type: 'uint256' }] } ], | ||
calls: [{ reference: 'fooCall', methodName: 'foo', methodParameters: [42] }], | ||
// pass it in here! | ||
context: { | ||
extraContext: 'extraContext', | ||
foo4: true | ||
} | ||
}, | ||
{ | ||
reference: 'testContract2', | ||
contractAddress: '0x66BF8e2E890eA0392e158e77C6381b34E0771318', | ||
abi: [ { name: 'fooTwo', type: 'function', inputs: [ { name: 'example', type: 'uint256' } ], outputs: [ { name: 'amounts', type: 'uint256', name: "path", "type": "address[]" }] } ], | ||
calls: [{ reference: 'fooTwoCall', methodName: 'fooTwo', methodParameters: [42] }], | ||
// pass it in here! | ||
context: { | ||
extraContext: 'extraContext2', | ||
foo4: false | ||
} | ||
} | ||
]; | ||
const results: ContractCallResults = await multicall.call(contractCallContext); | ||
console.log(results); | ||
// results: | ||
{ | ||
results: { | ||
testContract: { | ||
originalContractCallContext: { | ||
reference: 'testContract', | ||
contractAddress: '0x6795b15f3b16Cf8fB3E56499bbC07F6261e9b0C3', | ||
abi: [ { name: 'foo', type: 'function', inputs: [ { name: 'example', type: 'uint256' } ], outputs: [ { name: 'amounts', type: 'uint256' }] } ], | ||
calls: [{ reference: 'fooCall', methodName: 'foo', methodParameters: [42] }], | ||
context: { | ||
extraContext: 'extraContext', | ||
foo4: true | ||
} | ||
}, | ||
callsReturnContext: [{ | ||
returnValues: [{ amounts: BigNumber }], | ||
decoded: true, | ||
reference: 'fooCall', | ||
methodName: 'foo', | ||
methodParameters: [42] | ||
}] | ||
}, | ||
testContract2: { | ||
originalContractCallContext: { | ||
reference: 'testContract2', | ||
contractAddress: '0x66BF8e2E890eA0392e158e77C6381b34E0771318', | ||
abi: [ { name: 'fooTwo', type: 'function', inputs: [ { name: 'example', type: 'uint256' } ], outputs: [ { name: 'amounts', type: 'uint256[]' ] } ], | ||
calls: [{ reference: 'fooTwoCall', methodName: 'fooTwo', methodParameters: [42] }], | ||
context: { | ||
extraContext: 'extraContext2', | ||
foo4: false | ||
} | ||
}, | ||
callsReturnContext: [{ | ||
returnValues: [{ amounts: [BigNumber, BigNumber, BigNumber] }], | ||
decoded: true, | ||
reference: 'fooCall', | ||
methodName: 'foo', | ||
methodParameters: [42] | ||
}] | ||
} | ||
}, | ||
blockNumber: 10994677 | ||
} | ||
``` | ||
### custom jsonrpc provider usage example | ||
@@ -199,0 +298,0 @@ |
79629
384
42
1479