perak:deon-connection

v1.0.4Published 7 years ago

This package has not had recent updates. Please investigate it's current state before committing to using it in your project.

Deon Connection

DeonDigital API wrapper for Meteor

Package status

Experimental

Install

meteor add perak:deon-connection

Usage

In server scope:

1
2import { DeonConnection } from "meteor/perak:deon-connection";
3
4global.DCON = new DeonConnection();
5
6Meteor.startup(function() {
7
8	DCON.connect({
9		url: "http://localhost:8081",
10		eventsUrl: "ws://localhost:8081/contractUpdates"
11	});
12
13	DCON.onContractUpdate(function(event) {
14		console.log("Yay! We have an event!!!");
15		console.log(event);
16	});
17
18});
19

And... that's it! Now your application is listening for events on websocket and you can send GET and POST requests to DeonDigital API.

DeonConnection methods

connect(options)

Connect to Deon API.

options is object with following members:

{
	url: "URL to Deon API root",
	eventsUrl: "URL to Deon's event stream websocket"
}

onContractUpdate(callback)

Register callback which will fire when event is received from Deon API. Callback will be called with one argument event which is object.

apiPost(url, data [, callback])

HTTP POST request to Deon API. If no callback is given then it executes synchroneously and returns response object. If you provide callback, it is called with two arguments: error (Meteor.Error object) and response (Object).

apiGet(url [, callback])

HTTP GET request to Deon API. If no callback is given then it executes synchroneously and returns response object. If you provide callback, it is called with two arguments: error (Meteor.Error object) and response (Object).

Example: deploy Smart Contract

In server scope (assuming you got DCON variable from previous example):

1Meteor.methods({
2	"DeployContract": function(csl, templateName, contractName, entryPoint, peers) {
3
4		var template = {
5			name: templateName,
6			csl: csl
7		};
8
9		var response = DCON.apiPost("/templates", template);
10
11		var data = {
12			name: contractName,
13			templateId: response.id,
14			templateExpressionArguments: [
15			],
16			entryPoint: entryPoint,
17			peers: peers || []
18		};
19
20		var r = DCON.apiPost("/contracts", data);
21
22		return r;
23
24	}
25});
26

Now, from client you can call:

1Meteor.call(
2	"DeployContract",
3
4	"contract Dummy() = success",
5	"DummyTemplate",
6	"DummyContract",
7	"Dummy",
8	[],
9
10	function(err, res) {
11		if(err) {
12			alert(err);
13		} else {
14			alert("Yeah! It's deployed! " + res);
15		}
16	}
17);

That's all folks, Enjoy!