0

I have StorageDoubleMap for ERC1155 balances u32 -> AccountId -> u32;

How can I mutate this map like balances[1][SomeAccountId] -= 4?

2

3 Answers 3

3

Generally, you would use this syntax of try_mutate

similar to e.g.

<Balances<T>>::try_mutate(&asset_id, &owner, |balance| {
    *balance -= 4;
    Ok(())
})
2

To extend Lana's answer, you can just use mutate in the case that your operation is infallible:

<Balances<T>>::mutate(&asset_id, &owner, |balance| {
    *balance = balance.saturating_sub(4);
})
0

It's always good to search in the Substrate repository first.


/frame/im-online/src/lib.rs::mutate

AuthoredBlocks::<T>::mutate(&current_session, author, |authored| *authored += 1);

/frame/assets/src/lib.rs::try_mutate

Account::<T, I>::try_mutate(id, &who, |maybe_account| -> DispatchResult {
    maybe_account.as_mut().ok_or(Error::<T, I>::NoAccount)?.is_frozen = true;
    Ok(())
})?;

There are still a lot of examples. Check the Substrate repository.

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.