Skip to content

BlockApps-Rest

BlockApps-Rest is an extremely lightweight Node SDK for building front-end applications around the strato-api, bloc-server, and cirrus services, which enables rapid development of front end applications.

Configuration

To get started with BlockApps-rest, we must include our configuration file, config.yaml, which we store in the root directory of strato-getting-started. See details in the appendix below.

Generator Functions

BlockApps-Rest uses generator functions and the yield expression to invoke the API synchronous calls. This syntax removes the need for chaining promises.

/*
   user: the user object returned by createUser()
   contract: the contract object returned by uplocaContract()
*/
function* fireMissiles(user, contract, target) {
  const args = {
    target: util.toBytes32(target)
  };
  const result = yield rest.call(
    user, // user object
    contract, // contract object
    'fireMissiles', // method name
    args);
  return result;
}

Using Generator Functions

First let's make a function which will upload our contracts.

function* uploadContract(user, contractName, contractFilename, args, txParams, node)

This is where we take advantage of the generator function. We can write out upload function as:

function* addContract(userName, password, contractName, contractFilename) {
  const user = yield rest.createUser(userName, password);
  const constructorArgs = {}; // optional
  const contract = yield rest.uploadContract(user, contractName,contractFilename, constructorArgs) {
}

The full list of BlockApps-Rest methods are available here.

BlockApps-Rest Cheat Sheet

Create User

yield rest.createUser(username, password);

Upload Contract

yield rest.uploadContract(admin, contractName, contractFilename,args);

Get Contract State

yield rest.getState(contractName, contractAddress);

Call Contract Method

yield rest.callMethod(username, contract, methodName, args, value);

Appendix

config.yaml

Below is an example config.yaml:

apiDebug: true
password: '1234'
timeout: 600000
contractsPath: ./contracts
dataFilename: ./config/demo-data.yaml
deployFilename: ./config/<YOUR NODE NAME>.deploy.yaml

#WARNING - extra strict syntax
#DO NOT change the nodes order
#node 0 is the default url for all single node API calls
nodes:
  - id: 0
    explorerUrl: <YOUR EXPLORER INSTANCE>
    stratoUrl: <YOUR STRATO INSTANCE>
    blocUrl: <YOUR BLOCK INSTANCE>
    searchUrl: <YOUR CIRRUS INSTANCE>

apiDebug: flag to log detailed debugging information b

password: the password used to create contracts that are used in deployment

timeout: timeout to fail tests

contractsPath: the path from root directory to folder storing solidity files (.sol)

dataFilename: initialization data to be used in deploying

deployFilename: deploy file, contains information such as contract addresses required to interact with contracts created in the deployment