I'm looking at this ethereum smart contract that iterates over an area in a 2D bool matrix to reserve pixels. When looking at a call to that ethereum smart contract for a 20x11 area, it used 0.0315 Ether ($9.36 on that date).

I made a Tezos smart contract with the same logic checks (except it's storing a sp.big_map(tkey=sp.TPair(sp.TNat, sp.TNat), tvalue = sp.TBool) instead of a 2D bool matrix) and I'm seeing really high burn fees. When running the same operation to reserve a 20x11 area, the contract burned 3.68 Tez because it's storing 14741 bytes. If all I care about is setting the value of 220bits, then the storage requirement should only be 27.5 bytes which should bring down the burn fee to .006678 Tez.

What's the most efficient way to encode a 2D bool matrix?


For a 20*11 area, you can use a bytes array of length round_up(20 * 11 / 8) = 28 bytes. The type for bytes is sp.TBytes in SmartPy.

However, there is no update in place operation so operations may be a bit complex and costly to write (but highly doable).

  • Any chances you have an example of how I could create that byte array? I'm trying this: new_grid = sp.pack(int("".join(["0"] * 10000), 2)) but I'm having issues with unpacking it. It also appears as new_grid=0x050000 when initializing it – Jaime Apr 15 at 4:53
  • 1
    You can do sp.bytes("0x" + 28 * "00"). – FFF Apr 15 at 12:00
  • ok thanks. is this roughly what the code should look like when doing updates? smartpy.io/… – Jaime Apr 15 at 19:15
  • 1
    Probably but you should never add so many strings or bytes in n operations. Anyway, I just thought of something. You can also use nats and bit operations. It may render the code much more efficient. – FFF Apr 15 at 19:38
  • ok, I'll try using nats and bit operations – Jaime Apr 15 at 23:46

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.