Skip to content

Quick Start

Basic Blockchain Interactions

Now that a node is running on localhost or your preferred cloud environment, you can start interacting with your blockchain using the STRATO REST APIs.

Note for Windows Users

Further instructions assume you have STRATO accessible on localhost domain

Windows users on Docker Toolbox should use Docker VM IP address instead (usually 192.168.99.100)

Call Node

We can begin by pinging our node to make sure it's online:

curl -X GET http://localhost/apex-api/_ping
If the node is online, you'll receive pong as a response.

Alternatively, we can ping our node and recieve the network state as the response:

curl -X GET "http://localhost/apex-api/status"
Which returns a response like this:
{
  "lastBlock":{
    "number":"4",
    "hash":"95bd67e01c3eb1a43e1ea37179a80a9af7c08269",
    "parentHash":"82094405ab18c70ec78dafcd2726d98c5761a2e",
    "totalDifficulty":"655360",
    "nonce":"6"
  }
}

Create User

To upload and invoke Smart Contracts, we need to first generate a public address on the blockchain.

Note If you copy and paste these Bash commands in your terminal, be sure to remove any whitespacing

curl -X POST "http://localhost/bloc/v2.2/users/testUser?faucet=true"
    -H  "accept: application/json;charset=utf-8" 
    -H  "content-type: application/json;charset=utf-8" 
    -d "\"password\""
createAccount() {

  const username = 'alice';
  const password = 'securePassword';

  fetch('http://localhost/bloc/v2.2/users/' + username, {
      method: 'POST',
      body: JSON.stringify(password),
      headers: {
          'Content-Type': 'application/json',
          },
    })
    .then(function(response) {
        console.log(response);
    })  
    .catch(function(error) {
        console.log(error);
    })
}

And our node will respond with a blockchain address for the new user:

bf39b2f81bf6be824e7314e72701318485587e

Faucet User Account

To create transactions, an account must have a positive Ether balance to pay the gas price, or anti-spam fee for each transaction.

We accomplish this by "running the faucet," or adding an Ether balance, to our new account:

curl -X POST "http://localhost/strato-api/eth/v1.2/faucet" 
    -H  "accept: text/plain;charset=utf-8" 
    -H  "content-type: application/x-www-form-urlencoded" 
    -d "\"address=bf39b2f81bf6be824e7314e72701318485587e\""
faucet() {

  const address = 'bf39b2f81bf6be824e7314e72701318485587e'

  fetch('http://localhost/strato-api/eth/v1.2/faucet', {
      method: 'POST',
      body: `address=${address}`,
      headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
    })
    .then(function(response) {
        console.log(response);
    })  
    .catch(function(error) {
        console.log(error);
    })
}

Deploy a Smart Contract

Now that our user account has a positive Ether balance, we can deploy a contract to our blockchain.

We'll create a smart contract called SimpleStorage. In this contract, we:

  • Declare a state variable, storedData
  • Create a function to set an integer x as storedData
  • Create a function to retrieve an integer stored as storedData
contract SimpleStorage {
    uint storedData;
    function set(uint x) { storedData = x; }
    function get() returns (uint retVal) { 
        return storedData; 
    }
}

We can paste the contract source inline and send it in our API call as part of the request body:

curl -X POST "http://<url>/bloc/v2.2/users/<username>/<userAddress>/contract" 
    -H  "accept: application/json;charset=utf-8" 
    -H  "content-type: application/json;charset=utf-8" 
    -d "  {  \"value\": \"10\",  \"password\": \"password\", 
    \"contract\": \"SimpleStorage\",  \"src\": \"contract 
     SimpleStorage { uint storedData; function set(uint x) { 
     storedData = x; } function get() returns (uint retVal) 
     { return storedData; } }\"}"
uploadContract(){

    const username = 'alice';
    const password = 'securePassword';
    const address = 'bf39b2f81bf6be824e7314e72701318485587e';
    const args = { uint: '9'}
    const src = 
        `pragma solidity ^0.4.24;

        contract SimpleStorage {
            uint storedData;
            function set(uint x) { storedData = x; }
            function get() returns (uint retVal) { 
                return storedData; 
            }
        }`;
    const RequestBody = { password, src, args }

    fetch('http://localhost/bloc/v2.2/users/' + username + '/' + address + '/contract?resolve', {
        method: 'POST',
        body: JSON.stringify(RequestBody),
        headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
        },
    })
    .then(function(response) {
        console.log(response);
    })  
    .catch(function(error) {
        console.log(error);
    })
}

Our node will respond with the address of the newly created contract:

dca5bd846d09eaf943344c9bc068ee758ebb2021

Call Contract Method

With our contract deployed, let's call the set function and save some data on the blockchain! We'll call the method "set" with the parameter x set to 7.

curl -X POST "http://localhost/bloc/v2.2/users/<username>/<userAddress>/contract/<contractName>/<contractAddress>/call"
    -H  "accept: application/json;charset=utf-8" 
    -H  "content-type: application/json;charset=utf-8" 
    -d "{  \"password\": \"password\",  \"method\": 
        \"set\",  \"args\": {7},  \"value\": 0}"
callContract() {

    const blocURL = HOST_URL + '/bloc/v2.2/users/';
    const username = 'alice';
    const password = "securePassword";
    const methodName = 'set';
    const address = '87168271eb895681f7b724bde31c4f0';
    const contractAddress = 'b823216ffb44fcea8e5eee8435aef';
    const callArgs = {
      set: '7',
    };

    fetch(blocURL + username + '/' + address + '/contract/SimpleStorage/' + contractAddress + '/call?resolve', {
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        password: password,
        method: methodName,
        value: "0",
        args: callArgs
      })
    })
    .then(function(response) {
        console.log(response);
    })  
    .catch(function(error) {
        console.log(error);
    })
}

The node will return our request will the transaction hash:

6a105a7ce1fd03b5757fbb3f268da76a2d3eea28ef

Get Contract State

We can introspect further into our contract state through the /state endpoint. The /state route is the workhorse that drives applications - in most cases, UIs are just views of the data available at the /state route.

curl -X GET "http://localhost/bloc/v2.2/contracts/<contractName>/<contractAddress>/state" 
    -H  "accept: application/json;charset=utf-8"
getState() {

  const address = 'dca5bd846d09eaf943344c9bc068ee758ebb2021'

  fetch(`http://localhost/bloc/v2.2/contracts/SimpleStorage/${address}/state`, {
      method: 'GET',
      headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
    })
    .then(function(response) {
        console.log(response);
    })  
    .catch(function(error) {
        console.log(error);
    })
}

We'll get back the state of our smart contract:

{
    "get": "function () returns (UInt256)",
    "set": "function (UInt256) returns ()",
    "storedData": "7"
}