More Advanced Contracts. Image credit: Rawpixel, from Unsplash

More Advanced Contracts

Deep Dive into Ardor Lightweight Contracts Development

See all lightweight contracts articles

In my previous articles, I presented a sample hello world contract and answered some common questions. Now, let’s look into a more advanced use case.

Assume that we have a group of friends who would like to gamble on the outcome of a sporting event. Everyone in the group sends IGNIS tokens to the contract account. The contract account monitors the blockchain, and every few blocks chooses the winner based on some condition, then submits all the collected IGNIS to the winner’s account, minus the transaction fee, paid in IGNIS (no need to pay for Gas or the like).

In this simple example, the winner is selected based on a random number generated, based on a seed derived from the current block Id. This could easily be enhanced to support more advanced use cases, such as selecting a winner based on the value of an account property set by another Oracle contract monitoring, for example, the outcome of a horse race. The chance of winning is proportional to the amount paid by the account, multiple payments from the same account are allowed.

See below listing of the contract code with comments explaining its inner workings.

Contract Source Code

This is a block based contract so there is no need to trigger its execution using a message transaction, as it’s running and monitoring the blockchain all the time. It performs its action every few blocks as defined by the workstation specific, contract parameter “frequency”. The contract itself uses only existing public Ardor API and submits normal Ardor transactions. This makes sure a new release of Ardor won’t break this contract.

This contract currently uses IGNIS as payment token, it can be easily enhanced to support any child chain coin or asset or currency token. With a little more development effort, it can also allow the operator to define the token type with which it operates in its contract configuration file.

Testing your Contracts

A major problem with blockchain based contracts is how to test them for bugs and security problems, the Ardor lightweight contracts can be easily tested automatically using JUnit tests which utilize the existing testing infrastructure of Ardor itself and therefore saves a huge amount of effort.

See below the unit test code we developed for the contract above

Unit Test Code

As you can see, Ardor provides a simple and robust framework to develop your blockchain based contracts in Java using industry standard development tools.

Disclaimer: The lightweight contracts feature development is still evolving. This feature is currently available only on the Ardor testnet.