Skip to content

Commit 3f02210

Browse files
authored
fix: always return gas asset in balance (#1611)
1 parent 961835a commit 3f02210

8 files changed

Lines changed: 36 additions & 17 deletions

File tree

.changeset/seven-meals-sin.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@swapkit/toolboxes": patch
3+
"@swapkit/wallets": patch
4+
---
5+
6+
Always return gas asset of chain

packages/toolboxes/src/radix/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ export function radixValidateAddress(address: string) {
1818
function getBalance({ networkApi }: { networkApi: GatewayApiClient }) {
1919
return async function getBalance(address: string) {
2020
const fungibleResources = await fetchFungibleResources({ address, networkApi });
21-
const fungibleBalances = convertResourcesToBalances({ networkApi, resources: fungibleResources });
21+
const fungibleBalances = await convertResourcesToBalances({ networkApi, resources: fungibleResources });
22+
23+
const hasNativeAsset = fungibleBalances.some((asset) => asset.isGasAsset);
24+
if (!hasNativeAsset) {
25+
return [AssetValue.from({ chain: Chain.Radix }), ...fungibleBalances];
26+
}
27+
2228
return fungibleBalances;
2329
};
2430
}

packages/toolboxes/src/solana/toolbox.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,11 @@ async function getSolanaBalance(address: string) {
6767
const { PublicKey } = await import("@solana/web3.js");
6868
const { TOKEN_PROGRAM_ID } = await import("@solana/spl-token");
6969
const publicKey = new PublicKey(address);
70+
const { baseDecimal } = getChainConfig(Chain.Solana);
7071

71-
const balances: AssetValue[] = [];
72-
73-
// Get SOL balance
7472
const solBalance = await connection.getBalance(publicKey);
75-
if (solBalance > 0) {
76-
const { baseDecimal } = getChainConfig(Chain.Solana);
77-
balances.push(AssetValue.from({ chain: Chain.Solana, fromBaseDecimal: baseDecimal, value: solBalance }));
78-
}
73+
const balances = [AssetValue.from({ chain: Chain.Solana, fromBaseDecimal: baseDecimal, value: solBalance || 0 })];
7974

80-
// Get token balances
8175
const tokenAccounts = await connection.getParsedTokenAccountsByOwner(publicKey, { programId: TOKEN_PROGRAM_ID });
8276

8377
for (const { account } of tokenAccounts.value) {

packages/toolboxes/src/substrate/balance.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export async function getSubstrateBalance(
2929
} catch (error) {
3030
const errorMessage = error instanceof Error ? error.message : String(error);
3131
console.error(`Error fetching substrate balance: ${errorMessage}`);
32-
return [];
32+
return [gasAsset.set(0)];
3333
}
3434
}
3535

@@ -66,7 +66,7 @@ export async function getChainflipBalance(
6666
} catch (error) {
6767
const errorMessage = error instanceof Error ? error.message : String(error);
6868
console.error(`Error fetching chainflip balance: ${errorMessage}`);
69-
return [];
69+
return [gasAsset.set(0)];
7070
}
7171
}
7272

packages/toolboxes/src/sui/toolbox.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@ export async function getSuiToolbox({ provider: providerParam, ...signerParams }
4040
throw new SwapKitError("toolbox_sui_address_required" as any);
4141
}
4242

43+
const { baseDecimal: fromBaseDecimal, chain } = getChainConfig(Chain.Sui);
44+
4345
try {
4446
const suiClient = await getSuiClient();
4547
const { totalBalance } = await suiClient.getBalance({ owner: addressToQuery });
46-
const { baseDecimal: fromBaseDecimal, chain } = getChainConfig(Chain.Sui);
4748

4849
const suiBalances = [AssetValue.from({ chain, fromBaseDecimal, value: totalBalance })];
4950

@@ -60,8 +61,8 @@ export async function getSuiToolbox({ provider: providerParam, ...signerParams }
6061
}
6162

6263
return suiBalances;
63-
} catch (error) {
64-
throw new SwapKitError("toolbox_sui_balance_error" as any, { error });
64+
} catch {
65+
return [AssetValue.from({ chain })];
6566
}
6667
}
6768

packages/toolboxes/src/ton/toolbox.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ export async function getTONToolbox(toolboxParams: TONToolboxParams = {}) {
5252
try {
5353
const balance = await client.getBalance(Address.parse(address));
5454
return [AssetValue.from({ chain: Chain.Ton, value: SwapKitNumber.fromBigInt(balance, baseDecimal) })];
55-
} catch (error) {
56-
throw new SwapKitError("core_wallet_connection_not_found", { error });
55+
} catch {
56+
return [AssetValue.from({ chain: Chain.Ton })];
5757
}
5858
}
5959

packages/toolboxes/src/utils.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,15 @@ export function getBalance<T extends Chain>(chain: T) {
2020
return async function getBalance(address: string, scamFilter = true) {
2121
const balances = await SwapKitApi.getChainBalance({ address, chain, scamFilter });
2222
const { baseDecimal } = getChainConfig(chain);
23-
return balances.map(({ identifier, value, decimal }) => {
23+
const assetValues = balances.map(({ identifier, value, decimal }) => {
2424
return new AssetValue({ decimal: decimal || baseDecimal, identifier, value });
2525
});
26+
27+
const hasNativeAsset = assetValues.some((asset) => asset.isGasAsset);
28+
if (!hasNativeAsset) {
29+
return [AssetValue.from({ chain }), ...assetValues];
30+
}
31+
32+
return assetValues;
2633
};
2734
}

packages/wallets/src/radix/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ async function getBalance(address: string): Promise<AssetValue[]> {
124124
}
125125
}
126126

127+
const hasNativeAsset = balances.some((asset) => asset.isGasAsset);
128+
if (!hasNativeAsset) {
129+
return [AssetValue.from({ chain: Chain.Radix }), ...balances];
130+
}
131+
127132
return balances;
128133
}
129134

0 commit comments

Comments
 (0)