1.0.0 • Published 4 years ago

soundac-state v1.0.0

Weekly downloads
5
License
ISC
Repository
github
Last release
4 years ago

soundac_state

Build decentralized apps on the Soundac blockchain with ease!

Installation

npm install soundac_state and to use the companion project, soundac_transact, npm install soundac_transact

Explanation

The Soundac blockchain is one those blockchain which facilitates free and fast transactions.soundac_state, along with its companion project, soundac_transact makes it incredibly easy to create decentralized apps on the Soundac blockchain

About soundac_state

soundac_state uses the custom_json operation type to create transactions.

soundac_state is a library for building fully decentralized DApps with the Soundac blockchain. Using soundac_state you can define events that occur when a new transaction of a certain type is created, such as updating the state, displaying feedback for the user, etc. Using these events, you can build a fully decentralized DApp. Look below for an example tutorial.

Example

The following example will create a decentralized messaging app on the Soundac blockchain with a CLI. With your final result you will be able to send messages to a public chatroom through the Soundac blockchain. First, install the dependencies we'll need and set up the project:

mkdir basic-messaging-app
cd basic-messaging-app
npm init
npm install soundac_state soundac_transact museblockchain-js

Then create index.js and we can start building! First we'll import our dependencies:

const soundac = require('museblockchain-js'); // Used to create soundac client
soundac.config.set('websocket', 'wss://node.soundac.io');
const soundac_state = require('soundac_state'); // This will allow us to build our DApp.
const soundac_transact = require('soundac_transact'); // This makes it easy to make transactions on the Soundac blockchain.
const readline = require('readline'); // For the CLI (command line interface).

Next is some setup for readline, which we will use to create our basic CLI:

var rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
})

Then we will create some variables for our username and private key on the Soundac blockchain (instead of public keys like Bitcoin and Ethereum, Soundac uses usernames). We'll hardcode this for the sake of simplicity, but you could easily ask the user to input theirs. To complete this step you will have to have a Soundac account (which can be done at https://soundac.io/).

const username = 'ned'; // Put your username here.
const key = 'your-private-posting-key-here'; // Put your private posting key here.

Now we need to actually create our interface with the Soundac blockchain. Like others like web3 (for Ethereum), Soundac uses the museblockchain-js package to create the api interface with the Soundac blockchain. We'll provide this with a node to connect to (similar to how Infura works on Ethereum) and to get the blockchain data from:

soundac.config.set('websocket', 'wss://node.soundac.io');

Then we'll get the latest block to use as where we'll start processing blocks and transactions from:

soundac.api.getDynamicGlobalPropertiesAsync().then(function(result) {

Then actually create the soundac_state instance. This method uses the arguments:

client: the soundac api client to get transactions from,

startingBlock: which block to start at when processing blocks and transactions,

processSpeed: the amount of milliseconds to wait before getting the next block (when not fully caught up to the latest block),

prefix: the name of your DApp. This prefix is at the beginning of every transaction created for your DApp and ensures that no other DApps will use the same transaction ids as yours. Make sure to make this unique for your DApp!

mode: set to 'latests to stream latest blocks

And we will create it like so, using the result from retreiving the latest block's number:

  var processor = soundac_state(soundac, result.head_block_number, 100, 'basic_messaging_app_', 'latest');

Next we will define what will happen when someone creates a message transaction, which will result in a display of that message to the user. After that, we will start the processor:

processor.on('message', function(json, from) {
  console.log(from, 'says:', json.message);
});
processor.start();
});              // Close the function we started before.

Finally we handle user input and when our DApp actually creates these message transactions. We'll first initialize our transactor by using the soundac_transact package:

var transactor = soundac_transact(soundac, 'basic_messaging_app_');

Then we state that when the user creates a new line, intending to send a message, they will create a message transaction:

rl.on('line', function(input) {
  transactor.json(username, key, 'message', {
    message: input
  }, function(err, result) {
    if(err) {
      console.error(err);
    }
  });
});

The json function (used for creating a customJSON transaction) has 5 arguments:

username: username of the user to send a transaction from,

key: the private key of the above user, used to sign the transaction,

id: the id of the transaction to create,

json: the json of the transaction to create; in this case we create a small object which contains the message,

callback: the function to call once the transaction is created (or an error occurs).

Here is the full code:

const soundac = require('museblockchain-js');
const soundac_state = require('soundac_state');
const soundac_transact = require('soundac_transact');
const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

var username = 'your_username_goes_here';
var key = 'your_private_posting_key_goes_here';

soundac.config.set('websocket', 'wss://node.soundac.io');
soundac.api.getDynamicGlobalProperties().then((result) => {
  var processor = soundac_state(soundac, result.head_block_number, 100,'basic_messaging_app_');
  processor.on('message', function(json, from) {
    console.log(from, 'says:', json.message);
  });
  processor.start();
});

var transactor = soundac_transact(soundac, 'basic_messaging_app_');
rl.on('line', function(input) {
  transactor.json(username, key, 'message', {
    message: input
  }, function(err, result) {
    if(err) {
      console.error(err);
    }
  });
});

If you run your code using node index.js you should get a terminal wher you can enter in text. Simply type in a message, press enter, and in a few moments your message will show up. Try running multiple instances at the same time to see that it is actually running on a blockchain. You can also look from https://soundacdb.com/your_username_here (a Soundac block explorer) to see your recent transactions. You should see a few transactions titled custom_json which have the json data of the transactions you created while testing the messaging app.

Next

Before you start developing your own app, learn how to create a token using soundac_state and learn about design patterns to use when using soundac_state, read more about building decentralized apps using soft consensus

Built upon steem-state project (https://github.com/nicholascc/steem-state)