Quick Start
Installation
Lotion requires node v7.6.0 or higher, and a mac or linux machine.
$ npm install lotion
$ npm install -g lotion-cli
Building your first app
Let's start with the "Hello, World!" of distributed programs: a shared counter.
Create app.js
Create a file called app.js
and require
Lotion:
let lotion = require('lotion')
Design the initial state
Think about how to represent the entire state of your application as a single JavaScript object.
If you've used Redux or any other frontend state container library, this should feel familiar.
For this example, our entire application state will have this shape:
{
"count": Number
}
Initialize your application with an initial state like this:
let app = lotion({
initialState: {
count: 0
}
})
Transaction handlers
A transaction handler is a function that takes a (state, transaction)
pair and mutates the state
deterministically. state
and transaction
are both just plain old JavaScript objects.
If you've used Express or any other web framework with composable middleware, this should feel familiar.
For this example, let's create a transaction handler to increment state.count
if transaction.nonce
is equal to state.count
:
function transactionHandler(state, transaction) {
if (state.count === transaction.nonce) {
state.count++
}
}
app.use(transactionHandler)
Start app
Now let's put it all together, start our app, and log our application's Global Chain Identifier (GCI):
// app.js
let lotion = require('lotion')
let app = lotion({
initialState: {
count: 0
}
})
function transactionHandler(state, transaction) {
if (state.count === transaction.nonce) {
state.count++
}
}
app.use(transactionHandler)
app.start().then(appInfo => console.log(appInfo.GCI))
Now run it:
$ node app.js
# prints your GCI
Querying the state and creating transactions
Check the state of your running application
$ lotion state <GCI>
# { "count": 0 }
Create a transaction to increment the count:
$ lotion send <GCI> '{ "nonce" : 0 }'
# {
# "check_tx": {},
# "deliver_tx": {},
# "hash": "47A59EDED43DE2BD81DCB58D13AEEAA16C092E31",
# "height": "33"
# }
Now check the state again:
$ lotion state <GCI>
# { "count": 1 }
Cool, the state was updated just as we expected!