-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8744fae
commit 0e8a816
Showing
2 changed files
with
132 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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 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,99 @@ | ||
import { Coin } from '../src/wallet/coin'; | ||
import { CoinSelector } from '../src/wallet/coin-selector.ts'; | ||
import { Transaction } from 'bitcoinjs-lib'; | ||
|
||
describe('CoinSelector', () => { | ||
let coinSelector: CoinSelector; | ||
const feeRate: number = 5; | ||
|
||
beforeAll(() => { | ||
coinSelector = new CoinSelector(feeRate); | ||
}); | ||
|
||
it('should generate the correct cost of change', () => { | ||
// Fixed value for the cost of change based on the current feeRate | ||
const correctCostOfChange = 495; | ||
|
||
// Assert the correctness of the CoinSelector result | ||
expect(correctCostOfChange).toBe(coinSelector.costOfChange); | ||
}); | ||
|
||
it('should add change to the fee if it is dust', () => { | ||
const coins: Coin[] = []; | ||
let totalBalance: number = 0; | ||
|
||
// General Coin values for testing purposes | ||
const testValues: number[] = [ | ||
655, 904, 644, 234, 754, 392, 575, 930, 192, 59, | ||
]; | ||
|
||
// Calculate the total balance based on the fixed array of coin values | ||
for (const value of testValues) { | ||
const roundedValue = Math.round(value); | ||
const coin = new Coin({ value: roundedValue }); | ||
coins.push(coin); | ||
// Calculate the total balance by subtracting the spending fee | ||
totalBalance += coin.value - coin.estimateSpendingFee(feeRate); | ||
} | ||
|
||
// Fixed fees for the transaction | ||
const transactionFees = 250; | ||
|
||
// Fixed transaction output value that ensures the cost of change is dust | ||
const txOutValue = | ||
totalBalance - transactionFees - coinSelector.costOfChange + 1; | ||
|
||
// Set the transaction output value with a random output script | ||
const transaction: Transaction = new Transaction(); | ||
const randomString = [...Array(50)] | ||
.map(() => Math.random().toString(36)[2]) | ||
.join(''); | ||
transaction.addOutput(Buffer.from(randomString, 'hex'), txOutValue); | ||
|
||
// Select coins and change using the CoinSelector | ||
const { coins: selectedCoins, change } = coinSelector.select( | ||
coins, | ||
transaction, | ||
); | ||
|
||
// Assert that the selected coins' size is greater than 0 | ||
expect(selectedCoins.length).toBeGreaterThan(0); | ||
|
||
// The above calculations ensure that the change is always less than the dust | ||
expect(change).toBe(0); | ||
}); | ||
|
||
it('change should be considered when greater than dust', () => { | ||
const coins: Coin[] = []; | ||
|
||
// General Coin values for testing purposes | ||
const testValues: number[] = [5000, 4000]; | ||
|
||
for (const value of testValues) { | ||
const coin = new Coin({ value }); | ||
coins.push(coin); | ||
} | ||
|
||
// Fixed transaction output value that ensures the cost of change is always greater than dust | ||
const txOutValue = 500; | ||
|
||
// Set the transaction output value with a random output script | ||
const transaction: Transaction = new Transaction(); | ||
const randomString = [...Array(50)] | ||
.map(() => Math.random().toString(36)[2]) | ||
.join(''); | ||
transaction.addOutput(Buffer.from(randomString, 'hex'), txOutValue); | ||
|
||
// Select coins and change using the CoinSelector | ||
const { coins: selectedCoins, change } = coinSelector.select( | ||
coins, | ||
transaction, | ||
); | ||
|
||
// Assert that the selected coins' size is greater than 0 | ||
expect(selectedCoins.length).toBeGreaterThan(0); | ||
|
||
// The above calculations ensure that the change is always greater than the dust | ||
expect(change).toBeGreaterThan(0); | ||
}); | ||
}); |