Skip to content
This repository has been archived by the owner on Oct 12, 2023. It is now read-only.

Commit

Permalink
Merge pull request #687 from lucachaco/refactor-confirm-test
Browse files Browse the repository at this point in the history
  • Loading branch information
patogallaiovlabs authored Jun 8, 2021
2 parents 1b60a01 + cc168db commit c31a7a5
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 21 deletions.
85 changes: 85 additions & 0 deletions __test__/transactionServices.test.js
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));
});

});
28 changes: 7 additions & 21 deletions src/pages/wallet/transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import {
import { createErrorConfirmation } from '../../common/confirmation.controller';
import appActions from '../../redux/app/actions';
import walletActions from '../../redux/wallet/actions';
import Transaction, { btcTransaction, rbtcTransaction } from '../../common/transaction';
import { btcTransaction, rbtcTransaction } from '../../common/transaction';
import { broadcastTransaction } from '../../services/transactionServices';
import common from '../../common/common';
import { strings } from '../../common/i18n';
import Button from '../../components/common/button/button';
Expand Down Expand Up @@ -914,38 +915,23 @@ class Transfer extends Component {
const { navigation, addNotification, getBalance } = this.props;
const { coin, isRequestSendAll } = this;
const {
symbol, type, address, balance, precision,
symbol, type, address,
} = coin;
const { memo, amount } = this.state;
try {
this.setState({ loading: true });
const feeParams = this.getFeeParams();
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;
}
}

let transaction = new Transaction(coin, finalToAddress, value, extraParams);
await transaction.broadcast();
const transaction = await broadcastTransaction({
memo, amount, coin, feeParams, toAddress, isRequestSendAll,
});

this.setState({ loading: false });
const completedParams = {
coin,
hash: transaction.txHash,
};
navigation.navigate('TransferCompleted', completedParams);
transaction = null;
} catch (error) {
this.setState({ loading: false });
console.log(`confirm, error: ${error.message}`);
Expand Down
41 changes: 41 additions & 0 deletions src/services/transactionServices.js
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 };

0 comments on commit c31a7a5

Please sign in to comment.