0

I want to deliver some test where in part of the task is to create accounts and deposit them. I do it in the code below:

let account_id_bytes: [u8; 32] = [0u8; 32];
let account_id_1: AccountId32 = AccountId32::new(account_id_bytes);

Now I want to fund account_id_1 like:

Balances::force_set_balance(RuntimeOrigin::root(), account_id_1, 100).unwrap();

Function force_set_balance expects u64, but I have AccountId32 type. How to fund the account right? My guess is that conversion AccountId32 to u64 is not the best approach. Appreciate any tips.

1 Answer 1

1

You have to remember that the Substrate runtime is generic over many types, including the AccountId.

Function force_set_balance expects u64

If this is the case, then the context in which you are calling force_set_balance has been configured for AccountId = u64. This is usually the default configuration in tests, which is my guess where this is happening.

You can adjust your configuration to make AccountId = AccountId32 instead, and that will fix this problem.

You can find an example of that in the Polkadot integration tests: https://github.com/paritytech/polkadot-sdk/blob/master/polkadot/runtime/common/src/integration_tests.rs

Here you can see:

use sp_runtime::AccountId32;

type AccountId = AccountId32;

This being used in the configuration for frame_system:

impl frame_system::Config for Test {
    type AccountId = AccountId;
    // ...
}

And some helpers to make generating unique AccountId32 more easy:

fn account_id(i: u32) -> AccountId32 {
    let b4 = i.encode();
    let b32 = [&b4[..], &b4[..], &b4[..], &b4[..], &b4[..], &b4[..], &b4[..], &b4[..]].concat();
    let array: [u8; 32] = b32.try_into().unwrap();
    array.into()
}

fn signed(i: u32) -> RuntimeOrigin {
    let account_id = account_id(i);
    RuntimeOrigin::signed(account_id)
}

So ultimately, go and check your frame_system configuration and make sure your AccountId type is what you want and expect.

1

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.