I have two Balance type.

const a = registry.createType<Balance>('Balance', 10)
const b = registry.createType<Balance>('Balance', 20)

Is there a simpler way to calculate them? eg (+ - x /)

  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Jul 7, 2022 at 9:16

1 Answer 1


Any Balance type extends bn.js (API docs) so it has all the add, sub, etc.

This means for instance you can do -

const ex = api.consts.balances.existentialDeposit
const val = ex.muln(2).addn(123)

/* transfer 2 x existential + 123 */
await api.tx.balances.transfer("...", val)

/* if you wish to format different only */
const fmt = api.createType("Balance", val).toHuman()

You generally don't want to or need to use createType the JS API will convert the inputs to the correct formats, e.g where a Balance is required in a call, anything of type string, hex, BN, BigInt, number will work and will be converted.

In some cases, e.g. you want to add something and have stuff like toHuman in your code (as per the example above), it could be useful to create.

  • 'add function' return a BN type. Is there a simpler way to convert BN type to Balance?
    – kula
    Jul 7, 2022 at 9:19
  • 1
    Why does you need a specific balance type? You don't need this type to it pass anywhere for usage in the API - I guess I tried to convey this info from the docs above. If you need to create your own, see the last line in the example that does exactly that, BN -> Balance, and then uses the toHuman formatter on the value. Balance is just a BN - it just has some convenience Codec methods.
    – Werner S
    Jul 7, 2022 at 9:54
  • hm. thankyou . i just need a type to indicate balance type
    – kula
    Jul 7, 2022 at 13:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.