Skip to content

Commit 0d992db

Browse files
Revert "feat: pick gas fee token with enough balance" (#2213)
1 parent 478b0e3 commit 0d992db

File tree

16 files changed

+107
-338
lines changed

16 files changed

+107
-338
lines changed

apps/namadillo/src/App/Common/GasFeeModal.tsx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { useAtomValue } from "jotai";
1616
import { IoClose } from "react-icons/io5";
1717
import { twMerge } from "tailwind-merge";
1818
import { Asset, GasConfig, NamadaAsset } from "types";
19+
import { toDisplayAmount } from "utils";
1920
import { getDisplayGasFee } from "utils/gas";
2021
import { FiatCurrency } from "./FiatCurrency";
2122
import { TokenCard } from "./TokenCard";
@@ -121,10 +122,17 @@ export const GasFeeModal = ({
121122

122123
const findUserBalanceByTokenAddress = (tokenAddres: string): BigNumber => {
123124
// TODO: we need to refactor userShieldedBalances to return Balance[] type instead
124-
const balances = isShielded ? shieldedAmount.data : transparentAmount.data;
125+
const balances =
126+
isShielded ?
127+
shieldedAmount.data?.map((balance) => ({
128+
minDenomAmount: balance.minDenomAmount,
129+
tokenAddress: balance.address,
130+
}))
131+
: transparentAmount.data;
125132

126-
return BigNumber(
127-
balances?.find((b) => b.tokenAddress === tokenAddres)?.amount || "0"
133+
return new BigNumber(
134+
balances?.find((token) => token.tokenAddress === tokenAddres)
135+
?.minDenomAmount || "0"
128136
);
129137
};
130138

@@ -248,8 +256,9 @@ export const GasFeeModal = ({
248256
gasToken: item.token,
249257
});
250258

251-
const availableAmount = findUserBalanceByTokenAddress(
252-
item.token
259+
const availableAmount = toDisplayAmount(
260+
asset,
261+
findUserBalanceByTokenAddress(item.token)
253262
);
254263

255264
return {

apps/namadillo/src/atoms/accounts/atoms.ts

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Balance } from "@namada/indexer-client";
12
import {
23
AccountType,
34
GenDisposableSignerResponse,
@@ -6,15 +7,13 @@ import {
67
import { indexerApiAtom } from "atoms/api";
78
import { nativeTokenAddressAtom } from "atoms/chain";
89
import { shouldUpdateBalanceAtom } from "atoms/etc";
9-
import { namadaRegistryChainAssetsMapAtom } from "atoms/integrations";
1010
import { namadaExtensionConnectedAtom } from "atoms/settings";
1111
import { queryDependentFn } from "atoms/utils";
1212
import BigNumber from "bignumber.js";
1313
import { NamadaKeychain } from "hooks/useNamadaKeychain";
1414
import { atom } from "jotai";
1515
import { atomWithMutation, atomWithQuery } from "jotai-tanstack-query";
16-
import { Address } from "types";
17-
import { toDisplayAmount } from "utils";
16+
import { namadaAsset, toDisplayAmount } from "utils";
1817
import {
1918
fetchAccountBalance,
2019
fetchAccounts,
@@ -107,9 +106,9 @@ export const accountBalanceAtom = atomWithQuery<BigNumber>((get) => {
107106
...queryDependentFn(async (): Promise<BigNumber> => {
108107
const balance = transparentBalanceQuery.data
109108
?.filter(({ tokenAddress: ta }) => ta === tokenAddress.data)
110-
.map(({ tokenAddress, amount }) => ({
109+
.map(({ tokenAddress, minDenomAmount }) => ({
111110
token: tokenAddress,
112-
amount,
111+
amount: toDisplayAmount(namadaAsset(), new BigNumber(minDenomAmount)),
113112
}))
114113
.at(0);
115114
return balance ? BigNumber(balance.amount) : BigNumber(0);
@@ -137,35 +136,18 @@ export const disposableSignerAtom = atomWithQuery<GenDisposableSignerResponse>(
137136
}
138137
);
139138

140-
export const transparentBalanceAtom = atomWithQuery<
141-
{ tokenAddress: Address; amount: BigNumber }[]
142-
>((get) => {
139+
export const transparentBalanceAtom = atomWithQuery<Balance[]>((get) => {
143140
const enablePolling = get(shouldUpdateBalanceAtom);
144141
const api = get(indexerApiAtom);
145142
const defaultAccountQuery = get(defaultAccountAtom);
146-
const assetsMapAtom = get(namadaRegistryChainAssetsMapAtom);
147143

148144
const account = defaultAccountQuery.data;
149-
const assetsMap = assetsMapAtom.data;
150145

151146
return {
152147
refetchInterval: enablePolling ? 1000 : false,
153148
queryKey: ["transparent-balance", account],
154149
...queryDependentFn(async () => {
155-
if (!account || !assetsMap) {
156-
return [];
157-
}
158-
const balance = await fetchAccountBalance(api, account);
159-
160-
return balance
161-
.filter((b) => b.tokenAddress in assetsMap)
162-
.map((item) => ({
163-
tokenAddress: item.tokenAddress,
164-
amount: toDisplayAmount(
165-
assetsMap[item.tokenAddress],
166-
BigNumber(item.minDenomAmount)
167-
),
168-
}));
169-
}, [defaultAccountQuery, assetsMapAtom]),
150+
return account ? fetchAccountBalance(api, account) : [];
151+
}, [defaultAccountQuery]),
170152
};
171153
});

apps/namadillo/src/atoms/balance/atoms.ts

Lines changed: 11 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,10 @@ import { maspIndexerUrlAtom, rpcUrlAtom } from "atoms/settings";
2020
import { queryDependentFn } from "atoms/utils";
2121
import { isAxiosError } from "axios";
2222
import BigNumber from "bignumber.js";
23-
import * as E from "fp-ts/Either";
2423
import { sequenceT } from "fp-ts/lib/Apply";
2524
import { pipe } from "fp-ts/lib/function";
2625
import * as O from "fp-ts/Option";
2726
import invariant from "invariant";
28-
import * as t from "io-ts";
2927
import { atom, getDefaultStore } from "jotai";
3028
import { atomWithQuery } from "jotai-tanstack-query";
3129
import { atomWithStorage } from "jotai/utils";
@@ -111,43 +109,12 @@ export const viewingKeysAtom = atomWithQuery<
111109
};
112110
});
113111

114-
const NamadilloShieldedBalanceCodec = t.record(
115-
t.string, // Address
116-
t.array(
117-
t.type({
118-
tokenAddress: t.string,
119-
amount: t.string,
120-
})
121-
)
122-
);
123-
124112
export const storageShieldedBalanceAtom = atomWithStorage<
125-
Record<Address, { tokenAddress: Address; amount: string }[]>
126-
>(
127-
"namadillo:shieldedBalance",
128-
{},
129-
{
130-
// Invalidation function to ensure the stored data matches the codec
131-
getItem(key, initialValue) {
132-
const storedValue = localStorage.getItem(key);
133-
const maybeValue = NamadilloShieldedBalanceCodec.decode(
134-
JSON.parse(storedValue ?? "{}")
135-
);
136-
137-
return E.isRight(maybeValue) ? maybeValue.right : initialValue;
138-
},
139-
setItem(key, value) {
140-
localStorage.setItem(key, JSON.stringify(value));
141-
},
142-
removeItem(key) {
143-
localStorage.removeItem(key);
144-
},
145-
}
146-
);
113+
Record<Address, { address: Address; minDenomAmount: string }[]>
114+
>("namadillo:shieldedBalance", {});
147115

148116
export const shieldedSyncProgress = atom(0);
149117

150-
// After changing the stored data type, change the version in the key
151118
export const lastCompletedShieldedSyncAtom = atomWithStorage<
152119
Record<Address, Date | undefined>
153120
>("namadillo:last-shielded-sync", {}, undefined, { getOnInit: true });
@@ -165,13 +132,11 @@ export const shieldedBalanceAtom = atomWithQuery((get) => {
165132
const rpcUrl = get(rpcUrlAtom);
166133
const maspIndexerUrl = get(maspIndexerUrlAtom);
167134
const defaultAccount = get(defaultAccountAtom);
168-
const assetsMapAtom = get(namadaRegistryChainAssetsMapAtom);
169135

170136
const [viewingKey, allViewingKeys] = viewingKeysQuery.data ?? [];
171137
const chainTokens = chainTokensQuery.data?.map((t) => t.address);
172138
const chainId = chainParametersQuery.data?.chainId;
173139
const namTokenAddress = namTokenAddressQuery.data;
174-
const assetsMap = assetsMapAtom.data;
175140

176141
return {
177142
refetchInterval: enablePolling ? 1000 : false,
@@ -183,8 +148,7 @@ export const shieldedBalanceAtom = atomWithQuery((get) => {
183148
!chainTokens ||
184149
!chainId ||
185150
!namTokenAddress ||
186-
!rpcUrl ||
187-
!assetsMap
151+
!rpcUrl
188152
) {
189153
return [];
190154
}
@@ -204,24 +168,16 @@ export const shieldedBalanceAtom = atomWithQuery((get) => {
204168
chainTokens,
205169
chainId
206170
);
207-
const shieldedBalance = response
208-
// Filter out unknown assets
209-
.filter(([address]) => address in assetsMap)
210-
.map(([address, amount]) => {
211-
const asset = assetsMap[address];
212-
return {
213-
tokenAddress: address,
214-
amount: toDisplayAmount(asset, BigNumber(amount)),
215-
};
216-
});
171+
172+
const shieldedBalance = response.map(([address, amount]) => ({
173+
address,
174+
minDenomAmount: amount,
175+
}));
217176

218177
const storage = get(storageShieldedBalanceAtom);
219178
set(storageShieldedBalanceAtom, {
220179
...storage,
221-
[viewingKey.key]: shieldedBalance.map((balance) => ({
222-
...balance,
223-
amount: balance.amount.toString(),
224-
})),
180+
[viewingKey.key]: shieldedBalance,
225181
});
226182

227183
if (defaultAccount.data) {
@@ -242,7 +198,6 @@ export const shieldedBalanceAtom = atomWithQuery((get) => {
242198
chainTokensQuery,
243199
chainParametersQuery,
244200
namTokenAddressQuery,
245-
assetsMapAtom,
246201
]),
247202
};
248203
});
@@ -263,10 +218,8 @@ export const namadaShieldedAssetsAtom = atomWithQuery((get) => {
263218

264219
return mapNamadaAddressesToAssets({
265220
balances:
266-
shieldedBalance?.map((i) => ({
267-
...i,
268-
amount: BigNumber(i.amount),
269-
})) ?? [],
221+
shieldedBalance?.map((i) => ({ ...i, tokenAddress: i.address })) ??
222+
[],
270223
assets: Object.values(chainAssetsMap.data),
271224
});
272225
}, [viewingKeysQuery, chainTokensQuery, chainAssetsMap]),

apps/namadillo/src/atoms/balance/functions.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import { Balance } from "@namada/indexer-client";
12
import BigNumber from "bignumber.js";
23
import {
34
Address,
45
NamadaAsset,
56
NamadaAssetWithAmount,
67
TokenBalance,
78
} from "types";
8-
import { isNamadaAsset } from "utils";
9+
import { isNamadaAsset, toDisplayAmount } from "utils";
910

1011
export const getTotalDollar = (list?: TokenBalance[]): BigNumber =>
1112
(list ?? []).reduce(
@@ -20,16 +21,16 @@ export const mapNamadaAddressesToAssets = ({
2021
balances,
2122
assets,
2223
}: {
23-
balances: { tokenAddress: Address; amount: BigNumber }[];
24+
balances: Balance[];
2425
assets: NamadaAsset[];
2526
}): Record<Address, NamadaAssetWithAmount> => {
2627
const map: Record<Address, NamadaAssetWithAmount> = {};
27-
balances.forEach(({ tokenAddress, amount }) => {
28-
const asset = assets.find((asset) => asset.address === tokenAddress);
28+
balances.forEach((item) => {
29+
const asset = assets.find((asset) => asset.address === item.tokenAddress);
2930

3031
if (asset) {
31-
map[tokenAddress] = {
32-
amount,
32+
map[item.tokenAddress] = {
33+
amount: toDisplayAmount(asset, BigNumber(item.minDenomAmount)),
3334
asset,
3435
};
3536
}

apps/namadillo/src/atoms/chain/atoms.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ export const maspRewardsAtom = atomWithQuery((get) => {
121121
...queryDependentFn(async (): Promise<MaspAssetRewards[]> => {
122122
invariant(chainAssetsMap.data, "No chain assets map");
123123

124-
return await fetchMaspRewards(chainAssetsMap.data);
124+
return await fetchMaspRewards(Object.values(chainAssetsMap.data));
125125
}, [chainAssetsMap]),
126126
};
127127
});

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import {
44
NativeToken,
55
Parameters,
66
} from "@namada/indexer-client";
7+
import { getDenomFromIbcTrace } from "atoms/integrations";
78
import BigNumber from "bignumber.js";
8-
import { Address, MaspAssetRewards, NamadaAsset } from "types";
9+
import { MaspAssetRewards, NamadaAsset } from "types";
10+
import { unknownAsset } from "utils/assets";
911
import { getSdkInstance } from "utils/sdk";
1012

1113
export const fetchRpcUrlFromIndexer = async (
@@ -41,16 +43,16 @@ export const clearShieldedContext = async (chainId: string): Promise<void> => {
4143
};
4244

4345
export const fetchMaspRewards = async (
44-
assets: Record<Address, NamadaAsset>
46+
assets: NamadaAsset[]
4547
): Promise<MaspAssetRewards[]> => {
4648
const sdk = await getSdkInstance();
4749
const rewards = await sdk.rpc.globalShieldedRewardForTokens();
48-
4950
const existingRewards: MaspAssetRewards[] = rewards
50-
.filter((r) => r.maxRewardRate > 0 && r.address in assets)
51+
.filter((r) => r.maxRewardRate > 0)
5152
.map((r) => {
52-
const asset = assets[r.address];
53-
53+
const denom = getDenomFromIbcTrace(r.name);
54+
const asset =
55+
assets.find((asset) => asset.base === denom) ?? unknownAsset(denom);
5456
return {
5557
asset,
5658
address: r.address,

apps/namadillo/src/atoms/fees/atoms.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { isPublicKeyRevealed } from "lib/query";
1111
import isEqual from "lodash.isequal";
1212
import { Address } from "types";
1313
import { TxKind } from "types/txKind";
14-
import { toDisplayAmount } from "utils";
14+
import { isNamadaAsset, toDisplayAmount } from "utils";
1515
import { fetchGasEstimate, fetchTokensGasPrice } from "./services";
1616

1717
export type GasPriceTableItem = {
@@ -73,7 +73,10 @@ export const gasPriceTableAtom = atomWithQuery<GasPriceTable>((get) => {
7373
const baseAmount = BigNumber(minDenomAmount);
7474
return {
7575
token,
76-
gasPrice: toDisplayAmount(asset, baseAmount),
76+
gasPrice:
77+
asset && isNamadaAsset(asset) ?
78+
toDisplayAmount(asset, baseAmount)
79+
: baseAmount,
7780
};
7881
})
7982
);

apps/namadillo/src/hooks/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
export * from "./useProposalIdParam";
22
export * from "./useSdk";
3-
export * from "./useTransactionFee";

apps/namadillo/src/hooks/useOptimisticTransferUpdate.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ export const useOptimisticTransferUpdate = () => {
1717
const [viewingKeyData] = useAtomValue(viewingKeysAtom).data ?? [];
1818
const viewingKey = viewingKeyData?.key;
1919

20-
return (token: Address, incrementAmount: BigNumber) => {
20+
return (token: Address, incrementBaseDenomAmount: BigNumber) => {
2121
if (!viewingKey) {
2222
return;
2323
}
2424
setStorageShieldedBalance((storage) => ({
2525
...storage,
2626
[viewingKey]: storage[viewingKey].map((item) =>
27-
item.tokenAddress === token ?
27+
item.address === token ?
2828
{
2929
...item,
30-
amount: sum(item.amount, incrementAmount),
30+
minDenomAmount: sum(item.minDenomAmount, incrementBaseDenomAmount),
3131
}
3232
: item
3333
),

0 commit comments

Comments
 (0)