I have StorageDoubleMap
for ERC1155 balances u32 -> AccountId -> u32
;
How can I mutate this map like balances[1][SomeAccountId] -= 4
?
I have StorageDoubleMap
for ERC1155 balances u32 -> AccountId -> u32
;
How can I mutate this map like balances[1][SomeAccountId] -= 4
?
Generally, you would use this syntax of try_mutate
similar to e.g.
<Balances<T>>::try_mutate(&asset_id, &owner, |balance| {
*balance -= 4;
Ok(())
})
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);
})
It's always good to search in the Substrate repository first.
/frame/im-online/src/lib.rs::mutate
AuthoredBlocks::<T>::mutate(¤t_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.