1

I have the following smartpy.bigmap in smart contract storage:

import smartpy as sp
self.data.init(bgmp=sp.bigmap(tkey=sp.TAddress,tvalue=sp.TMutez))

I would like to total all the stored tvalue so based on the SmartPy docs I tried:

total=sp.tez(0)
sp.for v in self.data.bgmp.values():
    total+=v

I then wish to sp.verify(total>=minAmount<sp.TMutez>)

When run in the latest SmartPy.io IDE the following error is produced:

Error:
Failure: Missing var v in env []

If I hardcode total+=sp.tez(1) the error becomes:

Error:
WrongCondition in line 18: (sp.tez(0) + sp.tez(1)) >= self.data.minAmount

I am doubtful that this is a correctly totaled sp.TMutez value.

Any help is much appreciated!

1
  • I am not able to populate the same big map ? can you help? poolMemberToDeposit = sp.big_map(tkey = sp.TAddress, tvalue = sp.TMutez) poolMemberToDeposit[sp.sender] = sp.amount; can it be done ? Mar 3, 2020 at 13:49

2 Answers 2

2

There is no way of iterating over all values in a big_map in the language to which SmartPy compiles (Michelson). So it doesn't surprise me that this doesn't work in SmartPy either. Indeed, according the SmartPy documentation:

e.values(): Returns the list of values of a map, sorted by keys.

Here, I presume that map refers exclusively to normal, "simple" maps and not big_maps.

However, there is probably some other way of solving your problem. For instance, you can keep track of the big_map's total in a separate variable, and update it each time the big_map is updated.

1
  • thanks for this. going to track the total with an extra variable Dec 6, 2019 at 19:15
1

@arvidj is right of course and you cannot iterate on a bigmap. You need to keep your sum in the storage.

You also have another error: if it was a map, you would still need to use a local variable to accumulate the sum. This is introduced by total = sp.newLocal('total', ...). What the error is telling you is that your v is escaping it`s scope. With 1, we could also analyze the mistake but don't currently. The only way to currently see it is to check the generated script (in the output panel) which shows a code that you don't expect.

Then, with local variables. To set a local variable (introduced by newLocal) you currently need to use total.set(...). It will be changed to total.value = ... because we observe that the current syntax is too confusing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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