Skip to content

Int type uses LE as default in the toHex #5966

Open
@zjb0807

Description

@zjb0807

isLe = false should be isLe = true here. totalIssuance.toHex() returns big-endian encoding by default, which will differ from what u8aToHex returns.

public toHex (isLe = false): HexString {
// For display/JSON, this is BE, for compare, use isLe
return bnToHex(this, {
bitLength: this.bitLength(),
isLe,
isNegative: !this.isUnsigned
});
}

import '@polkadot/api-augment';
import { ApiPromise, WsProvider } from '@polkadot/api';
import { BN } from 'bn.js'
import { u8aToHex } from '@polkadot/util';
import { TypeRegistry } from '@polkadot/types';

async function main() {
  const provider = new WsProvider('wss://polkadot-rpc.publicnode.com');

  const api = await ApiPromise.create({ provider });

  const totalIssuance = await api.query.balances.totalIssuance();
  console.log(`totalIssuance Key: ${api.query.balances.totalIssuance.key()}, Value: ${totalIssuance.toString()}`);
  console.log(`totalIssuance HEX: ${totalIssuance.toHex(false)}`);
  console.log(`totalIssuance Hex(LE): ${totalIssuance.toHex(true)}`);
  console.log(`totalIssuance HEX(BE): ${totalIssuance.toHex(false)}`);
  console.log(`totalIssuance u8aToHex: ${u8aToHex(totalIssuance.toU8a())}`);

  await api.disconnect();
}

main();

output:

totalIssuance Key: 0xc2261276cc9d1f8598ea4b6a74b15c2f57c875e4cff74148e4628f264b974c80, Value: 14871543992046148402
totalIssuance HEX: 0x0000000000000000ce625677cb815b32
totalIssuance Hex(LE): 0x325b81cb775662ce0000000000000000
totalIssuance HEX(BE): 0x0000000000000000ce625677cb815b32
totalIssuance u8aToHex: 0x325b81cb775662ce0000000000000000

If the storage is defined as eraReward(u32): u128, to the data by entriesPaged, it is not the real storage key and value.

console.log(`${key.toHex()},${value.toHex()}`);

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3 - LowNon-essential improvements or minor fixes. Can be scheduled flexibly as time permits.

    Type

    No type

    Projects

    Status

    P3 - Low

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions