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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
const axios = require("axios");
module.exports = async (req, res) => {
let { amount, asset, username } = req.body;
let { user } = req;
l.info("attempting internal payment", user.username, amount);
if (!amount || amount < 0)
return res.status(500).send("Amount must be greater than zero");
try {
await db.transaction(async transaction => {
let account = await db.Account.findOne({
where: {
user_id: user.id,
asset
},
lock: transaction.LOCK.UPDATE,
transaction
});
if (account.balance < amount) {
throw new Error("Insufficient funds");
}
let fee = 0;
account.balance -= amount;
await account.save({ transaction });
let payment = await db.Payment.create(
{
amount: -amount,
account_id: account.id,
user_id: user.id,
rate: app.get("rates")[user.currency],
currency: user.currency,
confirmed: true,
hash: "Internal Transfer",
network: "COINOS"
},
{ transaction }
);
payment = payment.get({ plain: true });
payment.account = account.get({ plain: true });
l.info("sent internal", user.username, -payment.amount);
emit(user.username, "payment", payment);
emit(user.username, "user", user);
res.send(payment);
user = await db.User.findOne({
where: { username }
});
let params = {
user_id: user.id,
asset
};
account = await db.Account.findOne({
where: params,
lock: transaction.LOCK.UPDATE,
transaction
});
if (account) {
account.balance += amount;
await account.save({ transaction });
} else {
let name = asset.substr(0, 6);
let ticker = asset.substr(0, 3).toUpperCase();
let precision = 8;
const assets = (await axios.get("https://assets.blockstream.info/"))
.data;
if (assets[asset]) {
({ ticker, precision, name } = assets[asset]);
} else {
const existing = await db.Account.findOne({
where: {
asset
},
order: [["id", "ASC"]],
limit: 1
});
l.info("existing", existing);
if (existing) {
l.info(existing.name);
({ ticker, precision, name } = existing);
}
}
params = { ...params, ...{ ticker, precision, name } };
params.balance = amount;
params.pending = 0;
account = await db.Account.create(params, { transaction });
}
payment = await db.Payment.create(
{
amount,
account_id: account.id,
user_id: user.id,
rate: app.get("rates")[user.currency],
currency: user.currency,
confirmed: true,
hash: "Internal Transfer",
network: "COINOS",
received: true
},
{ transaction }
);
payment = payment.get({ plain: true });
payment.account = account.get({ plain: true });
emit(user.username, "account", account);
emit(user.username, "payment", payment);
l.info("received internal", user.username, payment.amount);
});
} catch (e) {
l.error(
"problem sending internal payment",
user.username,
user.balance,
e.message
);
return res.status(500).send(e.message);
}
};
|