-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
97 lines (84 loc) · 2.46 KB
/
app.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
console.log("CoinApp Running");
const express = require('express');
const Client = require('coinbase').Client;
const app = express();
app.use(express.json());
const API_KEY = "";
const API_SECRET = "";
const DEFAULT_ACCOUNT = "primary";
const client = new Client({
"apiKey": API_KEY,
"apiSecret": API_SECRET,
"api_version": "2021-01-22",
"strictSSL": false
});
app.post('/send/', function (req, res) {
const params = req.body;
// const to = params.to;
// const amount = params.amount;
// const currency = params.currency;
if (!params.accountID) {
params.accountID = DEFAULT_ACCOUNT;
}
getAccount(client, params, res)
.then((details) => {
const account = details.account;
console.log("getAccount:", account);
return sendMoney(account, details.params)
.then((output) => {
console.log("Successful: ", output);
res.status(200).send(output);
});
}).catch((err) => {
switch (err.name) {
case "ExpiredToken":
res.status(401).send("Coinbase session expired");
break;
case "ValidationError":
res.status(400).send(err.message);
break;
default:
res.status(400).send(err.message);
break;
}
});
});
function getAccount(client, params, res) {
if (!client) {
//Unauthorized
res.sendStatus(401);
return;
}
return new Promise((resolve, reject) => {
client.getAccount(params.accountID, function (err, account) {
if (err) {
reject(err);
return;
} else {
resolve({
account,
params
});
return;
}
});
});
}
function sendMoney(account, params) {
return new Promise((resolve, reject) => {
console.log("sendMoney: ", params);
account.sendMoney({
'to': `${params.to}`,
'amount': `${params.amount}`,
'currency': `${params.currency}`
}, function (err, tx) {
if (err) {
console.log("ErrorName", err.name);
reject(err);
} else {
resolve(tx);
}
});
});
}
app.listen(3000);