Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/hooks/__tests__/migrations.test.js
import { currency } from '../../components/Common/Ticker'; | import { currency } from '../../components/Common/Ticker'; | ||||
import BigNumber from 'bignumber.js'; | import BigNumber from 'bignumber.js'; | ||||
import BCHJS from '@psf/bch-js'; | import BCHJS from '@psf/bch-js'; | ||||
import useBCH from '../useBCH'; | import useBCH from '../useBCH'; | ||||
import { fromSmallestDenomination } from '@utils/cashMethods'; | import { | ||||
fromSmallestDenomination, | |||||
toSmallestDenomination, | |||||
} from '@utils/cashMethods'; | |||||
describe('Testing functions for upgrading Cashtab', () => { | describe('Testing functions for upgrading Cashtab', () => { | ||||
it('Replacement currency.dust parameter parsing matches legacy DUST parameter', () => { | it('Replacement currency.dust parameter parsing matches legacy DUST parameter', () => { | ||||
expect(parseFloat(new BigNumber(currency.dust).toFixed(8))).toBe( | expect(parseFloat(new BigNumber(currency.dust).toFixed(8))).toBe( | ||||
0.00000546, | 0.00000546, | ||||
); | ); | ||||
}); | }); | ||||
it('Replicate 8-decimal return value from instance of toSatoshi in TransactionBuilder with toSmallestDenomination', () => { | it('Replicate 8-decimal return value from instance of toSatoshi in TransactionBuilder with toSmallestDenomination', () => { | ||||
const BCH = new BCHJS(); | const BCH = new BCHJS(); | ||||
const { toSmallestDenomination } = useBCH(); | |||||
const testSendAmount = '0.12345678'; | const testSendAmount = '0.12345678'; | ||||
expect( | expect( | ||||
parseInt(toSmallestDenomination(new BigNumber(testSendAmount), 8)), | parseInt(toSmallestDenomination(new BigNumber(testSendAmount), 8)), | ||||
).toBe(BCH.BitcoinCash.toSatoshi(Number(testSendAmount).toFixed(8))); | ).toBe(BCH.BitcoinCash.toSatoshi(Number(testSendAmount).toFixed(8))); | ||||
}); | }); | ||||
it('Replicate 2-decimal return value from instance of toSatoshi in TransactionBuilder with toSmallestDenomination', () => { | it('Replicate 2-decimal return value from instance of toSatoshi in TransactionBuilder with toSmallestDenomination', () => { | ||||
const BCH = new BCHJS(); | const BCH = new BCHJS(); | ||||
const { toSmallestDenomination } = useBCH(); | |||||
const testSendAmount = '0.12'; | const testSendAmount = '0.12'; | ||||
expect( | expect( | ||||
parseInt(toSmallestDenomination(new BigNumber(testSendAmount), 8)), | parseInt(toSmallestDenomination(new BigNumber(testSendAmount), 8)), | ||||
).toBe(BCH.BitcoinCash.toSatoshi(Number(testSendAmount).toFixed(8))); | ).toBe(BCH.BitcoinCash.toSatoshi(Number(testSendAmount).toFixed(8))); | ||||
}); | }); | ||||
it('Replicate 8-decimal return value from instance of toSatoshi in remainder comparison with toSmallestDenomination', () => { | it('Replicate 8-decimal return value from instance of toSatoshi in remainder comparison with toSmallestDenomination', () => { | ||||
const BCH = new BCHJS(); | const BCH = new BCHJS(); | ||||
const { toSmallestDenomination } = useBCH(); | |||||
// note: do not specify 8 decimals as this test SHOULD fail when currency.dust changes or cashDecimals changes if not updated | // note: do not specify 8 decimals as this test SHOULD fail when currency.dust changes or cashDecimals changes if not updated | ||||
expect( | expect( | ||||
parseFloat(toSmallestDenomination(new BigNumber(currency.dust))), | parseFloat(toSmallestDenomination(new BigNumber(currency.dust))), | ||||
).toBe( | ).toBe( | ||||
BCH.BitcoinCash.toSatoshi( | BCH.BitcoinCash.toSatoshi( | ||||
parseFloat(new BigNumber(currency.dust).toFixed(8)), | parseFloat(new BigNumber(currency.dust).toFixed(8)), | ||||
), | ), | ||||
); | ); | ||||
}); | }); | ||||
it('toSmallestDenomination() returns false if input is not a BigNumber', () => { | it('toSmallestDenomination() returns false if input is not a BigNumber', () => { | ||||
const { toSmallestDenomination } = useBCH(); | |||||
const testInput = 132.12345678; | const testInput = 132.12345678; | ||||
expect(toSmallestDenomination(testInput)).toBe(false); | expect(toSmallestDenomination(testInput)).toBe(false); | ||||
}); | }); | ||||
it(`toSmallestDenomination() returns false if input is a BigNumber with more decimals than specified by cashDecimals parameter`, () => { | it(`toSmallestDenomination() returns false if input is a BigNumber with more decimals than specified by cashDecimals parameter`, () => { | ||||
const { toSmallestDenomination } = useBCH(); | |||||
const testInput = new BigNumber('132.123456789'); | const testInput = new BigNumber('132.123456789'); | ||||
expect(toSmallestDenomination(testInput, 8)).toBe(false); | expect(toSmallestDenomination(testInput, 8)).toBe(false); | ||||
}); | }); | ||||
it(`toSmallestDenomination() returns expected value if input is a BigNumber with 8 decimal places`, () => { | it(`toSmallestDenomination() returns expected value if input is a BigNumber with 8 decimal places`, () => { | ||||
const { toSmallestDenomination } = useBCH(); | |||||
const testInput = new BigNumber('100.12345678'); | const testInput = new BigNumber('100.12345678'); | ||||
expect(toSmallestDenomination(testInput, 8)).toStrictEqual( | expect(toSmallestDenomination(testInput, 8)).toStrictEqual( | ||||
new BigNumber('10012345678'), | new BigNumber('10012345678'), | ||||
); | ); | ||||
}); | }); | ||||
it(`toSmallestDenomination() returns expected value if input is a BigNumber with 2 decimal places`, () => { | it(`toSmallestDenomination() returns expected value if input is a BigNumber with 2 decimal places`, () => { | ||||
const { toSmallestDenomination } = useBCH(); | |||||
const testInput = new BigNumber('100.12'); | const testInput = new BigNumber('100.12'); | ||||
expect(toSmallestDenomination(testInput, 2)).toStrictEqual( | expect(toSmallestDenomination(testInput, 2)).toStrictEqual( | ||||
new BigNumber('10012'), | new BigNumber('10012'), | ||||
); | ); | ||||
}); | }); | ||||
it(`toSmallestDenomination() returns expected value if input is a BigNumber with 1 decimal place`, () => { | it(`toSmallestDenomination() returns expected value if input is a BigNumber with 1 decimal place`, () => { | ||||
const { toSmallestDenomination } = useBCH(); | |||||
const testInput = new BigNumber('100.1'); | const testInput = new BigNumber('100.1'); | ||||
expect(toSmallestDenomination(testInput, 8)).toStrictEqual( | expect(toSmallestDenomination(testInput, 8)).toStrictEqual( | ||||
new BigNumber('10010000000'), | new BigNumber('10010000000'), | ||||
); | ); | ||||
}); | }); | ||||
it('toSmallestDenomination() returns exact result as toSatoshi but in BigNumber format', () => { | it('toSmallestDenomination() returns exact result as toSatoshi but in BigNumber format', () => { | ||||
const BCH = new BCHJS(); | const BCH = new BCHJS(); | ||||
const { toSmallestDenomination } = useBCH(); | |||||
const testAmount = new BigNumber('0.12345678'); | const testAmount = new BigNumber('0.12345678'); | ||||
// Match legacy implementation, inputting a BigNumber converted to a string by .toFixed(8) | // Match legacy implementation, inputting a BigNumber converted to a string by .toFixed(8) | ||||
const testAmountInSatoshis = BCH.BitcoinCash.toSatoshi( | const testAmountInSatoshis = BCH.BitcoinCash.toSatoshi( | ||||
testAmount.toFixed(8), | testAmount.toFixed(8), | ||||
); | ); | ||||
const testAmountInCashDecimals = toSmallestDenomination(testAmount); | const testAmountInCashDecimals = toSmallestDenomination(testAmount); | ||||
expect(testAmountInSatoshis).toStrictEqual(12345678); | expect(testAmountInSatoshis).toStrictEqual(12345678); | ||||
expect(testAmountInCashDecimals).toStrictEqual( | expect(testAmountInCashDecimals).toStrictEqual( | ||||
new BigNumber(testAmountInSatoshis), | new BigNumber(testAmountInSatoshis), | ||||
); | ); | ||||
}); | }); | ||||
it(`BigNumber version of remainder variable is equivalent to Math.floor version`, () => { | it(`BigNumber version of remainder variable is equivalent to Math.floor version`, () => { | ||||
const { toSmallestDenomination } = useBCH(); | |||||
// Test case for sending 0.12345678 BCHA | // Test case for sending 0.12345678 BCHA | ||||
let satoshisToSendTest = toSmallestDenomination( | let satoshisToSendTest = toSmallestDenomination( | ||||
new BigNumber('0.12345678'), | new BigNumber('0.12345678'), | ||||
); | ); | ||||
// Assume total BCHA available in utxos is 500 sats higher than 0.123456578 BCHA | // Assume total BCHA available in utxos is 500 sats higher than 0.123456578 BCHA | ||||
let originalAmountTest = satoshisToSendTest.plus(500); | let originalAmountTest = satoshisToSendTest.plus(500); | ||||
// Assume 229 byte tx fee | // Assume 229 byte tx fee | ||||
let txFeeTest = 229; | let txFeeTest = 229; | ||||
Show All 38 Lines |