NULS Smart Contract

Nancy Schorr
4 min readNov 8, 2020

--

Simplified

NULS/Nerve is an impressive platform because it is written from the ground up almost exclusively in Java. If you already know Java, you won’t need to learn two or three new languages to code a blockchain.

The NULS smart contract platform is similarly simple. The only code you write is your business logic — all in Java. NULS manages all the rest.

The documentation for creating a smart contract can seem a tad confusing. What follows are brief steps to get set up with a NULS smart contract.

1. Install the free community version of JetBrains IntelliJ IDEA (or paid version).

2. Install Java JDK1.8. You can get it from many sources, like: https://openjdk.java.net/install/ or yum, etc.

3. From the Intellij Welcome Page, the bottom right has a drop-down menu with a wheel icon labeled ‘Configure’, select: Structure for New Projects Select the default SDK: JDK1.8.

Welcome Screen Drop-down Menu

4. From the same Welcome Page drop-down menu, select Plugins. Search in the Marketplace (free) for the NULS Plugin.

NULS Plugin

Select Nuls and press OK.

5. From the Welcome Page, select Create New Project.

Select Maven in the left menu and check Create from archetype.

Select the io.nuls.ccc.nuls-archetype and press Next.

Use the Maven Archetype

If you don’t see the plugin in the list, you can install it from here: plugins.jetbrains.com/plugin/12368-nuls

Fill in the project information in the next screen. Verify it in the following screen.

At the end of the chain of dialog screens, press OK — and your project will be generated. Give it a minute to collect its resources and complete everything for you.

In the src folder, you’ll find two java files have been created. These are your starter files.

6. To compile, the only thing you must edit is the pom.xml file.

Enter the sender address, password, and private key.

Here’s the settings you need to edit in pom.xml:

<sender.address>NULSyourAccountNumberHere</sender.address><sender.password>YourPasswordHere</sender.password><sender.privateKey>PrivateKeyHere</sender.privateKey>

You can also remove the private key pom.xml setting and enter that information at build time in Intellij via the run configuration area as a ‘-D’ command line setting.

Next, bring up the Maven window which you can find under the View menu.

When you’re done, you only need to run clean and package to generate your jar.

Your jar will be in the target output directory. Upload the jar to your blockchain with the Wallet, and you’re done.

You can interact with and test your smart contract using the Wallet.

Note: The NULS plugin can also be found here: plugins.jetbrains.com/plugin/12368-nuls

For more help, please see the two official NULS sets of instructions:
https://github.com/nuls-io/nuls-smartcontract-archetype
https://docs.nuls.io/Docs/s_tools.html#new-nuls-smart-contract-maven-project

Contents of 2 Java Files Created

SimpleStorage.java:

package io.nuls.v2;
import io.nuls.contract.sdk.Contract;
import io.nuls.contract.sdk.annotation.Payable;
import io.nuls.contract.sdk.annotation.Required;
import io.nuls.contract.sdk.annotation.View;
public class SimpleStorage implements Contract {
private String storedData;
@View
public String getStoredData() {
return storedData;
}
@Payable
public void setStoredData(@Required String storedData) {
this.storedData = storedData;
}
}

Owner.java:

package io.nuls.v2.util;
import io.nuls.contract.sdk.Address;
import io.nuls.contract.sdk.Msg;
import io.nuls.contract.sdk.Utils;
public class Owner {
private Address ownerAddress;
private static Owner owner;
private Owner(Address address){
this.ownerAddress = address;
}
public static Owner getOwner(Address sender){
if(owner == null){
owner = new Owner(sender);
}
return owner;
}
public Address getOwnerAddress(){
return this.ownerAddress;
}
public void requireOwner(String message){
Utils.require(Msg.sender().equals(ownerAddress),message);
}
}

--

--