2021-04-15 20:01:46 +02:00
|
|
|
import { rawEncode } from 'ethereumjs-abi';
|
|
|
|
|
|
|
|
import {
|
|
|
|
multiplyCurrencies,
|
|
|
|
addCurrencies,
|
|
|
|
conversionGTE,
|
|
|
|
conversionUtil,
|
2021-07-06 19:48:49 +02:00
|
|
|
} from '../../../shared/modules/conversion.utils';
|
2021-04-15 20:01:46 +02:00
|
|
|
|
|
|
|
import {
|
|
|
|
calcGasTotal,
|
2022-01-10 17:23:53 +01:00
|
|
|
generateERC20TransferData,
|
2021-04-15 20:01:46 +02:00
|
|
|
isBalanceSufficient,
|
|
|
|
isTokenBalanceSufficient,
|
|
|
|
} from './send.utils';
|
|
|
|
|
2021-07-06 19:48:49 +02:00
|
|
|
jest.mock('../../../shared/modules/conversion.utils', () => ({
|
2021-04-15 20:01:46 +02:00
|
|
|
addCurrencies: jest.fn((a, b) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
let [a1, b1] = [a, b];
|
2020-08-14 13:48:42 +02:00
|
|
|
if (String(a).match(/^0x.+/u)) {
|
2021-02-04 19:15:23 +01:00
|
|
|
a1 = Number(String(a).slice(2));
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2020-08-14 13:48:42 +02:00
|
|
|
if (String(b).match(/^0x.+/u)) {
|
2021-02-04 19:15:23 +01:00
|
|
|
b1 = Number(String(b).slice(2));
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
return a1 + b1;
|
2018-06-29 19:19:40 +02:00
|
|
|
}),
|
2021-04-15 20:01:46 +02:00
|
|
|
conversionUtil: jest.fn((val) => parseInt(val, 16)),
|
|
|
|
conversionGTE: jest.fn((obj1, obj2) => obj1.value >= obj2.value),
|
|
|
|
multiplyCurrencies: jest.fn((a, b) => `${a}x${b}`),
|
|
|
|
conversionGreaterThan: (obj1, obj2) => obj1.value > obj2.value,
|
|
|
|
conversionLessThan: (obj1, obj2) => obj1.value < obj2.value,
|
|
|
|
}));
|
|
|
|
|
2021-04-28 21:53:59 +02:00
|
|
|
jest.mock('../../helpers/utils/token-util', () => ({
|
2021-04-15 20:01:46 +02:00
|
|
|
calcTokenAmount: (a, d) => `calc:${a}${d}`,
|
|
|
|
}));
|
|
|
|
|
|
|
|
jest.mock('ethereumjs-abi', () => ({
|
|
|
|
rawEncode: jest.fn().mockReturnValue(16, 1100),
|
|
|
|
}));
|
|
|
|
|
|
|
|
describe('send utils', () => {
|
|
|
|
describe('calcGasTotal()', () => {
|
|
|
|
it('should call multiplyCurrencies with the correct params and return the multiplyCurrencies return', () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const result = calcGasTotal(12, 15);
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(result).toStrictEqual('12x15');
|
|
|
|
expect(multiplyCurrencies).toHaveBeenCalledWith(12, 15, {
|
|
|
|
multiplicandBase: 16,
|
|
|
|
multiplierBase: 16,
|
|
|
|
toNumericBase: 'hex',
|
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2018-05-05 17:11:53 +02:00
|
|
|
|
2022-01-10 17:23:53 +01:00
|
|
|
describe('generateERC20TransferData()', () => {
|
2021-04-15 20:01:46 +02:00
|
|
|
it('should return undefined if not passed a send token', () => {
|
|
|
|
expect(
|
2022-01-10 17:23:53 +01:00
|
|
|
generateERC20TransferData({
|
2020-11-03 00:41:28 +01:00
|
|
|
toAddress: 'mockAddress',
|
|
|
|
amount: '0xa',
|
|
|
|
sendToken: undefined,
|
|
|
|
}),
|
2021-04-15 20:01:46 +02:00
|
|
|
).toBeUndefined();
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-06-02 06:23:01 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('should call abi.rawEncode with the correct params', () => {
|
2022-01-10 17:23:53 +01:00
|
|
|
generateERC20TransferData({
|
2020-11-03 00:41:28 +01:00
|
|
|
toAddress: 'mockAddress',
|
|
|
|
amount: 'ab',
|
|
|
|
sendToken: { address: '0x0' },
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(rawEncode.mock.calls[0].toString()).toStrictEqual(
|
|
|
|
[
|
|
|
|
['address', 'uint256'],
|
2022-06-28 18:51:09 +02:00
|
|
|
['0xmockAddress', '0xab'],
|
2021-04-15 20:01:46 +02:00
|
|
|
].toString(),
|
|
|
|
);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-05-05 17:11:53 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('should return encoded token transfer data', () => {
|
|
|
|
expect(
|
2022-01-10 17:23:53 +01:00
|
|
|
generateERC20TransferData({
|
2020-11-03 00:41:28 +01:00
|
|
|
toAddress: 'mockAddress',
|
|
|
|
amount: '0xa',
|
|
|
|
sendToken: { address: '0x0' },
|
|
|
|
}),
|
2021-04-15 20:01:46 +02:00
|
|
|
).toStrictEqual('0xa9059cbb');
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2018-05-05 17:11:53 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
describe('isBalanceSufficient()', () => {
|
|
|
|
it('should correctly call addCurrencies and return the result of calling conversionGTE', () => {
|
2018-05-05 17:11:53 +02:00
|
|
|
const result = isBalanceSufficient({
|
|
|
|
amount: 15,
|
|
|
|
balance: 100,
|
|
|
|
conversionRate: 3,
|
|
|
|
gasTotal: 17,
|
|
|
|
primaryCurrency: 'ABC',
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(addCurrencies).toHaveBeenCalledWith(15, 17, {
|
|
|
|
aBase: 16,
|
|
|
|
bBase: 16,
|
|
|
|
toNumericBase: 'hex',
|
|
|
|
});
|
|
|
|
expect(conversionGTE).toHaveBeenCalledWith(
|
2020-11-03 00:41:28 +01:00
|
|
|
{
|
|
|
|
value: 100,
|
|
|
|
fromNumericBase: 'hex',
|
|
|
|
fromCurrency: 'ABC',
|
|
|
|
conversionRate: 3,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: 32,
|
|
|
|
fromNumericBase: 'hex',
|
|
|
|
conversionRate: 3,
|
|
|
|
fromCurrency: 'ABC',
|
|
|
|
},
|
2021-04-15 20:01:46 +02:00
|
|
|
);
|
2018-05-05 17:11:53 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(result).toStrictEqual(true);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2018-05-05 17:11:53 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
describe('isTokenBalanceSufficient()', () => {
|
|
|
|
it('should correctly call conversionUtil and return the result of calling conversionGTE', () => {
|
2018-05-05 17:11:53 +02:00
|
|
|
const result = isTokenBalanceSufficient({
|
|
|
|
amount: '0x10',
|
|
|
|
tokenBalance: 123,
|
|
|
|
decimals: 10,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2021-04-15 20:01:46 +02:00
|
|
|
|
|
|
|
expect(conversionUtil).toHaveBeenCalledWith('0x10', {
|
|
|
|
fromNumericBase: 'hex',
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(conversionGTE).toHaveBeenCalledWith(
|
2020-11-03 00:41:28 +01:00
|
|
|
{
|
|
|
|
value: 123,
|
|
|
|
fromNumericBase: 'hex',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: 'calc:1610',
|
|
|
|
},
|
2021-04-15 20:01:46 +02:00
|
|
|
);
|
2018-05-05 17:11:53 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(result).toStrictEqual(false);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|