Hi I have a rust contract with React frontend and have a change method called donation. I would like to add attached_deposit and whomever the sender_account is to my HashMap supporters. If this is the first donation from that person then add them and their donation, otherwise add this deposit to what they already have donated. Currently I can only add them with insert which doesn't help as it overrides any existing value. I want to accumulate donations from people. What I have is as follows:
#[payable]
pub fn donation(
&mut self,
token_id: TokenId,
) {
assert_at_least_one_yocto();
//measure the initial storage being used on the contract
let initial_storage_usage = env::storage_usage();
let supporter_id = env::predecessor_account_id();
//get the token object form the token ID
let mut token = self.tokens_by_id.get(&token_id).expect("No token");
//insert supporter and amount
token.supporters.insert(supporter_id, env::attached_deposit());
//insert the metadata back into the token by ID
self.tokens_by_id.insert(&token_id, &token);
//calculate the required storage which was the used - initial
let required_storage_in_bytes = env::storage_usage() - initial_storage_usage;
//refund any excess storage if the user attached too much. Panic if they didn't attach enough to cover the required.
refund_deposit(required_storage_in_bytes);
}
I use the following in frontend:
await contract.donation({token_id: "token-2", supporter_id: "sp02.testnet"}, "300000000000000", "3000000000000000000000000")
Any help would be greatly appreciated!!