Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
0xAlec committed Jun 10, 2024
1 parent 350d864 commit dcd31a3
Show file tree
Hide file tree
Showing 3 changed files with 370 additions and 0 deletions.
1 change: 1 addition & 0 deletions jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = {
statements: 100,
},
},
maxWorkers: 1,
modulePathIgnorePatterns: ['<rootDir>/framegear/'],
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
transform: {
Expand Down
287 changes: 287 additions & 0 deletions src/swap/core/getSwap.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,287 @@
import { getSwap } from './getSwap';
import { sendRequest } from '../../queries/request';
import { CDP_GETSWAPTRADE } from '../../definitions/swap';
import { getParamsForToken } from './getParamsForToken';
import { getTransaction } from './getTransaction';
import type { Token } from '../../token/types';
import { Swap } from '../types';

jest.mock('../../queries/request');

const ETH: Token = {
name: 'ETH',
address: '',
symbol: 'ETH',
decimals: 18,
image: 'https://wallet-api-production.s3.amazonaws.com/uploads/tokens/eth_288.png',
chainId: 8453,
};
const DEGEN: Token = {
name: 'DEGEN',
address: '0x4ed4e862860bed51a9570b96d89af5e1b0efefed',
symbol: 'DEGEN',
decimals: 18,
image:
'https://d3r81g40ycuhqg.cloudfront.net/wallet/wais/3b/bf/3bbf118b5e6dc2f9e7fc607a6e7526647b4ba8f0bea87125f971446d57b296d2-MDNmNjY0MmEtNGFiZi00N2I0LWIwMTItMDUyMzg2ZDZhMWNm',
chainId: 8453,
};
const testFromAddress = '0x6Cd01c0F55ce9E0Bf78f5E90f72b4345b16d515d';
const testAmount = '3305894409732200';
const testAmountReference = 'from';

describe('getSwap', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('should return a swap', async () => {
const mockParams = {
fromAddress: testFromAddress as `0x${string}`,
amountReference: testAmountReference,
from: ETH,
to: DEGEN,
amount: testAmount,
};
const mockApiParams = getParamsForToken(mockParams);

const mockResponse = {
id: 1,
jsonrpc: '2.0',
result: {
tx: {
data: '0x0000000000000',
gas: '463613',
gasPrice: '2106527',
from: '0xaeE5834a78a30F6762407F6F8c3A2090054b0086',
to: '0xdef1c0ded9bec7f1a1670819833240f027b25eff',
value: '100000000000000',
},
quote: {
from: {
address: '',
chainId: 8453,
decimals: 18,
image: 'https://wallet-api-production.s3.amazonaws.com/uploads/tokens/eth_288.png',
name: 'ETH',
symbol: 'ETH',
},
to: {
address: '0x4ed4e862860bed51a9570b96d89af5e1b0efefed',
chainId: 8453,
decimals: 18,
image:
'https://d3r81g40ycuhqg.cloudfront.net/wallet/wais/3b/bf/3bbf118b5e6dc2f9e7fc607a6e7526647b4ba8f0bea87125f971446d57b296d2-MDNmNjY0MmEtNGFiZi00N2I0LWIwMTItMDUyMzg2ZDZhMWNm',
name: 'DEGEN',
symbol: 'DEGEN',
},
fromAmount: '100000000000000',
toAmount: '19395353519910973703',
amountReference: 'from',
priceImpact: '0.94',
chainId: 8453,
highPriceImpact: false,
slippage: '3',
warning: undefined,
},
fee: {
baseAsset: {
name: 'DEGEN',
address: '0x4ed4e862860bed51a9570b96d89af5e1b0efefed',
currencyCode: 'DEGEN',
decimals: 18,
imageURL:
'https://d3r81g40ycuhqg.cloudfront.net/wallet/wais/3b/bf/3bbf118b5e6dc2f9e7fc607a6e7526647b4ba8f0bea87125f971446d57b296d2-MDNmNjY0MmEtNGFiZi00N2I0LWIwMTItMDUyMzg2ZDZhMWNm',
blockchain: 'eth',
aggregators: [Array],
swappable: true,
unverified: false,
chainId: 8453,
},
percentage: '1',
amount: '195912661817282562',
},
approveTx: undefined,
chainId: '8453',
},
};

(sendRequest as jest.Mock).mockResolvedValue(mockResponse);

const trade = mockResponse.result;
const expectedResponse = {
approveTransaction: trade.approveTx
? getTransaction(trade.approveTx, trade.chainId)
: undefined,
fee: trade.fee,
quote: trade.quote,
transaction: getTransaction(trade.tx, trade.chainId),
warning: trade.quote.warning,
};

const quote = (await getSwap(mockParams)) as Swap;

expect(quote.approveTransaction?.transaction).toEqual(
expectedResponse.approveTransaction?.transaction,
);
expect(quote.transaction.transaction).toEqual(expectedResponse.transaction.transaction);
expect(quote.fee).toEqual(expectedResponse.fee);
expect(quote.warning).toEqual(expectedResponse.warning);

expect(sendRequest).toHaveBeenCalledTimes(1);
expect(sendRequest).toHaveBeenCalledWith(CDP_GETSWAPTRADE, [mockApiParams]);
});

it('should return a swap with an approve transaction', async () => {
const mockParams = {
fromAddress: testFromAddress as `0x${string}`,
amountReference: testAmountReference,
from: DEGEN,
to: ETH,
amount: testAmount,
};
const mockApiParams = getParamsForToken(mockParams);

const mockResponse = {
id: 1,
jsonrpc: '2.0',
result: {
approveTx: {
data: '0x0000000000000',
gas: '463613',
gasPrice: '2106527',
from: '0xaeE5834a78a30F6762407F6F8c3A2090054b0086',
to: '0xdef1c0ded9bec7f1a1670819833240f027b25eff',
value: '100000000000000',
},
tx: {
data: '0x0000000000000',
gas: '463613',
gasPrice: '2106527',
from: '0xaeE5834a78a30F6762407F6F8c3A2090054b0086',
to: '0xdef1c0ded9bec7f1a1670819833240f027b25eff',
value: '100000000000000',
},
quote: {
from: {
address: '',
chainId: 8453,
decimals: 18,
image: 'https://wallet-api-production.s3.amazonaws.com/uploads/tokens/eth_288.png',
name: 'ETH',
symbol: 'ETH',
},
to: {
address: '0x4ed4e862860bed51a9570b96d89af5e1b0efefed',
chainId: 8453,
decimals: 18,
image:
'https://d3r81g40ycuhqg.cloudfront.net/wallet/wais/3b/bf/3bbf118b5e6dc2f9e7fc607a6e7526647b4ba8f0bea87125f971446d57b296d2-MDNmNjY0MmEtNGFiZi00N2I0LWIwMTItMDUyMzg2ZDZhMWNm',
name: 'DEGEN',
symbol: 'DEGEN',
},
fromAmount: '100000000000000',
toAmount: '19395353519910973703',
amountReference: 'from',
priceImpact: '0.94',
chainId: 8453,
highPriceImpact: false,
slippage: '3',
warning: undefined,
},
fee: {
baseAsset: {
name: 'DEGEN',
address: '0x4ed4e862860bed51a9570b96d89af5e1b0efefed',
currencyCode: 'DEGEN',
decimals: 18,
imageURL:
'https://d3r81g40ycuhqg.cloudfront.net/wallet/wais/3b/bf/3bbf118b5e6dc2f9e7fc607a6e7526647b4ba8f0bea87125f971446d57b296d2-MDNmNjY0MmEtNGFiZi00N2I0LWIwMTItMDUyMzg2ZDZhMWNm',
blockchain: 'eth',
aggregators: [Array],
swappable: true,
unverified: false,
chainId: 8453,
},
percentage: '1',
amount: '195912661817282562',
},
chainId: '8453',
},
};

(sendRequest as jest.Mock).mockResolvedValue(mockResponse);

const trade = mockResponse.result;
const expectedResponse = {
approveTransaction: trade.approveTx
? getTransaction(trade.approveTx, trade.chainId)
: undefined,
fee: trade.fee,
quote: trade.quote,
transaction: getTransaction(trade.tx, trade.chainId),
warning: trade.quote.warning,
};

const quote = (await getSwap(mockParams)) as Swap;

expect(quote.approveTransaction?.transaction).toEqual(
expectedResponse.approveTransaction?.transaction,
);
expect(quote.transaction.transaction).toEqual(expectedResponse.transaction.transaction);
expect(quote.fee).toEqual(expectedResponse.fee);
expect(quote.warning).toEqual(expectedResponse.warning);

expect(sendRequest).toHaveBeenCalledTimes(1);
expect(sendRequest).toHaveBeenCalledWith(CDP_GETSWAPTRADE, [mockApiParams]);
});

it('should throw an error if sendRequest fails', async () => {
const mockParams = {
fromAddress: testFromAddress as `0x${string}`,
amountReference: testAmountReference,
from: ETH,
to: DEGEN,
amount: testAmount,
};
const mockApiParams = getParamsForToken(mockParams);

const mockError = new Error('getSwap: Error: Failed to send request');
(sendRequest as jest.Mock).mockRejectedValue(mockError);

await expect(getSwap(mockParams)).rejects.toThrow('getSwap: Error: Failed to send request');

expect(sendRequest).toHaveBeenCalledTimes(1);
expect(sendRequest).toHaveBeenCalledWith(CDP_GETSWAPTRADE, [mockApiParams]);
});

it('should return an error object from getSwap', async () => {
const mockParams = {
fromAddress: testFromAddress as `0x${string}`,
amountReference: testAmountReference,
from: ETH,
to: DEGEN,
amount: testAmount,
};
const mockApiParams = getParamsForToken(mockParams);

const mockResponse = {
id: 1,
jsonrpc: '2.0',
error: {
code: -1,
message: 'Invalid response',
},
};

(sendRequest as jest.Mock).mockResolvedValue(mockResponse);

const error = await getSwap(mockParams);
expect(error).toEqual({
code: -1,
error: 'Invalid response',
});

expect(sendRequest).toHaveBeenCalledTimes(1);
expect(sendRequest).toHaveBeenCalledWith(CDP_GETSWAPTRADE, [mockApiParams]);
});
});
82 changes: 82 additions & 0 deletions src/swap/core/getTransaction.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { getTransaction } from './getTransaction';
import { TransactionParams } from '../types';
import { RawTransactionData } from '../types';

