From c6dc858ee5491e6c584d94d702438e9b24adbfc7 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Mon, 30 Aug 2021 14:48:26 -0700 Subject: handle conversions for internal payments --- db/index.js | 44 ++++++++++++++++++++++---------------------- docker-compose.dev.yml | 4 ++-- docker-compose.yml | 4 ++++ routes/assets.js | 5 +++-- routes/liquid/send.js | 1 - routes/send.js | 23 ++++++++++++++++++++++- 6 files changed, 53 insertions(+), 28 deletions(-) diff --git a/db/index.js b/db/index.js index a3d29743..532b5e77 100644 --- a/db/index.js +++ b/db/index.js @@ -13,11 +13,11 @@ db = new Sequelize( host: dbOptions.connection.host, dialect: "mariadb", logging: false, - dialectOptions: { multipleStatements: true, timezone: "Etc/GMT+7" } + dialectOptions: { multipleStatements: true, timezone: "Etc/GMT+7" }, } ); -db.authenticate().catch(err => { +db.authenticate().catch((err) => { console.debug("Error connecting to database: " + err.message); console.log(dbOptions.connection.database + "." + dbOptions.connection.user); }); @@ -69,100 +69,100 @@ const { User, Account, Payment, Invoice, Key, Order, Referral } = db; // move relationships to specfic models User.hasMany(Account, { as: "accounts", - foreignKey: "user_id" + foreignKey: "user_id", }); User.hasMany(Invoice, { as: "invoices", - foreignKey: "user_id" + foreignKey: "user_id", }); User.hasMany(Key, { as: "keys", - foreignKey: "user_id" + foreignKey: "user_id", }); User.hasMany(Order, { as: "orders", - foreignKey: "user_id" + foreignKey: "user_id", }); User.hasMany(Payment, { as: "payments", - foreignKey: "user_id" + foreignKey: "user_id", }); User.belongsTo(Account, { as: "account", - foreignKey: "account_id" + foreignKey: "account_id", }); Invoice.belongsTo(Account, { as: "account", - foreignKey: "account_id" + foreignKey: "account_id", }); Account.belongsTo(User, { as: "user", - foreignKey: "user_id" + foreignKey: "user_id", }); Invoice.belongsTo(User, { as: "user", - foreignKey: "user_id" + foreignKey: "user_id", }); Key.belongsTo(User, { as: "user", - foreignKey: "user_id" + foreignKey: "user_id", }); Payment.belongsTo(Account, { as: "account", - foreignKey: "account_id" + foreignKey: "account_id", }); Payment.belongsTo(User, { as: "user", - foreignKey: "user_id" + foreignKey: "user_id", }); Payment.belongsTo(Invoice, { as: "invoice", - foreignKey: "invoice_id" + foreignKey: "invoice_id", }); Order.belongsTo(User, { as: "user", - foreignKey: "user_id" + foreignKey: "user_id", }); Order.belongsTo(Account, { as: "acc1", - foreignKey: "a1_id" + foreignKey: "a1_id", }); Order.belongsTo(Account, { as: "acc2", - foreignKey: "a2_id" + foreignKey: "a2_id", }); User.hasMany(Referral, { as: "referral_codes", - foreignKey: "sponsor_id" + foreignKey: "sponsor_id", }); User.hasMany(Referral, { as: "sponsors", - foreignKey: "user_id" + foreignKey: "user_id", }); Referral.belongsTo(User, { as: "sponsor", - foreignKey: "sponsor_id" + foreignKey: "sponsor_id", }); Referral.belongsTo(User, { as: "user", - foreignKey: "user_id" + foreignKey: "user_id", }); debug("added model relationships"); diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index 11a41e64..9a8f79a3 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -12,7 +12,7 @@ services: volumes: - ../coinos-ui:/coinos-ui electrs-liquid: - image: vulpemventures/electrs-liquid:latest + image: asoltys/electrs-liquid:latest container_name: electrs depends_on: - liquid @@ -55,7 +55,7 @@ services: - ${LIQUID_ESPLORA_PORT}:5000 restart: unless-stopped clightning: - image: elementsproject/lightningd:latest + image: asoltys/lightning container_name: cl environment: LIGHTNINGD_NETWORK: regtest diff --git a/docker-compose.yml b/docker-compose.yml index 00ddb05a..1e4402da 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -52,3 +52,7 @@ services: - ./mysql:/var/lib/mysql ports: - 3307:3306 +networks: + default: + external: + name: net diff --git a/routes/assets.js b/routes/assets.js index d163e8e6..72d4c329 100644 --- a/routes/assets.js +++ b/routes/assets.js @@ -333,10 +333,12 @@ app.post( return res.status(500).send("Faucet feature temporarily disabled"); const { user } = req; - const { asset, amount } = req.body; + let { asset, amount } = req.body; amount = parseInt(amount); try { + if (amount <= 0) throw new Error("Amount to load cannot be negative"); + await db.transaction(async transaction => { let account = await getAccount( config.liquid.btcasset, @@ -344,7 +346,6 @@ app.post( transaction ); - if (amount < 0) throw new Error("Amount to load cannot be negative"); if (account.asset !== config.liquid.btcasset) throw new Error( "Faucet has to be funded with bitcoin. Try sending from another wallet." diff --git a/routes/liquid/send.js b/routes/liquid/send.js index 5abc692f..0c0b2012 100644 --- a/routes/liquid/send.js +++ b/routes/liquid/send.js @@ -3,7 +3,6 @@ const lcad = config.liquid.cadasset; const { Transaction } = require("@asoltys/liquidjs-lib"); sendLiquid = async ({ asset, amount, user, address, memo, tx, limit }) => { - if (amount > 10000000) throw new Error("Amount too large"); l.info("sending liquid", amount, address); if (!tx) { ({ tx } = await liquidTx({ diff --git a/routes/send.js b/routes/send.js index e8fdb79c..cdaa8a9b 100644 --- a/routes/send.js +++ b/routes/send.js @@ -158,7 +158,28 @@ module.exports = ah(async (req, res, next) => { received: true, }; - if (invoice) params.invoice_id = invoice.id; + if (invoice) { + params.invoice_id = invoice.id; + + let c = convert[invoice.text]; + if (c) { + l.info( + "internal payment detected for conversion", + invoice.text, + c.address, + recipient.username + ); + + recipient.account = a2; + + sendLiquid({ + address: c.address, + amount: amount - 100, + user: recipient, + limit: amount, + }).catch(console.log); + } + } let p2 = await db.Payment.create(params, { transaction }); -- cgit v1.2.3