Browse Source

networks: extract estimateFee as a bind

hk-custom-address
Daniel Cousens 10 years ago
parent
commit
c3a39444e3
  1. 57
      src/networks.js

57
src/networks.js

@ -13,8 +13,7 @@ var networks = {
scriptHash: 0x05, scriptHash: 0x05,
wif: 0x80, wif: 0x80,
dustThreshold: 546, // https://github.com/bitcoin/bitcoin/blob/v0.9.2/src/core.h#L151-L162 dustThreshold: 546, // https://github.com/bitcoin/bitcoin/blob/v0.9.2/src/core.h#L151-L162
feePerKb: 10000, // https://github.com/bitcoin/bitcoin/blob/v0.9.2/src/main.cpp#L53 feePerKb: 10000 // https://github.com/bitcoin/bitcoin/blob/v0.9.2/src/main.cpp#L53
estimateFee: estimateFee('bitcoin')
}, },
testnet: { testnet: {
magic: 0xd9b4bef9, magic: 0xd9b4bef9,
@ -27,8 +26,7 @@ var networks = {
scriptHash: 0xc4, scriptHash: 0xc4,
wif: 0xef, wif: 0xef,
dustThreshold: 546, dustThreshold: 546,
feePerKb: 10000, feePerKb: 10000
estimateFee: estimateFee('testnet')
}, },
litecoin: { litecoin: {
magic: 0xd9b4bef9, magic: 0xd9b4bef9,
@ -42,8 +40,7 @@ var networks = {
wif: 0xb0, wif: 0xb0,
dustThreshold: 0, // https://github.com/litecoin-project/litecoin/blob/v0.8.7.2/src/main.cpp#L360-L365 dustThreshold: 0, // https://github.com/litecoin-project/litecoin/blob/v0.8.7.2/src/main.cpp#L360-L365
dustSoftThreshold: 100000, // https://github.com/litecoin-project/litecoin/blob/v0.8.7.2/src/main.h#L53 dustSoftThreshold: 100000, // https://github.com/litecoin-project/litecoin/blob/v0.8.7.2/src/main.h#L53
feePerKb: 100000, // https://github.com/litecoin-project/litecoin/blob/v0.8.7.2/src/main.cpp#L56 feePerKb: 100000 // https://github.com/litecoin-project/litecoin/blob/v0.8.7.2/src/main.cpp#L56
estimateFee: estimateFee('litecoin')
}, },
dogecoin: { dogecoin: {
messagePrefix: '\x19Dogecoin Signed Message:\n', messagePrefix: '\x19Dogecoin Signed Message:\n',
@ -56,8 +53,7 @@ var networks = {
wif: 0x9e, wif: 0x9e,
dustThreshold: 0, // https://github.com/dogecoin/dogecoin/blob/v1.7.1/src/core.h#L155-L160 dustThreshold: 0, // https://github.com/dogecoin/dogecoin/blob/v1.7.1/src/core.h#L155-L160
dustSoftThreshold: 100000000, // https://github.com/dogecoin/dogecoin/blob/v1.7.1/src/main.h#L62 dustSoftThreshold: 100000000, // https://github.com/dogecoin/dogecoin/blob/v1.7.1/src/main.h#L62
feePerKb: 100000000, // https://github.com/dogecoin/dogecoin/blob/v1.7.1/src/main.cpp#L58 feePerKb: 100000000 // https://github.com/dogecoin/dogecoin/blob/v1.7.1/src/main.cpp#L58
estimateFee: estimateFee('dogecoin')
}, },
viacoin: { viacoin: {
messagePrefix: '\x18Viacoin Signed Message:\n', messagePrefix: '\x18Viacoin Signed Message:\n',
@ -70,8 +66,7 @@ var networks = {
wif: 0xc7, wif: 0xc7,
dustThreshold: 560, dustThreshold: 560,
dustSoftThreshold: 100000, dustSoftThreshold: 100000,
feePerKb: 100000, // feePerKb: 100000
estimateFee: estimateFee('viacoin')
}, },
viacointestnet: { viacointestnet: {
messagePrefix: '\x18Viacoin Signed Message:\n', messagePrefix: '\x18Viacoin Signed Message:\n',
@ -84,8 +79,7 @@ var networks = {
wif: 0xff, wif: 0xff,
dustThreshold: 560, dustThreshold: 560,
dustSoftThreshold: 100000, dustSoftThreshold: 100000,
feePerKb: 100000, feePerKb: 100000
estimateFee: estimateFee('viacointestnet')
}, },
gamerscoin: { gamerscoin: {
messagePrefix: '\x19Gamerscoin Signed Message:\n', messagePrefix: '\x19Gamerscoin Signed Message:\n',
@ -98,8 +92,7 @@ var networks = {
wif: 0xA6, wif: 0xA6,
dustThreshold: 0, // https://github.com/gamers-coin/gamers-coinv3/blob/master/src/main.cpp#L358-L363 dustThreshold: 0, // https://github.com/gamers-coin/gamers-coinv3/blob/master/src/main.cpp#L358-L363
dustSoftThreshold: 100000, // https://github.com/gamers-coin/gamers-coinv3/blob/master/src/main.cpp#L51 dustSoftThreshold: 100000, // https://github.com/gamers-coin/gamers-coinv3/blob/master/src/main.cpp#L51
feePerKb: 100000, // https://github.com/gamers-coin/gamers-coinv3/blob/master/src/main.cpp#L54 feePerKb: 100000 // https://github.com/gamers-coin/gamers-coinv3/blob/master/src/main.cpp#L54
estimateFee: estimateFee('gamerscoin')
}, },
jumbucks: { jumbucks: {
messagePrefix: '\x19Jumbucks Signed Message:\n', messagePrefix: '\x19Jumbucks Signed Message:\n',
@ -112,8 +105,7 @@ var networks = {
wif: 0xab, wif: 0xab,
dustThreshold: 0, dustThreshold: 0,
dustSoftThreshold: 10000, dustSoftThreshold: 10000,
feePerKb: 10000, feePerKb: 10000
estimateFee: estimateFee('jumbucks')
}, },
zetacoin: { zetacoin: {
messagePrefix: '\x18Zetacoin Signed Message:\n', messagePrefix: '\x18Zetacoin Signed Message:\n',
@ -125,34 +117,35 @@ var networks = {
scriptHash: 0x09, scriptHash: 0x09,
wif: 0xe0, wif: 0xe0,
dustThreshold: 546, // https://github.com/zetacoin/zetacoin/blob/master/src/core.h#L159 dustThreshold: 546, // https://github.com/zetacoin/zetacoin/blob/master/src/core.h#L159
feePerKb: 10000, // https://github.com/zetacoin/zetacoin/blob/master/src/main.cpp#L54 feePerKb: 10000 // https://github.com/zetacoin/zetacoin/blob/master/src/main.cpp#L54
estimateFee: estimateFee('zetacoin')
} }
} }
function estimateFee (type) { function estimateFee (tx, network) {
return function (tx) { var baseFee = network.feePerKb
var network = networks[type] var byteSize = tx.toBuffer().length
var baseFee = network.feePerKb
var byteSize = tx.toBuffer().length
var fee = baseFee * Math.ceil(byteSize / 1000) var fee = baseFee * Math.ceil(byteSize / 1000)
if (network.dustSoftThreshold === undefined) return fee if (network.dustSoftThreshold === undefined) return fee
tx.outs.forEach(function (e) { tx.outs.forEach(function (e) {
if (e.value < network.dustSoftThreshold) { if (e.value < network.dustSoftThreshold) {
fee += baseFee fee += baseFee
} }
}) })
return fee return fee
}
} }
// FIXME: 1.5.3 compatibility patch(s) // FIXME: 1.5.3 compatibility patch(s)
function patchEstimateFee (network, tx) {
return estimateFee(tx, network)
}
for (var networkName in networks) { for (var networkName in networks) {
var network = networks[networkName] var network = networks[networkName]
network.estimateFee = patchEstimateFee.bind(null, network)
network.magicPrefix = network.messagePrefix network.magicPrefix = network.messagePrefix
} }

Loading…
Cancel
Save