Skip to content

How to create a smart contract

Ksenia Slavina edited this page May 27, 2019 · 2 revisions

Creating a smart contract. Reading and writing a list with built-in data types

In order to create a smart contract with the list support, it’s necessary to add the following line of code at the beginning of the smart contract:

import java.util.ArrayList;

Declare a variable in class

private final ArrayList<String> list;

Create a variable in constructor

list = new ArrayList<>();

Writing a list

In order to write a list, the standard methods are used for adding the element to an array.

    public void addRange(){
        for(int i = 0; i < 10000; i++) {
            list.add(i + "00000");
        }
    }

Reading an array

Method which returns the number of lines of an array list

   public int count(){

        return list.size();
    }

Accessing array elements through the index

   public String getAt(int i){

        return list.get(i);
    }

The Result

import java.util.ArrayList;

public class Contract extends SmartContract {

    private final ArrayList<String> list;

    public Contract() {
        list = new ArrayList<>();
    }

    public String hello() {
        return "hello";
    }

    public void addRange(){
        for(int i = 0; i < 10000; i++) {
            list.add("I'm a - " + String.valueOf(i));
        }
    }

    public int count(){
        return list.size();
    }

    public String getAt(int i){
        return list.get(i);
    }
}

Deployment

The standard deployment procedure contains 3 steps:

  1. Writing a smart contract
  2. Correct spelling for verification (Pressing the “Build” button)
  3. Deployment (Pressing the “Deploy” button)

The picture below represents the Credit Desktop Wallet window which displays methods of our smart contract, after clicking the “Build” button.

methods

Demonstration

Adding elements to an array

array_elements

Requesting the number of elements

request

Element by index

index_element