-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(frontend): add Solana balance service util (#4073)
# Motivation Small util to return the balance in Solana and set it in the `balanceStore`, given a generic `token`.
- Loading branch information
1 parent
9f7c6b7
commit 96e5ad0
Showing
4 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
src/frontend/src/tests/sol/services/sol-balance.services.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
); | ||
}); | ||
}); |