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!


Related topics

Topic Replies Views Activity
PyTEAL: Loop load from scratch slots 3 441 Oct 2021
AssetParam not working pyteal 6 503 Apr 2022
TealInputError: Argument is not a PyTeal expression: (<pyteal.BinaryExpr object at 0x000002695E9A4CD0>,) 1 588 Apr 2022
PyTeal App.localPut does not work as I expected 2 540 May 2022
Using Global get Ex on Noop call giving error when deploying app 1 688 Dec 2021