Skip to content
This repository has been archived by the owner on Dec 3, 2019. It is now read-only.

confio/weave-js

Repository files navigation

Weave Javascript Client

Installation

Setting it up:

npm install -g yarn
yarn install

If you want to build the libraries, stored under ./lib:

yarn build

Demo

First you need to check out weave and build the mycoind example (make deps && make install).

Setting up the js cli

In one terminal window (change /tmp/demo.db for wherever you want to store keys):

yarn cli /tmp/demo.db:

keys.add("demo");
keys.add("rcpt");
keys.list();
await keys.save();

Quit and restart the process, and make sure keys are still there...

yarn cli /tmp/demo.db:

keys.list()

Store the provided address as ADDRESS.

Setting up the blockchain

Once you have compiled everything, start a blockchain. More explaination on the weave README. But you can just run these in another shell (making sure $ADDRESS is set properly).

rm -rf ~/.mycoind
tendermint init --home ~/.mycoind
mycoind init CASH $ADDRESS
# verify it set up proper
grep address ~/.mycoind/config/genesis.json

tendermint node --home ~/.mycoind > /tmp/tendermint.log &
mycoind start

Running queries on the blockchain

yarn cli /tmp/demo.db:

// this connects to ws://localhost:46657 unless you provide a url
let client = new Client()
let demo = keys.get("demo");

let acct = await queryAccount(client, demo.address());
pprint(acct);

Sending Cash

Now it's time to make a transaction on the blockchain. Here goes nothing!

yarn cli /tmp/demo.db:

let client = new Client()
let demo = keys.get("demo");
let rcpt = keys.get("rcpt");
let chainID = await client.chainID();

// send some money
let tx = buildSendTx(demo, rcpt.address(), 5000, 'CASH', chainID);
let resp = await client.sendTx(tx);
pprint(resp);

let acctRcpt = await queryAccount(client, rcpt.address());
pprint(acctRcpt);
let acctSend = await queryAccount(client, demo.address());
pprint(acctSend);

// sequence auto-increments, to send a second time
tx = buildSendTx(demo, rcpt.address(), 1500, 'CASH', chainID);
await client.sendTx(tx);
acctRcpt = await queryAccount(client, rcpt.address());
pprint(acctRcpt);

// this save rcpt and updates demo's sequence for next send
await keys.save();
^d

Searching and subscribing to tx

We can look up the history and be notified of changes:

yarn cli /tmp/demo.db:

let client = new Client()
let demo = keys.get("demo");
let rcpt = keys.get("rcpt");
let chainID = await client.chainID();

// this returns all tx that modify demo account
await searchTx(client, demo.address());
// this returns all tx that demo signed
await searchMyTx(client, demo.address());

// now, let's try to subscribe to my changes
let txs = await client.search("cash", demo.address());
txs.map(x => x.height);
// this one is never triggered :(
client.subscribeTx("cash", demo.address(), () => console.log("match"));
// this one is triggered
client.subscribeAllTx(pprint);

// send some money
let tx = buildSendTx(demo, rcpt.address(), 5000, 'CASH', chainID);
await client.sendTx(tx);
// to store the new sequence numbers
await keys.save();

await client.search("cash", demo.address());
^d