This repository has been archived by the owner on Aug 7, 2021. It is now read-only.
/
ncb.js
108 lines (102 loc) · 5.41 KB
/
ncb.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
var HotTap = require('hottap').hottap;
var _ = require('underscore');
var base_url = 'https://coinbase.com/api/v1/';
function buy_price(amount, next) {
var url = base_url + 'prices/buy?qty=' + amount;
return HotTap(url).request('GET', function(err, response) {
if ( err ) { return next(err); };
if ( response.status != 200 ) {
return next('invalid status ' + response.status + ' received');
} else {
var json = JSON.parse(response.body);
if ( _.isUndefined(json.amount) || _.isUndefined(json.currency) ) {
return next('invalid response received');
} else {
if ( json.currency != 'USD' ) {
return next('invalid currency received');
} else {
return next(null, json.amount);
}
}
}
});
}
function sell_price(amount, next) {
var url = base_url + 'prices/sell?qty=' + amount;
return HotTap(url).request('GET', function(err, response) {
if ( err ) { return next(err); };
if ( response.status != 200 ) {
return next('invalid status ' + response.status + ' received');
} else {
var json = JSON.parse(response.body);
if ( _.isUndefined(json.amount) || _.isUndefined(json.currency) ) {
return next('invalid response received');
} else {
if ( json.currency != 'USD' ) {
return next('invalid currency received');
} else {
return next(null, json.amount);
}
}
}
});
}
function balance(api_key, next) {
var url = base_url + 'account/balance?api_key=' + api_key;
return HotTap(url).request("GET", function(err, response) {
if ( err ) { return next(err); };
if ( response.status != 200 ) {
return next('invalid status ' + response.status + ' received');
} else {
var json = JSON.parse(response.body);
if ( _.isUndefined(json.amount) || _.isUndefined(json.currency) ) {
return next('invalid response received');
} else {
if ( json.currency != 'BTC' ) {
return next('invalid currency received');
} else {
return next(null, json.amount);
}
}
}
});
}
function send_btc(api_key, email, amount, note, next) {
var url = base_url + 'transactions/send_money?api_key=' + api_key;
var o_body = {
transaction : {
to : email,
amount : amount,
note : note
}
};
return HotTap(url).request("POST",
{"Content-Type" : "application/json"},
JSON.stringify(o_body),
function(err, response) {
if ( err ) { return next(err); };
if ( response.status != 200 ) {
return next('invalid status ' + response.status + ' received');
} else {
var json = JSON.parse(response.body);
if ( _.isUndefined(json.success) ) {
return next('invalid response received (no success)');
} else {
if ( ! json.success ) {
return next(JSON.stringify(json.errors));
} else {
if ( _.isUndefined(json.transaction.id) ) {
return next('invalid response received (no transaction id)');
}
return next(null, json.transaction.id);
}
}
}
});
}
module.exports = {
balance:balance,
send_btc:send_btc,
buy_price:buy_price,
sell_price:sell_price
}