Takamaka Blockchain, framework Java for Smart Contract

Team Takamaka
Takamaka
Published in
4 min readNov 13, 2020

Takamaka is a Java framework for writing smart contracts. In other words, it allows programmers to use Java to write code that can be installed and ran on a blockchain. Programmers won’t have to worry about archiving objects on the blockchain: that’s completely transparent to them. That’s why Takamaka is totally different from past attempts to use Java to write smart contracts, in which programmers have to call specific methods in order to make data persistent on the blockchain.

Writing smart contracts in Java means that programmers don’t have to learn yet another programming language. They can simply use a stable and well-understood development platform, along with all its modern tools. Programmers can in fact use the latest Java features, such as streams and lambda expressions. That said, there are some restrictions to the type of code that can be executed inside a blockchain: the main limitation is deterministic behaviour.

Since you are writing Java code, you don’t have to learn or install anything new before you start writing programs for Takamaka: you can simply use your favourite Integrated Development Environment (IDE) for Java, or even a command line if you wish.

The concept of smart contract

A contract is a legal agreement between two or more parties. A solid contract should be unambiguous, otherwise, its interpretation could be questioned or misunderstood. The validity of a contract is usually dictated by the legal system. In software development, a smart contract is software with deterministic behaviour, whose semantics are clear and applied through a consensus system. Blockchains provide the perfect environment in which smart contracts can be implemented and executed, as their typically non-centralized nature reduces the risk of a single party overturning the rules of consent, for example by providing non-standard semantics for a smart contract.

Contracts can hold and transfer money to other contracts. So, traditionally, smart contracts are divided between those that contain money but have no code (externally owned accounts) and those that instead contains code (actual smart contracts). The former is typically controlled by an external agent (a wallet, a human being) while the latter by its code. Takamaka implements both versions as instances of the class of the abstract library takamaka.lang.Contract (inside takamaka_base.jar). That class extends takamaka.lang.Storage, so its instances can be kept inside the blockchain. The Takamaka library defines subclasses of takamaka.lang.Contract. Programmers can define their own subclasses.

Below you’ll find a simple smart contract that handles a Ponzi investment scheme: each investor repays the previous investor with a reward of at least 10%. As long as new investors keep coming, each user gets at least a 10% reward; on the other hand, the last investor will never see his investment back. This contract was inspired by a similar Ethereum contract from Iyer and Dannen, Building Games with Ethereum Smart Contracts, page 145, Apress 2018.

A simple Ponzi scheme contract

Create a new ponzi Java project in Eclipse. Create lib and dist folders inside the project. Put both takamaka_base.jar and takamaka_runtime.jar inside lib and add them to the ponzi build path. Create the takamaka.tests.ponzi package, then create a SimplePonzi.java class inside that package. After that, copy the following code into SimplePonzi.java:

package takamaka.tests.ponzi;

import static takamaka.lang.Takamaka.require;

import java.math.BigInteger;

import takamaka.lang.Contract;

public class SimplePonzi extends Contract {

private final BigInteger _10 = BigInteger.valueOf(10L);

private final BigInteger _11 = BigInteger.valueOf(11L);

private Contract currentInvestor;

private BigInteger currentInvestment = BigInteger.ZERO;

public void invest(Contract investor, BigInteger amount) {

// new investments must be 10% greater than current

BigInteger minimumInvestment = currentInvestment.multiply(_11).divide(_10);

require(amount.compareTo(minimumInvestment) > 0, () -> “you must invest more than “ + minimumInvestment);

// document new investor

currentInvestor = investor;

currentInvestment = amount;

}

}

This contract has a single method, called invest: it allows new users (investor) to invest a certain amount of coins. This amount has to be at least 10% higher than the current investment. The expression amount.compareTo(minimumInvestment) > 0 is a comparison between two Java BigInteger and should be read as amount > minimumInvestment: it can’t be written like this though, as comparison operators don’t work on reference types in Java. The static method takamaka.lang.Takamaka.require() can be used to request a precondition to hold. The require(condition, message) call throws an exception with the given message when the condition isn’t met. If the new investment is at least 10% higher than the current one, it will be saved in the state of the contract together with the new investor.

Note to readers: this code is meant to be only the starting point for our discussion. The final version of this step-by-step contract has been fully developed and described here https://takamaka.dev/docs/Takamaka.html#takamaka-smart-contracts-in-java.

Feel free to visit this section and give us your feedback through our contact channels, which you can find on www.takamaka.io.

--

--