55} from "@namada/indexer-client" ;
66import { FrontendSusFeeProps } from "@namada/sdk-multicore" ;
77import { assertNever } from "@namada/utils" ;
8- import { FrontendFee } from "types" ;
8+ import BigNumber from "bignumber.js" ;
9+ import { FrontendFee , FrontendFeeEntry } from "types" ;
910import { TxKind } from "types/txKind" ;
1011
1112export const fetchGasEstimate = async (
@@ -52,13 +53,23 @@ export const fetchTokensGasPrice = async (
5253 return ( await api . apiV1GasPriceGet ( ) ) . data ;
5354} ;
5455
56+ // TODO: movet o utils
57+ export const getFrontendFeeEntry = (
58+ frontendFee : FrontendFee ,
59+ address : string
60+ ) : FrontendFeeEntry | undefined => {
61+ return frontendFee [ address ] || frontendFee [ "*" ] ;
62+ } ;
63+
5564export const frontendSusMsgFromConfig = (
5665 frontendFee : FrontendFee ,
5766 token : string ,
5867 whichTarget : "shielded" | "transparent"
59- ) : FrontendSusFeeProps => {
60- const { percentage, shieldedTarget, transparentTarget } =
61- frontendFee [ token ] || frontendFee [ "*" ] ;
68+ ) : FrontendSusFeeProps | undefined => {
69+ const entry = getFrontendFeeEntry ( frontendFee , token ) ;
70+ if ( ! entry ) return ;
71+
72+ const { percentage, shieldedTarget, transparentTarget } = entry ;
6273
6374 const target =
6475 whichTarget === "shielded" ? shieldedTarget
@@ -72,3 +83,25 @@ export const frontendSusMsgFromConfig = (
7283
7384 return frontendSusFee ;
7485} ;
86+
87+ export const calculateAmountWithoutFrontendFee = (
88+ displayAmount : BigNumber ,
89+ frontendFee : FrontendFeeEntry
90+ ) : BigNumber => {
91+ return (
92+ displayAmount
93+ . div ( frontendFee . percentage . plus ( 1 ) )
94+ // We have to round UP here as sdk discards the remainder when calculating the fee,
95+ // basically rounding down. Otherwise we might end up with remaining dust.
96+ . decimalPlaces ( 6 , BigNumber . ROUND_UP )
97+ ) ;
98+ } ;
99+
100+ export const calculateAmountWithFrontendFee = (
101+ displayAmount : BigNumber ,
102+ frontendFee : FrontendFeeEntry
103+ ) : BigNumber => {
104+ return displayAmount
105+ . multipliedBy ( frontendFee . percentage . plus ( 1 ) )
106+ . decimalPlaces ( 6 , BigNumber . ROUND_DOWN ) ;
107+ } ;
0 commit comments