This repository has been archived by the owner on Oct 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #687 from lucachaco/refactor-confirm-test
- Loading branch information
Showing
3 changed files
with
133 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { buildTransaction } from '../src/services/transactionServices'; | ||
import BigNumber from "bignumber.js"; | ||
import common from './../src/common/common'; | ||
|
||
const legacyBtcInput={ | ||
amount: "0.001", | ||
feeParams: {fees: "0x25b7"}, | ||
isRequestSendAll: false, | ||
memo: null, | ||
toAddress: "mtSwjbJh1S6tih3L4PhC5JRzQ1mGd2mqx8", | ||
coin:{ | ||
account: "0", | ||
address: "myD8QmHBNa4zugWqEvY118XSciNvsdpkrU", | ||
addressType: "legacy", | ||
chain: "Bitcoin", | ||
coinType: 1, | ||
id: "BTCTestnet", | ||
networkId: 1, | ||
objectId: "Q4dJZA9EVX", | ||
path: "m/44'/1'/0'/0/0", | ||
precision: 18, | ||
privateKey: "707dd7a0645d37725f28a63d764bc91a34ca263a077230ba56209dddd3bc881f", | ||
symbol: "BTC", | ||
type: "Testnet" | ||
} | ||
} | ||
|
||
|
||
|
||
const balance = new BigNumber(2300); | ||
const gas = new BigNumber(2300); | ||
const segwitRbtcSendAllInput={ | ||
amount: "0.046984", | ||
feeParams: { gasPrice: "118480000", gas}, | ||
isRequestSendAll: true, | ||
memo: null, | ||
toAddress: "0xe4CAE969f26E093874728272dcFED1074f4778F5", | ||
coin:{ | ||
account: "0", | ||
address: "0x2FA4a8A4cFF02Efa4368a1e8c3301C5342D3b879", | ||
balance:balance, | ||
chain: "Rootstock", | ||
coinType: 37310, | ||
contractAddress: undefined, | ||
id: "RBTCTestnet", | ||
metadata: {networkId: 31, coinType: 37310, icon: 10, defaultName: "Smart Bitcoin", chain: "Rootstock"}, | ||
name: "Smart Bitcoin", | ||
networkId: 31, | ||
objectId: "gOrOxEcFeQ", | ||
path: "m/44'/37310'/0'/0/0", | ||
precision: 18, | ||
privateKey: "b265c01217804948d490d17b7383d61daf1991f87f1f5ae87b3ad0f84bf967e0", | ||
symbol: "RBTC", | ||
type: "Testnet" | ||
} | ||
} | ||
|
||
|
||
describe('Transaction Services', () => { | ||
it('should build a transaction with a legacy BTC address', async () => { | ||
const builtTransaction = await buildTransaction(legacyBtcInput); | ||
expect(builtTransaction).toHaveProperty('receiver'); | ||
expect(builtTransaction.receiver).toBe(legacyBtcInput.toAddress); | ||
expect(builtTransaction).toHaveProperty('sender'); | ||
expect(builtTransaction.sender).toBe(legacyBtcInput.coin.address); | ||
expect(builtTransaction).toHaveProperty('symbol'); | ||
expect(builtTransaction.symbol).toBe(legacyBtcInput.coin.symbol); | ||
expect(builtTransaction).toHaveProperty('value'); | ||
expect(builtTransaction.value).toBe(common.convertCoinAmountToUnitHex(legacyBtcInput.coin.symbol,legacyBtcInput.amount,legacyBtcInput.coin.precision)); | ||
}); | ||
|
||
it('should build a transaction with a segwit RBTC address when sending all', async () => { | ||
const builtTransaction = await buildTransaction(segwitRbtcSendAllInput); | ||
expect(builtTransaction).toHaveProperty('receiver'); | ||
expect(builtTransaction.receiver).toBe(segwitRbtcSendAllInput.toAddress.toLowerCase()); | ||
expect(builtTransaction).toHaveProperty('sender'); | ||
expect(builtTransaction.sender).toBe(segwitRbtcSendAllInput.coin.address); | ||
expect(builtTransaction).toHaveProperty('symbol'); | ||
expect(builtTransaction.symbol).toBe(segwitRbtcSendAllInput.coin.symbol); | ||
expect(builtTransaction).toHaveProperty('value'); | ||
const value = segwitRbtcSendAllInput.coin.balance.minus(common.convertUnitToCoinAmount(segwitRbtcSendAllInput.coin.symbol, segwitRbtcSendAllInput.feeParams.gas.times(segwitRbtcSendAllInput.feeParams.gasPrice), segwitRbtcSendAllInput.coin.precision)); | ||
expect(builtTransaction.value).toBe(common.convertCoinAmountToUnitHex(segwitRbtcSendAllInput.coin.symbol,value,segwitRbtcSendAllInput.coin.precision)); | ||
}); | ||
|
||
}); |
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,41 @@ | ||
import BigNumber from 'bignumber.js'; | ||
import common from '../common/common'; | ||
import Transaction from '../common/transaction'; | ||
|
||
const buildTransaction = async ({ | ||
memo, amount, coin, feeParams, toAddress, isRequestSendAll, | ||
}) => { | ||
const { | ||
symbol, balance, precision, | ||
} = coin; | ||
const extraParams = { data: '', memo, gasFee: feeParams }; | ||
let finalToAddress = toAddress; | ||
// In order to send all all balances, we cannot use the amount in the text box to calculate the amount sent, but use the coin balance. | ||
// The amount of the text box is fixed decimal places | ||
let value = new BigNumber(amount); | ||
if (isRequestSendAll) { | ||
if (symbol === 'BTC') { | ||
value = balance.minus(common.convertUnitToCoinAmount(symbol, feeParams.fees, precision)); | ||
} else if (symbol === 'RBTC') { | ||
finalToAddress = finalToAddress.toLowerCase(); | ||
value = balance.minus(common.convertUnitToCoinAmount(symbol, feeParams.gas.times(feeParams.gasPrice), precision)); | ||
} else { | ||
finalToAddress = finalToAddress.toLowerCase(); | ||
value = balance; | ||
} | ||
} | ||
|
||
return new Transaction(coin, finalToAddress, value, extraParams); | ||
}; | ||
|
||
const broadcastTransaction = async ({ | ||
memo, amount, coin, feeParams, toAddress, isRequestSendAll, | ||
}) => { | ||
const transaction = await buildTransaction({ | ||
memo, amount, coin, feeParams, toAddress, isRequestSendAll, | ||
}); | ||
await transaction.broadcast(); | ||
return transaction; | ||
}; | ||
|
||
export { buildTransaction, broadcastTransaction }; |