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 init() { console.log("-----------init multicall-------------"); const walletStore = useWalletStore(); this.contract = new walletStore.web3.eth.Contract( this.config.abi, this.config.contractAddress ); }, 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; }); } } } );