How-To do SmartContracts on Factom

Hackathon | September 26, 2018
Home » Company » Blog » How-To do SmartContracts on Factom

As an example let us model a game of Tic-Tac-Toe as a smart contract. A Factom SmartContract can be thought of as a program that encodes events into meaningful blockchain data. A well constructed SmartContract has three key elements: a way to express the initial and final ‘states’ of a contract, a means to sign and validate the author/committer of each input event, and an auditor program that recalculates the latest ‘state’ output in order to evaluate the correctness of the contract.

To do this on the Factom® Blockchain we must think of the game as a set of initial states,

and provide a set of valid events and a criteria for final states / completed execution. Unlike other computing blockchains a user may execute the contract using any programming language. For this demo we have chosen to use Python, and link to source code throughout this post.

Let’s Begin An empty board represents the initial state of the contract.

In the diagram above observe that we have added a numbering schema to define possible moves of the game. This serves as a way to encode game moves into event data.

We also need to express who is allowed to interact with this game.

There are two players PlayerX and PlayerO – when we begin the game a new chain is created with an event that contains the Public encryption keys for each player.

In the example code we use this library

In tic-tac-toe PlayerX goes first, but before that we can consider that the two players must have first agreed to play a game. We model this as a ‘Begin’ event where we add the first entry to a newly created chain.

Time 0: GameBegin(PlayerX, PlayerO)

Player X makes the first move of the game.

Time 1: Move(X, 11)

Followed by PlayerO

Time 2: Move(O, 02)

Above is what a snapshot of the board now looks like.

Here’s is what some of output looks like we we write the data to a Factom blockchain

Notice above: that the content of each entry also contains a cryptographic signature that matches the published identities from the initial GameBegin event.

Gameplay continues…

Time 3: Move(X, 22)

Time 4: Move(O, 00)

Time 5: Move(X, 01)

Time 6: Move(O, 10)

Time 7: Move(X, 21)

Time 8: GameOver(Win, PlayerX)

Above is the final view of the board.

You can see the example code that pushed this event data to Factom here:

To verify a single event we query the Factom chain and validate each signature using the appropriate public key:


See The full example code in github:

And finally we need to audit the actual gameplay rules.

In this python script we query and replay all events from the chain to verify that:

1. Each event was signed cryptographically by the correct player
2. No player’s moves were out of turn
3. No duplicate moves where found
4. The reported winner did in fact play a set of winning moves

In Conclusion
We have met the criteria we set out for our smart contract by providing:

  1. A way to encode events into meaningful data
  2. A way to express the Initial and Final states of a contract
  3. A way to sign and validate the author/committer of each event
  4. And an auditor program that lets each user evaluate the rules of the contract

Possible Enhancement
Just as we’ve added the pictures of the game boards above while demonstrating a sequence of moves, a smart contract developer could decide to publish a ‘state’ snapshot at key times. Just as Blockchains publish blocks to secure previous transactions, a snapshot serves a similar purpose. This snapshot would ‘roll up’ and summarize all the previous valid events allowing a user to reference smaller portions of the overall sequence without having to re-examine the entire chain.

We look forward to the seeing all the application during the Hackathon at Northwestern on September 29th – 30th. Judges from Perkins Coie, PWC, Factom and Northwestern will be on hand to award the first prize winner with the customary $5000 prize. To enter, form a team on Crowdforge and register to contend in the Factom Smart Contract Hackathon. Hope to see you there!

POSTED: September 26, 2018 BY Crystal Wiese IN Hackathon

Crystal Wiese is the Director of Marketing at Factom. Working within the startup tech scene for the past 10 years she has become passionate about taking great ideas and building a strong comprehensive narrative. Graduating from the Art Institute of Portland with a degree in Design Management, Crystal has always seen the value in creating strong ties between the technical, creative and business.