-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Fee and dust #215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Fee and dust #215
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
45a7241
wallet: Move dust and fee per kb into networks.js
weilu 79ec61d
Fix bitcoin dustThreshold and feePerKb values
weilu 122b613
Move fee estimation into networks.js
weilu bc3e077
Add fee estimation functions for dogecoin and litecoin
weilu c4285d9
Network specific constants reference the latest tags
weilu 5dcefc5
Network estimateFee tests no longer relies on fixtures
weilu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| // https://en.bitcoin.it/wiki/List_of_address_prefixes | ||
| // Dogecoin BIP32 is a proposed standard: https://bitcointalk.org/index.php?topic=409731 | ||
| module.exports = { | ||
|
|
||
| var networks = { | ||
| bitcoin: { | ||
| magicPrefix: '\x18Bitcoin Signed Message:\n', | ||
| bip32: { | ||
|
|
@@ -9,7 +10,10 @@ module.exports = { | |
| }, | ||
| pubKeyHash: 0x00, | ||
| scriptHash: 0x05, | ||
| wif: 0x80 | ||
| wif: 0x80, | ||
| 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 | ||
| estimateFee: estimateFee('bitcoin') | ||
| }, | ||
| dogecoin: { | ||
| magicPrefix: '\x19Dogecoin Signed Message:\n', | ||
|
|
@@ -19,7 +23,11 @@ module.exports = { | |
| }, | ||
| pubKeyHash: 0x1e, | ||
| scriptHash: 0x16, | ||
| wif: 0x9e | ||
| wif: 0x9e, | ||
| 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 | ||
| feePerKb: 100000000, // https://github.com/dogecoin/dogecoin/blob/v1.7.1/src/main.cpp#L58 | ||
| estimateFee: estimateFee('dogecoin') | ||
| }, | ||
| litecoin: { | ||
| magicPrefix: '\x19Litecoin Signed Message:\n', | ||
|
|
@@ -29,7 +37,11 @@ module.exports = { | |
| }, | ||
| pubKeyHash: 0x30, | ||
| scriptHash: 0x05, | ||
| wif: 0xb0 | ||
| wif: 0xb0, | ||
| 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 | ||
| feePerKb: 100000, // https://github.com/litecoin-project/litecoin/blob/v0.8.7.2/src/main.cpp#L56 | ||
| estimateFee: estimateFee('litecoin') | ||
| }, | ||
| testnet: { | ||
| magicPrefix: '\x18Bitcoin Signed Message:\n', | ||
|
|
@@ -39,6 +51,30 @@ module.exports = { | |
| }, | ||
| pubKeyHash: 0x6f, | ||
| scriptHash: 0xc4, | ||
| wif: 0xef | ||
| wif: 0xef, | ||
| dustThreshold: 546, | ||
| feePerKb: 10000, | ||
| estimateFee: estimateFee('bitcoin') | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be |
||
| } | ||
| } | ||
|
|
||
| function estimateFee(type) { | ||
| return function(tx) { | ||
| var network = networks[type] | ||
| var baseFee = network.feePerKb | ||
| var byteSize = tx.toBuffer().length | ||
|
|
||
| var fee = baseFee * Math.ceil(byteSize / 1000) | ||
| if(network.dustSoftThreshold == undefined) return fee | ||
|
|
||
| tx.outs.forEach(function(e){ | ||
| if(e.value < network.dustSoftThreshold) { | ||
| fee += baseFee | ||
| } | ||
| }) | ||
|
|
||
| return fee | ||
| } | ||
| } | ||
|
|
||
| module.exports = networks | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| var assert = require('assert') | ||
| var networks = require('../src/networks') | ||
| var sinon = require('sinon') | ||
| var Transaction = require('../src/transaction') | ||
|
|
||
| describe('networks', function() { | ||
| var txToBuffer | ||
| before(function(){ | ||
| txToBuffer = sinon.stub(Transaction.prototype, "toBuffer") | ||
| }) | ||
|
|
||
| after(function(){ | ||
| Transaction.prototype.toBuffer.restore() | ||
| }) | ||
|
|
||
| describe('bitcoin', function() { | ||
| describe('estimateFee', function() { | ||
| var estimateFee = networks.bitcoin.estimateFee | ||
|
|
||
| it('works at boundry', function() { | ||
| txToBuffer.returns(new Buffer(1000)) | ||
| var tx = new Transaction() | ||
| assert.equal(estimateFee(tx), 10000) | ||
| }) | ||
|
|
||
| it('rounds up to the closest kb for estimation', function() { | ||
| txToBuffer.returns(new Buffer(2800)) | ||
| var tx = new Transaction() | ||
| assert.equal(estimateFee(tx), 30000) | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('dogecoin', function() { | ||
| describe('estimateFee', function() { | ||
| var estimateFee = networks.dogecoin.estimateFee | ||
|
|
||
| it('regular fee per kb applies when every output has value no less than DUST_SOFT_LIMIT', function() { | ||
| txToBuffer.returns(new Buffer(1000)) | ||
| var tx = new Transaction() | ||
| tx.outs[0] = { value: 100000000 } | ||
|
|
||
| assert.equal(estimateFee(tx), 100000000) | ||
| }) | ||
|
|
||
| it('applies additional fee on every output with value below DUST_SOFT_LIMIT', function() { | ||
| txToBuffer.returns(new Buffer(1000)) | ||
| var tx = new Transaction() | ||
| tx.outs[0] = { value: 99999999 } | ||
| tx.outs[1] = { value: 99999999 } | ||
|
|
||
| assert.equal(estimateFee(tx), 3 * 100000000) | ||
| }) | ||
|
|
||
| it('rounds up to the closest kb for estimation', function() { | ||
| txToBuffer.returns(new Buffer(2800)) | ||
| var tx = new Transaction() | ||
|
|
||
| assert.equal(estimateFee(tx), 300000000) | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('litecoin', function() { | ||
| describe('estimateFee', function() { | ||
| var estimateFee = networks.litecoin.estimateFee | ||
|
|
||
| it('regular fee per kb applies when every output has value no less than DUST_SOFT_LIMIT', function() { | ||
| txToBuffer.returns(new Buffer(1000)) | ||
| var tx = new Transaction() | ||
| tx.outs[0] = { value: 100000 } | ||
|
|
||
| assert.equal(estimateFee(tx), 100000) | ||
| }) | ||
|
|
||
| it('applies additional fee on every output with value below DUST_SOFT_LIMIT', function() { | ||
| txToBuffer.returns(new Buffer(1000)) | ||
| var tx = new Transaction() | ||
| tx.outs[0] = { value: 99999 } | ||
| tx.outs[1] = { value: 99999 } | ||
|
|
||
| assert.equal(estimateFee(tx), 3 * 100000) | ||
| }) | ||
|
|
||
| it('rounds up to the closest kb for estimation', function() { | ||
| txToBuffer.returns(new Buffer(2800)) | ||
| var tx = new Transaction() | ||
|
|
||
| assert.equal(estimateFee(tx), 300000) | ||
| }) | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #216. If that gets merged, please change
dustsofthresholdtodustSoftThresholdThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done with a rebase, which also cleaned up the commit history