-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstant.js
197 lines (184 loc) · 7.46 KB
/
instant.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*jslint node: true */
'use strict';
var async = require('byteballcore/node_modules/async');
var notifications = require('./notifications.js');
var settlement = require('./settlement.js');
var book = require('./book.js');
var db = require('byteballcore/db.js');
var mutex = require('byteballcore/mutex.js');
var eventBus = require('byteballcore/event_bus.js');
const INSTANT_MARGIN = 0.02;
const MAX_BTC = 0.2;
const MAX_GB = 1;
// from customer's perspective, BTC/GB
const SAFE_BUY_RATE = 0.04;
const SAFE_SELL_RATE = 0.01;
// from customer's perspective, BTC/GB
var buy_rate = SAFE_BUY_RATE; // higher
var sell_rate = SAFE_SELL_RATE; // lower
function getBuyRate(){
return buy_rate;
}
function getSellRate(){
return sell_rate;
}
function handleInstantSellOrder(conn, byte_seller_deposit_id, byte_amount, device_address, onDone){
var satoshi_amount = book.bytes2satoshis(byte_amount, sell_rate);
if (satoshi_amount === 0)
throw Error("satoshi_amount=0");
conn.query("SELECT * FROM byte_buyer_orders WHERE is_active=1 AND price>=? ORDER BY price DESC, last_update ASC", [sell_rate], function(buyer_rows){
var total_satoshi = buyer_rows.reduce(function(acc, buyer_order){ return acc + buyer_order.satoshi_amount; }, 0);
if (total_satoshi < satoshi_amount){
book.insertSellerOrder(conn, byte_seller_deposit_id, byte_amount, device_address, sell_rate, function(){
var device = require('byteballcore/device.js');
device.sendMessageToDevice(device_address, 'text', "Your payment is now final but there's not enough liquidity to complete the exchange. We'll exchange your bytes as soon as possible.");
});
return onDone();
}
book.finishSellerDeposit(conn, byte_seller_deposit_id, 0, byte_amount, function(){
conn.query(
"INSERT INTO byte_seller_instant_deals (byte_seller_deposit_id, satoshi_amount, byte_amount, price) VALUES (?,?,?,?)",
[byte_seller_deposit_id, satoshi_amount, byte_amount, sell_rate],
function(res){
var byte_seller_instant_deal_id = res.insertId;
var remaining_satoshi_amount = satoshi_amount;
async.eachSeries(
buyer_rows,
function(buyer_order, cb){
var execution_price = buyer_order.price;
var bFull = (remaining_satoshi_amount >= buyer_order.satoshi_amount); // full execution of the book order
var bDone = (remaining_satoshi_amount <= buyer_order.satoshi_amount);
var transacted_satoshis = bFull ? buyer_order.satoshi_amount : remaining_satoshi_amount;
var transacted_bytes = book.satoshis2bytes(transacted_satoshis, execution_price);
var buyer_order_props = {
execution_price: execution_price,
transacted_satoshis: transacted_satoshis,
transacted_bytes: transacted_bytes,
byte_seller_instant_deal_id: byte_seller_instant_deal_id
};
book.markBuyerOrderMatched(conn, buyer_order.byte_buyer_order_id, buyer_order_props, function(){
remaining_satoshi_amount -= transacted_satoshis;
if (bFull)
return bDone ? cb('done') : cb();
book.insertRemainderBuyerOrder(conn, buyer_order, transacted_satoshis, function(){
bDone ? cb('done') : cb();
});
});
},
function(err){
if (!err)
throw Error('buyer rows not interrupted');
onDone();
}
);
}
);
});
});
}
function handleInstantBuyOrder(conn, byte_buyer_deposit_id, satoshi_amount, device_address, onDone){
var byte_amount = book.satoshis2bytes(satoshi_amount, buy_rate);
conn.query("SELECT * FROM byte_seller_orders WHERE is_active=1 AND price<=? ORDER BY price ASC, last_update ASC", [buy_rate], function(seller_rows){
var total_bytes = seller_rows.reduce(function(acc, seller_order){ return acc + seller_order.byte_amount; }, 0);
if (total_bytes < byte_amount){
book.insertBuyerOrder(conn, byte_buyer_deposit_id, satoshi_amount, device_address, buy_rate, function(){
var device = require('byteballcore/device.js');
device.sendMessageToDevice(device_address, 'text', "Your payment is now confirmed but there's not enough liquidity to complete the exchange. We'll exchange your bitcoins as soon as possible.");
});
return onDone();
}
book.finishBuyerDeposit(conn, byte_buyer_deposit_id, 0, satoshi_amount, function(){
conn.query(
"INSERT INTO byte_buyer_instant_deals (byte_buyer_deposit_id, satoshi_amount, byte_amount, price) VALUES (?,?,?,?)",
[byte_buyer_deposit_id, satoshi_amount, byte_amount, buy_rate],
function(res){
var byte_buyer_instant_deal_id = res.insertId;
var remaining_byte_amount = byte_amount;
async.eachSeries(
seller_rows,
function(seller_order, cb){
var execution_price = seller_order.price;
var bFull = (remaining_byte_amount >= seller_order.byte_amount); // full execution of the book order
var bDone = (remaining_byte_amount <= seller_order.byte_amount);
var transacted_bytes = bFull ? seller_order.byte_amount : remaining_byte_amount;
var transacted_satoshis = book.bytes2satoshis(transacted_bytes, execution_price);
if (transacted_satoshis === 0)
throw Error("transacted_satoshis=0");
var seller_order_props = {
execution_price: execution_price,
transacted_satoshis: transacted_satoshis,
transacted_bytes: transacted_bytes,
byte_buyer_instant_deal_id: byte_buyer_instant_deal_id
};
book.markSellerOrderMatched(conn, seller_order.byte_seller_order_id, seller_order_props, function(){
remaining_byte_amount -= transacted_bytes;
if (bFull)
return bDone ? cb('done') : cb();
book.insertRemainderSellerOrder(conn, seller_order, transacted_bytes, function(){
bDone ? cb('done') : cb();
});
});
},
function(err){
if (!err)
throw Error('seller rows not interrupted');
onDone();
}
);
}
);
});
});
}
function updateInstantRates(){
db.query("SELECT price, byte_amount FROM byte_seller_orders WHERE is_active=1 ORDER BY price ASC, last_update ASC", function(rows){
var accumulated_bytes = 0;
var bFound = false;
var price;
var max_price = SAFE_BUY_RATE;
for (var i=0; i<rows.length; i++){
price = rows[i].price;
if (price > max_price)
max_price = price;
accumulated_bytes += rows[i].byte_amount;
if (accumulated_bytes >= MAX_GB*1e9){
bFound = true;
break;
}
}
if (!bFound){
buy_rate = max_price;
return notifications.notifyAdmin('not enough sell-side liquidity');
}
buy_rate = Math.round(price*(1+INSTANT_MARGIN)*10000)/10000;
});
db.query("SELECT price, satoshi_amount FROM byte_buyer_orders WHERE is_active=1 ORDER BY price DESC, last_update ASC", function(rows){
var accumulated_satoshis = 0;
var bFound = false;
var price;
var min_price = SAFE_SELL_RATE;
for (var i=0; i<rows.length; i++){
price = rows[i].price;
if (price < min_price)
min_price = price;
accumulated_satoshis += rows[i].satoshi_amount;
if (accumulated_satoshis >= MAX_BTC*1e8){
bFound = true;
break;
}
}
if (!bFound){
sell_rate = min_price;
return notifications.notifyAdmin('not enough buy-side liquidity');
}
sell_rate = Math.round(price/(1+INSTANT_MARGIN)*10000)/10000;
});
}
eventBus.on('book_changed', updateInstantRates);
exports.MAX_BTC = MAX_BTC;
exports.MAX_GB = MAX_GB;
exports.getBuyRate = getBuyRate;
exports.getSellRate = getSellRate;
exports.handleInstantSellOrder = handleInstantSellOrder;
exports.handleInstantBuyOrder = handleInstantBuyOrder;
exports.updateInstantRates = updateInstantRates;