Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/utils/__tests__/cashMethods.test.js
Show First 20 Lines • Show All 205 Lines • ▼ Show 20 Lines | try { | ||||
generateGenesisOpReturn(configObj); | generateGenesisOpReturn(configObj); | ||||
} catch (err) { | } catch (err) { | ||||
errorThrown = err.message; | errorThrown = err.message; | ||||
} | } | ||||
expect(errorThrown).toStrictEqual('Invalid token configuration'); | expect(errorThrown).toStrictEqual('Invalid token configuration'); | ||||
}); | }); | ||||
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 BCH = new BCHJS(); | |||||
const isOneToMany = false; | const isOneToMany = false; | ||||
const { destinationAddress, wallet, utxos } = sendBCHMock; | const { destinationAddress, wallet, utxos } = sendBCHMock; | ||||
let txBuilder = new TransactionBuilder(); | let txBuilder = new TransactionBuilder(); | ||||
const satoshisToSendInput = new BigNumber(2184); | const satoshisToSendInput = new BigNumber(2184); | ||||
const feeInSatsPerByte = currency.defaultFee; | const feeInSatsPerByte = currency.defaultFee; | ||||
// mock tx input | // mock tx input | ||||
const inputObj = generateTxInput( | const inputObj = generateTxInput( | ||||
Show All 16 Lines | it(`signUtxosByAddress() successfully returns a txBuilder object for a one to one XEC tx`, () => { | ||||
const satoshisToSendOutput = fromXecToSatoshis( | const satoshisToSendOutput = fromXecToSatoshis( | ||||
new BigNumber(singleSendValue), | new BigNumber(singleSendValue), | ||||
); | ); | ||||
const txFee = new BigNumber(totalInputUtxoValue).minus( | const txFee = new BigNumber(totalInputUtxoValue).minus( | ||||
new BigNumber(satoshisToSendOutput), | new BigNumber(satoshisToSendOutput), | ||||
); | ); | ||||
const changeAddress = wallet.Path1899.cashAddress; | const changeAddress = wallet.Path1899.cashAddress; | ||||
const outputObj = generateTxOutput( | const outputObj = generateTxOutput( | ||||
BCH, | |||||
isOneToMany, | isOneToMany, | ||||
singleSendValue, | singleSendValue, | ||||
satoshisToSendOutput, | satoshisToSendOutput, | ||||
totalInputUtxoValue, | totalInputUtxoValue, | ||||
destinationAddress, | destinationAddress, | ||||
null, | null, | ||||
changeAddress, | changeAddress, | ||||
txFee, | txFee, | ||||
inputObj.txBuilder, | inputObj.txBuilder, | ||||
); | ); | ||||
const txBuilderResponse = signUtxosByAddress( | const txBuilderResponse = signUtxosByAddress( | ||||
mockSingleInputUtxo, | mockSingleInputUtxo, | ||||
wallet, | wallet, | ||||
outputObj, | outputObj, | ||||
); | ); | ||||
expect(txBuilderResponse.toString()).toStrictEqual( | expect(txBuilderResponse.toString()).toStrictEqual( | ||||
mockOneToOneSendXecTxBuilderObj.toString(), | mockOneToOneSendXecTxBuilderObj.toString(), | ||||
); | ); | ||||
}); | }); | ||||
it(`signUtxosByAddress() successfully returns a txBuilder object for a one to many XEC tx`, () => { | it(`signUtxosByAddress() successfully returns a txBuilder object for a one to many XEC tx`, () => { | ||||
const BCH = new BCHJS(); | |||||
const isOneToMany = true; | const isOneToMany = true; | ||||
const { wallet, utxos } = sendBCHMock; | const { wallet, utxos } = sendBCHMock; | ||||
let txBuilder = new TransactionBuilder(); | let txBuilder = new TransactionBuilder(); | ||||
let destinationAddressAndValueArray = [ | let destinationAddressAndValueArray = [ | ||||
'ecash:qrmz0egsqxj35x5jmzf8szrszdeu72fx0uxgwk3r48,3000', | 'ecash:qrmz0egsqxj35x5jmzf8szrszdeu72fx0uxgwk3r48,3000', | ||||
'ecash:qq9h6d0a5q65fgywv4ry64x04ep906mdku8f0gxfgx,3000', | 'ecash:qq9h6d0a5q65fgywv4ry64x04ep906mdku8f0gxfgx,3000', | ||||
'ecash:qzvydd4n3lm3xv62cx078nu9rg0e3srmqq0knykfed,3000', | 'ecash:qzvydd4n3lm3xv62cx078nu9rg0e3srmqq0knykfed,3000', | ||||
]; | ]; | ||||
Show All 26 Lines | const txFee = new BigNumber(totalInputUtxoValue) | ||||
.minus( | .minus( | ||||
new BigNumber( | new BigNumber( | ||||
mockOneToManySendXecTxBuilderObj.transaction.tx.outs[3].value, | mockOneToManySendXecTxBuilderObj.transaction.tx.outs[3].value, | ||||
), | ), | ||||
); // change value | ); // change value | ||||
destinationAddressAndValueArray = validAddressArrayInput; | destinationAddressAndValueArray = validAddressArrayInput; | ||||
const changeAddress = wallet.Path1899.cashAddress; | const changeAddress = wallet.Path1899.cashAddress; | ||||
const outputObj = generateTxOutput( | const outputObj = generateTxOutput( | ||||
BCH, | |||||
isOneToMany, | isOneToMany, | ||||
singleSendValue, | singleSendValue, | ||||
satoshisToSendOutput, | satoshisToSendOutput, | ||||
totalInputUtxoValue, | totalInputUtxoValue, | ||||
null, | null, | ||||
destinationAddressAndValueArray, | destinationAddressAndValueArray, | ||||
changeAddress, | changeAddress, | ||||
txFee, | txFee, | ||||
▲ Show 20 Lines • Show All 513 Lines • ▼ Show 20 Lines | it(`generateTxInput() throws error for a one to many XEC tx with invalid utxos input`, async () => { | ||||
} catch (err) { | } catch (err) { | ||||
thrownError = err; | thrownError = err; | ||||
} | } | ||||
expect(thrownError.message).toStrictEqual('Invalid tx input parameter'); | expect(thrownError.message).toStrictEqual('Invalid tx input parameter'); | ||||
}); | }); | ||||
it(`generateTxOutput() returns a txBuilder instance for a valid one to one XEC tx`, () => { | it(`generateTxOutput() returns a txBuilder instance for a valid one to one XEC tx`, () => { | ||||
// txbuilder output params | // txbuilder output params | ||||
const BCH = new BCHJS(); | |||||
const { destinationAddress, wallet } = sendBCHMock; | const { destinationAddress, wallet } = sendBCHMock; | ||||
const isOneToMany = false; | const isOneToMany = false; | ||||
const singleSendValue = fromSatoshisToXec( | const singleSendValue = fromSatoshisToXec( | ||||
mockOneToOneSendXecTxBuilderObj.transaction.tx.outs[0].value, | mockOneToOneSendXecTxBuilderObj.transaction.tx.outs[0].value, | ||||
); | ); | ||||
const totalInputUtxoValue = | const totalInputUtxoValue = | ||||
mockOneToOneSendXecTxBuilderObj.transaction.inputs[0].value; | mockOneToOneSendXecTxBuilderObj.transaction.inputs[0].value; | ||||
const satoshisToSend = fromXecToSatoshis(new BigNumber(singleSendValue)); | const satoshisToSend = fromXecToSatoshis(new BigNumber(singleSendValue)); | ||||
// for unit test purposes, calculate fee by subtracting satoshisToSend from totalInputUtxoValue | // for unit test purposes, calculate fee by subtracting satoshisToSend from totalInputUtxoValue | ||||
// no change output to be subtracted in this tx | // no change output to be subtracted in this tx | ||||
const txFee = new BigNumber(totalInputUtxoValue).minus( | const txFee = new BigNumber(totalInputUtxoValue).minus( | ||||
new BigNumber(satoshisToSend), | new BigNumber(satoshisToSend), | ||||
); | ); | ||||
const destinationAddressAndValueArray = null; | const destinationAddressAndValueArray = null; | ||||
let txBuilder = new TransactionBuilder(); | let txBuilder = new TransactionBuilder(); | ||||
const changeAddress = wallet.Path1899.cashAddress; | const changeAddress = wallet.Path1899.cashAddress; | ||||
const outputObj = generateTxOutput( | const outputObj = generateTxOutput( | ||||
BCH, | |||||
isOneToMany, | isOneToMany, | ||||
singleSendValue, | singleSendValue, | ||||
satoshisToSend, | satoshisToSend, | ||||
totalInputUtxoValue, | totalInputUtxoValue, | ||||
destinationAddress, | destinationAddress, | ||||
destinationAddressAndValueArray, | destinationAddressAndValueArray, | ||||
changeAddress, | changeAddress, | ||||
txFee, | txFee, | ||||
txBuilder, | txBuilder, | ||||
); | ); | ||||
expect(outputObj.toString()).toStrictEqual( | expect(outputObj.toString()).toStrictEqual( | ||||
mockOneToOneSendXecTxBuilderObj.toString(), | mockOneToOneSendXecTxBuilderObj.toString(), | ||||
); | ); | ||||
}); | }); | ||||
it(`generateTxOutput() returns a txBuilder instance for a valid one to many XEC tx`, () => { | it(`generateTxOutput() returns a txBuilder instance for a valid one to many XEC tx`, () => { | ||||
// txbuilder output params | // txbuilder output params | ||||
const BCH = new BCHJS(); | |||||
const { destinationAddress, wallet } = sendBCHMock; | const { destinationAddress, wallet } = sendBCHMock; | ||||
const isOneToMany = true; | const isOneToMany = true; | ||||
const singleSendValue = null; | const singleSendValue = null; | ||||
const totalInputUtxoValue = | const totalInputUtxoValue = | ||||
mockOneToManySendXecTxBuilderObj.transaction.inputs[0].value + | mockOneToManySendXecTxBuilderObj.transaction.inputs[0].value + | ||||
mockOneToManySendXecTxBuilderObj.transaction.inputs[1].value + | mockOneToManySendXecTxBuilderObj.transaction.inputs[1].value + | ||||
mockOneToManySendXecTxBuilderObj.transaction.inputs[2].value; | mockOneToManySendXecTxBuilderObj.transaction.inputs[2].value; | ||||
const satoshisToSend = new BigNumber( | const satoshisToSend = new BigNumber( | ||||
Show All 10 Lines | const txFee = new BigNumber(totalInputUtxoValue) | ||||
), | ), | ||||
); // change value | ); // change value | ||||
const destinationAddressAndValueArray = validAddressArrayInput; | const destinationAddressAndValueArray = validAddressArrayInput; | ||||
let txBuilder = new TransactionBuilder(); | let txBuilder = new TransactionBuilder(); | ||||
const changeAddress = wallet.Path1899.cashAddress; | const changeAddress = wallet.Path1899.cashAddress; | ||||
const outputObj = generateTxOutput( | const outputObj = generateTxOutput( | ||||
BCH, | |||||
isOneToMany, | isOneToMany, | ||||
singleSendValue, | singleSendValue, | ||||
satoshisToSend, | satoshisToSend, | ||||
totalInputUtxoValue, | totalInputUtxoValue, | ||||
destinationAddress, | destinationAddress, | ||||
destinationAddressAndValueArray, | destinationAddressAndValueArray, | ||||
changeAddress, | changeAddress, | ||||
txFee, | txFee, | ||||
txBuilder, | txBuilder, | ||||
); | ); | ||||
expect(outputObj.toString()).toStrictEqual( | expect(outputObj.toString()).toStrictEqual( | ||||
mockOneToManySendXecTxBuilderObj.toString(), | mockOneToManySendXecTxBuilderObj.toString(), | ||||
); | ); | ||||
}); | }); | ||||
it(`generateTxOutput() throws an error on invalid input params for a one to one XEC tx`, () => { | it(`generateTxOutput() throws an error on invalid input params for a one to one XEC tx`, () => { | ||||
// txbuilder output params | // txbuilder output params | ||||
const BCH = new BCHJS(); | |||||
const { wallet } = sendBCHMock; | const { wallet } = sendBCHMock; | ||||
const isOneToMany = false; | const isOneToMany = false; | ||||
const singleSendValue = null; // invalid due to singleSendValue being mandatory when isOneToMany is false | const singleSendValue = null; // invalid due to singleSendValue being mandatory when isOneToMany is false | ||||
const totalInputUtxoValue = | const totalInputUtxoValue = | ||||
mockOneToOneSendXecTxBuilderObj.transaction.inputs[0].value; | mockOneToOneSendXecTxBuilderObj.transaction.inputs[0].value; | ||||
const satoshisToSend = fromXecToSatoshis(new BigNumber(singleSendValue)); | const satoshisToSend = fromXecToSatoshis(new BigNumber(singleSendValue)); | ||||
// for unit test purposes, calculate fee by subtracting satoshisToSend from totalInputUtxoValue | // for unit test purposes, calculate fee by subtracting satoshisToSend from totalInputUtxoValue | ||||
// no change output to be subtracted in this tx | // no change output to be subtracted in this tx | ||||
const txFee = new BigNumber(totalInputUtxoValue).minus(satoshisToSend); | const txFee = new BigNumber(totalInputUtxoValue).minus(satoshisToSend); | ||||
const destinationAddressAndValueArray = null; | const destinationAddressAndValueArray = null; | ||||
let txBuilder = new TransactionBuilder(); | let txBuilder = new TransactionBuilder(); | ||||
const changeAddress = wallet.Path1899.cashAddress; | const changeAddress = wallet.Path1899.cashAddress; | ||||
let thrownError; | let thrownError; | ||||
try { | try { | ||||
generateTxOutput( | generateTxOutput( | ||||
BCH, | |||||
isOneToMany, | isOneToMany, | ||||
singleSendValue, | singleSendValue, | ||||
satoshisToSend, | satoshisToSend, | ||||
totalInputUtxoValue, | totalInputUtxoValue, | ||||
null, | null, | ||||
destinationAddressAndValueArray, | destinationAddressAndValueArray, | ||||
changeAddress, | changeAddress, | ||||
txFee, | txFee, | ||||
txBuilder, | txBuilder, | ||||
); | ); | ||||
} catch (err) { | } catch (err) { | ||||
thrownError = err; | thrownError = err; | ||||
} | } | ||||
expect(thrownError.message).toStrictEqual('Invalid tx input parameter'); | expect(thrownError.message).toStrictEqual('Invalid tx input parameter'); | ||||
}); | }); | ||||
it(`generateTxOutput() throws an error on invalid input params for a one to many XEC tx`, () => { | it(`generateTxOutput() throws an error on invalid input params for a one to many XEC tx`, () => { | ||||
// txbuilder output params | // txbuilder output params | ||||
const BCH = new BCHJS(); | |||||
const { wallet } = sendBCHMock; | const { wallet } = sendBCHMock; | ||||
const isOneToMany = true; | const isOneToMany = true; | ||||
const singleSendValue = null; | const singleSendValue = null; | ||||
const totalInputUtxoValue = | const totalInputUtxoValue = | ||||
mockOneToManySendXecTxBuilderObj.transaction.inputs[0].value + | mockOneToManySendXecTxBuilderObj.transaction.inputs[0].value + | ||||
mockOneToManySendXecTxBuilderObj.transaction.inputs[1].value + | mockOneToManySendXecTxBuilderObj.transaction.inputs[1].value + | ||||
mockOneToManySendXecTxBuilderObj.transaction.inputs[2].value; | mockOneToManySendXecTxBuilderObj.transaction.inputs[2].value; | ||||
const satoshisToSend = new BigNumber( | const satoshisToSend = new BigNumber( | ||||
Show All 11 Lines | const txFee = new BigNumber(totalInputUtxoValue) | ||||
); // change value | ); // change value | ||||
const destinationAddressAndValueArray = null; // invalid as this is mandatory when isOneToMany is true | const destinationAddressAndValueArray = null; // invalid as this is mandatory when isOneToMany is true | ||||
let txBuilder = new TransactionBuilder(); | let txBuilder = new TransactionBuilder(); | ||||
const changeAddress = wallet.Path1899.cashAddress; | const changeAddress = wallet.Path1899.cashAddress; | ||||
let thrownError; | let thrownError; | ||||
try { | try { | ||||
generateTxOutput( | generateTxOutput( | ||||
BCH, | |||||
isOneToMany, | isOneToMany, | ||||
singleSendValue, | singleSendValue, | ||||
satoshisToSend, | satoshisToSend, | ||||
totalInputUtxoValue, | totalInputUtxoValue, | ||||
null, | null, | ||||
destinationAddressAndValueArray, | destinationAddressAndValueArray, | ||||
changeAddress, | changeAddress, | ||||
txFee, | txFee, | ||||
▲ Show 20 Lines • Show All 606 Lines • Show Last 20 Lines |