PyTEAL: Increment slot value in a Seq expression - Algorand
PyTEAL: Increment slot value in a Seq expression
post by hernandp on Oct 25, 2021
How I can increment a slot value by 1 without PyTEAL compiler yelling about store/load order?
e.g:
def verify():
verify_from = Txn.application_args[0]
verify_to = Txn.application_args[1]
bitfield = ScratchVar(TealType.uint64, SLOTID_VERIFIED_BITS)
return Seq([
Assert(And(is_proper_group_size(),\
Txn.group_index() < Global.group_size(),\
Txn.group_index() > Int(0),\
Btoi(verify_to) > Btoi(verify_from),\
Txn.sender() == App.globalGet(Bytes("vphash")))) ,\
bitfield.store(bitfield.load() + Int(1)),\
Approve()])
It wont me let to do bitfield.store(bitfield.load() + 1), but this should be pretty “transpilable” to TEAL without problems. Should be noted that bitfield is a fixed slot at position SLOTID_VERIFIED_BITS.
Thanks in advance.
post by fabrice on Oct 25, 2021
I added a comment on your Issue to handle that too.
post by jasonpaulos on Oct 28, 2021
I believe this is a different problem from the linked github issue. Right now PyTeal has a strict requirement that a scratch slot cannot be loaded unless it has a value stored in it first, and I believe that’s the problem here.
If you were to add bitfield.store(Int(0)) somewhere before that section of code is called, it should fix the issue.
Though I recognize this constraint can be a bit annoying, since it’s sometimes useful to rely on the default value of 0 for scratch slots. Let us know if you think this safety check should be somehow made optional or turned into a warning instead.
post by hernandp on Nov 17, 2021
Maybe this is necroposting, but Jason, you are right. Storing the value first made it work. Thank you!