1 of 12

Monster Arena

A simple game idea leveraging boxes and ASAs

(alt. title: an excuse to make everybody learn a bit of TEAL)

2 of 12

Ewwww why plain TEAL tho?

  • Pyteal is syntactically similar to TEAL
  • Beaker is better at abstraction, but runs on top of Pyteal
  • Familiarizing ourselves with TEAL code is a good thing!
    • Ability to read closed source projects
    • Ability to write more performant code
  • Ideally a mixed approach (like what happens in embedded systems)
  • Lines of code ~= money

3 of 12

Exhibit A: First Hackathon advanced challenges

  • Lots of room for improvement in the Pyteal compiler!
  • From a sample of 12 contracts with glaring issues (dead code, constant expressions being re-computed multiple times, etc.)
  • Contestants had to add code to improve compilation
  • Tested scored with S(badAP, contestantAP) = 0.2 * Δsize + 0.8 * Δbudget
  • Adding just a few lines, two contestants managed to get up to 40 points!

4 of 12

Int 1337

5 of 12

But…

6 of 12

TEAL generalities

  • The stack is used to work with every opcode (depth 1000)
  • Only two data types: uint64 and byte arrays (4k max.)
  • Parameters first, operator at the end (postfix)
  • Non-negative integer arithmetic (e.g. 5-4 fails)
  • Boxes: bags of bytes for potentially unlimited storage
  • Approval / rejection based on top of stack on finishing (or returning)

7 of 12

Some important opcodes

  • Branching: bnz s, bz s, b s
  • Subroutines: callsub LABEL, retsub
  • Operators and logic and conversion: +, -, *, /, &&, ||, !=, ==, <, >, <=, >=, !
  • Type conversion: btoi, itob
  • Stack manipulation: push[bytes, int] X, pop, dig n, bury n, dup, swap
  • Scratchspace: load n, store n
  • Transaction data: txn f, txna f
  • State access: app_[global, local]_[get, put]
  • Box manipulation: box_[create, delete, extract, replace, get, put]
  • Errors and logging: assert, err, log

8 of 12

Snippet 1: branching and logic

9 of 12

Snippet 2: Reading from a box

10 of 12

Snippet 3: Local state get/put

11 of 12

Snippet 4: Loop

12 of 12

Monster Arena

  • A little game using the blockchain for data storage and rewards
  • There’s monsters with the following data: pos_x, pos_y, ASA_id
  • Monsters appear anywhere on the playing field
  • For the purpose of this example, map unbounded on non-negative ints. (x,y)
  • Monsters are stored in a 4k box, max. 170 active monsters (4096/24)
  • ASAs should be unique for every monster and minted on the spot
  • Monsters can be killed