Skip to content

Commit

Permalink
Fix transferrable balance calculation (#335)
Browse files Browse the repository at this point in the history
* Apply correct transferrable balance formula

* add changeset

* add unit tests for calculateSystemAccountBalance function
  • Loading branch information
mmaurello authored Sep 2, 2024
1 parent 04145d8 commit 6f6cfa5
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/famous-buttons-greet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@moonbeam-network/xcm-builder': patch
---

Apply correct transferrable balance formula
85 changes: 84 additions & 1 deletion packages/builder/src/balance/BalanceBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@
import { describe, expect, it } from 'vitest';

import { TypeRegistry, U128 } from '@polkadot/types';
import {
FrameSystemAccountInfo,
PalletBalancesAccountData,
} from '@polkadot/types/lookup';
import { SubstrateQueryConfig } from '../types/substrate/SubstrateQueryConfig';
import { BalanceBuilder } from './BalanceBuilder';
import {
BalanceBuilder,
calculateSystemAccountBalance,
} from './BalanceBuilder';
import { PalletBalancesAccountDataOld } from './BalanceBuilder.interfaces';

function balanceOf(number: number | string): U128 {
return new U128(new TypeRegistry(), number);
Expand Down Expand Up @@ -193,4 +201,79 @@ describe('balanceBuilder', () => {
});
});
});

describe('calculateSystemAccountBalance', () => {
it('should correctly calculate balance with PalletBalancesAccountData', async () => {
const response = {
data: {
flags: balanceOf(0),
free: balanceOf(1000),
frozen: balanceOf(300),
reserved: balanceOf(200),
} as PalletBalancesAccountData,
} as FrameSystemAccountInfo;

const result = await calculateSystemAccountBalance(response);
expect(result).toBe(BigInt(900));
});

it('should correctly calculate balance with PalletBalancesAccountDataOld', async () => {
const response = {
data: {
feeFrozen: balanceOf(0),
free: balanceOf(1000),
miscFrozen: balanceOf(300),
reserved: balanceOf(200),
} as PalletBalancesAccountDataOld,
} as unknown as FrameSystemAccountInfo;

const result = await calculateSystemAccountBalance(response);
expect(result).toBe(BigInt(900));
});

it('should handle when reserved is greater than frozen', async () => {
const response = {
data: {
flags: balanceOf(0),
free: balanceOf(1000),
frozen: balanceOf(300),
reserved: balanceOf(400),
} as PalletBalancesAccountData,
} as FrameSystemAccountInfo;

const result = await calculateSystemAccountBalance(response);
expect(result).toBe(BigInt(1000));
});

it('should return 0 when all balances are zero', async () => {
const response = {
data: {
flags: balanceOf(0),
free: balanceOf(0),
frozen: balanceOf(0),
reserved: balanceOf(0),
} as PalletBalancesAccountData,
} as FrameSystemAccountInfo;

const result = await calculateSystemAccountBalance(response);
expect(result).toBe(BigInt(0));
});

it('should handle large numbers correctly', async () => {
const largeNumber = '123456789012345678901234567890';
const response = {
data: {
flags: balanceOf(0),
free: balanceOf(largeNumber),
frozen: balanceOf(largeNumber),
reserved: balanceOf(100),
} as PalletBalancesAccountData,
} as FrameSystemAccountInfo;

const result = await calculateSystemAccountBalance(response);
expect(result).toBe(
BigInt(largeNumber) - (BigInt(largeNumber) - BigInt(100)),
);
});
});
});
5 changes: 4 additions & 1 deletion packages/builder/src/balance/BalanceBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,10 @@ export async function calculateSystemAccountBalance(
const balance = response.data as PalletBalancesAccountData &
PalletBalancesAccountDataOld;

const free = BigInt(balance.free.toString());
const frozen = balance.miscFrozen ?? balance.frozen;
const frozenMinusReserved = BigInt(frozen.sub(balance.reserved).toString());
const locked = frozenMinusReserved < 0n ? 0n : frozenMinusReserved;

return BigInt(balance.free.sub(frozen).add(balance.reserved).toString());
return free - locked;
}

0 comments on commit 6f6cfa5

Please sign in to comment.