2021-04-15 20:01:46 +02:00
|
|
|
import { rawEncode } from 'ethereumjs-abi';
|
|
|
|
|
|
|
|
import {
|
2022-01-10 17:23:53 +01:00
|
|
|
generateERC20TransferData,
|
2021-04-15 20:01:46 +02:00
|
|
|
isBalanceSufficient,
|
|
|
|
isTokenBalanceSufficient,
|
2023-01-13 20:28:52 +01:00
|
|
|
ellipsify,
|
2021-04-15 20:01:46 +02:00
|
|
|
} from './send.utils';
|
|
|
|
|
|
|
|
jest.mock('ethereumjs-abi', () => ({
|
|
|
|
rawEncode: jest.fn().mockReturnValue(16, 1100),
|
|
|
|
}));
|
|
|
|
|
|
|
|
describe('send utils', () => {
|
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()', () => {
|
2023-01-24 15:44:49 +01:00
|
|
|
it('should correctly sum the appropriate currencies and ensure that balance is greater', () => {
|
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(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()', () => {
|
2023-01-24 15:44:49 +01:00
|
|
|
it('should return true for a sufficient balance for token spend', () => {
|
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
|
|
|
|
2023-01-24 15:44:49 +01:00
|
|
|
expect(result).toStrictEqual(true);
|
|
|
|
});
|
2021-04-15 20:01:46 +02:00
|
|
|
|
2023-01-24 15:44:49 +01:00
|
|
|
it('should return false for an insufficient balance for token spend', () => {
|
|
|
|
const result = isTokenBalanceSufficient({
|
|
|
|
amount: '0x10000',
|
|
|
|
tokenBalance: 123,
|
|
|
|
decimals: 10,
|
|
|
|
});
|
2018-05-05 17:11:53 +02:00
|
|
|
|
2023-01-24 15:44:49 +01:00
|
|
|
expect(result).toStrictEqual(true);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2023-01-13 20:28:52 +01:00
|
|
|
|
|
|
|
describe('ellipsify()', () => {
|
|
|
|
it('should ellipsify a contract address', () => {
|
|
|
|
expect(
|
|
|
|
ellipsify('0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC'),
|
|
|
|
).toStrictEqual('0xCcCC...cccC');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return an empty string if the passed text is not defined', () => {
|
|
|
|
expect(ellipsify(undefined)).toStrictEqual('');
|
|
|
|
});
|
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|