diff options
| author | Adam Soltys <[email protected]> | 2023-02-23 01:44:12 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2023-02-23 01:44:12 -0800 |
| commit | 39b669f53574258a016d338ce6fc6d0eeeae38b4 (patch) | |
| tree | 7e7d753e29199b61aa132bf70bc697a8b64915a9 | |
| parent | 753147ba339bc335443f9ed3a9f03eb492df1f7b (diff) | |
| download | coinos-server-39b669f53574258a016d338ce6fc6d0eeeae38b4.tar.gz coinos-server-39b669f53574258a016d338ce6fc6d0eeeae38b4.zip | |
return min max with fee estimation
| -rw-r--r-- | lib/db.js | 2 | ||||
| -rw-r--r-- | routes/lnurl.js | 14 | ||||
| -rw-r--r-- | routes/payments.js | 82 |
3 files changed, 57 insertions, 41 deletions
@@ -17,7 +17,7 @@ export let g = async k => { }; export let s = (k, v) => { if (k === "user:null" || k === "user:undefined") { - warn("###### NULL USER #######") + warn("###### NULL USER #######"); console.trace(); } db.set(k, JSON.stringify(v)); diff --git a/routes/lnurl.js b/routes/lnurl.js index 381b3008..fc8107a3 100644 --- a/routes/lnurl.js +++ b/routes/lnurl.js @@ -45,7 +45,7 @@ export default { let uid = await g(`user:${username.toLowerCase()}`); if (!uid) { let u = await got(`${classic}/admin/migrate/${username}?zero=true`, { - headers: { authorization: `Bearer ${admin}` }, + headers: { authorization: `Bearer ${admin}` } }).json(); if (!u) fail("user not found"); @@ -66,7 +66,7 @@ export default { let metadata = JSON.stringify([ ["text/plain", `Paying ${username}@${host}`], - ["text/identifier", `${username}@${host}`], + ["text/identifier", `${username}@${host}`] ]); let id = v4(); @@ -77,7 +77,7 @@ export default { maxSendable: 100000000000, metadata, callback: `${URL}/api/lnurl/${id}`, - tag: "payRequest", + tag: "payRequest" }); }, @@ -91,21 +91,21 @@ export default { let metadata = JSON.stringify([ ["text/plain", `Paying ${username}@${host}`], - ["text/identifier", `${username}@${host}`], + ["text/identifier", `${username}@${host}`] ]); ({ text: pr } = await generate({ invoice: { amount: Math.round(amount / 1000), - type: types.lightning, + type: types.lightning }, memo: metadata, - user, + user })); await s(`lnurlp:${id}`, pr); } res.send({ pr, routes: [] }); - }, + } }; diff --git a/routes/payments.js b/routes/payments.js index d2e604a5..dad95399 100644 --- a/routes/payments.js +++ b/routes/payments.js @@ -178,50 +178,66 @@ export default { }, async bitcoin({ body: { txid, wallet } }, res) { - if (wallet === config.bitcoin.wallet) { - let { confirmations, details } = await bc.getTransaction(txid); - if (details.category === "receive") { - for (let { address, amount, vout } of details) { - let p = await g(`payment:${txid}:${vout}`); - if (!p || confirmations < 1) { - await credit( - address, - sats(amount), - "", - `${txid}:${vout}`, - types.bitcoin - ); - } else { - await confirm(address, txid, vout); + try { + if (wallet === config.bitcoin.wallet) { + let { confirmations, details } = await bc.getTransaction(txid); + if (details.category === "receive") { + for (let { address, amount, vout } of details) { + let p = await g(`payment:${txid}:${vout}`); + if (!p || confirmations < 1) { + await credit( + address, + sats(amount), + "", + `${txid}:${vout}`, + types.bitcoin + ); + } else { + await confirm(address, txid, vout); + } } } } + res.send({}); + } catch (e) { + warn(`problem processing ${txid}`); + bail(res, e.message); } - res.send({}); }, async fee(req, res) { - let { amount, address, feeRate, subtract } = req.body; - let subtractFeeFromOutputs = subtract ? [0] : []; - let replaceable = true; - let outs = [{ [address]: btc(amount) }]; - let count = await bc.getBlockCount(); + try { + let { amount, address, feeRate, subtract } = req.body; - let raw = await bc.createRawTransaction([], outs, 0, replaceable); - let tx = await bc.fundRawTransaction(raw, { - feeRate, - subtractFeeFromOutputs, - replaceable - }); + let subtractFeeFromOutputs = subtract ? [0] : []; + let replaceable = true; + let outs = [{ [address]: btc(amount) }]; + let count = await bc.getBlockCount(); + + let { feerate: min } = await bc.estimateSmartFee(40); + let { feerate: max } = await bc.estimateSmartFee(1); - if (config.bitcoin.walletpass) - await bc.walletPassphrase(config.bitcoin.walletpass, 300); + if (feeRate) feeRate = btc(feeRate); + else feeRate = max; - let { hex } = await bc.signRawTransactionWithWallet(tx.hex); - let { vsize } = await bc.decodeRawTransaction(hex); - feeRate = Math.round((sats(tx.fee) * 1000) / vsize); + let raw = await bc.createRawTransaction([], outs, 0, replaceable); - res.send({ feeRate, tx }); + let tx = await bc.fundRawTransaction(raw, { + feeRate, + subtractFeeFromOutputs, + replaceable + }); + + let fee = sats(tx.fee); + + min = sats(min); + max = Math.round(sats(max) * 2); + feeRate = sats(feeRate); + + res.send({ feeRate, min, max, fee, tx }); + } catch (e) { + bail(res, "problem estimating fee"); + } }, async send(req, res) { |
