-
Notifications
You must be signed in to change notification settings - Fork 122
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
fix: fixing gas estimate on IBC transfers #1607
Changes from all commits
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 |
---|---|---|
|
@@ -6,9 +6,7 @@ import { | |
Stack, | ||
} from "@namada/components"; | ||
import svgImg from "App/Assets/ShieldedParty.svg"; | ||
import { TransactionFee } from "App/Common/TransactionFee"; | ||
import { SelectedWallet } from "App/Transfer/SelectedWallet"; | ||
import { getIbcGasConfig } from "integrations/utils"; | ||
import { useEffect, useMemo, useState } from "react"; | ||
import { | ||
AddressWithAssetAndAmount, | ||
|
@@ -32,7 +30,6 @@ type ShieldAllPanelProps = { | |
}; | ||
|
||
export const ShieldAllPanel = ({ | ||
registry, | ||
wallet, | ||
walletAddress, | ||
isLoading, | ||
|
@@ -77,7 +74,7 @@ export const ShieldAllPanel = ({ | |
[selectableAssets] | ||
); | ||
|
||
const gasConfig = getIbcGasConfig(registry); | ||
//const gasConfig = getIbcGasConfig(registry); | ||
|
||
return ( | ||
<ShieldAllContainer> | ||
|
@@ -118,7 +115,7 @@ export const ShieldAllPanel = ({ | |
<Stack as="footer" gap={4}> | ||
<footer className="flex justify-between items-center"> | ||
<img src={ibcTransferImageBlack} className="w-20" /> | ||
{gasConfig && <TransactionFee gasConfig={gasConfig} />} | ||
{/*gasConfig && <TransactionFee gasConfig={gasConfig} />*/} | ||
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. can we delete this and add a comment like |
||
</footer> | ||
<ActionButton | ||
backgroundColor="black" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,10 @@ import { Chain, Chains } from "@chain-registry/types"; | |
import { ActionButton, Stack } from "@namada/components"; | ||
import { mapUndefined } from "@namada/utils"; | ||
import { InlineError } from "App/Common/InlineError"; | ||
import { chainAssetsMapAtom } from "atoms/chain"; | ||
import BigNumber from "bignumber.js"; | ||
import { TransactionFeeProps } from "hooks/useTransactionFee"; | ||
import { useAtomValue } from "jotai"; | ||
import { useMemo, useState } from "react"; | ||
import { | ||
Address, | ||
|
@@ -115,9 +117,16 @@ export const TransferModule = ({ | |
const [customAddressActive, setCustomAddressActive] = useState( | ||
destination.enableCustomAddress && !destination.availableWallets | ||
); | ||
const chainAssetsMap = useAtomValue(chainAssetsMapAtom); | ||
|
||
const [memo, setMemo] = useState<undefined | string>(); | ||
|
||
const gasConfig = gasConfigProp ?? feeProps?.gasConfig; | ||
|
||
const displayGasFee = useMemo(() => { | ||
return gasConfig ? getDisplayGasFee(gasConfig, chainAssetsMap) : undefined; | ||
}, [gasConfig]); | ||
|
||
const selectedAsset = mapUndefined( | ||
(address) => source.availableAssets?.[address], | ||
source.selectedAssetAddress | ||
|
@@ -132,14 +141,16 @@ export const TransferModule = ({ | |
return undefined; | ||
} | ||
|
||
if (!gasConfig || gasConfig.gasToken !== selectedAssetAddress) { | ||
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. 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. Good catch! |
||
if (!displayGasFee || !displayGasFee.totalDisplayAmount) { | ||
return availableAmount; | ||
} | ||
|
||
const totalFees = getDisplayGasFee(gasConfig); | ||
const amountMinusFees = availableAmount.minus(totalFees); | ||
const amountMinusFees = availableAmount.minus( | ||
displayGasFee.totalDisplayAmount | ||
); | ||
|
||
return BigNumber.max(amountMinusFees, 0); | ||
}, [source.selectedAssetAddress, source.availableAmount, gasConfig]); | ||
}, [source.selectedAssetAddress, source.availableAmount, displayGasFee]); | ||
|
||
const validationResult = useMemo((): ValidationResult => { | ||
if (!source.wallet) { | ||
|
@@ -323,9 +334,10 @@ export const TransferModule = ({ | |
onChangeAddress={destination.onChangeCustomAddress} | ||
memo={memo} | ||
onChangeMemo={setMemo} | ||
gasConfig={gasConfig} | ||
feeProps={feeProps} | ||
changeFeeEnabled={changeFeeEnabled} | ||
gasDisplayAmount={displayGasFee?.totalDisplayAmount} | ||
gasAsset={displayGasFee?.asset} | ||
/> | ||
{isIbcTransfer && requiresIbcChannels && ( | ||
<IbcChannels | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,37 @@ | ||
import { Asset } from "@chain-registry/types"; | ||
import { isTransparentAddress } from "App/Transfer/common"; | ||
import BigNumber from "bignumber.js"; | ||
import { GasConfig } from "types"; | ||
import { findAssetByDenom } from "integrations/utils"; | ||
import { Address, GasConfig, GasConfigToDisplay } from "types"; | ||
import { isNamadaAsset, toDisplayAmount } from "utils"; | ||
import { unknownAsset } from "./assets"; | ||
|
||
export const getDisplayGasFee = (gasConfig: GasConfig): BigNumber => { | ||
return BigNumber(gasConfig.gasPrice) | ||
.multipliedBy(gasConfig.gasLimit) | ||
.decimalPlaces(6); | ||
export const calculateGasFee = (gasConfig: GasConfig): BigNumber => { | ||
return BigNumber(gasConfig.gasPrice).multipliedBy(gasConfig.gasLimit); | ||
}; | ||
|
||
export const getDisplayGasFee = ( | ||
gasConfig: GasConfig, | ||
chainAssetsMap?: Record<Address, Asset> | ||
): GasConfigToDisplay => { | ||
const { gasToken } = gasConfig; | ||
let asset: Asset; | ||
|
||
if (isTransparentAddress(gasToken) && chainAssetsMap) { | ||
// The gasConfig token might be the address of the token on Namada chain | ||
asset = chainAssetsMap[gasToken] ?? unknownAsset(gasToken); | ||
} else { | ||
// However, if the gasConfig contains a token used by Keplr, it could be the asset | ||
// denomination unit, like "uosmo" | ||
asset = findAssetByDenom(gasToken) ?? unknownAsset(gasToken); | ||
} | ||
|
||
const totalDisplayAmount = calculateGasFee(gasConfig); | ||
return { | ||
totalDisplayAmount: | ||
isNamadaAsset(asset) ? totalDisplayAmount : ( | ||
toDisplayAmount(asset, totalDisplayAmount).decimalPlaces(6) | ||
), | ||
asset, | ||
}; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because of the uosmo address from two channels, we have an intermittent case of showing the osmo fee as zero
But probably something to handle in a different PR