From c81e34de11c7a1c2ea45fb81f7e9318adec2da76 Mon Sep 17 00:00:00 2001 From: Pegasus <83475418+0xp3gasus@users.noreply.github.com> Date: Thu, 19 Sep 2024 12:09:52 +0200 Subject: [PATCH] Radix balance (#1278) * Standarise balance * Changeset version file * Test fix * Test fixed --- .changeset/old-olives-hunt.md | 5 ++++ .../xchain-radix/__tests__/client.test.ts | 26 ++++++++--------- packages/xchain-radix/src/client.ts | 12 ++++---- packages/xchain-radix/src/const.ts | 2 +- packages/xchain-radix/src/radix-client.ts | 28 ++++++++++--------- 5 files changed, 41 insertions(+), 32 deletions(-) create mode 100644 .changeset/old-olives-hunt.md diff --git a/.changeset/old-olives-hunt.md b/.changeset/old-olives-hunt.md new file mode 100644 index 000000000..20fcec6a1 --- /dev/null +++ b/.changeset/old-olives-hunt.md @@ -0,0 +1,5 @@ +--- +'@xchainjs/xchain-radix': patch +--- + +Native balance always returned diff --git a/packages/xchain-radix/__tests__/client.test.ts b/packages/xchain-radix/__tests__/client.test.ts index 3f60eb9b6..7c97e0141 100644 --- a/packages/xchain-radix/__tests__/client.test.ts +++ b/packages/xchain-radix/__tests__/client.test.ts @@ -7,7 +7,7 @@ import { } from '@radixdlt/babylon-gateway-api-sdk' import { Convert, Instruction, RadixEngineToolkit } from '@radixdlt/radix-engine-toolkit' import { Balance, Fees, Network, XChainClientParams } from '@xchainjs/xchain-client' -import { Asset, AssetType, baseAmount } from '@xchainjs/xchain-util' +import { AssetType, baseAmount } from '@xchainjs/xchain-util' // eslint-disable-next-line ordered-imports/ordered-imports import { generateMnemonic } from 'bip39' @@ -20,7 +20,7 @@ import { stateEntityFungiblesPageResponse, stateEntityNonFungiblesPageResponse, } from '../__mocks__/mocks' -import { AssetXRD, Client, Tx, TxParams, XRD_DECIMAL, feesEstimationPublicKeys } from '../src' +import { AssetXRD, Client, RadixChain, Tx, TxParams, XRD_DECIMAL, feesEstimationPublicKeys } from '../src' describe('RadixClient Test', () => { const createClient = (): Client => { @@ -264,20 +264,20 @@ describe('RadixClient Test', () => { const stateEntityDetailsResponseMock = jest.fn().mockResolvedValue(mockEntityDeatilsResponse) client.radixClient.gatewayClient.state.getEntityDetailsVaultAggregated = stateEntityDetailsResponseMock - const assets: Asset[] = [ - { - symbol: 'resource_rdx1tknxxxxxxxxxradxrdxxxxxxxxx009923554798xxxxxxxxxradxrd', - ticker: 'resource_rdx1tknxxxxxxxxxradxrdxxxxxxxxx009923554798xxxxxxxxxradxrd', - chain: 'radix', - type: AssetType.NATIVE, - }, - ] const balances: Balance[] = await client.getBalance( 'account_rdx16x47guzq44lmplg0ykfn2eltwt5wweylpuupstsxnfm8lgva7tdg2w', - assets, + [ + { + chain: RadixChain, + symbol: 'resource_rdx1th2hexq3yrz8sj0nn3033gajnj7ztl0erp4nn9mcl5rj9au75tum0u', + ticker: 'resource_rdx1th2hexq3yrz8sj0nn3033gajnj7ztl0erp4nn9mcl5rj9au75tum0u', + type: AssetType.TOKEN, + }, + ], ) - expect(balances.length).toBe(1) - expect(balances[0].asset.symbol).toBe('resource_rdx1tknxxxxxxxxxradxrdxxxxxxxxx009923554798xxxxxxxxxradxrd') + expect(balances.length).toBe(2) + expect(balances[0].asset.symbol).toBe('XRD') + expect(balances[1].asset.symbol).toBe('resource_rdx1th2hexq3yrz8sj0nn3033gajnj7ztl0erp4nn9mcl5rj9au75tum0u') }) it('client should be able to estimate the fee for a given transaction', async () => { diff --git a/packages/xchain-radix/src/client.ts b/packages/xchain-radix/src/client.ts index 5e399ab8d..189203179 100644 --- a/packages/xchain-radix/src/client.ts +++ b/packages/xchain-radix/src/client.ts @@ -26,7 +26,7 @@ import { singleFee, } from '@xchainjs/xchain-client' import { getSeed } from '@xchainjs/xchain-crypto' -import { Address, AssetType, baseAmount } from '@xchainjs/xchain-util' +import { Address, AssetType, TokenAsset, baseAmount, eqAsset } from '@xchainjs/xchain-util' import { bech32m } from 'bech32' import BIP32Factory, { BIP32Interface } from 'bip32' import { derivePath } from 'ed25519-hd-key' @@ -42,7 +42,7 @@ import { xrdRootDerivationPaths, } from './const' import { RadixSpecificClient } from './radix-client' -import { Balance, CompatibleAsset, Tx, TxFrom, TxParams, TxTo, TxsPage } from './types/radix' +import { Balance, Tx, TxFrom, TxParams, TxTo, TxsPage } from './types/radix' import { getAssetResource } from './utils' const xChainJsNetworkToRadixNetworkId = (network: Network): number => { @@ -252,14 +252,16 @@ export default class Client extends BaseXChainClient { * @param {Asset[]} assets - Assets to retrieve the balance for (optional). * @returns {Promise} An array containing the balance of the address. */ - async getBalance(address: Address, assets?: CompatibleAsset[]): Promise { + async getBalance(address: Address, assets?: TokenAsset[]): Promise { const balances: Balance[] = await this.radixSpecificClient.fetchBalances(address) // If assets is undefined, return all balances if (!assets) { return balances } - const filteredBalances: Balance[] = balances.filter((balance) => - assets.some((asset) => balance.asset.symbol === asset.symbol), + const filteredBalances: Balance[] = balances.filter( + (balance) => + eqAsset(balance.asset, this.getAssetInfo().asset) || + assets.some((asset) => balance.asset.symbol === asset.symbol), ) return filteredBalances } diff --git a/packages/xchain-radix/src/const.ts b/packages/xchain-radix/src/const.ts index 70742dc3f..ea9357492 100644 --- a/packages/xchain-radix/src/const.ts +++ b/packages/xchain-radix/src/const.ts @@ -13,7 +13,7 @@ export const STOKENET_GATEWAY_URL = 'https://stokenet.radixdlt.com' export const XRD_DECIMAL = 18 -export const RADIX_ASSET_RESOURCE = 'resource_tdx_2_1tknxxxxxxxxxradxrdxxxxxxxxx009923554798xxxxxxxxxtfd2jc' +export const RADIX_ASSET_RESOURCE = 'resource_rdx1tknxxxxxxxxxradxrdxxxxxxxxx009923554798xxxxxxxxxradxrd' export const AssetXRD: Asset = { symbol: `XRD`, diff --git a/packages/xchain-radix/src/radix-client.ts b/packages/xchain-radix/src/radix-client.ts index c1a317bce..1a72b9ca1 100644 --- a/packages/xchain-radix/src/radix-client.ts +++ b/packages/xchain-radix/src/radix-client.ts @@ -28,10 +28,10 @@ import { enumeration, generateRandomNonce, } from '@radixdlt/radix-engine-toolkit' -import { AssetType, TokenAsset, assetAmount, assetToBase } from '@xchainjs/xchain-util' +import { AssetType, assetAmount, assetToBase } from '@xchainjs/xchain-util' -import { RadixChain } from './const' -import { Balance, MethodToCall } from './types/radix' +import { AssetXRD, RADIX_ASSET_RESOURCE, RadixChain } from './const' +import { Balance, CompatibleAsset, MethodToCall } from './types/radix' type PartialTransactionPreviewResponse = { receipt: { @@ -123,22 +123,24 @@ export class RadixSpecificClient { }) batch.forEach((item) => { if (item.aggregation_level === 'Global') { - const asset: TokenAsset = { - chain: RadixChain, - symbol: item.resource_address, - ticker: item.resource_address, - type: AssetType.TOKEN, - } + const asset: CompatibleAsset = + item.resource_address === RADIX_ASSET_RESOURCE + ? AssetXRD + : { + chain: RadixChain, + symbol: item.resource_address, + ticker: item.resource_address, + type: AssetType.TOKEN, + } const divisibility = divisibilities.get(item.resource_address) || 0 // We need to do this because item.amount can be either string or number // depending on the type of resource const amount = typeof item.amount === 'string' ? parseFloat(item.amount) : item.amount - const balance: Balance = { - asset: asset, + balances.push({ + asset, amount: assetToBase(assetAmount(amount, divisibility)), - } - balances.push(balance) + }) } }) }