aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2020-05-18 17:46:20 -0700
committerAdam Soltys <[email protected]>2020-05-18 17:46:20 -0700
commitaf2d18b6508a3b50ee8272850707b5bf8c0e50a2 (patch)
treea75fb42f37f12c8557534e29b48cfac57ad30274
parente8c461d90eb0f9618129a23b771851fdfec6edb1 (diff)
downloadcoinos-server-af2d18b6508a3b50ee8272850707b5bf8c0e50a2.tar.gz
coinos-server-af2d18b6508a3b50ee8272850707b5bf8c0e50a2.zip
handle reissuance tokens
-rw-r--r--routes/assets.js69
-rw-r--r--routes/liquid/receive.js49
-rw-r--r--routes/send.js31
3 files changed, 122 insertions, 27 deletions
diff --git a/routes/assets.js b/routes/assets.js
index f2221e36..4a963b48 100644
--- a/routes/assets.js
+++ b/routes/assets.js
@@ -55,7 +55,7 @@ app.post("/assets", auth, async (req, res) => {
contract_hash
}
]);
- const { asset, hex } = ria[0];
+ const { asset, hex, token } = ria[0];
const brt = await lq.blindRawTransaction(hex, true, [], false);
const srt = await lq.signRawTransactionWithWallet(brt);
const allowed = (await lq.testMempoolAccept([srt.hex]))[0].allowed;
@@ -73,7 +73,12 @@ app.post("/assets", auth, async (req, res) => {
});
if (Math.round(funded.fee * SATS) > account.balance) {
- l.error("amount exceeds balance", asset_amount, funded.fee, account.balance);
+ l.error(
+ "amount exceeds balance",
+ asset_amount,
+ funded.fee,
+ account.balance
+ );
throw new Error(`Insufficient funds to pay fee of ${funded.fee} BTC`);
}
@@ -93,18 +98,56 @@ app.post("/assets", auth, async (req, res) => {
{ transaction }
);
- const payment = await db.Payment.create({
- account_id: account.id,
- user_id,
- hash: txid,
- amount: asset_amount * SATS,
- received: true,
- confirmed: false,
- address: asset_address,
- network: "LBTC"
- });
+ const asset_payment = await db.Payment.create(
+ {
+ account_id: account.id,
+ user_id,
+ hash: txid,
+ amount: asset_amount * SATS,
+ received: true,
+ confirmed: false,
+ address: asset_address,
+ network: "LBTC"
+ },
+ { transaction }
+ );
+
+ account = await db.Account.create(
+ {
+ asset: token,
+ user_id,
+ ticker: `${ticker}REISSUANCETOKEN`,
+ precision: 8,
+ name: `${name} Reissuance Token`,
+ balance: 0,
+ pending: token_amount * SATS
+ },
+ { transaction }
+ );
- issuances[txid] = { user_id, asset, asset_amount, payment_id: payment.id };
+ const token_payment = await db.Payment.create(
+ {
+ account_id: account.id,
+ user_id,
+ hash: txid,
+ amount: token_amount * SATS,
+ received: true,
+ confirmed: false,
+ address: token_address,
+ network: "LBTC"
+ },
+ { transaction }
+ );
+
+ issuances[txid] = {
+ user_id,
+ asset,
+ asset_amount,
+ token,
+ token_amount,
+ asset_payment_id: asset_payment.id,
+ token_payment_id: token_payment.id,
+ };
let user = await getUserById(user_id, transaction);
emit(user.username, "user", user);
diff --git a/routes/liquid/receive.js b/routes/liquid/receive.js
index f827f466..581d7ebc 100644
--- a/routes/liquid/receive.js
+++ b/routes/liquid/receive.js
@@ -58,6 +58,18 @@ zmqRawTx.on("message", async (topic, message, sequence) => {
if (assets[asset]) {
({ ticker, precision, name } = assets[asset]);
+ } else {
+ const existing = await db.Account.findOne({
+ where: {
+ asset
+ },
+ order: [["id", "ASC"]],
+ limit: 1
+ });
+
+ if (existing) {
+ ({ ticker, precision, name } = existing);
+ }
}
params = { ...params, ...{ ticker, precision, name } };
@@ -136,8 +148,16 @@ zmqRawBlock.on("message", async (topic, message, sequence) => {
json.tx.map(async tx => {
if (issuances[tx.txid]) {
await db.transaction(async transaction => {
- const { user_id, asset, asset_amount, payment_id } = issuances[tx.txid];
- const account = await db.Account.findOne({
+ const {
+ user_id,
+ asset,
+ asset_amount,
+ asset_payment_id,
+ token,
+ token_amount,
+ token_payment_id
+ } = issuances[tx.txid];
+ let account = await db.Account.findOne({
where: { user_id, asset },
lock: transaction.LOCK.UPDATE,
transaction
@@ -146,8 +166,26 @@ zmqRawBlock.on("message", async (topic, message, sequence) => {
account.pending = 0;
await account.save({ transaction });
- const payment = await db.Payment.findOne({
- where: { id: payment_id },
+ let payment = await db.Payment.findOne({
+ where: { id: asset_payment_id },
+ lock: transaction.LOCK.UPDATE,
+ transaction
+ });
+
+ payment.confirmed = true;
+ await payment.save({ transaction });
+
+ account = await db.Account.findOne({
+ where: { user_id, asset: token },
+ lock: transaction.LOCK.UPDATE,
+ transaction
+ });
+ account.balance = token_amount * SATS;
+ account.pending = 0;
+ await account.save({ transaction });
+
+ payment = await db.Payment.findOne({
+ where: { id: token_payment_id },
lock: transaction.LOCK.UPDATE,
transaction
});
@@ -158,8 +196,7 @@ zmqRawBlock.on("message", async (topic, message, sequence) => {
const user = await getUserById(user_id, transaction);
emit(user.username, "user", user);
});
- }
- else if (hashes.includes(tx.txid)) queue[tx.txid] = 1;
+ } else if (hashes.includes(tx.txid)) queue[tx.txid] = 1;
});
});
diff --git a/routes/send.js b/routes/send.js
index bca04e6b..7091c4bc 100644
--- a/routes/send.js
+++ b/routes/send.js
@@ -9,14 +9,14 @@ module.exports = async (req, res) => {
return res.status(500).send("Amount must be greater than zero");
try {
- await db.transaction(async (transaction) => {
+ await db.transaction(async transaction => {
let account = await db.Account.findOne({
where: {
user_id: user.id,
- asset,
+ asset
},
lock: transaction.LOCK.UPDATE,
- transaction,
+ transaction
});
if (account.balance < amount) {
@@ -37,7 +37,7 @@ module.exports = async (req, res) => {
currency: user.currency,
confirmed: true,
hash: "Internal Transfer",
- network: "COINOS",
+ network: "COINOS"
},
{ transaction }
);
@@ -52,18 +52,18 @@ module.exports = async (req, res) => {
res.send(payment);
user = await db.User.findOne({
- where: { username },
+ where: { username }
});
let params = {
user_id: user.id,
- asset,
+ asset
};
account = await db.Account.findOne({
where: params,
lock: transaction.LOCK.UPDATE,
- transaction,
+ transaction
});
if (account) {
@@ -79,6 +79,21 @@ module.exports = async (req, res) => {
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 } };
@@ -97,7 +112,7 @@ module.exports = async (req, res) => {
confirmed: true,
hash: "Internal Transfer",
network: "COINOS",
- received: true,
+ received: true
},
{ transaction }
);