diff options
| author | Gyulai Zoltan <[email protected]> | 2021-10-12 13:53:33 +0300 |
|---|---|---|
| committer | Gyulai Zoltan <[email protected]> | 2021-10-12 13:53:33 +0300 |
| commit | 92ffc6e2e597867a55b592a20b16c233efad9cef (patch) | |
| tree | 9bfad78872592637a241624de9edde36986b4b07 | |
| parent | 5f949a00ab98792db22bb5ab0104a06c714932f2 (diff) | |
| download | coinos-server-withdraw.tar.gz coinos-server-withdraw.zip | |
feat: added lightning withdrawal with hardcoded fee estimationwithdraw
| -rw-r--r-- | lib/send.js | 10 | ||||
| -rw-r--r-- | routes/lightning/withdraw.js | 11 | ||||
| -rw-r--r-- | routes/liquid/util/decrementacc.js | 3 | ||||
| -rw-r--r-- | routes/payments.js | 1 |
4 files changed, 21 insertions, 4 deletions
diff --git a/lib/send.js b/lib/send.js index 293473d9..8edd1e58 100644 --- a/lib/send.js +++ b/lib/send.js @@ -1,7 +1,9 @@ const bolt11 = require("bolt11"); const { Op } = require("sequelize"); -send = async (amount, memo, hash, user) => { +const ESTIMATED_FEE = 1000; + +send = async (amount, memo, hash, user, subtractFee = false) => { let payreq = bolt11.decode(hash); let routingInfo = payreq.tags.find(t => t.tagName === "routing_info"); if ( @@ -12,6 +14,12 @@ send = async (amount, memo, hash, user) => { if (!amount || payreq.satoshis > amount) amount = payreq.satoshis; + if(subtractFee) { + amount -= ESTIMATED_FEE; + if(amount < 0) + throw new Error('Subtracted fee is greater than amount to send') + } + if (seen.includes(hash)) { l.warn("attempted to pay a paid invoice", user.username); throw new Error("Invoice has been paid, can't pay again"); diff --git a/routes/lightning/withdraw.js b/routes/lightning/withdraw.js new file mode 100644 index 00000000..d1712d39 --- /dev/null +++ b/routes/lightning/withdraw.js @@ -0,0 +1,11 @@ +module.exports = ah(async (req, res) => { + let { amount, memo, payreq } = req.body; + let { user } = req; + + try { + res.send(await send(amount, memo, payreq, user, true)); + } catch (e) { + l.error("problem sending lightning withdrawal", user.username, e.message, e.stack); + res.status(500).send(e.message); + } +}); diff --git a/routes/liquid/util/decrementacc.js b/routes/liquid/util/decrementacc.js index 5be30e13..eca785e0 100644 --- a/routes/liquid/util/decrementacc.js +++ b/routes/liquid/util/decrementacc.js @@ -17,11 +17,8 @@ module.exports = async (user, transaction, limit, fee, assets, amount, address, }); if (faucet) { - // TODO: is faucet ok with 0 fee in withdraw? covered = faucet.balance; if (covered > fee) covered = fee; - // covered = fee; - // if (covered > faucet.balance) covered = faucet.balance; await faucet.decrement({balance: covered}, {transaction}); await faucet.reload({transaction}); await faucet.save({transaction}); diff --git a/routes/payments.js b/routes/payments.js index cb09de63..6f4601db 100644 --- a/routes/payments.js +++ b/routes/payments.js @@ -128,6 +128,7 @@ ah(async () => { app.post("/lightning/invoice", require("./lightning/invoice")); app.post("/lightning/query", auth, require("./lightning/query")); app.post("/lightning/send", auth, require("./lightning/send")); + app.post("/lightning/withdraw", auth, require("./lightning/withdraw")); require("./lightning/receive"); } |
