Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix transferable balance in api.derive.balances.all with new formula #5856

Open
TarikGul opened this issue Apr 19, 2024 · 2 comments
Open

Fix transferable balance in api.derive.balances.all with new formula #5856

TarikGul opened this issue Apr 19, 2024 · 2 comments
Assignees

Comments

@TarikGul
Copy link
Member

Currently the following code snippet shows that we are using an outdated balance formula for retrieving the transferable balance. Related issue: polkadot-js/apps#9988 (Please see the issue for more detail).

function calcLocked (api: DeriveApi, bestNumber: BlockNumber, locks: (PalletBalancesBalanceLock | BalanceLockTo212)[]): AllLocked {
let lockedBalance = api.registry.createType('Balance');
let lockedBreakdown: (PalletBalancesBalanceLock | BalanceLockTo212)[] = [];
let vestingLocked = api.registry.createType('Balance');
let allLocked = false;
if (Array.isArray(locks)) {
// only get the locks that are valid until passed the current block
lockedBreakdown = (locks as BalanceLockTo212[]).filter(({ until }): boolean => !until || (bestNumber && until.gt(bestNumber)));
allLocked = lockedBreakdown.some(({ amount }) => amount && amount.isMax());
vestingLocked = api.registry.createType('Balance', lockedBreakdown.filter(({ id }) => id.eq(VESTING_ID)).reduce((result: BN, { amount }) => result.iadd(amount), new BN(0)));
// get the maximum of the locks according to https://github.com/paritytech/substrate/blob/master/srml/balances/src/lib.rs#L699
const notAll = lockedBreakdown.filter(({ amount }) => amount && !amount.isMax());
if (notAll.length) {
lockedBalance = api.registry.createType('Balance', bnMax(...notAll.map(({ amount }): Balance => amount)));
}
}
return { allLocked, lockedBalance, lockedBreakdown, vestingLocked };
}

function calcShared (api: DeriveApi, bestNumber: BlockNumber, data: DeriveBalancesAccountData, locks: (PalletBalancesBalanceLock | BalanceLockTo212)[]): DeriveBalancesAllAccountData {
const { allLocked, lockedBalance, lockedBreakdown, vestingLocked } = calcLocked(api, bestNumber, locks);
return objectSpread({}, data, {
availableBalance: api.registry.createType('Balance', allLocked ? 0 : bnMax(new BN(0), data?.freeBalance ? data.freeBalance.sub(lockedBalance) : new BN(0))),
lockedBalance,
lockedBreakdown,
vestingLocked
});
}

The curent implementation needs to take into account:

https://github.com/paritytech/polkadot-sdk/blob/3069b0af398e5a0374802c31201637e97f4a530a/substrate/frame/balances/src/impl_fungible.rs#L44-L70

@TarikGul TarikGul self-assigned this Apr 19, 2024
@TarikGul
Copy link
Member Author

rel: paritytech/polkadot-sdk#1833

@TarikGul
Copy link
Member Author

This is currently what I am working with: max(free - (frozen - on_hold), free - ED)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

No branches or pull requests

1 participant