-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor with proper fee calculation
- Loading branch information
Showing
2 changed files
with
103 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
packages/account-kit/src/steps/deposit/usePrepareWithdraw.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { useQuery } from "@tanstack/react-query"; | ||
import { | ||
SendTransactionParameters, | ||
estimateFeesPerGas, | ||
getBalance, | ||
prepareTransactionRequest, | ||
readContract, | ||
} from "viem/actions"; | ||
import { getChainContractAddress, serializeTransaction } from "viem/utils"; | ||
import { AppAccountClient } from "../../common"; | ||
import { Hex } from "viem"; | ||
|
||
const withdrawGas = 21000n; | ||
|
||
export type UsePrepareWithdrawOptions = { | ||
appAccountClient: AppAccountClient | undefined; | ||
userAddress: Hex | undefined; | ||
}; | ||
|
||
export function usePrepareWithdraw({ appAccountClient, userAddress }: UsePrepareWithdrawOptions) { | ||
const queryKey = ["prepareWithdraw", appAccountClient?.chain.id, appAccountClient?.account.address, userAddress]; | ||
return useQuery( | ||
appAccountClient && userAddress | ||
? { | ||
queryKey, | ||
queryFn: async (): Promise<SendTransactionParameters> => { | ||
const [balance, fees] = await Promise.all([ | ||
getBalance(appAccountClient, { address: appAccountClient.account.address }), | ||
estimateFeesPerGas(appAccountClient), | ||
]); | ||
let fee = withdrawGas * fees.maxFeePerGas; | ||
|
||
// If this is an L2, add L1 fee | ||
const gasPriceOracleAddress = getChainContractAddress({ | ||
chain: appAccountClient.chain, | ||
contract: "gasPriceOracle", | ||
}); | ||
if (gasPriceOracleAddress) { | ||
const request = await prepareTransactionRequest(appAccountClient, { | ||
chain: appAccountClient.chain, | ||
account: appAccountClient.account, | ||
to: userAddress, | ||
value: balance, | ||
gas: withdrawGas, | ||
...fees, | ||
}); | ||
|
||
const transaction = serializeTransaction({ | ||
...request, | ||
type: "eip1559", | ||
}); | ||
|
||
fee += await readContract(appAccountClient, { | ||
address: gasPriceOracleAddress, | ||
abi: [ | ||
{ | ||
inputs: [{ internalType: "bytes", name: "_data", type: "bytes" }], | ||
name: "getL1Fee", | ||
outputs: [{ internalType: "uint256", name: "", type: "uint256" }], | ||
stateMutability: "view", | ||
type: "function", | ||
}, | ||
], | ||
functionName: "getL1Fee", | ||
args: [transaction], | ||
}); | ||
} | ||
|
||
const withdrawAmount = balance - fee; | ||
if (withdrawAmount <= 0n) { | ||
throw new Error(`Account balance (${balance}) not enough to cover estimated transfer fee (${fee}).`); | ||
} | ||
|
||
return { | ||
chain: appAccountClient.chain, | ||
account: appAccountClient.account, | ||
to: userAddress, | ||
value: withdrawAmount, | ||
gas: withdrawGas, | ||
...fees, | ||
}; | ||
}, | ||
// TODO: figure out a good refresh rate (L1 block time? L2 commit time?) | ||
refetchInterval: 4_000, | ||
} | ||
: { queryKey, enabled: false }, | ||
); | ||
} |