-
Notifications
You must be signed in to change notification settings - Fork 526
[๐ง Smart Account] EIP-5792 atomic batch TX assembly + store-based fee estimation #1913
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
Changes from 4 commits
f83ff44
12de689
3672133
416f3b1
c8c4c0b
cb42016
1f26614
395bbcb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,25 @@ | ||
| import { useCallback, useEffect, useMemo, useState } from "react"; | ||
| import { EstimateSmartAccountFeeMsg } from "@keplr-wallet/background"; | ||
| import { InExtensionMessageRequester } from "@keplr-wallet/router-extension"; | ||
| import { BACKGROUND_PORT } from "@keplr-wallet/router"; | ||
| import { CoinPretty, Int } from "@keplr-wallet/unit"; | ||
| import { | ||
| ALLOWED_DELEGATORS, | ||
| buildDummyAuthorizationList, | ||
| } from "@keplr-wallet/background"; | ||
| import { computeEIP1559TxFees } from "@keplr-wallet/hooks-evm"; | ||
| import { CoinPretty, Dec, Int } from "@keplr-wallet/unit"; | ||
| import { useStore } from "../../../../../stores"; | ||
| import { formatFee } from "../../utils"; | ||
|
|
||
| type FeeState = | ||
| | { status: "loading" } | ||
| | { status: "success"; estimatedFeeWei: string } | ||
| | { status: "success"; gasUsed: number } | ||
| | { status: "error" }; | ||
|
|
||
| export function useSmartAccountFee( | ||
| chainId: string, | ||
| vaultId: string, | ||
| hexAddress: string, | ||
| isValid: boolean | ||
| ) { | ||
| const { chainStore, queriesStore, priceStore } = useStore(); | ||
| const { chainStore, queriesStore, priceStore, ethereumAccountStore } = | ||
| useStore(); | ||
|
|
||
| const nativeCurrency = useMemo(() => { | ||
| try { | ||
|
|
@@ -30,46 +32,81 @@ export function useSmartAccountFee( | |
| } | ||
| }, [chainStore, chainId]); | ||
|
|
||
| const evmChainId = useMemo(() => { | ||
| try { | ||
| const unwrapped = chainStore.getModularChain(chainId).unwrapped; | ||
| if (unwrapped.type === "evm") return unwrapped.evm.chainId; | ||
| if (unwrapped.type === "ethermint") return unwrapped.evm.chainId; | ||
| return 0; | ||
| } catch { | ||
| return 0; | ||
| } | ||
| }, [chainStore, chainId]); | ||
|
|
||
| const nativeSymbol = nativeCurrency?.coinDenom ?? "ETH"; | ||
|
|
||
| const [feeState, setFeeState] = useState<FeeState>({ status: "loading" }); | ||
| const [retryCount, setRetryCount] = useState(0); | ||
|
|
||
| useEffect(() => { | ||
| if (!isValid) return; | ||
| if (!isValid || !hexAddress || !evmChainId) return; | ||
| let cancelled = false; | ||
| setFeeState({ status: "loading" }); | ||
| new InExtensionMessageRequester() | ||
| .sendMessage( | ||
| BACKGROUND_PORT, | ||
| new EstimateSmartAccountFeeMsg(chainId, vaultId) | ||
| ) | ||
|
|
||
| const ethereumAccount = ethereumAccountStore.getAccount(chainId); | ||
|
|
||
| ethereumAccount | ||
| .simulateGas(hexAddress, { | ||
| to: hexAddress, | ||
| value: "0x0", | ||
| data: "0x", | ||
| authorizationList: buildDummyAuthorizationList( | ||
| ALLOWED_DELEGATORS[0], | ||
| evmChainId | ||
| ), | ||
| }) | ||
| .then((result) => { | ||
| if (!cancelled) | ||
| setFeeState({ | ||
| status: "success", | ||
| estimatedFeeWei: result.estimatedFeeWei, | ||
| }); | ||
| if (!cancelled) { | ||
| setFeeState({ status: "success", gasUsed: result.gasUsed }); | ||
| } | ||
|
piatoss3612 marked this conversation as resolved.
|
||
| }) | ||
| .catch(() => { | ||
| if (!cancelled) setFeeState({ status: "error" }); | ||
| }); | ||
|
|
||
| return () => { | ||
| cancelled = true; | ||
| }; | ||
| }, [chainId, vaultId, isValid, retryCount]); | ||
| }, [ | ||
| chainId, | ||
| hexAddress, | ||
| evmChainId, | ||
| isValid, | ||
| retryCount, | ||
| ethereumAccountStore, | ||
| ]); | ||
|
|
||
| // Fee from reactive queries (feeHistory percentile + baseFee margin, fillUnsignedEVMTx์ ๋์ผ ๋ก์ง) | ||
| const ethereumQueries = queriesStore.get(chainId).ethereum; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This new unconditional lookup Useful? React with ๐ย / ๐.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| const txFees = ethereumQueries | ||
| ? computeEIP1559TxFees(ethereumQueries, "average", chainId) | ||
| : undefined; | ||
|
|
||
| const estimatedFeeWei = useMemo(() => { | ||
| if (feeState.status !== "success") return null; | ||
| const feePerGas = txFees?.maxFeePerGas ?? txFees?.gasPrice; | ||
| if (!feePerGas || feePerGas.isZero()) return null; | ||
| const gasLimit = Math.ceil(feeState.gasUsed * 1.3); | ||
|
Comment on lines
+115
to
+117
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When gas simulation succeeds but fee queries are still empty or errored, Useful? React with ๐ย / ๐.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์์ ํ์ต๋๋ค.
Comment on lines
+115
to
+117
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When gas simulation succeeds but fee queries are still empty, this branch returns Useful? React with ๐ย / ๐.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. txFees๋ MobX reactive query ๊ธฐ๋ฐ์ด๋ผ RPC ์๋ต ์ค๋ฉด ์๋ ๊ฐฑ์ ๋ฉ๋๋ค. ์๊ตฌ ๊ต์ฐฉ์ ๋ฐ์ํ์ง ์์ต๋๋ค. |
||
| const fee = feePerGas.mul(new Dec(gasLimit)).truncate(); | ||
| return "0x" + BigInt(fee.toString()).toString(16); | ||
| }, [feeState, txFees]); | ||
|
|
||
| const estimatedFeeWei = | ||
| feeState.status === "success" ? feeState.estimatedFeeWei : null; | ||
| const isEstimating = feeState.status === "loading"; | ||
| const isFailed = feeState.status === "error"; | ||
|
|
||
| const feeDisplay = useMemo( | ||
| () => | ||
| feeState.status === "success" | ||
| ? formatFee(feeState.estimatedFeeWei, nativeSymbol) | ||
| : null, | ||
| [feeState, nativeSymbol] | ||
| () => (estimatedFeeWei ? formatFee(estimatedFeeWei, nativeSymbol) : null), | ||
| [estimatedFeeWei, nativeSymbol] | ||
| ); | ||
|
|
||
| const feeUsd = useMemo(() => { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,8 @@ | ||
| export * from "./messages"; | ||
| export * from "./service"; | ||
| export { ALLOWED_DELEGATORS } from "./constants"; | ||
| export { | ||
| buildDummyAuthorizationList, | ||
| createAtomicBatchTransaction, | ||
| buildDelegationStateOverride, | ||
| } from "./helper"; |
Uh oh!
There was an error while loading. Please reload this page.