Browse Source

Transaction is able to estimate fees

hk-custom-address
Wei Lu 11 years ago
parent
commit
b7d65fb757
  1. 9
      src/transaction.js
  2. 3
      test/fixtures/mainnet_tx.json
  3. 20
      test/transaction.js

9
src/transaction.js

@ -377,6 +377,15 @@ Transaction.prototype.validateSig = function(index, script, sig, pub) {
convert.coerceToBytes(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) { var TransactionIn = function (data) {
if (typeof data == "string") if (typeof data == "string")
this.outpoint = { hash: data.split(':')[0], index: data.split(':')[1] } this.outpoint = { hash: data.split(':')[0], index: data.split(':')[1] }

3
test/fixtures/mainnet_tx.json

File diff suppressed because one or more lines are too long

20
test/transaction.js

@ -2,6 +2,10 @@ var Transaction = require('../src/transaction').Transaction
var convert = require('../src/convert') var convert = require('../src/convert')
var ECKey = require('../src/eckey').ECKey var ECKey = require('../src/eckey').ECKey
var assert = require('assert') 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('Transaction', function() {
describe('deserialize', 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…
Cancel
Save