aboutsummaryrefslogtreecommitdiffstats
path: root/routes
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2020-10-11 01:12:37 -0700
committerAdam Soltys <[email protected]>2020-10-11 01:12:37 -0700
commite894563fbf75706649db1b67ecdc4661bb31f9c2 (patch)
tree8a5f8350f18d2fcfe36988b8c468d2b5fb749d42 /routes
parent8b5df12518f245b8e29871a4a71d5bbcee172ce8 (diff)
downloadcoinos-server-e894563fbf75706649db1b67ecdc4661bb31f9c2.tar.gz
coinos-server-e894563fbf75706649db1b67ecdc4661bb31f9c2.zip
get maker to check the ask price
Diffstat (limited to 'routes')
-rw-r--r--routes/invoices.js9
-rw-r--r--routes/liquid/receive.js176
-rw-r--r--routes/liquid/send.js1
-rw-r--r--routes/swaps.js81
4 files changed, 161 insertions, 106 deletions
diff --git a/routes/invoices.js b/routes/invoices.js
index 11d62eb6..59adb862 100644
--- a/routes/invoices.js
+++ b/routes/invoices.js
@@ -17,6 +17,8 @@ app.get("/invoice", auth, ah(async (req, res, next) => {
app.post("/invoice", ah(async (req, res, next) => {
try {
let { invoice, user } = req.body;
+ let { blindkey } = invoice;
+
if (!user) ({ user } = req);
else {
user = await db.User.findOne({
@@ -38,7 +40,7 @@ app.post("/invoice", ah(async (req, res, next) => {
invoice.currency,
);
- if (invoice.network === "liquid") {
+ if (invoice.network === "liquid" && !invoice.unconfidential) {
invoice.unconfidential = (
await lq.getAddressInfo(invoice.address)
).unconfidential;
@@ -58,7 +60,10 @@ app.post("/invoice", ah(async (req, res, next) => {
invoice = exists ? await exists.update(invoice) : await db.Invoice.create(invoice);
addresses[invoice.address] = user.username;
- if (invoice.unconfidential) addresses[invoice.unconfidential] = user.username;
+ if (invoice.unconfidential) {
+ addresses[invoice.unconfidential] = user.username;
+ // await lq.importBlindingKey(invoice.address, blindkey);
+ }
res.send(invoice);
} catch(e) {
l.error(e.message, e.stack);
diff --git a/routes/liquid/receive.js b/routes/liquid/receive.js
index 097dc53a..2976bf57 100644
--- a/routes/liquid/receive.js
+++ b/routes/liquid/receive.js
@@ -12,16 +12,15 @@ const network =
config.liquid.network === "mainnet" ? "liquid" : config.liquid.network
];
-const getAccount = async (params) => {
+const getAccount = async params => {
let account = await db.Account.findOne({
where: params
});
-
if (account) {
l.info("found account", params, account.asset, account.id);
return account;
- }
+ }
let { asset, pubkey } = params;
let name = asset.substr(0, 6);
@@ -29,7 +28,7 @@ const getAccount = async (params) => {
let ticker = asset.substr(0, 3).toUpperCase();
let precision = 8;
- const assets = app.get('assets');
+ const assets = app.get("assets");
if (assets[asset]) {
({ domain, ticker, precision, name } = assets[asset]);
@@ -37,10 +36,10 @@ const getAccount = async (params) => {
const existing = await db.Account.findOne({
where: {
asset,
- pubkey,
+ pubkey
},
order: [["id", "ASC"]],
- limit: 1,
+ limit: 1
});
if (existing) {
@@ -51,7 +50,7 @@ const getAccount = async (params) => {
params = { ...params, ...{ domain, ticker, precision, name } };
params.balance = 0;
- params.network = 'liquid';
+ params.network = "liquid";
return db.Account.create(params);
};
@@ -71,88 +70,93 @@ zmqRawTx.on("message", async (topic, message, sequence) => {
if (payments.includes(blinded.txid)) return;
Promise.all(
- tx.vout.map(async (o) => {
- if (!(o.scriptPubKey && o.scriptPubKey.addresses)) return;
+ tx.vout.map(async o => {
+ try {
+ if (!(o.scriptPubKey && o.scriptPubKey.addresses)) return;
- const { asset } = o;
- const value = toSats(o.value);
- const address = o.scriptPubKey.addresses[0];
+ const { asset } = o;
+ const value = toSats(o.value);
+ const address = o.scriptPubKey.addresses[0];
- if (
- Object.keys(addresses).includes(address) &&
- !change.includes(address)
- ) {
- let user = await getUser(addresses[address]);
+ if (
+ Object.keys(addresses).includes(address) &&
+ !change.includes(address)
+ ) {
+ let user = await getUser(addresses[address]);
- let invoice = await db.Invoice.findOne({
- where: {
- unconfidential: address,
- user_id: user.id,
- network: "liquid",
- },
- order: [["id", "DESC"]],
- include: {
- model: db.Account,
- as: "account",
- },
- });
+ let invoice = await db.Invoice.findOne({
+ where: {
+ unconfidential: address,
+ user_id: user.id,
+ network: "liquid"
+ },
+ order: [["id", "DESC"]],
+ include: {
+ model: db.Account,
+ as: "account"
+ }
+ });
- if (!invoice) return;
+ if (!invoice) return;
- let confirmed = 0;
+ let confirmed = 0;
- let { account } = invoice;
+ let { account } = invoice;
- if (account.asset === asset && (!account.pubkey || account.network === 'liquid')) {
- account.pending += value;
- await account.save();
- } else {
- account = await getAccount(
- {
+ if (
+ account.asset === asset &&
+ (!account.pubkey || account.network === "liquid")
+ ) {
+ account.pending += value;
+ await account.save();
+ } else {
+ account = await getAccount({
seed: account.seed,
path: account.path,
user_id: user.id,
asset,
pubkey: account.pubkey,
pending: value,
- index: 0,
- },
- );
- }
+ index: 0
+ });
+ }
- if (config.liquid.walletpass)
- await lq.walletPassphrase(config.liquid.walletpass, 300);
+ if (config.liquid.walletpass)
+ await lq.walletPassphrase(config.liquid.walletpass, 300);
- await user.save();
+ await user.save();
- const currency = invoice ? invoice.currency : user.currency;
- const rate = invoice ? invoice.rate : app.get("rates")[user.currency];
- const tip = invoice ? invoice.tip : 0;
- const memo = invoice ? invoice.memo : "";
+ const currency = invoice ? invoice.currency : user.currency;
+ const rate = invoice ? invoice.rate : app.get("rates")[user.currency];
+ const tip = invoice ? invoice.tip : 0;
+ const memo = invoice ? invoice.memo : "";
- let payment = await db.Payment.create({
- account_id: account.id,
- user_id: user.id,
- hash: blinded.txid,
- amount: value - tip,
- currency,
- memo,
- rate,
- received: true,
- tip,
- confirmed,
- address,
- network: "liquid",
- });
+ let payment = await db.Payment.create({
+ account_id: account.id,
+ user_id: user.id,
+ hash: blinded.txid,
+ amount: value - tip,
+ currency,
+ memo,
+ rate,
+ received: true,
+ tip,
+ confirmed,
+ address,
+ network: "liquid"
+ });
- payments.push(blinded.txid);
- payment = payment.get({ plain: true });
- payment.account = account.get({ plain: true });
+ payments.push(blinded.txid);
+ payment = payment.get({ plain: true });
+ payment.account = account.get({ plain: true });
- emit(user.username, "payment", payment);
- emit(user.username, "account", payment.account);
- l.info("liquid detected", address, user.username, asset, value);
- notify(user, `${value} SAT payment detected`);
+ emit(user.username, "payment", payment);
+ emit(user.username, "account", payment.account);
+ l.info("liquid detected", address, user.username, asset, value);
+ notify(user, `${value} SAT payment detected`);
+ }
+ } catch (e) {
+ l.error("Problem processing transaction", e.message, e.stack);
}
})
);
@@ -162,16 +166,16 @@ let queue = {};
zmqRawBlock.on("message", async (topic, message, sequence) => {
const payments = await db.Payment.findAll({
- where: { confirmed: 0 },
+ where: { confirmed: 0 }
});
const block = elements.Block.fromHex(message.toString("hex"), true);
const hash = await lq.getBlockHash(block.height);
const json = await lq.getBlock(hash, 2);
- json.tx.map(async (tx) => {
+ json.tx.map(async tx => {
if (issuances[tx.txid]) {
- await db.transaction(async (transaction) => {
+ await db.transaction(async transaction => {
const {
user_id,
asset,
@@ -179,7 +183,7 @@ zmqRawBlock.on("message", async (topic, message, sequence) => {
asset_payment_id,
token,
token_amount,
- token_payment_id,
+ token_payment_id
} = issuances[tx.txid];
const user = await getUserById(user_id);
@@ -187,7 +191,7 @@ zmqRawBlock.on("message", async (topic, message, sequence) => {
let account = await db.Account.findOne({
where: { user_id, asset },
lock: transaction.LOCK.UPDATE,
- transaction,
+ transaction
});
account.balance = asset_amount * SATS;
account.pending = 0;
@@ -197,10 +201,10 @@ zmqRawBlock.on("message", async (topic, message, sequence) => {
where: { id: asset_payment_id },
include: {
model: db.Account,
- as: "account",
+ as: "account"
},
lock: transaction.LOCK.UPDATE,
- transaction,
+ transaction
});
payment.confirmed = true;
@@ -215,7 +219,7 @@ zmqRawBlock.on("message", async (topic, message, sequence) => {
account = await db.Account.findOne({
where: { user_id, asset: token },
lock: transaction.LOCK.UPDATE,
- transaction,
+ transaction
});
account.balance = token_amount * SATS;
account.pending = 0;
@@ -225,10 +229,10 @@ zmqRawBlock.on("message", async (topic, message, sequence) => {
where: { id: token_payment_id },
include: {
model: db.Account,
- as: "account",
+ as: "account"
},
lock: transaction.LOCK.UPDATE,
- transaction,
+ transaction
});
payment.confirmed = true;
@@ -241,7 +245,7 @@ zmqRawBlock.on("message", async (topic, message, sequence) => {
emit(user.username, "payment", payment);
}
});
- } else if (payments.find((p) => p.hash === tx.txid)) queue[tx.txid] = 1;
+ } else if (payments.find(p => p.hash === tx.txid)) queue[tx.txid] = 1;
});
});
@@ -253,21 +257,21 @@ setInterval(async () => {
for (let i = 0; i < arr.length; i++) {
const hash = arr[i];
- await db.transaction(async (transaction) => {
+ await db.transaction(async transaction => {
let p = await db.Payment.findOne({
where: { hash, confirmed: 0, received: 1 },
include: [
{
model: db.Account,
- as: "account",
+ as: "account"
},
{
model: db.User,
- as: "user",
- },
+ as: "user"
+ }
],
lock: transaction.LOCK.UPDATE,
- transaction,
+ transaction
});
const { user } = p;
diff --git a/routes/liquid/send.js b/routes/liquid/send.js
index ebda5723..a04bc007 100644
--- a/routes/liquid/send.js
+++ b/routes/liquid/send.js
@@ -1,4 +1,5 @@
const btc = config.liquid.btcasset;
+const lcad = config.liquid.cadasset;
module.exports = ah(async (req, res) => {
let { user } = req;
diff --git a/routes/swaps.js b/routes/swaps.js
index b25e6550..a75be910 100644
--- a/routes/swaps.js
+++ b/routes/swaps.js
@@ -509,7 +509,7 @@ app.post(
include: [
{
model: db.User,
- as: "user",
+ as: "user"
},
{
model: db.Account,
@@ -592,8 +592,21 @@ app.post(
addresses[u_address_p] = order.user.username;
- console.log("new math", order.v2, Math.round(order.v2 * remaining / amount), amount, remaining, order.v1 - remaining);
- order.decrement({ v1: remaining, v2: Math.round(order.v2 * remaining / amount) }, { transaction });
+ console.log(
+ "new math",
+ order.v2,
+ Math.round((order.v2 * remaining) / amount),
+ amount,
+ remaining,
+ order.v1 - remaining
+ );
+ order.decrement(
+ {
+ v1: remaining,
+ v2: Math.round((order.v2 * remaining) / amount)
+ },
+ { transaction }
+ );
await order.save({ transaction });
await order.reload({ transaction });
@@ -835,10 +848,8 @@ app.post(
setInterval(async () => {
if (!app.get("rates")) return;
const amount = 0.0001;
- const btc =
- config.liquid.btcasset;
- const lcad =
- config.liquid.cadasset;
+ const btc = config.liquid.btcasset;
+ const lcad = config.liquid.cadasset;
const { CAD, USD } = app.get("rates");
const user = await db.User.findOne({
where: {
@@ -902,7 +913,10 @@ setInterval(async () => {
},
{ transaction }
);
- if (!bestBid || (params.v2 / params.v1 > bestBid.rate && p.acc2.balance >= params.v2)) {
+ if (
+ !bestBid ||
+ (params.v2 / params.v1 > bestBid.rate && p.acc2.balance >= params.v2)
+ ) {
p.v1 = params.v1;
p.v2 = params.v2;
await p.save();
@@ -950,16 +964,47 @@ setInterval(async () => {
v2: amount * SATS
};
- if (p && p.acc1.balance >= p.v1) {
- p.v1 = params.v1;
- p.v2 = params.v2;
- await p.save();
- p = p.get({ plain: true });
- p.a1 = params.a1;
- p.a2 = params.a2;
- p.rate = p.v2 / p.v1;
+ if (p) {
+ let bestAsk = await db.Order.findOne(
+ {
+ where: {
+ "$acc1.asset$": btc,
+ "$acc2.asset$": lcad,
+ accepted: false,
+ id: {
+ [Op.ne]: p.id
+ }
+ },
+ include: [
+ {
+ model: db.Account,
+ as: "acc1"
+ },
+ {
+ model: db.Account,
+ as: "acc2"
+ }
+ ],
+ order: [["rate", "ASC"]],
+ limit: 1
+ },
+ { transaction }
+ );
+
+ if (
+ !bestAsk ||
+ (params.v1 / params.v2 < bestAsk.rate && p.acc1.balance >= params.v1)
+ ) {
+ p.v1 = params.v1;
+ p.v2 = params.v2;
+ await p.save();
+ p = p.get({ plain: true });
+ p.a1 = params.a1;
+ p.a2 = params.a2;
+ p.rate = p.v2 / p.v1;
- broadcast("order", shallow(p));
+ broadcast("order", shallow(p));
+ }
} else {
try {
await swap(user, params);
@@ -968,4 +1013,4 @@ setInterval(async () => {
}
}
});
-}, 10000);
+}, 20000);