Skip to content

Commit

Permalink
feat: estimate addExpense cost even if we don't have splitter deployed
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtechsimetka committed Sep 18, 2023
1 parent 5f79795 commit a211c1c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
15 changes: 12 additions & 3 deletions src/lib/objects/split/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<bigint> {
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')
Expand Down
33 changes: 10 additions & 23 deletions src/lib/objects/split/views/summary.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit a211c1c

Please sign in to comment.