algorand-devrel / beaker

A framework for writing Smart Contracts on Algorand
https://beaker.algo.xyz
MIT License
98 stars 33 forks source link

"Invalid Box Reference" #199

Closed MrJackdaw closed 1 year ago

MrJackdaw commented 1 year ago

👋 Hellow again

I modeled an original experiment with Boxes on your documentation example, and ran into an Invalid box reference error (below). Everything above message invalid Box referenc... at:448 is my insertion.

Note that this is where box_* first appears in the compiled teal code. Am I doing anything obviously wrong?

Error

Error joining DAO
[ 'stack', 'led', 'program', 'lines', 'teal_line', 'message' ]
message invalid Box referenc... at:448
teal_line 448 lines 5
stack load 44
itob
concat
store 45
load 39
box_del <--- Error
pop
load 39
load 45
box_put

Code

The property declaration

Contract Class property. DMember inherits from abi.NamedTuple and has four fields (see below)

    members = Mapping(abi.Address, DMember)
    """ Member records"""

The implementation

This method generates the erroring TEAL code.

    @external
    def register(self, member: abi.Address, fee: abi.PaymentTransaction) -> Expr:
        """Add new member to group"""

        return Seq(
            Assert(
                # REDACTED for clarity
            ),
            # Add new member
            (joined := abi.Uint64()).set(Global.round()),
            (last_proposal := abi.Uint64()).set(Int(0)),
            (strikes := abi.Uint64()).set(Int(0)),
            (loaned := abi.Uint64()).set(Int(0)),
            (nm := DMember()).set(joined, last_proposal, strikes, loaned),
            self.members[member].set(nm),
        )
barnjamin commented 1 year ago

Invalid box ref happens when you're not passing the box reference in with the transaction.

Note in the caller of the app we pass the boxes we intend to reference https://github.com/algorand-devrel/beaker/blob/master/examples/boxen/main.py#L68

The box name (as bytes) should match the box you're trying to access

MrJackdaw commented 1 year ago

Thanks @barnjamin ; that resolved the issue.

For anyone else: I found a helpful example from beaker-ts (since I'm using that too) here.

As usual; please close or lock as needed.