Skip to content

Commit

Permalink
Merge pull request #5 from paytaca/improvements/p2sh-outputs
Browse files Browse the repository at this point in the history
Included P2SH output count on BCH send fee calculation
  • Loading branch information
joemarct authored Apr 20, 2022
2 parents 259f636 + e390acd commit 1465098
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/bch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ class BCH {
const keyPairs = []

let transactionBuilder = new bchjs.TransactionBuilder()
let outputsCount = 0
let p2pkhOutputsCount = 0
let p2shOutputsCount = 0
let totalInput = new BigNumber(0)
let totalOutput = new BigNumber(0)

Expand Down Expand Up @@ -141,7 +142,7 @@ class BCH {
const dataOpRetGen = new OpReturnGenerator()
const dataOpReturn = dataOpRetGen.generateDataOpReturn(data)
transactionBuilder.addOutput(dataOpReturn, 0)
outputsCount += 1
p2pkhOutputsCount += 1
}

for (let i = 0; i < recipients.length; i++) {
Expand All @@ -151,20 +152,26 @@ class BCH {
bchjs.Address.toLegacyAddress(recipient.address),
parseInt(sendAmount)
)
outputsCount += 1

if (bchjs.Address.isP2SHAddress(recipient.address)) {
p2shOutputsCount += 1
} else {
p2pkhOutputsCount += 1
}
totalOutput = totalOutput.plus(sendAmount)
}

if (feeFunder !== undefined) {
inputsCount += 1 // Add extra for the fee funder input
}
outputsCount += 1 // Add extra for sending the BCH change,if any
p2pkhOutputsCount += 1 // Add extra for sending the BCH change,if any
let byteCount = bchjs.BitcoinCash.getByteCount(
{
P2PKH: inputsCount
},
{
P2PKH: outputsCount
P2PKH: p2pkhOutputsCount,
P2SH: p2shOutputsCount
}
)

Expand Down

0 comments on commit 1465098

Please sign in to comment.