Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

131
Views
Payable method add or/add accumulate attached_deposit to Map

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!!

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!