Use Node-RED to build a smart contract

Consult this article for a step-by-step tutorial on how to build a simple smart contract flow in Node-RED.

Introduction

In this article, we will create, deploy and interact with a simple smart contract. 

To begin, log into the Node-RED UI of your TBSP running instance.

Video

The video below describes the required steps for building a simple smart contract flow in Node-RED.

SC1_NodeRED

Required Nodes

This flow involves the following TYMLEZ nodes:

  • Smart contract marker node
    Screen Shot 2020-06-30 at 3.58.16 PM
  • Endpoint for the contract

node_contract

  • Get memory field
    node_get_memory
  • Set memory field
    node_set_memory
  • Extract arguments from HTTP call
    node_arguments

The entire flow should look like this:

sc_helloworldPart1_1

Let's create the flow, one step at a time.

Steps

Create the first endpoint

  • Create a new flow called HelloWorld_Part1:

sc_helloworldPart1_2

  • Add the node smartContract v1.0 to the canvas:

Screen Shot 2020-06-30 at 3.54.37 PM

  • Add a contract node to the canvas.

sc_helloworldPart1_3

Open the node and enter the following parameters:

GET
/helloWorld_Part1
init
  • Add a argument node to the canvas.

sc_helloworldPart1_4

Open the node and enter the following parameters:

get argument "greeting"
0
greeting
  • Add a set memory field node to the canvas.

sc_helloworldPart1_30

Open the node and enter the following parameters:

set memory field
greeting
greeting
  • Add a done node to the canvas.

sc_helloworldPart1_5

Open the node and enter the following parameters:

Published successfully
  • Connect all the nodes.

Add a new endpoint

In this section, you will add a new endpoint for the interaction with the contract.

  • Add a contract node to the canvas.

sc_helloworldPart1_6

Open the node and enter the following parameters:

GET
/getGreeting
getGreeting
  • Add a get memory field node to the canvas.

sc_helloworldPart1_7

Open the node and enter the following parameters:

get memory field
greeting
payload
  • Add a done node to the canvas.

sc_helloworldPart1_8

Open the node and enter the following parameters:

done
  • Connect all nodes of the second endpoint get_greeting with each other. Then, deploy the template:

sc_helloworldPart1_1

Interacting with the deployed SC via the command line interface

Open a terminal session and enter the following code:

tymlez-sc publish  --templateName=SC_HelloWorld_Part1 Hello

If the contract is successfully published, the system will return a valid contract ID:

[INFO] commands.publish - publishing contract SC_HelloWorld_Part1....
[INFO] commands.publish - Signed tx...
[INFO] commands.publish - Post signed tx ....
[INFO] commands.publish - Smart Contract published successfully
[INFO] commands.publish - Contract id: <GENERATED_CONTRACT_ID>

Note

Note down the contract ID.

Open a terminal session and enter the following command, including the contract ID:

tymlez-sc call --functionName=getGreeting

This command should return the stored greeting:

[INFO] commands.call - { result: 'Hello' }

You can also import the entire flow as a JSON format into Node-RED:

[{"id":"c1ea1139.5f667","type":"save-memory","z":"c4ba12e4.5e20b","name":"greeting","field":"","source":"","reverse":false,"variable":"","frompayloadinput":"greeting","x":1000,"y":300,"wires":[["c759492b.567b78"]]}]

Navigation

Go back