Skip to content

Commit

Permalink
feat: estimate transaction cost (#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtechsimetka authored Sep 19, 2023
1 parent 4485a1a commit 8968e99
Show file tree
Hide file tree
Showing 20 changed files with 1,353 additions and 152 deletions.
2 changes: 2 additions & 0 deletions knip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const config: KnipConfig = {
'src/lib/objects/hello-world/index.ts',
'src/lib/objects/external/iframe.svelte',
'src/lib/objects/split/types.ts',
'src/lib/objects/split/contracts/**/*',
],
paths: {
// This ain't pretty, but Svelte basically does the same
Expand All @@ -28,6 +29,7 @@ const config: KnipConfig = {
},
ignoreExportsUsedInFile: true,
ignoreBinaries: ['docker'],
ignoreDependencies: ['@typechain/ethers-v6'],
}

export default config
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"knip": "knip",
"start:blockchain": "hardhat node",
"cli": "tsx --tsconfig ./.svelte-kit/tsconfig.json --no-warnings ./src/cli/cli.ts",
"typechain": "typechain --target ethers-v6 --out-dir ./src/lib/objects/split/contracts/types ./src/lib/objects/split/contracts/abis/*.json",
"waku:start": "docker compose -f ./docker-compose.yaml up -d",
"waku:stop": "docker kill waku-objects-playground-waku-1"
},
Expand All @@ -24,6 +25,7 @@
"@sveltejs/adapter-auto": "^2.1.0",
"@sveltejs/kit": "^1.21.0",
"@total-typescript/ts-reset": "^0.4.2",
"@typechain/ethers-v6": "^0.5.0",
"@types/node": "^20.5.4",
"@typescript-eslint/eslint-plugin": "^5.61.0",
"@typescript-eslint/parser": "^5.61.0",
Expand All @@ -49,6 +51,7 @@
"svelte-check": "^3.4.4",
"svelte-preprocess": "^5.0.4",
"tsx": "^3.12.8",
"typechain": "^8.3.1",
"typescript": "^5.1.6",
"vite": "^4.3.9",
"vitest": "^0.32.4",
Expand Down
167 changes: 167 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions src/lib/adapters/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,36 @@ export async function sendTransaction(
return tx
}

export async function estimateTransaction(
wallet: BaseWallet,
to: string,
amount: bigint,
tokenAddress?: string,
): Promise<bigint> {
const provider = getProvider()
const txWallet = wallet.connect(provider)

let gasEstimate: bigint

if (tokenAddress) {
const contract = new Contract(tokenAddress, abi, txWallet)
gasEstimate = await contract.transfer.estimateGas(to, amount)
} else {
const txRequest: TransactionRequest = {
to,
value: amount,
}
gasEstimate = await provider.estimateGas(txRequest)
}
const fee = await provider.getFeeData()

if (fee.maxFeePerGas && fee.maxPriorityFeePerGas)
return gasEstimate * (fee.maxFeePerGas + fee.maxPriorityFeePerGas)
else if (fee.gasPrice) return gasEstimate * fee.gasPrice

throw new Error('Could not estimate transaction fee')
}

export async function waitForTransaction(
txHash: string,
confirm?: number | undefined,
Expand Down
Loading

1 comment on commit 8968e99

@vercel
Copy link

@vercel vercel bot commented on 8968e99 Sep 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.