Skip to content

Commit

Permalink
Radix balance (#1278)
Browse files Browse the repository at this point in the history
* Standarise balance

* Changeset version file

* Test fix

* Test fixed
  • Loading branch information
0xp3gasus authored Sep 19, 2024
1 parent 00f4d4f commit c81e34d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 32 deletions.
5 changes: 5 additions & 0 deletions .changeset/old-olives-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@xchainjs/xchain-radix': patch
---

Native balance always returned
26 changes: 13 additions & 13 deletions packages/xchain-radix/__tests__/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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 => {
Expand Down Expand Up @@ -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 () => {
Expand Down
12 changes: 7 additions & 5 deletions packages/xchain-radix/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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 => {
Expand Down Expand Up @@ -252,14 +252,16 @@ export default class Client extends BaseXChainClient {
* @param {Asset[]} assets - Assets to retrieve the balance for (optional).
* @returns {Promise<Balance[]>} An array containing the balance of the address.
*/
async getBalance(address: Address, assets?: CompatibleAsset[]): Promise<Balance[]> {
async getBalance(address: Address, assets?: TokenAsset[]): Promise<Balance[]> {
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
}
Expand Down
2 changes: 1 addition & 1 deletion packages/xchain-radix/src/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down
28 changes: 15 additions & 13 deletions packages/xchain-radix/src/radix-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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)
})
}
})
}
Expand Down

0 comments on commit c81e34d

Please sign in to comment.