0.0.35 • Published 6 years ago

js-fluence-client v0.0.35

Weekly downloads
3
License
Apache-2.0
Repository
github
Last release
6 years ago

IMPORTANT: Client and README are under heavy development and can be outdated. Ask a question in gitter or file an issue.

Fluence Javascript Client

Fluence Javascript Client allows you to interact with a real-time cluster from a browser or use it like npm module.

Motivation

Browser client simplifies interaction with the real-time cluster for a user. It has zero dependencies and can be added to other code. Developers can deploy their backend code to Fluence network and built a web3 application based on Fluence Client to interact with this backend code. It is written on TypeScript to reach more clean, understandable and typesafe code with good reusability.

Documentation

Installation

npm install js-fluence-client

Before started

You need to have a deployed real-time cluster. You can use this part of project to deploy 4-nodes cluster with increment and multiply functions as an example.

TODO: HOWTO deploy custom code (link to some other readme)

Init client and session

For interaction with the default increment and multiply cluster you can use IncrementAndMultiply class. If we have a local cluster, we can initialize client like this:

let client = new IncrementAndMultiply("localhost", 46157);

Than we can use built-in functions:

// mutate state but returns nothing
client.incrementCounter();

// get result after counter
let counter = await client.getCounter().result();
console.log(JSON.stringify(counter));

// get result of moltiplying
let res = await client.multiply(107, 124).result();

// should be 13268
console.log(JSON.stringify(res));

If you deployed your own code, you can use CustomCommands

let client = new CustomCommands("localhost", 46157);

// will return some result based on logic of deployed code
let res = await client.submit("<some-custom-command>").result();

If you want to write your own library, you can use IncrementAndMultiply.ts as a basis:

import {TendermintClient} from "../TendermintClient";
import {Engine} from "../Engine";
import {Signer} from "../Signer";
import {Client} from "../Client";
import {Session} from "../Session";

class IncrementAndMultiply {

    // a session is needed for ordered transactions, you can use multiple sessions if needed
    private session: Session;

    constructor(host: string, port: number) {
        // there is initializing RPC to tendermint cluster
        let tm = new TendermintClient(host, port);

        // creates engine that can start new sessions
        let engine = new Engine(tm);

        // default signing key for now
        // signing key can be generated or received after some authorize processes
        let signingKey = "TVAD4tNeMH2yJfkDZBSjrMJRbavmdc3/fGU2N2VAnxT3hAtSkX+Lrl4lN5OEsXjD7GGG7iEewSod472HudrkrA==";

        // creates signer that can sign messages
        let signer = new Signer(signingKey);

        // `client002` is a default client for now
        // creates client with id and signer
        let client = new Client("client002", signer);

        // generates the random session. If you want to generate session on your own - use createSession(client, "some-id")
        this.session = engine.genSession(client);
    }

    // uses the session to submit commands you want to
    async incrementCounter() {
        console.log("do increment");
        return this.session.invoke("inc");
    }

    async getCounter() {
        let res = await this.session.invoke("get");
        console.log(`get result is: ${JSON.stringify(res)}`);
        return res
    }

    async multiply(first: number, second: number) {
        let res = await this.session.invoke("multiplier.mul", [first.toString(), second.toString()]).result();
        console.log(`multiply result is: ${JSON.stringify(res)}`);
        return res;
    }
}

You can find other examples in the examples folder.

0.0.35

6 years ago

0.0.34

6 years ago

0.0.33

6 years ago

0.0.32

6 years ago

0.0.31

6 years ago

0.0.30

6 years ago

0.0.29

6 years ago

0.0.28

6 years ago

0.0.27

6 years ago

0.0.26

6 years ago

0.0.25

6 years ago

0.0.24

6 years ago

0.0.21

6 years ago

0.0.20

6 years ago

0.0.19

6 years ago

0.0.18

6 years ago

0.0.17

6 years ago

0.0.16

6 years ago

0.0.15

6 years ago

0.0.14

6 years ago

0.0.13

6 years ago

0.0.12

6 years ago

0.0.11

6 years ago

0.0.10

6 years ago

0.0.9

6 years ago

0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago