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

Rework api.derive.balance.{account, all} to work with the new frame account data #5955

Merged
merged 4 commits into from
Aug 18, 2024

Conversation

TarikGul
Copy link
Member

@TarikGul TarikGul commented Aug 18, 2024

closes: #5856

Changes

DeriveBalancesAllAccountData: This now adds a new field transferable which has a type of Balance | null. This will only be applicable if the underlying runtime supports FrameSystemAccountInfo type.

export interface DeriveBalancesAllAccountData extends DeriveBalancesAccountData {
  /**
   * Calculated available balance. This uses the formula: max(0, free - locked)
   * This is only correct when the return type of `api.query.system.account` is `AccountInfo` which was replaced by `FrameSystemAccountInfo`.
   * See `transferable` for the correct balance calculation.
   *
   * ref: https://github.com/paritytech/substrate/pull/12951
   */
  availableBalance: Balance;
  /**
   * The amount of balance locked away.
   */
  lockedBalance: Balance;
  /**
   * The breakdown of locked balances.
   */
  lockedBreakdown: (PalletBalancesBalanceLock | BalanceLockTo212)[];
  /**
   * Calculated transferable balance. This uses the formula: free - max(frozen - reserve, ed)
   * This is only correct when the return type of `api.query.system.account` is `FrameSystemAccountInfo`.
   * Which is the most up to date calulcation for transferrable balances.
   *
   * ref: https://github.com/paritytech/polkadot-sdk/issues/1833
   */
  transferable: Balance | null;
  /**
   * Amount locked in vesting.
   */
  vestingLocked: Balance;
}

DeriveBalancesAccountData: This now adds an optional field frameSystemAccountInfo which returns 2 fields (frozen, and flags) that were not present in older AccountData formats.

export interface DeriveBalancesAccountData {
  frameSystemAccountInfo?: {
    frozen: Balance;
    flags: Balance;
  }
  freeBalance: Balance;
  frozenFee: Balance;
  frozenMisc: Balance;
  reservedBalance: Balance;
  votingBalance: Balance;
}

Transferable Formula

free - max(frozen - reserve, ed)

edit: Formula changed to free - max(0, frozen - reserve) in #5956

@TarikGul TarikGul marked this pull request as ready for review August 18, 2024 20:02
@TarikGul TarikGul changed the title Rework api.derive.balance.account and all to work with the new frame account data Rework api.derive.balance.{account, all} to work with the new frame account data Aug 18, 2024
@TarikGul TarikGul added the -auto label Aug 18, 2024
@polkadot-js-bot polkadot-js-bot merged commit e2b4e0d into master Aug 18, 2024
6 checks passed
@polkadot-js-bot polkadot-js-bot deleted the tg-balance branch August 18, 2024 21:51
@joepetrowski
Copy link

This is nice since so many people recalculate this. Is it broadcast anywhere?

@TarikGul
Copy link
Member Author

This is nice since so many people recalculate this. Is it broadcast anywhere?

I haven't thought about broadcasting this, but totally can in channels.

The release should be out today then propagated downstream in the pjs libs to apps.

@kianenigma
Copy link
Contributor

kianenigma commented Aug 20, 2024

This is nice since so many people recalculate this. Is it broadcast anywhere?

Yes, but I'd actually say this code is a great example to be moved into a runtime api. The runtime knows end of the day how much is transferrable better than anyone else.

@TarikGul
Copy link
Member Author

Yes, I totally agree this should be a runtime-api. I think back to the TransactionPaymentApi::query_fee_fetails as a similar example to how the runtime-api really had a 100x positive impact.

@polkadot-js-bot
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@polkadot-js polkadot-js locked as resolved and limited conversation to collaborators Aug 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix transferable balance in api.derive.balances.all with new formula
4 participants