aboutsummaryrefslogtreecommitdiffstats
path: root/sendLiquid.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2020-03-09 10:59:04 -0700
committerAdam Soltys <[email protected]>2020-03-09 10:59:10 -0700
commit1ae4c03116ce3a650af88f78a4b5d19581238c71 (patch)
tree10fce06803f0a13c5ba990f08d687c4deced6186 /sendLiquid.js
parentddb61fe99e4103c1ba8a5d29e3aa66a580c63bf6 (diff)
downloadcoinos-server-1ae4c03116ce3a650af88f78a4b5d19581238c71.tar.gz
coinos-server-1ae4c03116ce3a650af88f78a4b5d19581238c71.zip
fee estimation
Diffstat (limited to 'sendLiquid.js')
-rw-r--r--sendLiquid.js52
1 files changed, 31 insertions, 21 deletions
diff --git a/sendLiquid.js b/sendLiquid.js
index 855a2617..a86395a2 100644
--- a/sendLiquid.js
+++ b/sendLiquid.js
@@ -4,35 +4,43 @@ const reverse = require("buffer-reverse");
const BitcoinCore = require("bitcoin-core");
const l = require("pino")();
-const SATS = 100000000;
+const toSats = n => parseInt((n * 100000000).toFixed())
const bc = new BitcoinCore(config.liquid);
-module.exports = (app, db, emit) => async (req, res) => {
- let { address, amount } = req.body;
- let rawtx, hex, fee, total;
+module.exports = (addresses, app, db, emit) => async (req, res) => {
let { user } = req;
+ let {
+ address,
+ tx: { hex }
+ } = req.body;
- try {
- amount = parseInt(amount);
-
- rawtx = await bc.createRawTransaction([], {
- [address]: (amount / SATS).toFixed(8)
- });
+ const isChange = async address =>
+ !((await bc.getAddressInfo(address)).ismine) ||
+ !Object.keys(addresses).includes(address) ||
+ address === user.liquid;
- rawtx = await bc.fundRawTransaction(rawtx, {
- subtractFeeFromOutputs: amount === user.balance ? [0] : []
- });
+ const unblinded = await bc.unblindRawTransaction(hex);
+ tx = await bc.decodeRawTransaction(unblinded.hex);
+
+ let total = 0;
+ let change = 0;
+ let fee = 0;
- hex = await bc.blindRawTransaction(rawtx.hex);
+ for (let i = 0; i < tx.vout.length; i++) {
+ let o = tx.vout[i];
+ total += toSats(o.value);
+ if (o.scriptPubKey.type === 'fee') fee = toSats(o.value);
+
+ if (o.scriptPubKey.addresses) {
+ if ((await isChange(o.scriptPubKey.addresses[0]))) {
+ change += toSats(o.value);
+ }
+ }
+ }
- ({ fee } = rawtx);
- fee = parseInt(fee * SATS);
- total = amount + fee;
- } catch (e) {
- l.error("funding failed", e);
- return res.status(500).send(e.message);
- }
+ total = total - change;
+ let amount = total - fee;
try {
await db.transaction(async transaction => {
@@ -61,12 +69,14 @@ module.exports = (app, db, emit) => async (req, res) => {
if (config.liquid.walletpass)
await bc.walletPassphrase(config.liquid.walletpass, 300);
+ hex = await bc.blindRawTransaction(hex);
rawtx = (await bc.signRawTransactionWithWallet(hex)).hex;
let txid = await bc.sendRawTransaction(rawtx);
await db.transaction(async transaction => {
await user.save({ transaction });
emit(user.username, "user", user);
+ l.info("total", total);
const payment = await db.Payment.create(
{