-
Notifications
You must be signed in to change notification settings - Fork 0
/
bl3p.js
120 lines (109 loc) · 3.67 KB
/
bl3p.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
module.exports = function(RED) {
"use strict";
var bl3p = require('bl3p');
function Bl3pApiNode(config) {
RED.nodes.createNode(this,config);
}
RED.nodes.registerType("bl3p-api",Bl3pApiNode,{
credentials: {
publickey: {type:"text"},
privatekey: {type:"password"}
}
});
function AddOrderNode(config) {
RED.nodes.createNode(this,config);
var node = this;
var bl3papi = RED.nodes.getNode(config.bl3p);
var bl3p_auth = new bl3p.Bl3pAuth(bl3papi.credentials.publickey,bl3papi.credentials.privatekey);
node.on('input', function(msg) {
if ( msg.fee_currency === undefined ) {
node.send({result:false,payload:"fee_currency is required"});
return;
}
bl3p_auth.add_order(msg.amount, msg.type, msg.price, msg.fee_currency, msg.amount_funds, function(error,data){
if ( data ) {
data = JSON.parse(data);
if ( data.result === "success" ) {
node.send({result:true,payload:data,order_id:data.data.order_id});
} else {
node.send({result:false,payload:data});
}
} else {
node.send({result:false,payload:error,data:data});
}
});
});
}
function CancelOrderNode(config) {
RED.nodes.createNode(this,config);
var node = this;
var bl3papi = RED.nodes.getNode(config.bl3p);
var bl3p_auth = new bl3p.Bl3pAuth(bl3papi.credentials.publickey,bl3papi.credentials.privatekey);
node.on('input', function(msg) {
if ( msg.order_id === undefined ) {
node.send({result:false,payload:"order_id is required"});
return;
}
bl3p_auth.cancel_order(msg.order_id, function(error,data){
if ( data ) {
data = JSON.parse(data);
if ( data.result === "success" ) {
node.send({result:true,payload:data});
} else {
node.send({result:false,payload:data});
}
} else {
node.send({result:false,payload:error});
}
});
});
}
function OrderInfoNode(config) {
RED.nodes.createNode(this,config);
var node = this;
var bl3papi = RED.nodes.getNode(config.bl3p);
var bl3p_auth = new bl3p.Bl3pAuth(bl3papi.credentials.publickey,bl3papi.credentials.privatekey);
node.on('input', function(msg) {
if ( msg.order_id === undefined ) {
node.send({result:false,payload:"order_id is required"});
return;
}
bl3p_auth.order_info(msg.order_id, function(error,data){
if ( data ) {
data = JSON.parse(data);
if ( data.result === "success" ) {
node.send({result:true,payload:data,order_id:msg.order_id});
} else {
node.send({result:false,payload:data,order_id:msg.order_id});
}
} else {
node.send({result:false,payload:error,orderid:msg.order_id});
}
});
});
}
function AccountInfoNode(config) {
RED.nodes.createNode(this,config);
var node = this;
var bl3papi = RED.nodes.getNode(config.bl3p);
var bl3p_auth = new bl3p.Bl3pAuth(bl3papi.credentials.publickey,bl3papi.credentials.privatekey);
node.on('input', function(msg) {
bl3p_auth.account_info(function(error,data){
if ( data ) {
data = JSON.parse(data);
if ( data.result === "success" ) {
node.send({result:true,payload:data});
} else {
node.send({result:false,payload:data});
}
} else {
node.send({result:false,payload:error});
}
});
});
}
RED.nodes.registerType("add-order",AddOrderNode);
RED.nodes.registerType("cancel-order",CancelOrderNode);
RED.nodes.registerType("order-info",OrderInfoNode);
RED.nodes.registerType("account-info",AccountInfoNode);
};