0

I'm porting a script from TypeScript using the Polkadot.JS/api to Rust using Subxt.

    let query = polkadot::storage().referenda().referendum_info_for_root();

    let mut results = api.storage().at_latest().await?.iter(query,10).await?;

    while let Some((key, value)) = results.next().await? {

With reference to the above, I'm having trouble getting the referendum id from the key. Within my TypeScript code I would use something like key.toHuman()[0].

Regards,

2
  • What kind of trouble did you encounter? Sep 25 at 2:52
  • You dont have to use toHuman() here, have you tried to print the key and the value?
    – Alex Bean
    Sep 26 at 7:47

1 Answer 1

0

I got some help from @Turboflakes the developer behind CrunchBot. He referred me to an article and supported with some code to help 'decode' the Storage key.

https://www.shawntabrizi.com/substrate/transparent-keys-in-substrate/

Within the while loop I use:

get_ref_id_from_storage_key(key.0.to_vec())

The function's details are:

pub fn get_ref_id_from_storage_key(key: Vec<u8>) -> u32 {
    let s = &key[key.len() - 4..];
    let v: [u8; 4] = s.try_into().expect("slice with incorrect length");
    u32::from_le_bytes(v)
}

Your Answer

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

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