aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2022-10-18 21:00:50 -0700
committerAdam Soltys <[email protected]>2022-10-18 21:58:51 -0700
commit14b363bcfcfdd03f2232c2d0779ee90fc5cc27e6 (patch)
treef928e100d091b26b1ec03808fa07933a50a8f178
parent22d2777bb6bf9e5eb61fd14c05d8ae9ae7586481 (diff)
downloadcoinos-server-14b363bcfcfdd03f2232c2d0779ee90fc5cc27e6.tar.gz
coinos-server-14b363bcfcfdd03f2232c2d0779ee90fc5cc27e6.zip
improve asset issuance to internal user
-rw-r--r--docker-compose.yml5
-rw-r--r--lib/sockets.js1
-rw-r--r--routes/assets.js116
-rw-r--r--routes/lightning/receive.js1
-rw-r--r--routes/liquid/receive.js19
-rw-r--r--routes/tickets.js7
6 files changed, 92 insertions, 57 deletions
diff --git a/docker-compose.yml b/docker-compose.yml
index 8261d746..4d1a89d9 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -179,6 +179,11 @@ services:
networks:
default:
ipv4_address: 172.18.0.32
+ redis:
+ container_name: redis
+ image: redis
+ volumes:
+ - ./config/redis:/data
networks:
default:
name: net
diff --git a/lib/sockets.js b/lib/sockets.js
index a8428d2f..22072dd0 100644
--- a/lib/sockets.js
+++ b/lib/sockets.js
@@ -115,6 +115,7 @@ app.register(async function(app) {
names[id] = username;
let user = await getUser(username);
+
if (user.locked) throw new Error("Account is locked");
let payments = await user.getPayments({
where: {
diff --git a/routes/assets.js b/routes/assets.js
index ecc44d84..de539946 100644
--- a/routes/assets.js
+++ b/routes/assets.js
@@ -120,7 +120,7 @@ app.post("/assets", auth, async (req, res) => {
l(
"attempting issuance",
req.user.username,
- contract,
+ JSON.stringify(contract),
"address",
address,
"pubkey",
@@ -177,6 +177,7 @@ app.post("/assets", auth, async (req, res) => {
as: "user"
},
order: [["balance", "DESC"]],
+ lock: transaction.LOCK.UPDATE,
transaction
});
@@ -199,23 +200,51 @@ app.post("/assets", auth, async (req, res) => {
await account.reload({ transaction });
emit(user.username, "account", account);
+ let invoice;
+ let recipient = user;
+ if (address) {
+ invoice = await db.Invoice.findOne({
+ where: { address },
+ include: {
+ model: db.User,
+ as: "user"
+ }
+ });
+ recipient = invoice.user;
+ }
+
+ if (!invoice) {
+ invoice = await db.Invoice.create(
+ {
+ account_id: account.id,
+ user_id,
+ amount: Math.round(params.asset_amount * SATS),
+ address: asset_address,
+ currency: user.currency,
+ rate: store.rates[user.currency],
+ network: "liquid"
+ },
+ { transaction }
+ );
+ }
+
account = await db.Account.create(
{
asset,
contract,
domain,
- user_id,
+ user_id: invoice.user_id,
ticker,
precision,
name,
balance: 0,
network: "liquid",
- pending: address ? 0 : Math.round(params.asset_amount * SATS)
+ pending: Math.round(params.asset_amount * SATS)
},
{ transaction }
);
- emit(user.username, "account", account);
+ emit(recipient.username, "account", account);
l(
"issued asset",
user.username,
@@ -225,24 +254,11 @@ app.post("/assets", auth, async (req, res) => {
account.id
);
- const { id: invoice_id } = await db.Invoice.create(
- {
- account_id: account.id,
- user_id,
- amount: Math.round(params.asset_amount * SATS),
- address: asset_address,
- currency: user.currency,
- rate: store.rates[user.currency],
- network: "liquid"
- },
- { transaction }
- );
-
const asset_payment = await db.Payment.create(
{
account_id: account.id,
- user_id,
- invoice_id,
+ user_id: invoice.user_id,
+ invoice_id: invoice.id,
hash: txid,
amount: Math.round(params.asset_amount * SATS),
received: true,
@@ -252,14 +268,14 @@ app.post("/assets", auth, async (req, res) => {
},
{ transaction }
);
- emit(user.username, "payment", asset_payment);
+ emit(recipient.username, "payment", asset_payment);
- if (address) {
+ if (invoice.user_id !== user.id) {
const asset_send = await db.Payment.create(
{
account_id: account.id,
user_id,
- invoice_id,
+ invoice_id: invoice.id,
hash: txid,
amount: -Math.round(params.asset_amount * SATS),
received: false,
@@ -269,7 +285,7 @@ app.post("/assets", auth, async (req, res) => {
},
{ transaction }
);
- emit(user.username, "payment", asset_send);
+ emit(recipient.username, "payment", asset_send);
}
store.issuances[txid] = {
@@ -280,10 +296,39 @@ app.post("/assets", auth, async (req, res) => {
};
if (token_amount) {
+ invoice = null;
+ recipient = user;
+
+ if (address) {
+ invoice = await db.Invoice.findOne({
+ where: { address },
+ include: {
+ model: db.User,
+ as: "user"
+ }
+ });
+ recipient = invoice.user;
+ }
+
+ if (!invoice) {
+ invoice = await db.Invoice.create(
+ {
+ account_id: account.id,
+ user_id,
+ amount: Math.round(params.token_amount * SATS),
+ address: token_address,
+ currency: user.currency,
+ rate: store.rates[user.currency],
+ network: "liquid"
+ },
+ { transaction }
+ );
+ }
+
account = await db.Account.create(
{
asset: token,
- user_id,
+ user_id: invoice.user_id,
domain,
ticker: `${ticker}REISSUANCETOKEN`,
precision: 8,
@@ -293,24 +338,12 @@ app.post("/assets", auth, async (req, res) => {
},
{ transaction }
);
- emit(user.username, "account", account);
-
- const { id: invoice_id } = await db.Invoice.create(
- {
- account_id: account.id,
- user_id,
- amount: Math.round(params.asset_amount * SATS),
- address: asset_address,
- currency: user.currency,
- rate: store.rates[user.currency],
- network: "liquid"
- },
- { transaction }
- );
+ emit(recipient.username, "account", account);
const token_payment = await db.Payment.create(
{
account_id: account.id,
+ invoice_id: invoice.id,
user_id,
hash: txid,
amount: Math.round(token_amount * SATS),
@@ -321,12 +354,13 @@ app.post("/assets", auth, async (req, res) => {
},
{ transaction }
);
- emit(user.username, "payment", token_payment);
+ emit(recipient.username, "payment", token_payment);
- if (address) {
+ if (invoice.user_id !== user.id) {
const token_send = await db.Payment.create(
{
account_id: account.id,
+ invoice_id: invoice.id,
user_id,
hash: txid,
amount: -Math.round(token_amount * SATS),
@@ -337,7 +371,7 @@ app.post("/assets", auth, async (req, res) => {
},
{ transaction }
);
- emit(user.username, "payment", token_send);
+ emit(recipient.username, "payment", token_send);
}
store.issuances[txid].token = token;
diff --git a/routes/lightning/receive.js b/routes/lightning/receive.js
index 97c4b47f..f4a20a59 100644
--- a/routes/lightning/receive.js
+++ b/routes/lightning/receive.js
@@ -143,7 +143,6 @@ if (config.lna.clightning) {
msatoshi_received,
payment_preimage: secret
} = await ln.waitanyinvoice(i);
- console.log("um");
let settled = status === "paid";
let received = parseInt(msatoshi_received / 1000);
diff --git a/routes/liquid/receive.js b/routes/liquid/receive.js
index 2f90af5e..718feba5 100644
--- a/routes/liquid/receive.js
+++ b/routes/liquid/receive.js
@@ -58,10 +58,8 @@ const getAccount = async (params, transaction) => {
params.seed = nc.seed;
}
- const assets = assets;
-
- if (assets[asset]) {
- ({ domain, ticker, precision, name } = assets[asset]);
+ if (store.assets[asset]) {
+ ({ domain, ticker, precision, name } = store.assets[asset]);
} else {
const existing = await db.Account.findOne({
where: {
@@ -149,13 +147,14 @@ zmqRawTx.on("message", async (topic, message, sequence) => {
let account = await db.Account.findOne({
where: {
- id: invoice.account_id
+ id: invoice.account_id,
+ asset
},
lock: transaction.LOCK.UPDATE,
transaction
});
- if (!(account && account.asset === asset)) {
+ if (!account) {
let pubkey = account ? account.pubkey : null;
account = await getAccount(
@@ -371,13 +370,7 @@ setInterval(async () => {
emit(user.username, "account", account);
emit(user.username, "payment", p);
- l(
- "liquid confirmed",
- user.username,
- account.asset,
- p.amount,
- p.tip
- );
+ l("liquid confirmed", user.username, account.asset, p.amount, p.tip);
notify(user, `${total} SAT payment confirmed`);
callWebhook(p.invoice, p);
diff --git a/routes/tickets.js b/routes/tickets.js
index 0334c2c6..7ca56740 100644
--- a/routes/tickets.js
+++ b/routes/tickets.js
@@ -6,13 +6,16 @@ app.get("/ticket", async (req, res) => {
res.send({ ticket: await redis.lLen("tickets") });
});
+app.get("/tickets", async (req, res) => {
+ res.send(await redis.lRange("tickets", 0, -1));
+});
+
app.post("/ticket", auth, async (req, res) => {
try {
let { asset } = req.body;
if (!asset || asset.length !== 64) throw new Error("invalid asset");
- await redis.lPush("tickets", req.body.asset);
- res.send(await redis.lPush("tickets", req.body.asset));
+ res.send(await redis.rPush("tickets", req.body.asset));
} catch (e) {
res.code(500).send(e.message);
}