Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

132
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda