Let's Play Algoriddle

Last week Algorand launched the ability for developers to write programs on Algorand that manage the logic of Algorand wallets and assets. I’ve been tracking this Algorand feature for a while and was excited to finally get to test it out.

So over the Thanksgiving holiday, inspired by Coin Artist’s Bitcoin puzzles, I built a small puzzle game on Algorand, called Algoriddle. I put 100 Algos in an escrow wallet on Algorand. The secret to unlocking the wallet and withdrawing the funds is hidden in this puzzle. Be the first to solve the riddle, and you will be able to unlock the Algos.

Once you solve the riddle you will need to send the solution to the Algoriddle wallet via on-chain transaction. Instructions for how to do that are at algoriddle.xyz.

Algoriddle is built using Algorand’s new smart contract functionality, launched as part of Algorand 2.0 last week. Here is how Algorand’s smart contracts work: smart contracts are logic programs tied to wallets. These programs act as a sort of bouncer in front of a wallet. Whenever there is a transaction made to a wallet that has a smart contract tied to it, the logic executes first. That logic can gate whether a transaction is accepted or denied, and it can also manage what should happen after a transaction is accepted. 

Programs are written in a custom Algorand-specific language called Teal. Teal is a stack-based assembly language - it’s a little more bare bones than a scripting language like Javascript, but then the benefit is that programs end up being quite compact and neat. The smart contract program managing the Algoriddle wallet even fits in a tweet!

Teal may seem intimidating at first, but it is actually neat. To explain line by line the program above, the first line pushes the argument passed into the program in the transaction to the stack. Then the next line hashes the argument using the sha256 hash function and pushes that to the stack. The third line pushes the known passcode (already sha256 hashed and base64 encoded) to the stack. Then the fourth line compares the two. The last three lines check that the transaction invoking the contract has an address to close the account funds to. If the argument passed in is the same as the passcode and there is an address set to send the account funds, the program passes. Not so daunting!

To debug, the Algorand team supplies a “dryrun” function where you can run programs locally and then inspect the bytes in the stack at each instruction throughout the program. For example, given the program 1+1=2:

int 1
int 1
+
int 2
==

You can dry run this program and see indeed first 1 was pushed to the stack, then the second 1 then the addition was evaluated and pushed to the stack, then the result was pushed and compared. In the dryrun output below, the leftmost column shows the position (in bytes) within the program of the instruction. Then the name of the instruction is in the column that follows. Then after the => is the resulting value of the operation shown in both decimal and hex that is pushed onto the stack.

tx[0] cost=6 trace:
  1 intcblock => <empty stack>
  5 intc_0 => 1 0x1
  6 intc_0 => 1 0x1
  7 + => 2 0x2
  8 intc_1 => 2 0x2
  9 == => 1 0x1

With Algorand smart contracts, you can fairly easily build programs that do things such as set accepted exchange rates between assets, or escrow funds and only release portions to designated parties after a certain date, you can even use Algorand smart contracts to quickly build new products like Groupon-meets-Kickstarter where only if a certain amount of funding is committed does the project actually get funded. I am excited to see what sorts of things people will build with Algorand smart contracts. 

If you build anything on Algorand, I would love to see it. If you try to solve the Algoriddle, good luck!

PS huge thanks to the Jason Weatherby from the Algorand team who was so responsive and helpful answering questions in the developer forum as I got up and running building Algoriddle.