Changeset View
Changeset View
Standalone View
Standalone View
cashtab/src/utils/__tests__/cashMethods.test.js
Show First 20 Lines • Show All 366 Lines • ▼ Show 20 Lines | |||||
it(`signUtxosByAddress() successfully returns a txBuilder object for a one to one XEC tx`, () => { | it(`signUtxosByAddress() successfully returns a txBuilder object for a one to one XEC tx`, () => { | ||||
const isOneToMany = false; | const isOneToMany = false; | ||||
const { destinationAddress, wallet, utxos } = sendBCHMock; | const { destinationAddress, wallet, utxos } = sendBCHMock; | ||||
let txBuilder = utxolib.bitgo.createTransactionBuilderForNetwork( | let txBuilder = utxolib.bitgo.createTransactionBuilderForNetwork( | ||||
utxolib.networks.ecash, | utxolib.networks.ecash, | ||||
); | ); | ||||
const satoshisToSendInput = new BigNumber(2184); | const satoshisToSendInput = new BigNumber(2184); | ||||
const feeInSatsPerByte = currency.defaultFee; | const feeInSatsPerByte = 2.01; | ||||
// mock tx input | // mock tx input | ||||
const inputObj = generateTxInput( | const inputObj = generateTxInput( | ||||
isOneToMany, | isOneToMany, | ||||
utxos, | utxos, | ||||
txBuilder, | txBuilder, | ||||
null, | null, | ||||
satoshisToSendInput, | satoshisToSendInput, | ||||
▲ Show 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | let txBuilder = utxolib.bitgo.createTransactionBuilderForNetwork( | ||||
utxolib.networks.ecash, | utxolib.networks.ecash, | ||||
); | ); | ||||
let destinationAddressAndValueArray = [ | let destinationAddressAndValueArray = [ | ||||
'ecash:qrmz0egsqxj35x5jmzf8szrszdeu72fx0uxgwk3r48,3000', | 'ecash:qrmz0egsqxj35x5jmzf8szrszdeu72fx0uxgwk3r48,3000', | ||||
'ecash:qq9h6d0a5q65fgywv4ry64x04ep906mdku8f0gxfgx,3000', | 'ecash:qq9h6d0a5q65fgywv4ry64x04ep906mdku8f0gxfgx,3000', | ||||
'ecash:qzvydd4n3lm3xv62cx078nu9rg0e3srmqq0knykfed,3000', | 'ecash:qzvydd4n3lm3xv62cx078nu9rg0e3srmqq0knykfed,3000', | ||||
]; | ]; | ||||
const satoshisToSendInput = new BigNumber(900000); | const satoshisToSendInput = new BigNumber(900000); | ||||
const feeInSatsPerByte = currency.defaultFee; | const feeInSatsPerByte = 2.01; | ||||
// mock tx input | // mock tx input | ||||
const inputObj = generateTxInput( | const inputObj = generateTxInput( | ||||
isOneToMany, | isOneToMany, | ||||
utxos, | utxos, | ||||
txBuilder, | txBuilder, | ||||
destinationAddressAndValueArray, | destinationAddressAndValueArray, | ||||
satoshisToSendInput, | satoshisToSendInput, | ||||
▲ Show 20 Lines • Show All 428 Lines • ▼ Show 20 Lines | it(`generateTokenTxInput() returns a valid object for a valid create token tx`, async () => { | ||||
const tokenId = | const tokenId = | ||||
'1c6c9c64d70b285befe733f175d0f384538576876bd280b10587df81279d3f5e'; | '1c6c9c64d70b285befe733f175d0f384538576876bd280b10587df81279d3f5e'; | ||||
const tokenInputObj = generateTokenTxInput( | const tokenInputObj = generateTokenTxInput( | ||||
'GENESIS', | 'GENESIS', | ||||
mockNonSlpUtxos, | mockNonSlpUtxos, | ||||
null, // no slpUtxos used for genesis tx | null, // no slpUtxos used for genesis tx | ||||
tokenId, | tokenId, | ||||
null, // no token send/burn amount for genesis tx | null, // no token send/burn amount for genesis tx | ||||
currency.defaultFee, | 2.01, | ||||
txBuilder, | txBuilder, | ||||
); | ); | ||||
expect(tokenInputObj.inputXecUtxos).toStrictEqual([mockNonSlpUtxos[0]]); | expect(tokenInputObj.inputXecUtxos).toStrictEqual([mockNonSlpUtxos[0]]); | ||||
expect(tokenInputObj.txBuilder.toString()).toStrictEqual( | expect(tokenInputObj.txBuilder.toString()).toStrictEqual( | ||||
mockCreateTokenTxBuilderObj.toString(), | mockCreateTokenTxBuilderObj.toString(), | ||||
); | ); | ||||
expect(tokenInputObj.remainderXecValue).toStrictEqual( | expect(tokenInputObj.remainderXecValue).toStrictEqual( | ||||
new BigNumber(698999), // tokenInputObj.inputXecUtxos - currency.etokenSats 546 - txFee | new BigNumber(698999), // tokenInputObj.inputXecUtxos - currency.etokenSats 546 - txFee | ||||
); | ); | ||||
}); | }); | ||||
it(`generateTokenTxInput() returns a valid object for a valid send token tx`, async () => { | it(`generateTokenTxInput() returns a valid object for a valid send token tx`, async () => { | ||||
let txBuilder = utxolib.bitgo.createTransactionBuilderForNetwork( | let txBuilder = utxolib.bitgo.createTransactionBuilderForNetwork( | ||||
utxolib.networks.ecash, | utxolib.networks.ecash, | ||||
); | ); | ||||
const tokenId = mockSlpUtxos[0].tokenId; | const tokenId = mockSlpUtxos[0].tokenId; | ||||
const tokenInputObj = generateTokenTxInput( | const tokenInputObj = generateTokenTxInput( | ||||
'SEND', | 'SEND', | ||||
mockNonSlpUtxos, | mockNonSlpUtxos, | ||||
mockSlpUtxos, | mockSlpUtxos, | ||||
tokenId, | tokenId, | ||||
new BigNumber(500), // sending 500 of these tokens | new BigNumber(500), // sending 500 of these tokens | ||||
currency.defaultFee, | 2.01, | ||||
txBuilder, | txBuilder, | ||||
); | ); | ||||
expect(tokenInputObj.inputTokenUtxos).toStrictEqual( | expect(tokenInputObj.inputTokenUtxos).toStrictEqual( | ||||
[mockSlpUtxos[0]].concat([mockSlpUtxos[1]]), // mockSlpUtxos[0] 400 + mockSlpUtxos[1] 6500 | [mockSlpUtxos[0]].concat([mockSlpUtxos[1]]), // mockSlpUtxos[0] 400 + mockSlpUtxos[1] 6500 | ||||
); | ); | ||||
expect(tokenInputObj.remainderTokenValue).toStrictEqual( | expect(tokenInputObj.remainderTokenValue).toStrictEqual( | ||||
new BigNumber(6400), // token change is mockSlpUtxos[0] 400 + mockSlpUtxos[1] 6500 - [tokenAmount] 500 | new BigNumber(6400), // token change is mockSlpUtxos[0] 400 + mockSlpUtxos[1] 6500 - [tokenAmount] 500 | ||||
Show All 10 Lines | it(`generateTokenTxInput() returns a valid object for a valid burn token tx`, async () => { | ||||
const tokenId = mockSlpUtxos[0].tokenId; | const tokenId = mockSlpUtxos[0].tokenId; | ||||
const tokenInputObj = generateTokenTxInput( | const tokenInputObj = generateTokenTxInput( | ||||
'BURN', | 'BURN', | ||||
mockNonSlpUtxos, | mockNonSlpUtxos, | ||||
mockSlpUtxos, | mockSlpUtxos, | ||||
tokenId, | tokenId, | ||||
new BigNumber(500), // burning 500 of these tokens | new BigNumber(500), // burning 500 of these tokens | ||||
currency.defaultFee, | 2.01, | ||||
txBuilder, | txBuilder, | ||||
); | ); | ||||
expect(tokenInputObj.inputTokenUtxos).toStrictEqual( | expect(tokenInputObj.inputTokenUtxos).toStrictEqual( | ||||
[mockSlpUtxos[0]].concat([mockSlpUtxos[1]]), // mockSlpUtxos[0] 400 + mockSlpUtxos[1] 6500 | [mockSlpUtxos[0]].concat([mockSlpUtxos[1]]), // mockSlpUtxos[0] 400 + mockSlpUtxos[1] 6500 | ||||
); | ); | ||||
expect(tokenInputObj.remainderTokenValue).toStrictEqual( | expect(tokenInputObj.remainderTokenValue).toStrictEqual( | ||||
new BigNumber(6400), // token change is mockSlpUtxos[0] 400 + mockSlpUtxos[1] 6500 - [tokenAmount] 500 | new BigNumber(6400), // token change is mockSlpUtxos[0] 400 + mockSlpUtxos[1] 6500 - [tokenAmount] 500 | ||||
▲ Show 20 Lines • Show All 74 Lines • ▼ Show 20 Lines | |||||
it(`generateTxInput() returns an input object for a valid one to one XEC tx`, async () => { | it(`generateTxInput() returns an input object for a valid one to one XEC tx`, async () => { | ||||
const isOneToMany = false; | const isOneToMany = false; | ||||
const utxos = mockNonSlpUtxos; | const utxos = mockNonSlpUtxos; | ||||
let txBuilder = utxolib.bitgo.createTransactionBuilderForNetwork( | let txBuilder = utxolib.bitgo.createTransactionBuilderForNetwork( | ||||
utxolib.networks.ecash, | utxolib.networks.ecash, | ||||
); | ); | ||||
const destinationAddressAndValueArray = null; | const destinationAddressAndValueArray = null; | ||||
const satoshisToSend = new BigNumber(2184); | const satoshisToSend = new BigNumber(2184); | ||||
const feeInSatsPerByte = currency.defaultFee; | const feeInSatsPerByte = 2.01; | ||||
const inputObj = generateTxInput( | const inputObj = generateTxInput( | ||||
isOneToMany, | isOneToMany, | ||||
utxos, | utxos, | ||||
txBuilder, | txBuilder, | ||||
destinationAddressAndValueArray, | destinationAddressAndValueArray, | ||||
satoshisToSend, | satoshisToSend, | ||||
feeInSatsPerByte, | feeInSatsPerByte, | ||||
Show All 11 Lines | let txBuilder = utxolib.bitgo.createTransactionBuilderForNetwork( | ||||
utxolib.networks.ecash, | utxolib.networks.ecash, | ||||
); | ); | ||||
const destinationAddressAndValueArray = [ | const destinationAddressAndValueArray = [ | ||||
'ecash:qrmz0egsqxj35x5jmzf8szrszdeu72fx0uxgwk3r48,3000', | 'ecash:qrmz0egsqxj35x5jmzf8szrszdeu72fx0uxgwk3r48,3000', | ||||
'ecash:qq9h6d0a5q65fgywv4ry64x04ep906mdku8f0gxfgx,3000', | 'ecash:qq9h6d0a5q65fgywv4ry64x04ep906mdku8f0gxfgx,3000', | ||||
'ecash:qzvydd4n3lm3xv62cx078nu9rg0e3srmqq0knykfed,3000', | 'ecash:qzvydd4n3lm3xv62cx078nu9rg0e3srmqq0knykfed,3000', | ||||
]; | ]; | ||||
const satoshisToSend = new BigNumber(900000); | const satoshisToSend = new BigNumber(900000); | ||||
const feeInSatsPerByte = currency.defaultFee; | const feeInSatsPerByte = 2.01; | ||||
const inputObj = generateTxInput( | const inputObj = generateTxInput( | ||||
isOneToMany, | isOneToMany, | ||||
utxos, | utxos, | ||||
txBuilder, | txBuilder, | ||||
destinationAddressAndValueArray, | destinationAddressAndValueArray, | ||||
satoshisToSend, | satoshisToSend, | ||||
feeInSatsPerByte, | feeInSatsPerByte, | ||||
); | ); | ||||
expect(inputObj.txBuilder).not.toStrictEqual(null); | expect(inputObj.txBuilder).not.toStrictEqual(null); | ||||
expect(inputObj.totalInputUtxoValue).toStrictEqual(new BigNumber(1400000)); | expect(inputObj.totalInputUtxoValue).toStrictEqual(new BigNumber(1400000)); | ||||
expect(inputObj.txFee).toStrictEqual(889); | expect(inputObj.txFee).toStrictEqual(889); | ||||
expect(inputObj.inputUtxos.length).not.toStrictEqual(0); | expect(inputObj.inputUtxos.length).not.toStrictEqual(0); | ||||
}); | }); | ||||
it(`generateTxInput() throws error for a one to many XEC tx with invalid destinationAddressAndValueArray input`, async () => { | it(`generateTxInput() throws error for a one to many XEC tx with invalid destinationAddressAndValueArray input`, async () => { | ||||
const isOneToMany = true; | const isOneToMany = true; | ||||
const utxos = mockNonSlpUtxos; | const utxos = mockNonSlpUtxos; | ||||
let txBuilder = utxolib.bitgo.createTransactionBuilderForNetwork( | let txBuilder = utxolib.bitgo.createTransactionBuilderForNetwork( | ||||
utxolib.networks.ecash, | utxolib.networks.ecash, | ||||
); | ); | ||||
const destinationAddressAndValueArray = null; // invalid since isOneToMany is true | const destinationAddressAndValueArray = null; // invalid since isOneToMany is true | ||||
const satoshisToSend = new BigNumber(900000); | const satoshisToSend = new BigNumber(900000); | ||||
const feeInSatsPerByte = currency.defaultFee; | const feeInSatsPerByte = 2.01; | ||||
let thrownError; | let thrownError; | ||||
try { | try { | ||||
generateTxInput( | generateTxInput( | ||||
isOneToMany, | isOneToMany, | ||||
utxos, | utxos, | ||||
txBuilder, | txBuilder, | ||||
destinationAddressAndValueArray, | destinationAddressAndValueArray, | ||||
Show All 13 Lines | let txBuilder = utxolib.bitgo.createTransactionBuilderForNetwork( | ||||
utxolib.networks.ecash, | utxolib.networks.ecash, | ||||
); | ); | ||||
const destinationAddressAndValueArray = [ | const destinationAddressAndValueArray = [ | ||||
'ecash:qrmz0egsqxj35x5jmzf8szrszdeu72fx0uxgwk3r48,3000', | 'ecash:qrmz0egsqxj35x5jmzf8szrszdeu72fx0uxgwk3r48,3000', | ||||
'ecash:qq9h6d0a5q65fgywv4ry64x04ep906mdku8f0gxfgx,3000', | 'ecash:qq9h6d0a5q65fgywv4ry64x04ep906mdku8f0gxfgx,3000', | ||||
'ecash:qzvydd4n3lm3xv62cx078nu9rg0e3srmqq0knykfed,3000', | 'ecash:qzvydd4n3lm3xv62cx078nu9rg0e3srmqq0knykfed,3000', | ||||
]; | ]; | ||||
const satoshisToSend = new BigNumber(900000); | const satoshisToSend = new BigNumber(900000); | ||||
const feeInSatsPerByte = currency.defaultFee; | const feeInSatsPerByte = 2.01; | ||||
let thrownError; | let thrownError; | ||||
try { | try { | ||||
generateTxInput( | generateTxInput( | ||||
isOneToMany, | isOneToMany, | ||||
utxos, | utxos, | ||||
txBuilder, | txBuilder, | ||||
destinationAddressAndValueArray, | destinationAddressAndValueArray, | ||||
▲ Show 20 Lines • Show All 750 Lines • ▼ Show 20 Lines | describe('Correctly executes cash utility functions', () => { | ||||
it(`getCashtabByteCount for 1 input, 2000 outputs returns the same value as BCH.BitcoinCash.getByteCount( | it(`getCashtabByteCount for 1 input, 2000 outputs returns the same value as BCH.BitcoinCash.getByteCount( | ||||
{ P2PKH: utxos.length }, | { P2PKH: utxos.length }, | ||||
{ P2PKH: p2pkhOutputNumber }, | { P2PKH: p2pkhOutputNumber }, | ||||
);`, () => { | );`, () => { | ||||
expect(getCashtabByteCount(1, 2000)).toBe(68158); | expect(getCashtabByteCount(1, 2000)).toBe(68158); | ||||
}); | }); | ||||
it('calculates fee correctly for 2 P2PKH outputs', () => { | it('calculates fee correctly for 2 P2PKH outputs', () => { | ||||
const utxosMock = [{}, {}]; | const utxosMock = [{}, {}]; | ||||
expect(calcFee(utxosMock, 2, currency.defaultFee)).toBe(752); | expect(calcFee(utxosMock, 2, 2.01)).toBe(752); | ||||
}); | }); | ||||
it(`Converts a hash160 to an ecash address`, () => { | it(`Converts a hash160 to an ecash address`, () => { | ||||
expect( | expect( | ||||
hash160ToAddress('76458db0ed96fe9863fc1ccec9fa2cfab884b0f6'), | hash160ToAddress('76458db0ed96fe9863fc1ccec9fa2cfab884b0f6'), | ||||
).toBe('ecash:qpmytrdsakt0axrrlswvaj069nat3p9s7cjctmjasj'); | ).toBe('ecash:qpmytrdsakt0axrrlswvaj069nat3p9s7cjctmjasj'); | ||||
}); | }); | ||||
it(`outputScriptToAddress determines P2PKH address type from output script and returns the ecash address`, () => { | it(`outputScriptToAddress determines P2PKH address type from output script and returns the ecash address`, () => { | ||||
expect( | expect( | ||||
Show All 33 Lines |