Skip to content

Commit c3a3944

Browse files
committed
networks: extract estimateFee as a bind
1 parent 6a551d9 commit c3a3944

File tree

1 file changed

+25
-32
lines changed

1 file changed

+25
-32
lines changed

src/networks.js

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ var networks = {
1313
scriptHash: 0x05,
1414
wif: 0x80,
1515
dustThreshold: 546, // https://github.com/bitcoin/bitcoin/blob/v0.9.2/src/core.h#L151-L162
16-
feePerKb: 10000, // https://github.com/bitcoin/bitcoin/blob/v0.9.2/src/main.cpp#L53
17-
estimateFee: estimateFee('bitcoin')
16+
feePerKb: 10000 // https://github.com/bitcoin/bitcoin/blob/v0.9.2/src/main.cpp#L53
1817
},
1918
testnet: {
2019
magic: 0xd9b4bef9,
@@ -27,8 +26,7 @@ var networks = {
2726
scriptHash: 0xc4,
2827
wif: 0xef,
2928
dustThreshold: 546,
30-
feePerKb: 10000,
31-
estimateFee: estimateFee('testnet')
29+
feePerKb: 10000
3230
},
3331
litecoin: {
3432
magic: 0xd9b4bef9,
@@ -42,8 +40,7 @@ var networks = {
4240
wif: 0xb0,
4341
dustThreshold: 0, // https://github.com/litecoin-project/litecoin/blob/v0.8.7.2/src/main.cpp#L360-L365
4442
dustSoftThreshold: 100000, // https://github.com/litecoin-project/litecoin/blob/v0.8.7.2/src/main.h#L53
45-
feePerKb: 100000, // https://github.com/litecoin-project/litecoin/blob/v0.8.7.2/src/main.cpp#L56
46-
estimateFee: estimateFee('litecoin')
43+
feePerKb: 100000 // https://github.com/litecoin-project/litecoin/blob/v0.8.7.2/src/main.cpp#L56
4744
},
4845
dogecoin: {
4946
messagePrefix: '\x19Dogecoin Signed Message:\n',
@@ -56,8 +53,7 @@ var networks = {
5653
wif: 0x9e,
5754
dustThreshold: 0, // https://github.com/dogecoin/dogecoin/blob/v1.7.1/src/core.h#L155-L160
5855
dustSoftThreshold: 100000000, // https://github.com/dogecoin/dogecoin/blob/v1.7.1/src/main.h#L62
59-
feePerKb: 100000000, // https://github.com/dogecoin/dogecoin/blob/v1.7.1/src/main.cpp#L58
60-
estimateFee: estimateFee('dogecoin')
56+
feePerKb: 100000000 // https://github.com/dogecoin/dogecoin/blob/v1.7.1/src/main.cpp#L58
6157
},
6258
viacoin: {
6359
messagePrefix: '\x18Viacoin Signed Message:\n',
@@ -70,8 +66,7 @@ var networks = {
7066
wif: 0xc7,
7167
dustThreshold: 560,
7268
dustSoftThreshold: 100000,
73-
feePerKb: 100000, //
74-
estimateFee: estimateFee('viacoin')
69+
feePerKb: 100000
7570
},
7671
viacointestnet: {
7772
messagePrefix: '\x18Viacoin Signed Message:\n',
@@ -84,8 +79,7 @@ var networks = {
8479
wif: 0xff,
8580
dustThreshold: 560,
8681
dustSoftThreshold: 100000,
87-
feePerKb: 100000,
88-
estimateFee: estimateFee('viacointestnet')
82+
feePerKb: 100000
8983
},
9084
gamerscoin: {
9185
messagePrefix: '\x19Gamerscoin Signed Message:\n',
@@ -98,8 +92,7 @@ var networks = {
9892
wif: 0xA6,
9993
dustThreshold: 0, // https://github.com/gamers-coin/gamers-coinv3/blob/master/src/main.cpp#L358-L363
10094
dustSoftThreshold: 100000, // https://github.com/gamers-coin/gamers-coinv3/blob/master/src/main.cpp#L51
101-
feePerKb: 100000, // https://github.com/gamers-coin/gamers-coinv3/blob/master/src/main.cpp#L54
102-
estimateFee: estimateFee('gamerscoin')
95+
feePerKb: 100000 // https://github.com/gamers-coin/gamers-coinv3/blob/master/src/main.cpp#L54
10396
},
10497
jumbucks: {
10598
messagePrefix: '\x19Jumbucks Signed Message:\n',
@@ -112,8 +105,7 @@ var networks = {
112105
wif: 0xab,
113106
dustThreshold: 0,
114107
dustSoftThreshold: 10000,
115-
feePerKb: 10000,
116-
estimateFee: estimateFee('jumbucks')
108+
feePerKb: 10000
117109
},
118110
zetacoin: {
119111
messagePrefix: '\x18Zetacoin Signed Message:\n',
@@ -125,34 +117,35 @@ var networks = {
125117
scriptHash: 0x09,
126118
wif: 0xe0,
127119
dustThreshold: 546, // https://github.com/zetacoin/zetacoin/blob/master/src/core.h#L159
128-
feePerKb: 10000, // https://github.com/zetacoin/zetacoin/blob/master/src/main.cpp#L54
129-
estimateFee: estimateFee('zetacoin')
120+
feePerKb: 10000 // https://github.com/zetacoin/zetacoin/blob/master/src/main.cpp#L54
130121
}
131122
}
132123

133-
function estimateFee (type) {
134-
return function (tx) {
135-
var network = networks[type]
136-
var baseFee = network.feePerKb
137-
var byteSize = tx.toBuffer().length
124+
function estimateFee (tx, network) {
125+
var baseFee = network.feePerKb
126+
var byteSize = tx.toBuffer().length
138127

139-
var fee = baseFee * Math.ceil(byteSize / 1000)
140-
if (network.dustSoftThreshold === undefined) return fee
128+
var fee = baseFee * Math.ceil(byteSize / 1000)
129+
if (network.dustSoftThreshold === undefined) return fee
141130

142-
tx.outs.forEach(function (e) {
143-
if (e.value < network.dustSoftThreshold) {
144-
fee += baseFee
145-
}
146-
})
131+
tx.outs.forEach(function (e) {
132+
if (e.value < network.dustSoftThreshold) {
133+
fee += baseFee
134+
}
135+
})
147136

148-
return fee
149-
}
137+
return fee
150138
}
151139

152140
// FIXME: 1.5.3 compatibility patch(s)
141+
function patchEstimateFee (network, tx) {
142+
return estimateFee(tx, network)
143+
}
144+
153145
for (var networkName in networks) {
154146
var network = networks[networkName]
155147

148+
network.estimateFee = patchEstimateFee.bind(null, network)
156149
network.magicPrefix = network.messagePrefix
157150
}
158151

0 commit comments

Comments
 (0)