2021-04-09 19:20:32 +02:00
|
|
|
import nock from 'nock';
|
2021-07-09 17:24:00 +02:00
|
|
|
import { MOCKS } from '../../../test/jest';
|
2021-04-23 16:53:10 +02:00
|
|
|
import {
|
|
|
|
MAINNET_CHAIN_ID,
|
|
|
|
BSC_CHAIN_ID,
|
2021-07-09 17:24:00 +02:00
|
|
|
POLYGON_CHAIN_ID,
|
2021-04-23 16:53:10 +02:00
|
|
|
LOCALHOST_CHAIN_ID,
|
2021-07-09 17:24:00 +02:00
|
|
|
RINKEBY_CHAIN_ID,
|
2021-07-30 13:35:30 +02:00
|
|
|
KOVAN_CHAIN_ID,
|
2022-02-18 15:24:47 +01:00
|
|
|
AVALANCHE_CHAIN_ID,
|
2022-06-08 16:32:04 +02:00
|
|
|
ETH_SYMBOL,
|
2021-04-28 21:53:59 +02:00
|
|
|
} from '../../../shared/constants/network';
|
2021-04-23 16:53:10 +02:00
|
|
|
import {
|
|
|
|
SWAPS_CHAINID_CONTRACT_ADDRESS_MAP,
|
2021-09-08 20:08:55 +02:00
|
|
|
SWAPS_CHAINID_DEFAULT_TOKEN_MAP,
|
|
|
|
WETH_CONTRACT_ADDRESS,
|
|
|
|
WBNB_CONTRACT_ADDRESS,
|
|
|
|
WMATIC_CONTRACT_ADDRESS,
|
2021-07-09 17:24:00 +02:00
|
|
|
ETHEREUM,
|
|
|
|
POLYGON,
|
|
|
|
BSC,
|
2021-07-30 13:35:30 +02:00
|
|
|
RINKEBY,
|
2022-02-18 15:24:47 +01:00
|
|
|
AVALANCHE,
|
2021-04-28 21:53:59 +02:00
|
|
|
} from '../../../shared/constants/swaps';
|
2020-10-06 20:28:38 +02:00
|
|
|
import {
|
|
|
|
TOKENS,
|
2021-03-15 17:51:43 +01:00
|
|
|
EXPECTED_TOKENS_RESULT,
|
2020-10-06 20:28:38 +02:00
|
|
|
MOCK_TRADE_RESPONSE_2,
|
|
|
|
AGGREGATOR_METADATA,
|
|
|
|
TOP_ASSETS,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from './swaps-util-test-constants';
|
2021-04-09 19:20:32 +02:00
|
|
|
import {
|
2020-11-03 00:41:28 +01:00
|
|
|
fetchTradesInfo,
|
|
|
|
fetchTokens,
|
|
|
|
fetchAggregatorMetadata,
|
|
|
|
fetchTopAssets,
|
2021-04-23 16:53:10 +02:00
|
|
|
isContractAddressValid,
|
2021-07-09 17:24:00 +02:00
|
|
|
getNetworkNameByChainId,
|
|
|
|
getSwapsLivenessForNetwork,
|
2021-07-22 19:29:31 +02:00
|
|
|
countDecimals,
|
2021-09-08 20:08:55 +02:00
|
|
|
shouldEnableDirectWrapping,
|
2022-02-18 17:48:38 +01:00
|
|
|
showRemainingTimeInMinAndSec,
|
2022-06-08 16:32:04 +02:00
|
|
|
getSwapsTokensReceivedFromTxMeta,
|
2021-04-09 19:20:32 +02:00
|
|
|
} from './swaps.util';
|
|
|
|
|
2021-04-28 21:53:59 +02:00
|
|
|
jest.mock('../../helpers/utils/storage-helpers.js', () => ({
|
2021-04-09 19:20:32 +02:00
|
|
|
getStorageItem: jest.fn(),
|
|
|
|
setStorageItem: jest.fn(),
|
|
|
|
}));
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-04-09 19:20:32 +02:00
|
|
|
describe('Swaps Util', () => {
|
2021-04-15 20:01:46 +02:00
|
|
|
afterAll(() => {
|
2021-04-09 19:20:32 +02:00
|
|
|
nock.cleanAll();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('fetchTradesInfo', () => {
|
2020-10-06 20:28:38 +02:00
|
|
|
const expectedResult1 = {
|
2020-11-03 00:41:28 +01:00
|
|
|
zeroEx: {
|
|
|
|
trade: {
|
|
|
|
// the ethereum transaction data for the swap
|
|
|
|
data:
|
|
|
|
'0xa6c3bf330000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000002386f26fc1000000000000000000000000000000000000000000000000000000000000000004e0000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000005591360f8c7640fea5771c9682d6b5ecb776e1f8000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000021486a000000000000000000000000000000000000000000000000002386f26fc1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005efe3c3b5dfc3a75ffc8add04bbdbac1e42fa234bf4549d8dab1bc44c8056eaf0e1dfe8600000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000003c00000000000000000000000000000000000000000000000000000000000000420000000000000000000000000000000000000000000000000000000000000042000000000000000000000000000000000000000000000000000000000000001c4dc1600f3000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000005591360f8c7640fea5771c9682d6b5ecb776e1f800000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000140000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000036691c4f426eb8f42f150ebde43069a31cb080ad000000000000000000000000000000000000000000000000002386f26fc10000000000000000000000000000000000000000000000000000000000000021486a00000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000020000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000024f47261b0000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000010400000000000000000000000000000000000000000000000000000000000000869584cd0000000000000000000000001000000000000000000000000000000000000011000000000000000000000000000000000000000000000000000000005efe201b',
|
|
|
|
from: '0x2369267687A84ac7B494daE2f1542C40E37f4455',
|
|
|
|
value: '0x14401eab384000',
|
|
|
|
to: '0x61935cbdd02287b511119ddb11aeb42f1593b7ef',
|
|
|
|
gas: '0xa',
|
|
|
|
gasPrice: undefined,
|
2020-10-06 20:28:38 +02:00
|
|
|
},
|
2020-11-03 00:41:28 +01:00
|
|
|
sourceAmount: '10000000000000000',
|
|
|
|
destinationAmount: '2248687',
|
|
|
|
error: null,
|
2021-04-07 02:19:55 +02:00
|
|
|
fee: 0.875,
|
2020-11-03 00:41:28 +01:00
|
|
|
sourceToken: TOKENS[0].address,
|
|
|
|
destinationToken: TOKENS[1].address,
|
|
|
|
fetchTime: 553,
|
|
|
|
aggregator: 'zeroEx',
|
|
|
|
aggType: 'AGG',
|
|
|
|
approvalNeeded: {
|
|
|
|
data:
|
|
|
|
'0x095ea7b300000000000000000000000095e6f48254609a6ee006f7d493c8e5fb97094cef0000000000000000000000000000000000000000004a817c7ffffffdabf41c00',
|
|
|
|
to: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
|
|
|
|
value: '0x0',
|
|
|
|
from: '0x2369267687A84ac7B494daE2f1542C40E37f4455',
|
|
|
|
gas: '0x12',
|
|
|
|
gasPrice: '0x34',
|
2020-10-06 20:28:38 +02:00
|
|
|
},
|
2020-11-03 00:41:28 +01:00
|
|
|
maxGas: 10,
|
|
|
|
averageGas: 1,
|
|
|
|
slippage: '3',
|
2020-10-06 20:28:38 +02:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-10-06 20:28:38 +02:00
|
|
|
const expectedResult2 = {
|
2020-11-03 00:41:28 +01:00
|
|
|
zeroEx: {
|
2020-10-06 20:28:38 +02:00
|
|
|
...expectedResult1.zeroEx,
|
|
|
|
sourceAmount: '20000000000000000',
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2021-04-09 19:20:32 +02:00
|
|
|
it('should fetch trade info on prod', async () => {
|
2022-03-21 10:26:52 +01:00
|
|
|
nock('https://swap.metaswap.codefi.network')
|
2021-11-30 19:56:28 +01:00
|
|
|
.get('/networks/1/trades')
|
2021-04-09 19:20:32 +02:00
|
|
|
.query(true)
|
|
|
|
.reply(200, MOCK_TRADE_RESPONSE_2);
|
|
|
|
|
2021-03-18 11:20:06 +01:00
|
|
|
const result = await fetchTradesInfo(
|
|
|
|
{
|
|
|
|
TOKENS,
|
|
|
|
slippage: '3',
|
|
|
|
sourceToken: TOKENS[0].address,
|
|
|
|
destinationToken: TOKENS[1].address,
|
|
|
|
value: '2000000000000000000',
|
|
|
|
fromAddress: '0xmockAddress',
|
|
|
|
sourceSymbol: TOKENS[0].symbol,
|
|
|
|
sourceDecimals: TOKENS[0].decimals,
|
|
|
|
sourceTokenInfo: { ...TOKENS[0] },
|
|
|
|
destinationTokenInfo: { ...TOKENS[1] },
|
|
|
|
},
|
|
|
|
{ chainId: MAINNET_CHAIN_ID },
|
|
|
|
);
|
2021-04-09 19:20:32 +02:00
|
|
|
expect(result).toStrictEqual(expectedResult2);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-04-09 19:20:32 +02:00
|
|
|
describe('fetchTokens', () => {
|
2021-04-15 20:01:46 +02:00
|
|
|
beforeAll(() => {
|
2022-03-21 10:26:52 +01:00
|
|
|
nock('https://swap.metaswap.codefi.network')
|
2021-04-15 20:01:46 +02:00
|
|
|
.persist()
|
2021-11-30 19:56:28 +01:00
|
|
|
.get('/networks/1/tokens')
|
2021-04-09 19:20:32 +02:00
|
|
|
.reply(200, TOKENS);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fetch tokens', async () => {
|
2021-03-18 11:20:06 +01:00
|
|
|
const result = await fetchTokens(MAINNET_CHAIN_ID);
|
2021-04-09 19:20:32 +02:00
|
|
|
expect(result).toStrictEqual(EXPECTED_TOKENS_RESULT);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-04-09 19:20:32 +02:00
|
|
|
it('should fetch tokens on prod', async () => {
|
2021-03-18 11:20:06 +01:00
|
|
|
const result = await fetchTokens(MAINNET_CHAIN_ID);
|
2021-04-09 19:20:32 +02:00
|
|
|
expect(result).toStrictEqual(EXPECTED_TOKENS_RESULT);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-04-09 19:20:32 +02:00
|
|
|
describe('fetchAggregatorMetadata', () => {
|
2021-04-15 20:01:46 +02:00
|
|
|
beforeAll(() => {
|
2022-03-21 10:26:52 +01:00
|
|
|
nock('https://swap.metaswap.codefi.network')
|
2021-04-15 20:01:46 +02:00
|
|
|
.persist()
|
2021-11-30 19:56:28 +01:00
|
|
|
.get('/networks/1/aggregatorMetadata')
|
2021-04-09 19:20:32 +02:00
|
|
|
.reply(200, AGGREGATOR_METADATA);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fetch aggregator metadata', async () => {
|
2021-03-18 11:20:06 +01:00
|
|
|
const result = await fetchAggregatorMetadata(MAINNET_CHAIN_ID);
|
2021-04-09 19:20:32 +02:00
|
|
|
expect(result).toStrictEqual(AGGREGATOR_METADATA);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-04-09 19:20:32 +02:00
|
|
|
it('should fetch aggregator metadata on prod', async () => {
|
2021-03-18 11:20:06 +01:00
|
|
|
const result = await fetchAggregatorMetadata(MAINNET_CHAIN_ID);
|
2021-04-09 19:20:32 +02:00
|
|
|
expect(result).toStrictEqual(AGGREGATOR_METADATA);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-04-09 19:20:32 +02:00
|
|
|
describe('fetchTopAssets', () => {
|
2021-04-15 20:01:46 +02:00
|
|
|
beforeAll(() => {
|
2022-03-21 10:26:52 +01:00
|
|
|
nock('https://swap.metaswap.codefi.network')
|
2021-04-15 20:01:46 +02:00
|
|
|
.persist()
|
2021-11-30 19:56:28 +01:00
|
|
|
.get('/networks/1/topAssets')
|
2021-04-09 19:20:32 +02:00
|
|
|
.reply(200, TOP_ASSETS);
|
|
|
|
});
|
|
|
|
|
2020-10-06 20:28:38 +02:00
|
|
|
const expectedResult = {
|
|
|
|
'0x514910771af9ca656af840dff83e8264ecf986ca': {
|
|
|
|
index: '0',
|
|
|
|
},
|
|
|
|
'0x04fa0d235c4abf4bcf4787af4cf447de572ef828': {
|
|
|
|
index: '1',
|
|
|
|
},
|
|
|
|
'0x0bc529c00c6401aef6d220be8c6ea1667f6ad93e': {
|
|
|
|
index: '2',
|
|
|
|
},
|
|
|
|
'0x80fb784b7ed66730e8b1dbd9820afd29931aab03': {
|
|
|
|
index: '3',
|
|
|
|
},
|
|
|
|
'0xc011a73ee8576fb46f5e1c5751ca3b9fe0af2a6f': {
|
|
|
|
index: '4',
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2021-04-09 19:20:32 +02:00
|
|
|
it('should fetch top assets', async () => {
|
2021-03-18 11:20:06 +01:00
|
|
|
const result = await fetchTopAssets(MAINNET_CHAIN_ID);
|
2021-04-09 19:20:32 +02:00
|
|
|
expect(result).toStrictEqual(expectedResult);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-04-09 19:20:32 +02:00
|
|
|
it('should fetch top assets on prod', async () => {
|
2021-03-18 11:20:06 +01:00
|
|
|
const result = await fetchTopAssets(MAINNET_CHAIN_ID);
|
2021-04-09 19:20:32 +02:00
|
|
|
expect(result).toStrictEqual(expectedResult);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2021-04-23 16:53:10 +02:00
|
|
|
|
|
|
|
describe('isContractAddressValid', () => {
|
|
|
|
let usedTradeTxParams;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
usedTradeTxParams = {
|
|
|
|
data: 'testData',
|
|
|
|
from: '0xe53a5bc256898bfa5673b20aceeb2b2152075d17',
|
|
|
|
gas: '2427c',
|
|
|
|
gasPrice: '27592f5a00',
|
2021-09-08 20:08:55 +02:00
|
|
|
to: WETH_CONTRACT_ADDRESS,
|
2021-04-23 16:53:10 +02:00
|
|
|
value: '0xde0b6b3a7640000',
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2021-09-08 20:08:55 +02:00
|
|
|
it('returns true if "to" is WETH contract address', () => {
|
2021-04-23 16:53:10 +02:00
|
|
|
expect(
|
2021-09-08 20:08:55 +02:00
|
|
|
isContractAddressValid(usedTradeTxParams.to, MAINNET_CHAIN_ID),
|
2021-04-23 16:53:10 +02:00
|
|
|
).toBe(true);
|
|
|
|
});
|
|
|
|
|
2021-09-08 20:08:55 +02:00
|
|
|
it('returns true if "to" is WETH contract address with some uppercase chars', () => {
|
2021-04-23 16:53:10 +02:00
|
|
|
usedTradeTxParams.to = '0xc02AAA39B223fe8d0a0e5c4f27ead9083c756cc2';
|
|
|
|
expect(
|
2021-09-08 20:08:55 +02:00
|
|
|
isContractAddressValid(usedTradeTxParams.to, MAINNET_CHAIN_ID),
|
2021-04-23 16:53:10 +02:00
|
|
|
).toBe(true);
|
|
|
|
});
|
|
|
|
|
2021-09-08 20:08:55 +02:00
|
|
|
it('returns true if "to" is ETH mainnet contract address on ETH mainnet', () => {
|
2021-04-23 16:53:10 +02:00
|
|
|
usedTradeTxParams.to =
|
|
|
|
SWAPS_CHAINID_CONTRACT_ADDRESS_MAP[MAINNET_CHAIN_ID];
|
|
|
|
expect(
|
2021-09-08 20:08:55 +02:00
|
|
|
isContractAddressValid(usedTradeTxParams.to, MAINNET_CHAIN_ID),
|
2021-04-30 20:48:56 +02:00
|
|
|
).toBe(true);
|
2021-04-23 16:53:10 +02:00
|
|
|
});
|
|
|
|
|
2021-09-08 20:08:55 +02:00
|
|
|
it('returns true if "to" is WBNB contract address on BSC mainnet', () => {
|
|
|
|
usedTradeTxParams.to = WBNB_CONTRACT_ADDRESS;
|
|
|
|
expect(isContractAddressValid(usedTradeTxParams.to, BSC_CHAIN_ID)).toBe(
|
|
|
|
true,
|
|
|
|
);
|
2021-04-30 20:48:56 +02:00
|
|
|
});
|
|
|
|
|
2021-09-08 20:08:55 +02:00
|
|
|
it('returns true if "to" is WMATIC contract address on Polygon mainnet', () => {
|
|
|
|
usedTradeTxParams.to = WMATIC_CONTRACT_ADDRESS;
|
2021-04-30 20:48:56 +02:00
|
|
|
expect(
|
2021-09-08 20:08:55 +02:00
|
|
|
isContractAddressValid(usedTradeTxParams.to, POLYGON_CHAIN_ID),
|
|
|
|
).toBe(true);
|
2021-04-30 20:48:56 +02:00
|
|
|
});
|
|
|
|
|
2021-09-08 20:08:55 +02:00
|
|
|
it('returns false if "to" is BSC contract address on ETH mainnet', () => {
|
2021-04-30 20:48:56 +02:00
|
|
|
usedTradeTxParams.to = SWAPS_CHAINID_CONTRACT_ADDRESS_MAP[BSC_CHAIN_ID];
|
2021-04-23 16:53:10 +02:00
|
|
|
expect(
|
2021-09-08 20:08:55 +02:00
|
|
|
isContractAddressValid(usedTradeTxParams.to, MAINNET_CHAIN_ID),
|
2021-04-23 16:53:10 +02:00
|
|
|
).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns false if contractAddress is null', () => {
|
2021-09-08 20:08:55 +02:00
|
|
|
expect(isContractAddressValid(null, LOCALHOST_CHAIN_ID)).toBe(false);
|
2021-04-23 16:53:10 +02:00
|
|
|
});
|
|
|
|
|
2021-04-30 20:48:56 +02:00
|
|
|
it('returns false if chainId is incorrect', () => {
|
|
|
|
expect(
|
2021-09-08 20:08:55 +02:00
|
|
|
isContractAddressValid(usedTradeTxParams.to, 'incorrectChainId'),
|
2021-04-30 20:48:56 +02:00
|
|
|
).toBe(false);
|
|
|
|
});
|
|
|
|
|
2021-09-08 20:08:55 +02:00
|
|
|
it('returns true if "to" is BSC contract address on BSC network', () => {
|
|
|
|
usedTradeTxParams.to = SWAPS_CHAINID_CONTRACT_ADDRESS_MAP[BSC_CHAIN_ID];
|
|
|
|
expect(isContractAddressValid(usedTradeTxParams.to, BSC_CHAIN_ID)).toBe(
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns true if "to" is Polygon contract address on Polygon network', () => {
|
2021-04-23 16:53:10 +02:00
|
|
|
usedTradeTxParams.to =
|
2021-09-08 20:08:55 +02:00
|
|
|
SWAPS_CHAINID_CONTRACT_ADDRESS_MAP[POLYGON_CHAIN_ID];
|
2021-04-23 16:53:10 +02:00
|
|
|
expect(
|
2021-09-08 20:08:55 +02:00
|
|
|
isContractAddressValid(usedTradeTxParams.to, POLYGON_CHAIN_ID),
|
2021-04-23 16:53:10 +02:00
|
|
|
).toBe(true);
|
|
|
|
});
|
|
|
|
|
2021-09-08 20:08:55 +02:00
|
|
|
it('returns true if "to" is Rinkeby contract address on Rinkeby network', () => {
|
|
|
|
usedTradeTxParams.to =
|
|
|
|
SWAPS_CHAINID_CONTRACT_ADDRESS_MAP[RINKEBY_CHAIN_ID];
|
2021-04-23 16:53:10 +02:00
|
|
|
expect(
|
2021-09-08 20:08:55 +02:00
|
|
|
isContractAddressValid(usedTradeTxParams.to, RINKEBY_CHAIN_ID),
|
2021-04-23 16:53:10 +02:00
|
|
|
).toBe(true);
|
|
|
|
});
|
|
|
|
|
2021-09-08 20:08:55 +02:00
|
|
|
it('returns true if "to" is testnet contract address', () => {
|
2021-04-23 16:53:10 +02:00
|
|
|
usedTradeTxParams.to =
|
|
|
|
SWAPS_CHAINID_CONTRACT_ADDRESS_MAP[LOCALHOST_CHAIN_ID];
|
|
|
|
expect(
|
2021-09-08 20:08:55 +02:00
|
|
|
isContractAddressValid(usedTradeTxParams.to, LOCALHOST_CHAIN_ID),
|
2021-04-23 16:53:10 +02:00
|
|
|
).toBe(true);
|
|
|
|
});
|
|
|
|
|
2021-09-08 20:08:55 +02:00
|
|
|
it('returns true if "to" is testnet contract address with some uppercase chars', () => {
|
2021-04-23 16:53:10 +02:00
|
|
|
usedTradeTxParams.to = '0x881D40237659C251811CEC9c364ef91dC08D300C';
|
|
|
|
expect(
|
2021-09-08 20:08:55 +02:00
|
|
|
isContractAddressValid(usedTradeTxParams.to, LOCALHOST_CHAIN_ID),
|
2021-04-23 16:53:10 +02:00
|
|
|
).toBe(true);
|
|
|
|
});
|
|
|
|
|
2021-09-08 20:08:55 +02:00
|
|
|
it('returns false if "to" has mismatch with current chainId', () => {
|
|
|
|
usedTradeTxParams.to = SWAPS_CHAINID_CONTRACT_ADDRESS_MAP[BSC_CHAIN_ID];
|
2021-04-23 16:53:10 +02:00
|
|
|
expect(
|
2021-09-08 20:08:55 +02:00
|
|
|
isContractAddressValid(usedTradeTxParams.to, LOCALHOST_CHAIN_ID),
|
2021-04-23 16:53:10 +02:00
|
|
|
).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
2021-07-09 17:24:00 +02:00
|
|
|
|
|
|
|
describe('getNetworkNameByChainId', () => {
|
|
|
|
it('returns "ethereum" for mainnet chain ID', () => {
|
|
|
|
expect(getNetworkNameByChainId(MAINNET_CHAIN_ID)).toBe(ETHEREUM);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns "bsc" for mainnet chain ID', () => {
|
|
|
|
expect(getNetworkNameByChainId(BSC_CHAIN_ID)).toBe(BSC);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns "polygon" for mainnet chain ID', () => {
|
|
|
|
expect(getNetworkNameByChainId(POLYGON_CHAIN_ID)).toBe(POLYGON);
|
|
|
|
});
|
|
|
|
|
2021-07-30 13:35:30 +02:00
|
|
|
it('returns "rinkeby" for Rinkeby chain ID', () => {
|
|
|
|
expect(getNetworkNameByChainId(RINKEBY_CHAIN_ID)).toBe(RINKEBY);
|
|
|
|
});
|
|
|
|
|
2022-02-18 15:24:47 +01:00
|
|
|
it('returns "avalanche" for Avalanche chain ID', () => {
|
|
|
|
expect(getNetworkNameByChainId(AVALANCHE_CHAIN_ID)).toBe(AVALANCHE);
|
|
|
|
});
|
|
|
|
|
2021-07-09 17:24:00 +02:00
|
|
|
it('returns an empty string for an unsupported network', () => {
|
2021-07-30 13:35:30 +02:00
|
|
|
expect(getNetworkNameByChainId(KOVAN_CHAIN_ID)).toBe('');
|
2021-07-09 17:24:00 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getSwapsLivenessForNetwork', () => {
|
|
|
|
it('returns info that Swaps are enabled and cannot use API v2 for localhost chain ID', () => {
|
|
|
|
const expectedSwapsLiveness = {
|
|
|
|
swapsFeatureIsLive: true,
|
|
|
|
};
|
|
|
|
expect(
|
|
|
|
getSwapsLivenessForNetwork(
|
|
|
|
MOCKS.createFeatureFlagsResponse(),
|
|
|
|
LOCALHOST_CHAIN_ID,
|
|
|
|
),
|
|
|
|
).toMatchObject(expectedSwapsLiveness);
|
|
|
|
});
|
|
|
|
|
2021-07-30 13:35:30 +02:00
|
|
|
it('returns info that Swaps are enabled and cannot use API v2 for Rinkeby chain ID', () => {
|
|
|
|
const expectedSwapsLiveness = {
|
|
|
|
swapsFeatureIsLive: true,
|
|
|
|
};
|
|
|
|
expect(
|
|
|
|
getSwapsLivenessForNetwork(
|
|
|
|
MOCKS.createFeatureFlagsResponse(),
|
|
|
|
RINKEBY_CHAIN_ID,
|
|
|
|
),
|
|
|
|
).toMatchObject(expectedSwapsLiveness);
|
|
|
|
});
|
|
|
|
|
2021-07-09 17:24:00 +02:00
|
|
|
it('returns info that Swaps are disabled and cannot use API v2 if network name is not found', () => {
|
|
|
|
const expectedSwapsLiveness = {
|
|
|
|
swapsFeatureIsLive: false,
|
|
|
|
};
|
|
|
|
expect(
|
|
|
|
getSwapsLivenessForNetwork(
|
|
|
|
MOCKS.createFeatureFlagsResponse(),
|
2021-07-30 13:35:30 +02:00
|
|
|
KOVAN_CHAIN_ID,
|
2021-07-09 17:24:00 +02:00
|
|
|
),
|
|
|
|
).toMatchObject(expectedSwapsLiveness);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns info that Swaps are enabled and can use API v2 for mainnet chain ID', () => {
|
|
|
|
const expectedSwapsLiveness = {
|
|
|
|
swapsFeatureIsLive: true,
|
|
|
|
};
|
|
|
|
expect(
|
|
|
|
getSwapsLivenessForNetwork(
|
|
|
|
MOCKS.createFeatureFlagsResponse(),
|
|
|
|
MAINNET_CHAIN_ID,
|
|
|
|
),
|
|
|
|
).toMatchObject(expectedSwapsLiveness);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns info that Swaps are enabled but can only use API v1 for mainnet chain ID', () => {
|
|
|
|
const expectedSwapsLiveness = {
|
|
|
|
swapsFeatureIsLive: true,
|
|
|
|
};
|
|
|
|
const swapsFeatureFlags = MOCKS.createFeatureFlagsResponse();
|
|
|
|
swapsFeatureFlags[ETHEREUM].extension_active = false;
|
|
|
|
expect(
|
|
|
|
getSwapsLivenessForNetwork(swapsFeatureFlags, MAINNET_CHAIN_ID),
|
|
|
|
).toMatchObject(expectedSwapsLiveness);
|
|
|
|
});
|
|
|
|
});
|
2021-07-22 19:29:31 +02:00
|
|
|
|
|
|
|
describe('countDecimals', () => {
|
|
|
|
it('returns 0 decimals for an undefined value', () => {
|
|
|
|
expect(countDecimals()).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns 0 decimals for number: 1', () => {
|
|
|
|
expect(countDecimals(1)).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns 1 decimals for number: 1.1', () => {
|
|
|
|
expect(countDecimals(1.1)).toBe(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns 3 decimals for number: 1.123', () => {
|
|
|
|
expect(countDecimals(1.123)).toBe(3);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns 9 decimals for number: 1.123456789', () => {
|
|
|
|
expect(countDecimals(1.123456789)).toBe(9);
|
|
|
|
});
|
|
|
|
});
|
2021-09-08 20:08:55 +02:00
|
|
|
|
|
|
|
describe('shouldEnableDirectWrapping', () => {
|
|
|
|
const randomTokenAddress = '0x881d40237659c251811cec9c364ef91234567890';
|
|
|
|
|
|
|
|
it('returns true if swapping from ETH to WETH', () => {
|
|
|
|
expect(
|
|
|
|
shouldEnableDirectWrapping(
|
|
|
|
MAINNET_CHAIN_ID,
|
|
|
|
SWAPS_CHAINID_DEFAULT_TOKEN_MAP[MAINNET_CHAIN_ID]?.address,
|
|
|
|
WETH_CONTRACT_ADDRESS,
|
|
|
|
),
|
|
|
|
).toBe(true);
|
|
|
|
});
|
|
|
|
|
2021-11-17 18:04:50 +01:00
|
|
|
it('returns true if swapping from ETH with uppercase chars to WETH', () => {
|
|
|
|
const ethAddressWithUpperCaseChars =
|
|
|
|
'0X0000000000000000000000000000000000000000';
|
|
|
|
expect(
|
|
|
|
shouldEnableDirectWrapping(
|
|
|
|
MAINNET_CHAIN_ID,
|
|
|
|
ethAddressWithUpperCaseChars,
|
|
|
|
WETH_CONTRACT_ADDRESS,
|
|
|
|
),
|
|
|
|
).toBe(true);
|
|
|
|
});
|
|
|
|
|
2021-09-08 20:08:55 +02:00
|
|
|
it('returns true if swapping from WETH to ETH', () => {
|
|
|
|
expect(
|
|
|
|
shouldEnableDirectWrapping(
|
|
|
|
MAINNET_CHAIN_ID,
|
|
|
|
WETH_CONTRACT_ADDRESS,
|
|
|
|
SWAPS_CHAINID_DEFAULT_TOKEN_MAP[MAINNET_CHAIN_ID]?.address,
|
|
|
|
),
|
|
|
|
).toBe(true);
|
|
|
|
});
|
|
|
|
|
2021-11-17 18:04:50 +01:00
|
|
|
it('returns true if swapping from WETH with uppercase chars to ETH', () => {
|
|
|
|
const wethContractAddressWithUpperCaseChars =
|
|
|
|
'0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2';
|
|
|
|
expect(
|
|
|
|
shouldEnableDirectWrapping(
|
|
|
|
MAINNET_CHAIN_ID,
|
|
|
|
wethContractAddressWithUpperCaseChars,
|
|
|
|
SWAPS_CHAINID_DEFAULT_TOKEN_MAP[MAINNET_CHAIN_ID]?.address,
|
|
|
|
),
|
|
|
|
).toBe(true);
|
|
|
|
});
|
|
|
|
|
2021-09-08 20:08:55 +02:00
|
|
|
it('returns false if swapping from ETH to a non-WETH token', () => {
|
|
|
|
expect(
|
|
|
|
shouldEnableDirectWrapping(
|
|
|
|
MAINNET_CHAIN_ID,
|
|
|
|
SWAPS_CHAINID_DEFAULT_TOKEN_MAP[MAINNET_CHAIN_ID]?.address,
|
|
|
|
randomTokenAddress,
|
|
|
|
),
|
|
|
|
).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns true if swapping from BNB to WBNB', () => {
|
|
|
|
expect(
|
|
|
|
shouldEnableDirectWrapping(
|
|
|
|
BSC_CHAIN_ID,
|
|
|
|
SWAPS_CHAINID_DEFAULT_TOKEN_MAP[BSC_CHAIN_ID]?.address,
|
|
|
|
WBNB_CONTRACT_ADDRESS,
|
|
|
|
),
|
|
|
|
).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns true if swapping from WBNB to BNB', () => {
|
|
|
|
expect(
|
|
|
|
shouldEnableDirectWrapping(
|
|
|
|
BSC_CHAIN_ID,
|
|
|
|
WBNB_CONTRACT_ADDRESS,
|
|
|
|
SWAPS_CHAINID_DEFAULT_TOKEN_MAP[BSC_CHAIN_ID]?.address,
|
|
|
|
),
|
|
|
|
).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns false if swapping from BNB to a non-WBNB token', () => {
|
|
|
|
expect(
|
|
|
|
shouldEnableDirectWrapping(
|
|
|
|
BSC_CHAIN_ID,
|
|
|
|
SWAPS_CHAINID_DEFAULT_TOKEN_MAP[BSC_CHAIN_ID]?.address,
|
|
|
|
randomTokenAddress,
|
|
|
|
),
|
|
|
|
).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns true if swapping from MATIC to WMATIC', () => {
|
|
|
|
expect(
|
|
|
|
shouldEnableDirectWrapping(
|
|
|
|
POLYGON_CHAIN_ID,
|
|
|
|
SWAPS_CHAINID_DEFAULT_TOKEN_MAP[POLYGON_CHAIN_ID]?.address,
|
|
|
|
WMATIC_CONTRACT_ADDRESS,
|
|
|
|
),
|
|
|
|
).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns true if swapping from WMATIC to MATIC', () => {
|
|
|
|
expect(
|
|
|
|
shouldEnableDirectWrapping(
|
|
|
|
POLYGON_CHAIN_ID,
|
|
|
|
WMATIC_CONTRACT_ADDRESS,
|
|
|
|
SWAPS_CHAINID_DEFAULT_TOKEN_MAP[POLYGON_CHAIN_ID]?.address,
|
|
|
|
),
|
|
|
|
).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns false if swapping from MATIC to a non-WMATIC token', () => {
|
|
|
|
expect(
|
|
|
|
shouldEnableDirectWrapping(
|
|
|
|
POLYGON_CHAIN_ID,
|
|
|
|
SWAPS_CHAINID_DEFAULT_TOKEN_MAP[POLYGON_CHAIN_ID]?.address,
|
|
|
|
randomTokenAddress,
|
|
|
|
),
|
|
|
|
).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns false if a source token is undefined', () => {
|
|
|
|
expect(
|
|
|
|
shouldEnableDirectWrapping(
|
|
|
|
MAINNET_CHAIN_ID,
|
|
|
|
undefined,
|
|
|
|
WETH_CONTRACT_ADDRESS,
|
|
|
|
),
|
|
|
|
).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns false if a destination token is undefined', () => {
|
|
|
|
expect(
|
|
|
|
shouldEnableDirectWrapping(MAINNET_CHAIN_ID, WETH_CONTRACT_ADDRESS),
|
|
|
|
).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
2022-02-18 17:48:38 +01:00
|
|
|
|
|
|
|
describe('showRemainingTimeInMinAndSec', () => {
|
|
|
|
it('returns 0:00 if we do not pass an integer', () => {
|
|
|
|
expect(showRemainingTimeInMinAndSec('5')).toBe('0:00');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns 0:05 if 5 seconds are remaining', () => {
|
|
|
|
expect(showRemainingTimeInMinAndSec(5)).toBe('0:05');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns 2:59', () => {
|
|
|
|
expect(showRemainingTimeInMinAndSec(179)).toBe('2:59');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getFeeForSmartTransaction', () => {
|
|
|
|
it('returns estimated for for STX', () => {
|
|
|
|
// TODO: Implement tests for this function.
|
|
|
|
expect(true).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
2022-06-08 16:32:04 +02:00
|
|
|
|
|
|
|
describe('getSwapsTokensReceivedFromTxMeta', () => {
|
|
|
|
const createProps = () => {
|
|
|
|
return {
|
|
|
|
tokenSymbol: ETH_SYMBOL,
|
|
|
|
txMeta: {
|
|
|
|
swapMetaData: {
|
|
|
|
token_to_amount: 5,
|
|
|
|
},
|
|
|
|
txReceipt: {
|
|
|
|
status: '0x0',
|
|
|
|
},
|
|
|
|
preTxBalance: '8b11',
|
|
|
|
postTxBalance: '8b11',
|
|
|
|
},
|
|
|
|
tokenAddress: '0x881d40237659c251811cec9c364ef91234567890',
|
|
|
|
accountAddress: '0xc011a73ee8576fb46f5e1c5751ca3b9fe0af2a6f',
|
|
|
|
tokenDecimals: 6,
|
|
|
|
approvalTxMeta: null,
|
|
|
|
chainId: MAINNET_CHAIN_ID,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
it('returns an estimated amount if preTxBalance and postTxBalance are the same for ETH', () => {
|
|
|
|
const props = createProps();
|
|
|
|
expect(
|
|
|
|
getSwapsTokensReceivedFromTxMeta(
|
|
|
|
props.tokenSymbol,
|
|
|
|
props.txMeta,
|
|
|
|
props.tokenAddress,
|
|
|
|
props.accountAddress,
|
|
|
|
props.tokenDecimals,
|
|
|
|
props.approvalTxMeta,
|
|
|
|
props.chainId,
|
|
|
|
),
|
|
|
|
).toBe(props.txMeta.swapMetaData.token_to_amount);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns null if there is no txMeta', () => {
|
|
|
|
const props = createProps();
|
|
|
|
props.txMeta = undefined;
|
|
|
|
expect(
|
|
|
|
getSwapsTokensReceivedFromTxMeta(
|
|
|
|
props.tokenSymbol,
|
|
|
|
props.txMeta,
|
|
|
|
props.tokenAddress,
|
|
|
|
props.accountAddress,
|
|
|
|
props.tokenDecimals,
|
|
|
|
props.approvalTxMeta,
|
|
|
|
props.chainId,
|
|
|
|
),
|
|
|
|
).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns null if there is no txMeta.txReceipt', () => {
|
|
|
|
const props = createProps();
|
|
|
|
props.txMeta.txReceipt = undefined;
|
|
|
|
expect(
|
|
|
|
getSwapsTokensReceivedFromTxMeta(
|
|
|
|
props.tokenSymbol,
|
|
|
|
props.txMeta,
|
|
|
|
props.tokenAddress,
|
|
|
|
props.accountAddress,
|
|
|
|
props.tokenDecimals,
|
|
|
|
props.approvalTxMeta,
|
|
|
|
props.chainId,
|
|
|
|
),
|
|
|
|
).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns null if there is no txMeta.postTxBalance', () => {
|
|
|
|
const props = createProps();
|
|
|
|
props.txMeta.postTxBalance = undefined;
|
|
|
|
expect(
|
|
|
|
getSwapsTokensReceivedFromTxMeta(
|
|
|
|
props.tokenSymbol,
|
|
|
|
props.txMeta,
|
|
|
|
props.tokenAddress,
|
|
|
|
props.accountAddress,
|
|
|
|
props.tokenDecimals,
|
|
|
|
props.approvalTxMeta,
|
|
|
|
props.chainId,
|
|
|
|
),
|
|
|
|
).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns null if there is no txMeta.preTxBalance', () => {
|
|
|
|
const props = createProps();
|
|
|
|
props.txMeta.preTxBalance = undefined;
|
|
|
|
expect(
|
|
|
|
getSwapsTokensReceivedFromTxMeta(
|
|
|
|
props.tokenSymbol,
|
|
|
|
props.txMeta,
|
|
|
|
props.tokenAddress,
|
|
|
|
props.accountAddress,
|
|
|
|
props.tokenDecimals,
|
|
|
|
props.approvalTxMeta,
|
|
|
|
props.chainId,
|
|
|
|
),
|
|
|
|
).toBeNull();
|
|
|
|
});
|
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|