#How do you check if a box exists if its contents are too large? .maybe() and .length fail

1 messages · Page 1 of 1 (latest)

ocean hedge
#

Using box_ref.length adds an assert that the box exists, so it can't be used. I've tried manually removing the assert from the TEAL and placing the modified version into my app spec but I guess the AVM throws it back in and it can't be bypassed.

    box_len
    swap
    dup
    uncover 2
    assert // check BoxRef exists```

``` logic eval error: assert failed pc=107. Details: app=737469057, pc=107, opcodes=dup; uncover 2; assert```

Using `box_ref.maybe()[1]` still adds the box_get, which fails when the box content exceeds 4096 bytes:

```// box_exists = box_ref.maybe()[1]
    box_get
    bury 1```

```logic eval error: box_get produced a too big (4100) byte-array. Details: app=737470465, pc=102, opcodes=proto 5 0; bytec_0 // 0x0000000000000000; box_get```
#

I've also considered box_ref.get(), but it will just put me in the same situation as box_ref.maybe()

Retrieve the contents of the box, or return the default value if the box has not been created.

arg default : The default value to return if the box has not been created```
#

box_ref.create() will return True if the box was created, or False if it already existed, but it will also fail if the size parameter doesn't match the current size. Which means the only way it would work to verify if a box exists would be if I got the length of the box 😅

    *,
    size: UInt64 | int
) -> bool
Creates a box with the specified size, setting all bits to zero. Fails if the box already exists with a different size. Fails if the specified size is greater than the max box size (32,768)

Returns True if the box was created, False if the box already existed```

```logic eval error: box size mismatch 100 0. Details: app=737471345, pc=103, opcodes=bytec_0 // 0x0000000000000000; intc_1 // 0; box_create```
brittle verge
#

A: use tealscript. 😂

shy helm
#

don't let mr.patrick convert you to the dark side

ocean hedge
#

@quick tulip

shy helm
#

i don't think it's possible on a box ref object?

#
box_ref = BoxRef(key=b"abc123")
length, exists = op.Box.length(box_ref.key)
#

maybe this

#

?

ocean hedge
#

Nah, fails if it doesn't exist

#

Get the length of this Box. Fails if the box does not exist

#

It doesn't return a tuple either

#

Oh op.Box ?

shy helm
#

yes

ocean hedge
#

Wow thanks @shy helm

nova parcelBOT
#

Gave +1 Rep to @shy helm (current: #34 - 6)

shy helm
#

using the .length property on a box presupposes the box exists

#

so it will fail if it doesn't

#

op.Box.length() is different

valid badge
#

if key in self.boxmap

ocean hedge
valid badge
#

Ah right, you have a boxref not a boxmap. The opcode ogrp suggested is good then

brittle verge
#

Clearly Puya-py needs an exists method.
No one should have to call the raw opcode to work around such a basic thing.

valid badge
#

Right

#

Actually I think you can do if self.box

#

It should fallback on the bool dunder method of a boxref

ocean hedge
#

It would be cool if we could somehow abstract extracting, splicing and replacing with box maps to avoid using op— does BoxMap support indexing to avoid the 4096 byte array too big error?

Eg; self.some_box_map[key][0:4096]

valid badge
ocean hedge
#

No worries, thank you giorgio!

valid badge
#

Could you try the bool on the boxref? I'm not on my pc

ocean hedge
#

It does work but not if the box contents exceeds 4096 bytes, box_get portion fails for that reason

#

Any logic variation that would attempt box_get would fail

valid badge
#

Even if self.boxref: does?

ocean hedge
#

I didn’t attempt if self.boxref

#

Let me give it a try now

#

Yes if self.boxref works even when box exceeds 4096 bytes

#

But I do need to get the length anyways as well, and box_ref.length fails when box contents exceeds 4096 bytes

Using box_ref.length adds an assert that the box exists, so it can't be used. I've tried manually removing the assert from the TEAL and placing the modified version into my app spec but I guess the AVM throws it back in and it can't be bypassed.

    box_len
    swap
    dup
    uncover 2
    assert // check BoxRef exists```

``` logic eval error: assert failed pc=107. Details: app=737469057, pc=107, opcodes=dup; uncover 2; assert```