Move fee estimation into networks.js

This commit is contained in:
Wei Lu 2014-06-17 14:48:06 +08:00
parent 79ec61d085
commit 122b613eaf
3 changed files with 36 additions and 5 deletions

View file

@ -1,6 +1,11 @@
// https://en.bitcoin.it/wiki/List_of_address_prefixes
// Dogecoin BIP32 is a proposed standard: https://bitcointalk.org/index.php?topic=409731
module.exports = {
function bitcoinEstimateFee(txByteSize) {
return networks.bitcoin.feePerKb * Math.ceil(txByteSize / 1000)
}
var networks = {
bitcoin: {
magicPrefix: '\x18Bitcoin Signed Message:\n',
bip32: {
@ -10,8 +15,9 @@ module.exports = {
pubKeyHash: 0x00,
scriptHash: 0x05,
wif: 0x80,
dustThreshold: 546,
feePerKb: 10000
dustThreshold: 546, // https://github.com/bitcoin/bitcoin/blob/529047fcd18acd1b64dc95d6eb69edeaad75d405/src/core.h#L176-L188
feePerKb: 10000, // https://github.com/bitcoin/bitcoin/blob/3f39b9d4551d729c3a2e4decd810ac6887cfaeb3/src/main.cpp#L52
estimateFee: bitcoinEstimateFee
},
dogecoin: {
magicPrefix: '\x19Dogecoin Signed Message:\n',
@ -47,6 +53,8 @@ module.exports = {
scriptHash: 0xc4,
wif: 0xef,
dustThreshold: 546,
feePerKb: 10000
feePerKb: 10000,
estimateFee: bitcoinEstimateFee
}
}
module.exports = networks

View file

@ -237,7 +237,7 @@ function Wallet(seed, network) {
tmpTx.addOutput(getChangeAddress(), 0)
var byteSize = tmpTx.toBuffer().length
return network.feePerKb * Math.ceil(byteSize / 1000)
return network.estimateFee(byteSize)
}
function getChangeAddress() {

23
test/network.js Normal file
View file

@ -0,0 +1,23 @@
var assert = require('assert')
var networks = require('../src/networks')
var Transaction = require('../src/transaction')
var fixtureTxes = require('./fixtures/mainnet_tx')
var fixtureTx1Hex = fixtureTxes.prevTx
var fixtureTxBigHex = fixtureTxes.bigTx
describe('bitcoin', function() {
describe('estimateFee', function() {
var estimateFee = networks.bitcoin.estimateFee
it('works for fixture tx 1', function() {
var tx = Transaction.fromHex(fixtureTx1Hex)
assert.equal(estimateFee(tx.toBuffer().length), 10000)
})
it('works for fixture big tx', function() {
var tx = Transaction.fromHex(fixtureTxBigHex)
assert.equal(estimateFee(tx.toBuffer().length), 30000)
})
})
})