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

Int type uses LE as default in the toHex #5966

Open
zjb0807 opened this issue Aug 30, 2024 · 0 comments
Open

Int type uses LE as default in the toHex #5966

zjb0807 opened this issue Aug 30, 2024 · 0 comments

Comments

@zjb0807
Copy link

zjb0807 commented Aug 30, 2024

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()}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant