added multicall
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
import { defineStore } from "pinia";
|
||||
import MulticallJson from "@/assets/contracts/Multicall.json";
|
||||
import { useWalletStore } from "./wallet";
|
||||
|
||||
|
||||
export const useMultiCallStore = defineStore("multicall", {
|
||||
state: () => ({
|
||||
config: MulticallJson,
|
||||
contract: null,
|
||||
}),
|
||||
getters: {},
|
||||
actions: {
|
||||
async aggregate(calls, parameters = {}) {
|
||||
const callRequests = calls.map((call) => {
|
||||
const callData = call.encodeABI();
|
||||
return {
|
||||
target: call._parent._address,
|
||||
callData,
|
||||
};
|
||||
});
|
||||
|
||||
const { returnData } = await this.contract.methods.aggregate(callRequests).call(parameters);
|
||||
|
||||
return returnData.map((hex, index) => {
|
||||
const types = calls[index]._method.outputs.map((o) =>
|
||||
o.internalType !== o.type && o.internalType !== undefined ? o : o.type
|
||||
);
|
||||
const walletStore = useWalletStore();
|
||||
|
||||
let result = walletStore.web3.eth.abi.decodeParameters(types, hex);
|
||||
|
||||
delete result.__length__;
|
||||
|
||||
result = Object.values(result);
|
||||
return result.length === 1 ? result[0] : result;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
Reference in New Issue
Block a user