added multicall

This commit is contained in:
Michel Fromme
2023-06-29 21:09:08 +02:00
parent 524a5add62
commit 86f4e97f23
6 changed files with 544 additions and 406 deletions
+40
View File
@@ -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;
});
}
}
}
);