Skip to content

Commit

Permalink
Add reserved balance to transferrable balance (#333)
Browse files Browse the repository at this point in the history
* Add reserved balance to transferrable balance

* add changeset

* adjust tests
  • Loading branch information
mmaurello authored Aug 27, 2024
1 parent d225201 commit ede2f6e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/sixty-pens-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@moonbeam-network/xcm-builder': patch
---

Add reserved balance to transferrable balance
30 changes: 27 additions & 3 deletions packages/builder/src/balance/BalanceBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,35 @@ 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();
});

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();
});
Expand Down Expand Up @@ -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();
});
});
Expand Down
27 changes: 13 additions & 14 deletions packages/builder/src/balance/BalanceBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,7 @@ function system() {
args: [address],
transform: async (
response: FrameSystemAccountInfo,
): Promise<bigint> => {
const balance = response.data as PalletBalancesAccountData &
PalletBalancesAccountDataOld;
const frozen = balance.miscFrozen ?? balance.frozen;

return BigInt(balance.free.sub(frozen).toString());
},
): Promise<bigint> => calculateSystemAccountBalance(response),
}),
}),
accountEquilibrium: (): BalanceConfigBuilder => ({
Expand Down Expand Up @@ -139,13 +133,7 @@ function system() {
args: [substrateAddress],
transform: async (
response: FrameSystemAccountInfo,
): Promise<bigint> => {
const balance = response.data as PalletBalancesAccountData &
PalletBalancesAccountDataOld;
const frozen = balance.miscFrozen ?? balance.frozen;

return BigInt(balance.free.sub(frozen).toString());
},
): Promise<bigint> => calculateSystemAccountBalance(response),
});
},
}),
Expand All @@ -169,3 +157,14 @@ function tokens() {
}),
};
}

export async function calculateSystemAccountBalance(
response: FrameSystemAccountInfo,
): Promise<bigint> {
const balance = response.data as PalletBalancesAccountData &
PalletBalancesAccountDataOld;

const frozen = balance.miscFrozen ?? balance.frozen;

return BigInt(balance.free.sub(frozen).add(balance.reserved).toString());
}
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down

0 comments on commit ede2f6e

Please sign in to comment.