Skip to content
This repository was archived by the owner on May 24, 2022. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/fether-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"start": "npm-run-all -p start-*",
"start-css": "npm run build-css -- --watch --recursive",
"start-js": "cross-env SKIP_PREFLIGHT_CHECK=true BROWSER=none craco start --react-scripts ../../node_modules/react-scripts",
"test": "ln -s ../../../node_modules/react-scripts node_modules/ && cross-env SKIP_PREFLIGHT_CHECK=true craco test; rm node_modules/react-scripts"
"test": "ln -s ../../../node_modules/react-scripts node_modules/ && cross-env SKIP_PREFLIGHT_CHECK=true craco test; EXIT_CODE=$?; rm node_modules/react-scripts; exit $EXIT_CODE"
},
"dependencies": {
"@craco/craco": "^3.3.1",
Expand Down Expand Up @@ -83,4 +83,4 @@
"not ie <= 11",
"not op_mini all"
]
}
}
32 changes: 21 additions & 11 deletions packages/fether-react/src/stores/sendStore.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,14 @@ describe('method acceptRequest', () => {
});

describe('method clear', () => {
test('should clear tx', () => {
sendStore.setTx(mock.tx);
test('should clear txEth', () => {
sendStore.setTx(mock.txEth);
sendStore.clear();
expect(sendStore.tx).toEqual({});
});

test('should clear txErc20', () => {
sendStore.setTx(mock.txErc20);
sendStore.clear();
expect(sendStore.tx).toEqual({});
});
Expand Down Expand Up @@ -82,12 +88,10 @@ describe('@computed confirmations', () => {
});

describe('method send', () => {
beforeEach(() => {
sendStore.setTx(mock.tx);
});

test('should call transfer$ if the token is Erc20', () => {
sendStore.send(mock.erc20);
sendStore.setTx(mock.txErc20);
sendStore.send('password');
expect(mock.txErc20.token.address).toEqual('THIBCoin');
expect(mock.makeContract.transfer$).toHaveBeenCalledWith(
'0x123',
new BigNumber('10000000000000000'),
Expand All @@ -96,7 +100,9 @@ describe('method send', () => {
});

test('should call post$ if the token is ETH', () => {
sendStore.send(mock.eth);
sendStore.setTx(mock.txEth);
sendStore.send('password');
expect(mock.txEth.token.address).toEqual('ETH');
expect(lightJs.post$).toHaveBeenCalledWith({
from: '0x456',
gasPrice: new BigNumber('4000000000'),
Expand All @@ -107,14 +113,18 @@ describe('method send', () => {

test('should update txStatus', () => {
sendStore.setTxStatus = jest.fn();
sendStore.send(mock.eth);
sendStore.setTx(mock.txEth);
sendStore.send('password');
expect(mock.txEth.token.address).toEqual('ETH');
expect(sendStore.setTxStatus).toHaveBeenCalledWith({ estimating: true });
});

test('should call acceptRequest when txStatus is requested', () => {
sendStore.acceptRequest = jest.fn(() => Promise.resolve(true));
sendStore.send(mock.eth, 'foo');
expect(sendStore.acceptRequest).toHaveBeenCalledWith(1, 'foo');
sendStore.setTx(mock.txEth);
sendStore.send('password');
expect(mock.txEth.token.address).toEqual('ETH');
expect(sendStore.acceptRequest).toHaveBeenCalledWith(1, 'password');
});
});

Expand Down
53 changes: 43 additions & 10 deletions packages/fether-react/src/utils/testHelpers/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,24 @@
/* eslint-env jest */

import BigNumber from 'bignumber.js';
import { toWei } from '@parity/api/lib/util/wei';

const SECRET_PHRASE = 'foo';
const ADDRESS_FROM = '0x456';
const ADDRESS_TO = '0x123';
const GAS_PRICE = 4; // in Gwei
const GAS_ESTIMATE = 456;
const GAS_ESTIMATE_CONTRACT_TX = 123;
const AMOUNT = 0.01;

export const api = {
eth: {
estimateGas: jest.fn(() => Promise.resolve(new BigNumber(456)))
estimateGas: jest.fn(() => Promise.resolve(new BigNumber(GAS_ESTIMATE)))
},
parity: {
generateSecretPhrase: jest.fn(() => Promise.resolve('foo')),
generateSecretPhrase: jest.fn(() => Promise.resolve(SECRET_PHRASE)),
newAccountFromPhrase: jest.fn(() => Promise.resolve()),
phraseToAddress: jest.fn(() => Promise.resolve('0x123')),
phraseToAddress: jest.fn(() => Promise.resolve(ADDRESS_TO)),
setAccountName: jest.fn(() => Promise.resolve()),
setAccountMeta: jest.fn(() => Promise.resolve())
},
Expand All @@ -24,7 +33,7 @@ export const api = {
};

export const erc20 = {
address: 'foo',
address: 'THIBCoin',
decimals: 18
};

Expand All @@ -36,7 +45,8 @@ export const makeContract = {
contractObject: {
instance: {
transfer: {
estimateGas: () => Promise.resolve(new BigNumber(123))
estimateGas: () =>
Promise.resolve(new BigNumber(GAS_ESTIMATE_CONTRACT_TX))
}
}
},
Expand All @@ -50,9 +60,32 @@ export const post$ = {
})
};

export const tx = {
amount: 0.01, // In Ether or in token
from: '0x456',
gasPrice: 4, // in Gwei
to: '0x123'
export const txEth = {
amount: AMOUNT, // In Ether
from: ADDRESS_FROM,
gasPrice: GAS_PRICE,
to: ADDRESS_TO,
token: eth
};

const txErc20Base = {
amount: AMOUNT, // In token
from: ADDRESS_FROM,
gasPrice: GAS_PRICE,
to: ADDRESS_TO,
token: erc20
};

export const txErc20 = {
...txErc20Base,
args: [
txErc20Base.to,
new BigNumber(txErc20Base.amount).multipliedBy(
new BigNumber(10).pow(txErc20Base.token.decimals)
)
],
options: {
from: txErc20Base.from,
gasPrice: toWei(txErc20Base.gasPrice, 'shannon') // shannon == gwei
}
};
24 changes: 13 additions & 11 deletions packages/fether-react/src/utils/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,19 @@ const getEthereumTx = tx => {
txParams.to = token.address;
txParams.data =
'0x' +
new Abi(eip20).functions.find(f => f._name === 'transfer').encodeCall([
new Token('address', to),
new Token(
'uint',
'0x' +
new BigNumber(amount)
.multipliedBy(new BigNumber(10).pow(token.decimals))
.toNumber()
.toString(16)
)
]);
new Abi(eip20).functions
.find(f => f._name === 'transfer')
.encodeCall([
new Token('address', to),
new Token(
'uint',
'0x' +
new BigNumber(amount)
.multipliedBy(new BigNumber(10).pow(token.decimals))
.toNumber()
.toString(16)
)
]);
}

return new EthereumTx(txParams);
Expand Down
6 changes: 3 additions & 3 deletions packages/fether-react/src/utils/transaction.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ describe('estimateGas', () => {
});

test('should call estimateGasForErc20 with token', () => {
expect(estimateGas(mock.tx, mock.erc20, mock.api)).resolves.toEqual(
new BigNumber(153.75)
expect(estimateGas(mock.txErc20, mock.erc20, mock.api)).resolves.toEqual(
new BigNumber(154)
);
});

test('should call estimateGasForEth with token', () => {
expect(estimateGas(mock.tx, mock.eth, mock.api)).resolves.toEqual(
expect(estimateGas(mock.txEth, mock.eth, mock.api)).resolves.toEqual(
new BigNumber(570)
);
});
Expand Down