Transaction is able to estimate fees
This commit is contained in:
parent
e574693594
commit
b7d65fb757
3 changed files with 31 additions and 1 deletions
|
@ -377,6 +377,15 @@ Transaction.prototype.validateSig = function(index, script, sig, pub) {
|
|||
convert.coerceToBytes(pub));
|
||||
}
|
||||
|
||||
Transaction.feePerKb = 20000
|
||||
Transaction.prototype.estimateFee = function(feePerKb){
|
||||
var feePerKb = feePerKb || Transaction.feePerKb
|
||||
var size = this.ins.length * 180 + this.outs.length * 34 + 10
|
||||
var sizeInKb = BigInteger.valueOf(Math.ceil(size / 1000))
|
||||
|
||||
return BigInteger.valueOf(feePerKb).multiply(sizeInKb).intValue()
|
||||
}
|
||||
|
||||
var TransactionIn = function (data) {
|
||||
if (typeof data == "string")
|
||||
this.outpoint = { hash: data.split(':')[0], index: data.split(':')[1] }
|
||||
|
|
3
test/fixtures/mainnet_tx.json
vendored
3
test/fixtures/mainnet_tx.json
vendored
File diff suppressed because one or more lines are too long
|
@ -2,6 +2,10 @@ var Transaction = require('../src/transaction').Transaction
|
|||
var convert = require('../src/convert')
|
||||
var ECKey = require('../src/eckey').ECKey
|
||||
var assert = require('assert')
|
||||
var fixtureTxes = require('./fixtures/mainnet_tx')
|
||||
var fixtureTx1Hex = fixtureTxes.prevTx
|
||||
var fixtureTx2Hex = fixtureTxes.tx
|
||||
var fixtureTxBigHex = fixtureTxes.bigTx
|
||||
|
||||
describe('Transaction', function() {
|
||||
describe('deserialize', function() {
|
||||
|
@ -170,6 +174,22 @@ describe('Transaction', function() {
|
|||
})
|
||||
})
|
||||
|
||||
describe('estimateFee', function(){
|
||||
it('works for fixture tx 1', function(){
|
||||
var tx = Transaction.deserialize(fixtureTx1Hex)
|
||||
assert.equal(tx.estimateFee(), 20000)
|
||||
})
|
||||
|
||||
it('works for fixture big tx', function(){
|
||||
var tx = Transaction.deserialize(fixtureTxBigHex)
|
||||
assert.equal(tx.estimateFee(), 60000)
|
||||
})
|
||||
|
||||
it('allow feePerKb to be passed in as an argument', function(){
|
||||
var tx = Transaction.deserialize(fixtureTx2Hex)
|
||||
assert.equal(tx.estimateFee(10000), 10000)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
|
|
Loading…
Add table
Reference in a new issue