Skip to content

Getting Started

Simple Storage: The “Hello World” of Blockchain

Before we dive further into application development, let’s start with walking through the most basic example for application development using the Simple Storage Contract. Think of this as the “Hello World” for the blockchain environment.

In your text editor or IDE, create a smart contract called SimpleStorage. In this contract, we’ll: * 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

The Simple Storage contract uses very basic snippets of code written in Solidity. The entire contract is shown below:

contract SimpleStorage {
    uint storedData;
    function set(uint x) {
        storedData = x;
    }
    function get() returns (uint retVal) {
        return storedData;
    }
}

For more information on Solidity, you can read the docs here: https://solidity.readthedocs.io/

If we have a user account with a positive Ether balance, we can deploy the Simple Storage contract to our blockchain. To do that, paste the contract into the body of a POST request, removing all whitespace from the contract, like this:

curl -X POST "http://<your_ip_address>/bloc/v2.2/users/<username>/<user_account_address>/contract?resolve=false" -H "accept: application/json;charset=utf-8" -H "Content-Type: application/json;charset=utf-8" -d "{ \"password\": \"<your_user_password>\", \"contract\": \"<contract_name>\", \"src\": \"<your_smart_contract_contents>\"}"

Example:

curl -X POST "https://stratodemo.blockapps.net/bloc/v2.2/users/test1234/asdkjsdlfjsdlkfj/contract?resolve=false" -H "accept: application/json;charset=utf-8" -H "Content-Type: application/json;charset=utf-8" -d "{\"password\":\"securePassword\",\"contract\":\"SimpleStorage\",\"src\":\"contract SimpleStorage { uint storedData; function set(uint x) { storedData = x; } function get() returns (uint retVal) { return storedData; } }\"}"

Note: The latest Solidity version used by the SMD is 0.5.2. Currently, 0.4.25 is also supported. In order to use a specific Solidity version for your contract, you would need to specify the version in the first line of the text pasted into the browser.

Once you have uploaded your contract, you can interact with it on the platform. The most common smart contract interactions are getting or setting contract values, which are done through a POST request to the API endpoint that calls a function on the smart contract. To call the contract's function to get a value, you would use the function call endpoint (a POST endpoint), but in the body of the API call, you will specify the method of the function as a "get" command since that is the type of contract function that you are calling:

curl -X POST "https://stratodemo.blockapps.net/bloc/v2.2/users/testkelly/6256bde805e8dce574a79a8c78abbd8490838513/contract/SimpleStorage/a6f7cf2bcb0b9e4b7a986ca0aecad3ea1bf58ec0/call?resolve=true" -H "accept: application/json;charset=utf-8" -H "Content-Type: application/json;charset=utf-8" -d "{ \"args\": {}, \"method\": \"get\", \"password\": \"1234\"}"

To set a value in the contract, you would use the same endpoint, but pass different arguments and specify a different method:

curl -X POST "https://stratodemo.blockapps.net/bloc/v2.2/users/testkelly/6256bde805e8dce574a79a8c78abbd8490838513/contract/SimpleStorage/a6f7cf2bcb0b9e4b7a986ca0aecad3ea1bf58ec0/call?resolve=true" -H "accept: application/json;charset=utf-8" -H "Content-Type: application/json;charset=utf-8" -d "{ \"args\": {\"x\":16}, \"value\": 0, \"method\": \"set\", \"password\": \"1234\"}"