From a211c1cc780b2b01ce58c0136483094815ea4b32 Mon Sep 17 00:00:00 2001 From: Vojtech Simetka Date: Mon, 18 Sep 2023 21:12:32 +0200 Subject: [PATCH] feat: estimate addExpense cost even if we don't have splitter deployed --- src/lib/objects/split/blockchain.ts | 15 ++++++++-- src/lib/objects/split/views/summary.svelte | 33 +++++++--------------- 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/src/lib/objects/split/blockchain.ts b/src/lib/objects/split/blockchain.ts index b926809f..05baecc5 100644 --- a/src/lib/objects/split/blockchain.ts +++ b/src/lib/objects/split/blockchain.ts @@ -119,14 +119,23 @@ export async function addExpense( export async function estimateAddExpense( getContract: GetContract, - splitterAddress: string, + splitterAddress: string | undefined, amount: bigint, from: string, members: string[], metadata = '0x0000000000000000000000000000000000000000', ): Promise { - const splitter = getSplitterContract(getContract, splitterAddress) - const gasEstimate: bigint = await splitter.addExpense.estimateGas(metadata, amount, from, members) + let gasEstimate: bigint + let splitter: Splitter + if (splitterAddress) { + splitter = getSplitterContract(getContract, splitterAddress) + gasEstimate = await splitter.addExpense.estimateGas(metadata, amount, from, members) + } else { + // This is worse case estimateAddExpense cost + // we can not do it by estimating the transaction because we don't have the splitter contract deployed and the transaction would fail in the master splitter contract + gasEstimate = 47530n + 9000n * BigInt(members.length - 2) + splitter = getSplitterContract(getContract, await getMasterSplitterContractAddress(getContract)) + } const provider = splitter.runner?.provider if (!provider) throw new Error('Could not estimate transaction fee') diff --git a/src/lib/objects/split/views/summary.svelte b/src/lib/objects/split/views/summary.svelte index 1cce3bac..7b38f971 100644 --- a/src/lib/objects/split/views/summary.svelte +++ b/src/lib/objects/split/views/summary.svelte @@ -46,32 +46,19 @@ async function estimateFee(users: User[], amount: string, token: Token) { const members = users.map((u) => u.address) let amnt = toBigInt(amount, token.decimals) - fee = 0n + let fee = 0n let splitContractAddress = splitterAddress - try { - if (!splitContractAddress) { - fee = await estimateCreateSplitterContract(getContract, members) - - splitContractAddress = await getMasterSplitterContractAddress(getContract) - } - } catch (error) { - console.log('e1', error) - } - - if (!splitContractAddress) throw new Error('No splitter address') - - try { - fee += await estimateAddExpense( - getContract, - splitContractAddress, - amnt, - profile.address, - members, - ) - } catch (error) { - console.log('e2', error) + if (!splitContractAddress) { + fee = await estimateCreateSplitterContract(getContract, members) } + fee += await estimateAddExpense( + getContract, + splitContractAddress, + amnt, + profile.address, + members, + ) return fee }