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

Denominated units for Balances #673

Closed
ascjones opened this issue Aug 4, 2022 · 0 comments · Fixed by #750
Closed

Denominated units for Balances #673

ascjones opened this issue Aug 4, 2022 · 0 comments · Fixed by #750
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@ascjones
Copy link
Collaborator

ascjones commented Aug 4, 2022

Extracted from #432.

Any balances should be displayed denominated as the currency of the chain e.g. 100CAN, 1MCAN, instead of e.g. 10000000000000000000000000000

Details

By default all balances are represented by a u128 which is the smallest unit of a chain's balance e.g. in Polkadot it is called a Planck.

So in this case an account with 1 DOT == 10_000_000_000 Plancks.

At the moment we would display the big number which is obviously not very user friendly.

So we should convert the raw units into the denominated token. You can query the system_properties rpc to fetch arbitrary JSON encoded properties defined in the chainspec. By convention it may contain the following useful properties:

"properties": {
    "tokenDecimals": 12,
    "tokenSymbol": "KSM"
  },

These can be used to convert the raw units into more human readable balances.

If those properties are not present we should consider defaulting to some sensible defaults. I suggest looking at how polkadot.js handles this for inspiration: https://github.com/polkadot-js/api/search?q=tokenDecimals

Displaying it

The other thing to figure out is how to identify where such a Balance type is being used, and then to decode it into a custom type that can display the required conversion.

Suggest looking at how other custom display types are registered e.g. with AccountId and Hash types: https://github.com/paritytech/cargo-contract/blob/f903c0349ba97b0219e14efcd81f67f4c48733eb/transcode/src/lib.rs#L154-L156

The slightly tricky thing might be that Balance is typically a primitive u128 type and not its own type, so it may be required to check the typeName field of the type metadata to see if it is Balance or T::Balance, since usually a type alias or associated type is used for this. Needs some investigation though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants