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 /routes/payments.js | |
| parent | 753147ba339bc335443f9ed3a9f03eb492df1f7b (diff) | |
| download | coinos-server-39b669f53574258a016d338ce6fc6d0eeeae38b4.tar.gz coinos-server-39b669f53574258a016d338ce6fc6d0eeeae38b4.zip | |
return min max with fee estimation
Diffstat (limited to 'routes/payments.js')
| -rw-r--r-- | routes/payments.js | 82 |
1 files changed, 49 insertions, 33 deletions
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) { |
