Getting Started with Dispatch Devnet

The Dispatch Developer Network (“Devnet” for short) is now live. We’re going to cover a lot of information about it in this article, but if you stick to the end, I promise you’ll know all you need to build smart contracts on Dispatch. You’ll see it’s as easy as 1 + 1 = 2.

Highlights and tips to start:

  • If you haven’t done so already, please register for the Dispatch Developer Network. We will review your GitHub and send you a developer kit.
  • We love APIs as much as you do, so we’ve published our new API docs on api.dispatchlabs.io. Below, we will walk through how to use the API with the live Developer Network. You don’t even need to know how to code.
  • Dispatch allows you to build, deploy, and execute smart contracts. This article will walk through how to use our Node JS SDK step-by-step, and we will even show you how the code works.
  • We are working on an App Directory of real-world applications using distributed ledger tech. If you’d like your company or app to be featured and promoted, please register and we will get in touch with you.
  • If you want to look under the hood and learn how the Dispatch protocol works, check out the Technical White-paper.

In the remaining sections of this post, I’ll cover each of these points in further detail, as well as provide a few thoughts about things we have in the works for our community going forward.


Not a coder?

If you are not a developer, you are still awesome. You might be excited to see our recently released Light-paper, a less technical version of our white-paper. You can take our ledger for a test drive and explore using our network scanner or mobile wallet for iOS and Android.

Create an Account and get TestNet Tokens (without coding):

  1. Go to http://testnet.dispatchlabs.io/ to view the TestNet’s Scanner
  2. Click Deploy Smart Contract to view the built-in developer tool
  3. Click Compile -> and you will see example warnings below the contract
  4. Click Deploy -> and you will see a request for a private key
  5. Click Generate New Private Key and Address
  6. Save your private key somewhere!
  7. Copy paste your public address into our Discord channel and we will send you tokens.

Get your free developer’s kit.

If you are a developer, Dispatch will give you a free developer’s kit. Please fill out the developer registration form to apply for a kit.

What’s in the Dispatch developer’s kit?

  • Membership in the Dispatch Developer Network
  • Licenses for the Dispatch Test Network
  • Support from the developer chat on Discord
  • Potential spotlight on the Dispatch App Directory
  • Invite to our office for a coffee chat
  • More goodies in the future!

Play with the Disgo API

Every developer loves application programming interfaces (APIs) even if she doesn’t realize it yet. Our engineering team has spent the last couple weeks updating the API to help app developers build on Dispatch. It’s ready for you to test out, and we’d love your feedback. It’s easy to find our documentation at api.dispatchlabs.io and you can use the docs page to generate example requests in almost any language you want.

You don’t even need to write a line of code to test out all of these API calls. Just download the free Postman app and click Run in Postman.

Select the API call you want on the left and just click Send! Go ahead and walk through each of the API calls. You’ll also see some calls that are still under development for interacting with artifacts stored on the Dispatch Artifact Network, coming later in our roadmap.

In the top right you’ll see we’ve included a Dispatch environment so the API calls will work right away. You can edit the parameters to point to you own local cluster or to the Mainnet when you’re ready.

Postman can also create code snippets in almost any language. This is one of the best use cases for Postman and will save you tons of time. If you love our API, please let us know by joining the developer chat on Discord.


Deploy Smart Contracts with the Node JS SDK

Let’s walk through deploying a Smart Contract on Dispatch’s network. First we will clone the NodeJS software development kit from the Dispatch GitHub and then run through a few sample example transactions. Note that this guide is based on Mac OSX.

First, I created a new directory called getting-started and then cloned the Dispatch Node JS SDK repository from Dispatch’s GitHub.

mkdir getting-started
cd getting-started
git clone https://github.com/dispatchlabs/disnode_sdk

Check that you have Node JS installed

To check if you have Node.js and NPM installed, run these commands in your terminal:

node -v
npm -v

If you don’t have Node JS, let’s fix that. You can download and install Node JS from that project’s official website.

If you’d rather do it via command line, you can install node using Homebrew. If you don’t have Homebrew, run these commands in Terminal:

xcode-select --install 
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew doctor

Now just run the install command and Node JS and npm will be installed together:

brew install node

Install the Dispatch Node JS SDK

Once you’ve got the prerequisites installed, you can install the disnode client using npm. Make sure you are in the directory named disnode_sdk that you cloned from GitHub.

cd getting-started 
cd disnode_sdk
npm install

If you happen to get errors with using npm, I recommend uninstalling Node completely with this command and then reinstall with the latest version.

brew uninstall --force node
sudo rm -fr /usr/local/lib/node_modules/npm
brew install node
sudo npm install -g npm@latest

