1

I am working with the local-setup and I have a smart contract to vote for a provider. In my schema:

structs: 
    Proposal: 
        provider: String # provider name
        value: Uint64 # service value 
        votes: Uint64 # votes amount 
state: 
    proposals: Proposal[] 

and I have a function to vote:

let propuestas: ArrayOfMutableProposal = f.state.proposals();
    let nr_propuestas = propuestas.length();
let proveedor = f.params.proveedor().value();
for j in 0..nr_propuestas{
        let mut prop: Proposal = propuestas.get_proposal(j).value();
        if prop.provider== proveedor{
            propuestas.get_proposal(j).value().votes = prop.votos + 1;

        }
    }

but the value of votes does not change, and when I try to define the winner, all proposals have 0 votes.

Could you help me please? Thank you

1 Answer 1

1

To solve that you have to think in proxies.

What you are doing is get the struct from state storage through the proxy and set the struct value, but you do not send the struct back to through the proxy to the state storage, You could do something like this:

for j in 0..nr_propuestas{
        let prop_proxy = propuestas.get_proposal(j);
        let mut prop = prop_proxy.value();
        if prop.provider == proveedor{
            prop.votes += 1;
            prop_proxy.set_value(&prop);
        }
    }

Note the caching of prop_proxy. Any time you use the same proxy multiple times in your code it will pay off to cache the proxy in a variable to prevent having to build the proxy again, thereby improving speed and reducing gas.

Credit goes to Aide Majin and Eric Hop on the IOTA&Shimmer Discord

To learn more about WASM-VM on Shimmer read here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.