diff --git a/.changeset/sixty-pens-remain.md b/.changeset/sixty-pens-remain.md new file mode 100644 index 00000000..41d25429 --- /dev/null +++ b/.changeset/sixty-pens-remain.md @@ -0,0 +1,5 @@ +--- +'@moonbeam-network/xcm-builder': patch +--- + +Add reserved balance to transferrable balance diff --git a/packages/builder/src/balance/BalanceBuilder.test.ts b/packages/builder/src/balance/BalanceBuilder.test.ts index 55557134..6f53fa01 100644 --- a/packages/builder/src/balance/BalanceBuilder.test.ts +++ b/packages/builder/src/balance/BalanceBuilder.test.ts @@ -54,7 +54,23 @@ describe('balanceBuilder', () => { it('should transform correctly with frozen balance', async () => { await expect( config.transform({ - data: { free: balanceOf(999), frozen: balanceOf(99) }, + data: { + free: balanceOf(999), + frozen: balanceOf(99), + reserved: balanceOf(0), + }, + }), + ).resolves.toMatchSnapshot(); + }); + + it('should transform correctly with reserved balance', async () => { + await expect( + config.transform({ + data: { + free: balanceOf(999), + frozen: balanceOf(99), + reserved: balanceOf(99), + }, }), ).resolves.toMatchSnapshot(); }); @@ -62,7 +78,11 @@ describe('balanceBuilder', () => { it('should transform correctly with miscFrozen balance', async () => { await expect( config.transform({ - data: { free: balanceOf(999), miscFrozen: balanceOf(99) }, + data: { + free: balanceOf(999), + miscFrozen: balanceOf(99), + reserved: balanceOf(0), + }, }), ).resolves.toMatchSnapshot(); }); @@ -164,7 +184,11 @@ describe('balanceBuilder', () => { it('should transform correctly', async () => { await expect( - config.transform({ free: balanceOf(999), frozen: balanceOf(99) }), + config.transform({ + free: balanceOf(999), + frozen: balanceOf(99), + reserved: balanceOf(0), + }), ).resolves.toMatchSnapshot(); }); }); diff --git a/packages/builder/src/balance/BalanceBuilder.ts b/packages/builder/src/balance/BalanceBuilder.ts index 6fd70681..165261e7 100644 --- a/packages/builder/src/balance/BalanceBuilder.ts +++ b/packages/builder/src/balance/BalanceBuilder.ts @@ -82,13 +82,7 @@ function system() { args: [address], transform: async ( response: FrameSystemAccountInfo, - ): Promise => { - const balance = response.data as PalletBalancesAccountData & - PalletBalancesAccountDataOld; - const frozen = balance.miscFrozen ?? balance.frozen; - - return BigInt(balance.free.sub(frozen).toString()); - }, + ): Promise => calculateSystemAccountBalance(response), }), }), accountEquilibrium: (): BalanceConfigBuilder => ({ @@ -139,13 +133,7 @@ function system() { args: [substrateAddress], transform: async ( response: FrameSystemAccountInfo, - ): Promise => { - const balance = response.data as PalletBalancesAccountData & - PalletBalancesAccountDataOld; - const frozen = balance.miscFrozen ?? balance.frozen; - - return BigInt(balance.free.sub(frozen).toString()); - }, + ): Promise => calculateSystemAccountBalance(response), }); }, }), @@ -169,3 +157,14 @@ function tokens() { }), }; } + +export async function calculateSystemAccountBalance( + response: FrameSystemAccountInfo, +): Promise { + const balance = response.data as PalletBalancesAccountData & + PalletBalancesAccountDataOld; + + const frozen = balance.miscFrozen ?? balance.frozen; + + return BigInt(balance.free.sub(frozen).add(balance.reserved).toString()); +} diff --git a/packages/builder/src/balance/__snapshots__/BalanceBuilder.test.ts.snap b/packages/builder/src/balance/__snapshots__/BalanceBuilder.test.ts.snap index 27ca6e00..2369c5e0 100644 --- a/packages/builder/src/balance/__snapshots__/BalanceBuilder.test.ts.snap +++ b/packages/builder/src/balance/__snapshots__/BalanceBuilder.test.ts.snap @@ -31,6 +31,8 @@ exports[`balanceBuilder > system > account > should transform correctly with fro exports[`balanceBuilder > system > account > should transform correctly with miscFrozen balance 1`] = `900n`; +exports[`balanceBuilder > system > account > should transform correctly with reserved balance 1`] = `999n`; + exports[`balanceBuilder > system > accountEquilibrium > should be correct config 1`] = ` SubstrateQueryConfig { "args": [