What can I do with the SDK?

The SDK is a good starting point to integrate any existing Node or Javascript application with the Dispatch protocol. You can create accounts, send transactions including transfers of licenses, and you can even deploy and run smart contracts. We will demonstrate each of these with examples.

There’s one more thing… Change Config.json to point to the TestNet

Before you use the SDK, you will want to switch from using the MainNet to the TestNet. Find and edit the config.json in the lib directory and input the TestNet’s seed node: 35.197.127.151:1975 instead of the MainNet’s hostname seed.dispatchlabs.io. Click here to find it on GitHub.

Let’s run the examples!

It’s easy just run the examples with a command line from the disnode_sdk directory.

npm run examples

The examples will run, you’ll see the output of the account.js and transactions.js as well. If you’d like to comment them out, just edit the index.js file.

It’s as easy as 1 + 1 = 2

You’ll see on the terminal console the result of the contract in the contractResult is 2! Congratulations you just did addition on the Dispatch distributed ledger.

Contract execution result:
{
"transactionHash":
"a269d8867d506930c45a1ea7bd4c7f43b149d2d2066cdc38cf01b20a161704b0",
"status":"Ok",
"contractAddress":"2b109862e3276d7d6938138b7daddb60e246ac0f",
"contractResult":[2],
"created":"2018-09-18T20:06:11.113555674Z"
}

Validate with the Dispatch Ledger.

You should see the results instantly appear on the Dispatch ledger with our network scanner http://scanner.dispatchlabs.io/ or our mobile wallet for iOS and Android. Both are open source projects you can copy and use for your own project.

How does it work?

So you want to see what’s going on behind the scenes? If you browse the code on GitHub (or your local clone) go to the examples directory, and you will see the scripts to generate an account, transfer licenses, deploy a smart contract, and execute the smart contract.

Create Account

In the contract.js file first you’ll see two accounts “temp” and “test” are set as constants. For the “temp” account a new private key will be generated using the init command. The “test” account however will be configured with the name and private key of an account with existing licenses.

// Account() is a constructor with no required inputs
const temp = new DisNodeSDK.Account();
// Use account.init() to generate a private key
temp.init();
// Account() can also accept any account fields; the most important being the privateKey
const test = new DisNodeSDK.Account({name: 'NodeSDKTest', privateKey: '[redacted]' });

Transfer Licenses

Next we will send some licenses from the “test” account to the “temp” account.

// Account objects can send licenses to other accounts directly; returning the resulting transaction
let tx = test.sendTokens(temp, 5);

Write a Smart Contract

Learning Solidity is a great goal, and I’m trying to learn myself. However, it’s just as easy to find existing smart contracts from others for testing. We’ve written a very simple contract called “math” with a single function called “plus one” that adds 1 to a number.

// This is a smart contract that adds 1 to the input 
pragma solidity ^0.4.24;
contract math {
function plusOne(uint256 y)
pure public returns(uint256 x)
{ x = y + 1; }
}';

Deploy Smart Contract

We will insert the source code of the Smart Contract into the contract.js file for simplicity. You could also use a file operation in Node to get the code from a Solidity file.

// Provide source code as a string
const sourceCode = 'pragma solidity ^0.4.24;contract math { function plusOne(uint256 y) pure public returns(uint256 x) { x = y + 1; } }';
// Use Transaction.compileSource to easily compile solidity code
const compiled = DisNodeSDK.Transaction.compileSource(sourceCode);
// Accounts can create Smart Contracts using compiled values
const contract = test.createContract(compiled.contracts[0].bytecode, compiled.contracts[0].abi);

Execute Smart Contract

We can now execute the smart contract with a single command, and we are going to add the input parameter of “1.0”. Feel free to edit this to change the input.

// Exection happens from the account, to the contract, along with the method and parameters
const execute = test.executeContract(contract, 'plusOne', [1.0], compiled.contracts[0].abi);

Thanks for walking through the code with me, if you have any feedback on this article you email me at colin at dispatchlabs dot io. If you want to talk right away, you are invited to talk to us anytime on the developer chat on Discord.


Register for Dispatch Devnet:

Don’t forget to fill out the developer registration form to apply for a free developers kit. You’ll get everything you need to build smart contracts, distributed apps, and we’ll review apps to be listed on our App Directory.

Thanks for reading. We look forward to hearing from many of you in the future — and to all the cool stuff we know you’ll build — as we grow the Dispatch developer community. We’ll also be posting additional updates from our end as we move toward launch of Dispatch’s main network. So please stay tuned.

Cheers,

Colin Lowenberg

Join the Dispatch Developers on Discord


Learn more about Dispatch Labs: