King of Tezos: a smart-ponzi on Tezos

While writing a new programming language, it is often useful to write some real use-cases to test the syntax, the language expressiveness and the code cleanness comparing to other languages.

So I did for yallo-lang, a (still experimental) smart contract language for Tezos; for starting I chose a contract already existing on ethereum, king_of_ether (that’s right guys, a smart-ponzi).

Our minimal implementation is very simple: a single endpoint which allow the caller to become the king if he sends the contract balance * 2; the old king is then dismissed, but he receives in exchange twice the amount he sent to become a king. And so on.

We start with the yallo contract:

contract KingOfTezos {
	field theKing: address;
	field currentPrice: mutez;

	entry beTheKing() {
		assert (Tezos.amount() >= this.currentPrice);
		let op = Tezos.transfer (this.theKing, this.currentPrice);
		this.theKing = Tezos.sender();
		this.currentPrice = Tezos.amount() * 2n;
		[ op ]
	}
}

We transpile it to ligo: yallo.exe compile king_of_tezos.yallo -target ligo

At that is the result:

type storage = {  theKing: address;  currentPrice: tez;  }

type action = | BeTheKing of unit

let beTheKing (s: storage) = 
  let ovverraidable = if ((Tezos.amount) >= (s.currentPrice)) then () else failwith "Assertion" in
  let op: operation = Tezos.transaction (unit) (s.currentPrice) (match (Tezos.get_contract_opt s.theKing : unit contract option) with| None -> (failwith "invalid contract": unit contract) | Some(c) -> c) in
  let s = { s with theKing=Tezos.sender } in
  let s = { s with currentPrice=(Tezos.amount) * (2n) } in
  (([op]: operation list), (s: storage))


let main(a, s: action * storage): (operation list * storage) = 
  match a with | BeTheKing (arg) -> beTheKing(s)

Then ligo compiler does the rest, and we got king.tz:

{ parameter unit ;
  storage (pair (mutez %currentPrice) (address %theKing)) ;
  code { DUP ;
         CDR ;
         DUP ;
         CAR ;
         AMOUNT ;
         COMPARE ;
         GE ;
         IF { PUSH unit Unit } { PUSH string "Assertion" ; FAILWITH } ;
         DIG 1 ;
         DUP ;
         DUG 2 ;
         CDR ;
         CONTRACT unit ;
         IF_NONE
           { PUSH string "invalid contract" ; FAILWITH }
           { DUP ; DIP { DROP } } ;
         DIG 2 ;
         DUP ;
         DUG 3 ;
         CAR ;
         UNIT ;
         TRANSFER_TOKENS ;
         DIG 2 ;
         DUP ;
         DUG 3 ;
         CAR ;
         SENDER ;
         SWAP ;
         PAIR ;
         DUP ;
         PUSH nat 2 ;
         AMOUNT ;
         MUL ;
         SWAP ;
         CDR ;
         SWAP ;
         PAIR ;
         DUP ;
         NIL operation ;
         DIG 4 ;
         DUP ;
         DUG 5 ;
         CONS ;
         PAIR ;
         DIP { DROP 6 } } }

We then try it on carthagenet:

  1. Deploy
     tezos-client originate contract kot transferring 1 from my_account running king.tz --init "(Pair 2000000 \"tz1VnNQMNQ796WrY2TyWDaFpwsRTP41mKXWH\")" --force --burn-cap 0.555
    
  2. We try to become the king with an invalid amount
     tezos-client transfer 1 from my_account2 to kot --arg "Unit" --burn-cap 0.004
    

    Obviusly the simulation fails with: script reached FAILWITH instruction

  3. We try to become the king with a valid amount
     tezos-client transfer 2 from my_account to kot --arg "Unit" --burn-cap 0.004
    

    And now we are the king of tezos. We can send further transactions doubling the amount every time.

You can inspect the contract and few beTheKing call transactions on bettercalldev.

Operation list

2024

DQPU, the decentralized quantum simulator

2 minute read

During my Quantum Computing journey, I often needed to simulate some quantum circuits; sometimes they are small, but some other times they are bigger enough ...

The Time I Built a Probabilistic Computer

6 minute read

In early 2023, I embarked on a journey to explore the field of probabilistic computing. This endeavor culminated in the construction of a hardware prototype,...

Back to Top ↑

2023

Expanding the Commodore 64 Quantum Emulator

1 minute read

In a recent article I wrote, “Quantum Computing on a Commodore 64 in 200 Lines of BASIC”, published both on Medium and Hackaday.com, shows a two-qubit quantu...

Back to Top ↑

2020

OCaml and Quantum Computing

1 minute read

Qiskit is a python SDK developed by IBM and allows everyone to create quantum circuits, simulate them locally and also run the quantum circuit on a real quan...

Yallo, a new Tezos language

2 minute read

As someone noticed from the previous post, last weeks I started to write a new programming language for Tezos smart contracts. This project was initially int...

King of Tezos: a smart-ponzi on Tezos

2 minute read

While writing a new programming language, it is often useful to write some real use-cases to test the syntax, the language expressiveness and the code cleann...

Favorite dev quote

less than 1 minute read

Documentation is like sex: when it is good, it is very, very good; and when it is bad, it is better than nothing

New blog

less than 1 minute read

This is my new blog, based on jekyll. I’ll soon import old posts from my old blog.

Back to Top ↑

2016

Contractvm: decentralized applications on Bitcoin

less than 1 minute read

Contractvm is a general-purpose decentralized framework based on blockchain. The framework allows to implement arbitrary decentralized applications in an eas...

Back to Top ↑

2015

Dices provably fair - Nonce overflow vulnerability

less than 1 minute read

Most of bitcoin dice software use a system to prove the fair play of the server for each bet. Most of them implement this mechanism using two seed (server se...

Back to Top ↑

2014

Apache2: redirect different domains to subfolder

less than 1 minute read

In the aim to merge two of my server on digitalocean, today I tried to write a mod_rewrite rule to redirect a secondary domain to a subfolder. After one hour...

Back to Top ↑

2013

MineML: F# miner

less than 1 minute read

MineML is a multithread CPU based bitcoin miner written in F#. At the moment it’s a slow implementation, but the class structure offers the possibility to im...

Back to Top ↑