feat: apply fee discount for outgoing tx

This commit is contained in:
José Oliveira 2021-05-19 13:31:19 +01:00
parent 8128f05ffb
commit eb33203d26
5 changed files with 136 additions and 3 deletions

View file

@ -51,11 +51,32 @@ function balance (account, cryptoCode, settings, operatorId) {
return accountBalance(cryptoCode)
}
function sendCoins (account, tx, settings, operatorId) {
function estimateFee () {
return fetch('estimatesmartfee', [6, 'unset'])
.then(result => BN(result.feerate))
.catch(() => {})
}
function calculateFeeDiscount (feeDiscount) {
// 0 makes bitcoind do automatic fee selection
const AUTOMATIC_FEE = 0
if (!feeDiscount) return AUTOMATIC_FEE
return estimateFee()
.then(estimatedFee => {
if (!estimatedFee) return AUTOMATIC_FEE
const newFee = estimatedFee.times(feeDiscount)
if (newFee.lt(0.00001) || newFee.gt(0.1)) return AUTOMATIC_FEE
return newFee
})
}
function sendCoins (account, tx, settings, operatorId, feeDiscount) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
const coins = cryptoAtoms.shiftedBy(-unitScale).toFixed(8)
return checkCryptoCode(cryptoCode)
.then(() => calculateFeeDiscount(feeDiscount))
.then(newFee => fetch('settxfee', [newFee]))
.then(() => fetch('sendtoaddress', [toAddress, coins]))
.then((txId) => fetch('gettransaction', [txId]))
.then((res) => _.pick(['fee', 'txid'], res))
@ -148,5 +169,6 @@ module.exports = {
getStatus,
newFunding,
cryptoNetwork,
fetchRBF
fetchRBF,
estimateFee
}