Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: estimate transaction cost #399

Merged
merged 7 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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