Skip to content

Commit b19d375

Browse files
committed
feat: make ibc deposits work
1 parent c21153c commit b19d375

File tree

14 files changed

+105
-62
lines changed

14 files changed

+105
-62
lines changed

apps/namadillo/public/config.toml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
#localnet_enabled = false
66
#fathom_site_id = ""
77

8-
[server_fee]
9-
# tnam or znam address
10-
# target = "znam175uaqlukleyjjyfcccehp2pw3g696a57skewlv0fenldlw702gtjd6qkpjs09zxl6jafsz3e73h"
8+
[server_fee.default]
119
target = "tnam1qrku7yxdt6d23nhe826ntukygzkfyczuu5mm0yll"
12-
# between 0 and 1
13-
percentage = 0.001
14-
# raw max value
15-
max_value = 100
10+
percentage = 0.005
11+
12+
[server_fee.tnam1p54697ljxkw3ptffwy8xjhua9uwjc7kk9cc6fxnw]
13+
target = "znam175uaqlukleyjjyfcccehp2pw3g696a57skewlv0fenldlw702gtjd6qkpjs09zxl6jafsz3e73h"
14+
percentage = 0.1

apps/namadillo/src/App/Ibc/IbcTransfer.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ export const IbcTransfer = ({
6969
const { trackEvent } = useFathomTracker();
7070
const { storeTransaction } = useTransactionActions();
7171

72-
const { transferToNamada, gasConfig } = useIbcTransaction({
72+
const {
73+
transferToNamada,
74+
gasConfig: gasConfigQuery,
75+
frontendFeeConfig,
76+
} = useIbcTransaction({
7377
registry,
7478
sourceAddress,
7579
sourceChannel,
@@ -78,6 +82,16 @@ export const IbcTransfer = ({
7882
selectedAsset: selectedAssetWithAmount?.asset,
7983
});
8084

85+
// Transfer module expects gasToken to be address of the token on the side of namada
86+
const gasConfig = useMemo(() => {
87+
if (gasConfigQuery.data && selectedAssetWithAmount?.asset.address) {
88+
return {
89+
...gasConfigQuery.data,
90+
gasToken: selectedAssetWithAmount.asset.address,
91+
};
92+
}
93+
}, [gasConfigQuery.data, selectedAssetWithAmount?.asset.address]);
94+
8195
// DERIVED VALUES
8296
const shielded = isShieldedAddress(destinationAddress ?? "");
8397
const availableDisplayAmount = mapUndefined((baseDenom) => {
@@ -161,7 +175,8 @@ export const IbcTransfer = ({
161175
isShieldedAddress: shielded,
162176
onChangeAddress: setDestinationAddress,
163177
}}
164-
gasConfig={gasConfig.data}
178+
gasConfig={gasConfig}
179+
frontendFeeConfig={frontendFeeConfig}
165180
isSubmitting={
166181
transferToNamada.isPending ||
167182
/* isSuccess means that the transaction has been broadcasted, but doesn't take

apps/namadillo/src/App/Transfer/TransferModule.tsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export const TransferModule = ({
5151
currentStatus,
5252
currentStatusExplanation,
5353
gasConfig: gasConfigProp,
54+
frontendFeeConfig: frontendFeeConfigProp,
5455
onSubmitTransfer,
5556
completedAt,
5657
onComplete,
@@ -121,7 +122,10 @@ export const TransferModule = ({
121122
});
122123
};
123124

125+
// We have to do this to get correct config for ibc deposits
124126
const gasConfig = gasConfigProp ?? feeProps?.gasConfig;
127+
const frontendFeeConfig =
128+
frontendFeeConfigProp ?? feeProps?.frontendFeeConfig;
125129

126130
const displayGasFee = useMemo(() => {
127131
return gasConfig ?
@@ -138,10 +142,16 @@ export const TransferModule = ({
138142
.minus(displayGasFee.totalDisplayAmount)
139143
.decimalPlaces(6);
140144
}
141-
if (feeProps?.frontendFeeConfig?.percentage) {
145+
146+
const frontendSusFee =
147+
frontendFeeConfig?.[selectedAsset.asset.address || "default"];
148+
149+
if (frontendSusFee && (isShielding || isUnshielding)) {
142150
amountMinusFees = amountMinusFees
143-
.div(feeProps.frontendFeeConfig.percentage.plus(1))
144-
.decimalPlaces(6);
151+
.div(frontendSusFee.percentage.plus(1))
152+
// We have to round UP here as sdk discards the remainder when calculating the fee,
153+
// basically rounding down. Otherwise we might end up with remaining dust.
154+
.decimalPlaces(6, BigNumber.ROUND_UP);
145155
}
146156

147157
return BigNumber.max(amountMinusFees, 0);

apps/namadillo/src/App/Transfer/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
Asset,
99
AssetWithAmountAndChain,
1010
ChainRegistryEntry,
11+
FrontendFeeConfig,
1112
GasConfig,
1213
LedgerAccountInfo,
1314
WalletProvider,
@@ -56,7 +57,9 @@ export type TransferModuleProps = {
5657
};
5758
isSubmitting: boolean;
5859
errorMessage?: string;
60+
// Fee overrides - used for IBC deposits only
5961
gasConfig?: GasConfig;
62+
frontendFeeConfig?: FrontendFeeConfig;
6063
currentStatus: string;
6164
currentStatusExplanation?: string;
6265
onSubmitTransfer: (params: OnSubmitTransferParams) => Promise<void>;

apps/namadillo/src/atoms/integrations/services.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export type IbcTransferParams = TransparentParams | ShieldedParams;
6666
export const getShieldedArgs = async (
6767
target: string,
6868
token: string,
69+
namadaToken: string,
6970
amount: BigNumber,
7071
destinationChannelId: string
7172
): Promise<{ receiver: string; memo: string }> => {
@@ -89,6 +90,7 @@ export const getShieldedArgs = async (
8990
payload: {
9091
target,
9192
token,
93+
namadaToken,
9294
amount,
9395
destinationChannelId,
9496
chainId: chain.data.chainId,

apps/namadillo/src/atoms/settings/atoms.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { isUrlValid, sanitizeUrl } from "@namada/utils";
22
import { getCustomIndexerApi, indexerApiAtom } from "atoms/api";
33
import { chainParametersAtom, indexerRpcUrlAtom } from "atoms/chain";
44
import BigNumber from "bignumber.js";
5+
import { pipe } from "fp-ts/lib/function";
6+
import * as R from "fp-ts/Record";
57
import { Getter, Setter, atom, getDefaultStore } from "jotai";
68
import { atomWithMutation, atomWithQuery } from "jotai-tanstack-query";
79
import { atomWithStorage } from "jotai/utils";
@@ -179,13 +181,12 @@ export const maspIndexerUrlAtom = atom((get) => {
179181
// TODO: figure out where to have this atom
180182
export const serverFeeAtom = atom((get) => {
181183
const serverFee = get(defaultServerConfigAtom).data?.server_fee;
182-
return (
183-
serverFee && {
184-
target: serverFee.target,
185-
percentage: BigNumber(serverFee.percentage),
186-
maxValue:
187-
serverFee.max_value ? BigNumber(serverFee.max_value) : undefined,
188-
}
184+
return pipe(
185+
serverFee ?? {},
186+
R.map((fee) => ({
187+
target: fee.target,
188+
percentage: new BigNumber(fee.percentage),
189+
}))
189190
);
190191
});
191192

apps/namadillo/src/atoms/transfer/atoms.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ export const createIbcTxAtom = atomWithMutation((get) => {
223223
account,
224224
signer,
225225
memo,
226+
frontendFeeConfig,
226227
}: BuildTxAtomParams<IbcTransferProps>) => {
227228
invariant(
228229
signer,
@@ -238,7 +239,8 @@ export const createIbcTxAtom = atomWithMutation((get) => {
238239
gasConfig,
239240
rpcUrl,
240241
signer?.publicKey,
241-
memo
242+
memo,
243+
frontendFeeConfig
242244
);
243245
},
244246
};

apps/namadillo/src/atoms/transfer/services.ts

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,14 @@ export const createShieldingTransferTx = async (
190190
nativeToken: chain.nativeTokenAddress,
191191
buildTxFn: async (workerLink) => {
192192
const publicKeyRevealed = await isPublicKeyRevealed(account.address);
193+
// TODO: change "default" to a a "*"
194+
const frontendSusFee =
195+
frontendFeeConfig?.[token] || frontendFeeConfig?.["default"];
193196
const msgValue: ShieldingTransferProps = {
194197
target: destination,
195198
data: [{ source, token, amount }],
196199
bparams,
197-
frontendSusFee: frontendFeeConfig && {
198-
address: frontendFeeConfig.target,
199-
amount: frontendFeeConfig.percentage.toString(),
200-
},
200+
frontendSusFee,
201201
};
202202
const msg: Shield = {
203203
type: "shield",
@@ -249,14 +249,14 @@ export const createUnshieldingTransferTx = async (
249249
rpcUrl,
250250
nativeToken: chain.nativeTokenAddress,
251251
buildTxFn: async (workerLink) => {
252+
// TODO: change "default" to a a "*"
253+
const frontendSusFee =
254+
frontendFeeConfig?.[token] || frontendFeeConfig?.["default"];
252255
const msgValue: UnshieldingTransferProps = {
253256
source,
254257
data: [{ target: destination, token, amount }],
255258
bparams,
256-
frontendSusFee: frontendFeeConfig && {
257-
address: frontendFeeConfig.target,
258-
amount: frontendFeeConfig.percentage.toString(),
259-
},
259+
frontendSusFee,
260260
};
261261

262262
const msg: Unshield = {
@@ -299,14 +299,20 @@ export const createIbcTx = async (
299299
rpcUrl,
300300
nativeToken: chain.nativeTokenAddress,
301301
buildTxFn: async (workerLink) => {
302+
const firstProps = props[0];
303+
// TODO: change "default" to a a "*"
304+
const isUnshielding = firstProps.gasSpendingKey === firstProps.source;
305+
const frontendSusFee =
306+
isUnshielding ?
307+
frontendFeeConfig?.[firstProps.token] ||
308+
frontendFeeConfig?.["default"]
309+
: undefined;
310+
302311
const msgValue: IbcTransferProps = {
303-
...props[0],
304-
gasSpendingKey: props[0].gasSpendingKey,
312+
...firstProps,
313+
gasSpendingKey: firstProps.gasSpendingKey,
305314
bparams,
306-
frontendSusFee: frontendFeeConfig && {
307-
address: frontendFeeConfig.target,
308-
amount: frontendFeeConfig.percentage.toString(),
309-
},
315+
frontendSusFee,
310316
};
311317

312318
// We only check if we need to reveal the public key if the gas spending key is not provided
@@ -359,16 +365,19 @@ export const createOsmosisSwapTx = async (
359365
rpcUrl,
360366
nativeToken: chain.nativeTokenAddress,
361367
buildTxFn: async (workerLink) => {
368+
const firstProps = props[0];
369+
// TODO: change "default" to a a "*"
370+
// TODO: ignore when source is not MASP
371+
const frontendSusFee =
372+
frontendFeeConfig?.[firstProps.transfer.token] ||
373+
frontendFeeConfig?.["default"];
362374
const msgValue: OsmosisSwapProps = {
363375
...props[0],
364376
transfer: {
365377
...transfer,
366378
gasSpendingKey: transfer.gasSpendingKey,
367379
bparams,
368-
frontendSusFee: frontendFeeConfig && {
369-
address: frontendFeeConfig.target,
370-
amount: frontendFeeConfig.percentage.toString(),
371-
},
380+
frontendSusFee,
372381
},
373382
};
374383
const msg: OsmosisSwap = {

apps/namadillo/src/hooks/useIbcTransaction.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
createNotificationId,
2020
dispatchToastNotificationAtom,
2121
} from "atoms/notifications";
22+
import { serverFeeAtom } from "atoms/settings";
2223
import BigNumber from "bignumber.js";
2324
import invariant from "invariant";
2425
import { useAtomValue, useSetAtom } from "jotai";
@@ -31,6 +32,7 @@ import {
3132
Address,
3233
Asset,
3334
ChainRegistryEntry,
35+
FrontendFeeConfig,
3436
GasConfig,
3537
IbcTransferStage,
3638
TransferStep,
@@ -52,6 +54,7 @@ type useIbcTransactionProps = {
5254

5355
type useIbcTransactionOutput = {
5456
gasConfig: UseQueryResult<GasConfig>;
57+
frontendFeeConfig: FrontendFeeConfig;
5558
transferToNamada: UseMutationResult<
5659
TransferTransactionData,
5760
Error,
@@ -77,6 +80,7 @@ export const useIbcTransaction = ({
7780
const broadcastIbcTx = useAtomValue(broadcastIbcTransactionAtom);
7881
const dispatchNotification = useSetAtom(dispatchToastNotificationAtom);
7982
const chainParameters = useAtomValue(chainParametersAtom);
83+
const serverFee = useAtomValue(serverFeeAtom);
8084
const [txHash, setTxHash] = useState<string | undefined>();
8185
const [rpcUrl, setRpcUrl] = useState<string | undefined>();
8286
const [stargateClient, setStargateClient] = useState<
@@ -208,11 +212,13 @@ export const useIbcTransaction = ({
208212
const token =
209213
asset.traces?.find((trace) => trace.type === "ibc")?.chain.path ||
210214
asset.base;
215+
invariant(selectedAsset.address, "Asset address is required");
211216

212217
return shielded ?
213218
await getShieldedArgs(
214219
destinationAddress,
215220
token,
221+
selectedAsset.address,
216222
baseAmount,
217223
destinationChannel!
218224
)
@@ -271,5 +277,6 @@ export const useIbcTransaction = ({
271277
return {
272278
transferToNamada: transferToNamadaQuery,
273279
gasConfig: gasConfigQuery,
280+
frontendFeeConfig: serverFee,
274281
};
275282
};

apps/namadillo/src/hooks/useTransactionFee/useTransactionFee.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,6 @@ export const useTransactionFee = (
140140
gasToken,
141141
};
142142

143-
// TODO: mappings
144-
const frontendFeeConfig = serverFee && {
145-
target: serverFee.target,
146-
percentage: serverFee.percentage,
147-
maxFeeInMinDenom: serverFee.maxValue,
148-
};
149-
150143
const isLoading =
151144
userTransparentBalances.isLoading ||
152145
isLoadingNativeToken ||
@@ -155,7 +148,7 @@ export const useTransactionFee = (
155148

156149
return {
157150
gasConfig,
158-
frontendFeeConfig,
151+
frontendFeeConfig: serverFee,
159152
isLoading,
160153
gasEstimate,
161154
gasPriceTable,

0 commit comments

Comments
 (0)