Skip to content

Commit

Permalink
feat(frontend): add Solana balance service util (#4073)
Browse files Browse the repository at this point in the history
# Motivation

Small util to return the balance in Solana and set it in the
`balanceStore`, given a generic `token`.
  • Loading branch information
AntonioVentilii authored Jan 6, 2025
1 parent 9f7c6b7 commit 96e5ad0
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/frontend/src/lib/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@
"no_infura_erc20_provider": "No Infura ERC20 provider for network $network",
"no_infura_erc20_icp_provider": "No Infura ERC20 Icp provider for network $network",
"no_solana_rpc": "No Solana RPC for network $network",
"no_solana_network": "No Solana network for network $network",
"eth_address_unknown": "ETH address is unknown.",
"loading_address": "Error while loading the $symbol address.",
"loading_balance": "Error while loading the ETH balance.",
Expand Down
1 change: 1 addition & 0 deletions src/frontend/src/lib/types/i18n.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ interface I18nInit {
no_infura_erc20_provider: string;
no_infura_erc20_icp_provider: string;
no_solana_rpc: string;
no_solana_network: string;
eth_address_unknown: string;
loading_address: string;
loading_balance: string;
Expand Down
48 changes: 48 additions & 0 deletions src/frontend/src/sol/services/sol-balance.services.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { balancesStore } from '$lib/stores/balances.store';
import { i18n } from '$lib/stores/i18n.store';
import type { SolAddress } from '$lib/types/address';
import type { Token } from '$lib/types/token';
import type { ResultSuccess } from '$lib/types/utils';
import { replacePlaceholders } from '$lib/utils/i18n.utils';
import { loadSolLamportsBalance } from '$sol/api/solana.api';
import { mapNetworkIdToNetwork } from '$sol/utils/network.utils';
import { assertNonNullish } from '@dfinity/utils';
import { BigNumber } from '@ethersproject/bignumber';
import { get } from 'svelte/store';

export const loadSolBalance = async ({
address,
token
}: {
address: SolAddress;
token: Token;
}): Promise<ResultSuccess> => {
const {
id: tokenId,
network: { id: networkId }
} = token;

const solNetwork = mapNetworkIdToNetwork(networkId);

assertNonNullish(
solNetwork,
replacePlaceholders(get(i18n).init.error.no_solana_network, {
$network: networkId.description ?? ''
})
);

try {
const balance = await loadSolLamportsBalance({ address, network: solNetwork });

balancesStore.set({ tokenId, data: { data: BigNumber.from(balance), certified: false } });

return { success: true };
} catch (err: unknown) {
balancesStore.reset(tokenId);

// We don't want to disrupt the user experience if we can't load the balance.
console.error(`Error fetching ${tokenId.description} balance data:`, err);

return { success: false };
}
};
23 changes: 23 additions & 0 deletions src/frontend/src/tests/sol/services/sol-balance.services.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { SOLANA_TESTNET_TOKEN } from '$env/tokens/tokens.sol.env';
import { balancesStore } from '$lib/stores/balances.store';
import type { Token } from '$lib/types/token';
import { loadSolBalance } from '$sol/services/sol-balance.services';
import { mockSolAddress } from '$tests/mocks/sol.mock';
import { get } from 'svelte/store';

describe('sol-balance.services', () => {
// TODO: change DEVNET to use the normal RPC and not alchemy, and add it to this tests
const solanaTokens: Token[] = [SOLANA_TESTNET_TOKEN];

describe('loadSolBalance', () => {
it.each(solanaTokens)(
'should return the balance in SOL of the $name native token for the address',
async (token) => {
const result = await loadSolBalance({ address: mockSolAddress, token });

expect(result).toEqual({ success: true });
expect(get(balancesStore)?.[token.id]?.data.toNumber()).toBeGreaterThanOrEqual(0);
}
);
});
});

0 comments on commit 96e5ad0

Please sign in to comment.