jest.mock('../../queries/request');

describe('getTransaction', () => {
it('should construct an unsigned transaction', () => {
const tx: RawTransactionData = {
data: '0x123456',
gas: '100000',
to: '0xabcdef',
from: '0xabcdef',
gasPrice: '100000',
value: '1000000000000000000',
};
const chainId = '1';

const expectedTransaction = {
transaction: {
chainId: 1,
data: '0x123456',
gas: BigInt(100000),
to: '0xabcdef',
value: BigInt('1000000000000000000'),
},
withParams(params: TransactionParams) {
const { nonce, maxFeePerGas, maxPriorityFeePerGas } = params;

return {
chainId: 1,
data: '0x123456',
gas: BigInt(100000),
to: '0xabcdef',
value: BigInt('1000000000000000000'),
nonce,
maxFeePerGas,
maxPriorityFeePerGas,
};
},
};

const result = getTransaction(tx, chainId);

expect(result.transaction).toEqual(expectedTransaction.transaction);
});

it('should be able to add arbitrary params', () => {
const tx: RawTransactionData = {
data: '0x123456',
gas: '100000',
to: '0xabcdef',
from: '0xabcdef',
gasPrice: '100000',
value: '1000000000000000000',
};
const chainId = '1';

const expectedTransaction = {
transaction: {
chainId: 1,
data: '0x123456',
gas: BigInt(100000),
to: '0xabcdef',
value: BigInt('1000000000000000000'),
nonce: 1,
maxFeePerGas: BigInt(100000),
maxPriorityFeePerGas: BigInt(100000),
},
};

const result = getTransaction(tx, chainId);

const params: TransactionParams = {
nonce: 1,
maxFeePerGas: BigInt(100000),
maxPriorityFeePerGas: BigInt(100000),
};

expect(result.withParams(params)).toEqual(expectedTransaction.transaction);
});
});

0 comments on commit dcd31a3

Please sign in to comment.