diff options
| author | Adam Soltys <[email protected]> | 2020-09-18 22:54:12 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2020-09-18 22:54:12 -0700 |
| commit | c615a409acfdfb6bf1f4bc11651845979dea2e79 (patch) | |
| tree | 47f988d11cc418fa1a9e20f73b758c8d1efae11a /routes/liquid/send.js | |
| parent | ae48f3881cd6bf300c629d3332b2b72561e8e632 (diff) | |
| download | coinos-server-c615a409acfdfb6bf1f4bc11651845979dea2e79.tar.gz coinos-server-c615a409acfdfb6bf1f4bc11651845979dea2e79.zip | |
fee faucets
Diffstat (limited to 'routes/liquid/send.js')
| -rw-r--r-- | routes/liquid/send.js | 118 |
1 files changed, 74 insertions, 44 deletions
diff --git a/routes/liquid/send.js b/routes/liquid/send.js index a38f84e2..0562c750 100644 --- a/routes/liquid/send.js +++ b/routes/liquid/send.js @@ -1,8 +1,10 @@ +const btc = config.liquid.btcasset; + module.exports = ah(async (req, res) => { let { user } = req; let { address, memo, tx } = req.body; - const isChange = async (address) => + const isChange = async address => (await lq.getAddressInfo(address)).ismine && !Object.keys(addresses).includes(address); @@ -16,7 +18,7 @@ module.exports = ah(async (req, res) => { let { asset, value, - scriptPubKey: { type, addresses }, + scriptPubKey: { type, addresses } } = vout[i]; if (type === "fee") fee = toSats(value); @@ -36,53 +38,79 @@ module.exports = ah(async (req, res) => { const payments = []; try { - await db.transaction(async (transaction) => { + await db.transaction(async transaction => { for (let i = 0; i < assets.length; i++) { let asset = assets[i]; let amount = totals[asset]; if (change[asset]) amount -= change[asset]; let total = amount; - if (asset === config.liquid.btcasset) total += fee; - l.info("creating liquid payment", user.username, asset, total, fee); + if (asset === btc) { + let covered = 0; + let nonbtc = assets.filter(a => a !== btc); + if (nonbtc.length === 1) { + let faucet = await db.Account.findOne({ + where: { + asset: nonbtc[0], + user_id: null + } + }); - let account = await db.Account.findOne({ - where: { - user_id: user.id, - asset, - }, - lock: transaction.LOCK.UPDATE, - transaction, - }); + if (faucet) { + covered = faucet.balance; + if (covered > fee) covered = fee; + faucet.balance -= covered; + await faucet.save({ transaction }); + } - if (total > account.balance) { - l.warn("amount exceeds balance", { - total, - fee, - balance: account.balance, - }); - throw new Error("Insufficient funds"); + total += fee - covered; + } } - account.balance -= total; - await account.save({ transaction }); + if (asset !== btc || total) { + l.info("creating liquid payment", user.username, asset, total, fee); + + let account = await db.Account.findOne({ + where: { + user_id: user.id, + asset, + pubkey: null + }, + lock: transaction.LOCK.UPDATE, + transaction + }); - let payment = { - amount: -amount, - account_id: account.id, - fee, - memo, - user_id: user.id, - rate: app.get("rates")[user.currency], - currency: user.currency, - address, - confirmed: true, - received: false, - network: "liquid", - }; + if (total > account.balance) { + l.warn("amount exceeds balance", { + total, + fee, + balance: account.balance + }); + throw new Error( + `Insufficient funds, need ${total} ${account.ticker}, have ${account.balance}` + ); + } - payment.account = account; - payments.push(payment); + account.balance -= total; + await account.save({ transaction }); + + let payment = { + amount: -amount, + account_id: account.id, + fee, + memo, + user_id: user.id, + rate: app.get("rates")[user.currency], + currency: user.currency, + address, + confirmed: true, + received: false, + network: "liquid" + }; + + payment.account = account; + payments.push(payment); + } } }); } catch (e) { @@ -91,7 +119,7 @@ module.exports = ah(async (req, res) => { } try { - await db.transaction(async (transaction) => { + await db.transaction(async transaction => { if (config.liquid.walletpass) await lq.walletPassphrase(config.liquid.walletpass, 300); @@ -102,12 +130,14 @@ module.exports = ah(async (req, res) => { let main; for (let i = 0; i < assets.length; i++) { p = payments[i]; - let { account } = p; - p.hash = txid; - p = await db.Payment.create(p, { transaction }); - if (account.ticker !== "BTC" || !main) { - main = p.get({ plain: true }); - main.account = account.get({ plain: true }); + if (p) { + let { account } = p; + p.hash = txid; + p = await db.Payment.create(p, { transaction }); + if (account.ticker !== "BTC" || !main) { + main = p.get({ plain: true }); + main.account = account.get({ plain: true }); + } } } |
