Skip to content

Commit

Permalink
fix: WT-2084 patch tests (#1399)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrearampin authored Jan 24, 2024
1 parent 8cda7de commit 18ab3af
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 21 deletions.
8 changes: 4 additions & 4 deletions packages/checkout/sdk/src/env/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,8 @@ export const ERC721ABI = [
];

// Gas overrides -- Anti-spam mechanism for when the baseFee drops low
// https://github.com/immutable/imx-docs/blob/main/docs/main/zkEVM/overview/gas-configuration.md
export const GAS_OVERRIDES = {
maxFeePerGas: BigNumber.from(102e9),
maxPriorityFeePerGas: BigNumber.from(101e9),
// https://docs.immutable.com/docs/zkEVM/architecture/gas-config
export const IMMUTABLE_ZKVEM_GAS_OVERRIDES = {
maxFeePerGas: BigNumber.from(15e9),
maxPriorityFeePerGas: BigNumber.from(10e9),
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ import { TypedDataDomain } from 'ethers';
import { signApprovalTransactions, signFulfillmentTransactions, signMessage } from './signActions';
import { CheckoutErrorType } from '../../errors';
import { SignTransactionStatusType, UnsignedMessage } from './types';
import { GAS_OVERRIDES } from '../../env';
import { IMMUTABLE_ZKVEM_GAS_OVERRIDES } from '../../env';
import { ChainId, NetworkInfo } from '../../types';

describe('signActions', () => {
let mockProvider: Web3Provider;

describe('signApprovalTransactions', () => {
it('should sign approval transactions', async () => {
mockProvider = {
getNetwork: jest.fn().mockReturnValue({
chainId: ChainId.IMTBL_ZKEVM_TESTNET,
} as NetworkInfo),
getSigner: jest.fn().mockReturnValue({
sendTransaction: jest.fn().mockResolvedValue({
wait: jest.fn().mockResolvedValue({
Expand Down Expand Up @@ -41,19 +45,22 @@ describe('signActions', () => {
expect(mockProvider.getSigner().sendTransaction).toHaveBeenCalledWith({
data: '0xAPPROVAL1',
to: '0x123',
maxFeePerGas: GAS_OVERRIDES.maxFeePerGas,
maxPriorityFeePerGas: GAS_OVERRIDES.maxPriorityFeePerGas,
maxFeePerGas: IMMUTABLE_ZKVEM_GAS_OVERRIDES.maxFeePerGas,
maxPriorityFeePerGas: IMMUTABLE_ZKVEM_GAS_OVERRIDES.maxPriorityFeePerGas,
});
expect(mockProvider.getSigner().sendTransaction).toHaveBeenCalledWith({
data: '0xAPPROVAL2',
to: '0x123',
maxFeePerGas: GAS_OVERRIDES.maxFeePerGas,
maxPriorityFeePerGas: GAS_OVERRIDES.maxPriorityFeePerGas,
maxFeePerGas: IMMUTABLE_ZKVEM_GAS_OVERRIDES.maxFeePerGas,
maxPriorityFeePerGas: IMMUTABLE_ZKVEM_GAS_OVERRIDES.maxPriorityFeePerGas,
});
});

it('should return failed when approval transaction reverted', async () => {
mockProvider = {
getNetwork: jest.fn().mockReturnValue({
chainId: ChainId.IMTBL_ZKEVM_TESTNET,
} as NetworkInfo),
getSigner: jest.fn().mockReturnValue({
sendTransaction: jest.fn().mockResolvedValue({
wait: jest.fn().mockResolvedValue({
Expand Down Expand Up @@ -114,6 +121,9 @@ describe('signActions', () => {
describe('signFulfillmentTransactions', () => {
it('should sign fulfillment transactions', async () => {
mockProvider = {
getNetwork: jest.fn().mockReturnValue({
chainId: ChainId.IMTBL_ZKEVM_TESTNET,
} as NetworkInfo),
getSigner: jest.fn().mockReturnValue({
sendTransaction: jest.fn().mockResolvedValue({
wait: jest.fn().mockResolvedValue({
Expand All @@ -139,19 +149,22 @@ describe('signActions', () => {
expect(mockProvider.getSigner().sendTransaction).toHaveBeenCalledWith({
data: '0xFULFILLMENT1',
to: '0x123',
maxFeePerGas: GAS_OVERRIDES.maxFeePerGas,
maxPriorityFeePerGas: GAS_OVERRIDES.maxPriorityFeePerGas,
maxFeePerGas: IMMUTABLE_ZKVEM_GAS_OVERRIDES.maxFeePerGas,
maxPriorityFeePerGas: IMMUTABLE_ZKVEM_GAS_OVERRIDES.maxPriorityFeePerGas,
});
expect(mockProvider.getSigner().sendTransaction).toHaveBeenCalledWith({
data: '0xFULFILLMENT2',
to: '0x123',
maxFeePerGas: GAS_OVERRIDES.maxFeePerGas,
maxPriorityFeePerGas: GAS_OVERRIDES.maxPriorityFeePerGas,
maxFeePerGas: IMMUTABLE_ZKVEM_GAS_OVERRIDES.maxFeePerGas,
maxPriorityFeePerGas: IMMUTABLE_ZKVEM_GAS_OVERRIDES.maxPriorityFeePerGas,
});
});

it('should return failed when approval transaction reverted', async () => {
mockProvider = {
getNetwork: jest.fn().mockReturnValue({
chainId: ChainId.IMTBL_ZKEVM_TESTNET,
} as NetworkInfo),
getSigner: jest.fn().mockReturnValue({
sendTransaction: jest.fn().mockResolvedValue({
wait: jest.fn().mockResolvedValue({
Expand Down
17 changes: 16 additions & 1 deletion packages/checkout/sdk/src/transaction/transaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Web3Provider } from '@ethersproject/providers';
import { ethers } from 'ethers';
import { CheckoutError, CheckoutErrorType } from '../errors';
import { sendTransaction } from './transaction';
import { ChainId } from '../types';
import { ChainId, NetworkInfo } from '../types';

describe('transaction', () => {
it('should send the transaction and return success', async () => {
Expand All @@ -12,6 +12,9 @@ describe('transaction', () => {
confirmations: 5,
};
const mockProvider = {
getNetwork: jest.fn().mockReturnValue({
chainId: ChainId.IMTBL_ZKEVM_TESTNET,
} as NetworkInfo),
getSigner: jest.fn().mockReturnValue({
sendTransaction: () => transactionResponse,
}),
Expand All @@ -35,6 +38,9 @@ describe('transaction', () => {

it('should return errored status if transaction errors', async () => {
const mockProvider = {
getNetwork: jest.fn().mockReturnValue({
chainId: ChainId.ETHEREUM,
} as NetworkInfo),
getSigner: jest.fn().mockReturnValue({
sendTransaction: () => {
throw new Error('Transaction errored');
Expand Down Expand Up @@ -63,6 +69,9 @@ describe('transaction', () => {

it('should return insufficient funds status if transaction errors with insufficient funds', async () => {
const mockProvider = {
getNetwork: jest.fn().mockReturnValue({
chainId: ChainId.ETHEREUM,
} as NetworkInfo),
getSigner: jest.fn().mockReturnValue({
sendTransaction: () => {
const err: any = new Error('insufficient funds');
Expand Down Expand Up @@ -93,6 +102,9 @@ describe('transaction', () => {

it('should return user rejected request status if transaction errors with action rejected', async () => {
const mockProvider = {
getNetwork: jest.fn().mockReturnValue({
chainId: ChainId.ETHEREUM,
} as NetworkInfo),
getSigner: jest.fn().mockReturnValue({
sendTransaction: () => {
const err: any = new Error('user rejected request');
Expand Down Expand Up @@ -125,6 +137,9 @@ describe('transaction', () => {
'should return unpredictable gas limit request status if transaction errors with unpredictable gas limit',
async () => {
const mockProvider = {
getNetwork: jest.fn().mockReturnValue({
chainId: ChainId.ETHEREUM,
} as NetworkInfo),
getSigner: jest.fn().mockReturnValue({
sendTransaction: () => {
const err: any = new Error('unpredictable gas limit');
Expand Down
19 changes: 13 additions & 6 deletions packages/checkout/sdk/src/transaction/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@ import { ethers } from 'ethers';
import { TransactionRequest, Web3Provider } from '@ethersproject/providers';
import { CheckoutError, CheckoutErrorType } from '../errors';
import { SendTransactionResult } from '../types';
import { GAS_OVERRIDES } from '../env';
import { IMMUTABLE_ZKVEM_GAS_OVERRIDES } from '../env';
import { isZkEvmChainId } from '../utils/utils';

export const setTransactionGasLimits = (
export const setTransactionGasLimits = async (
web3Provider: Web3Provider,
transaction: TransactionRequest,
): TransactionRequest => {
): Promise<TransactionRequest> => {
const rawTx = transaction;
rawTx.maxFeePerGas = GAS_OVERRIDES.maxFeePerGas;
rawTx.maxPriorityFeePerGas = GAS_OVERRIDES.maxPriorityFeePerGas;

const { chainId } = await web3Provider.getNetwork();
if (!isZkEvmChainId(chainId)) return rawTx;

rawTx.maxFeePerGas = IMMUTABLE_ZKVEM_GAS_OVERRIDES.maxFeePerGas;
rawTx.maxPriorityFeePerGas = IMMUTABLE_ZKVEM_GAS_OVERRIDES.maxPriorityFeePerGas;

return rawTx;
};

Expand All @@ -20,7 +27,7 @@ export const sendTransaction = async (
try {
const signer = web3Provider.getSigner();

const rawTx = setTransactionGasLimits(transaction);
const rawTx = await setTransactionGasLimits(web3Provider, transaction);
const transactionResponse = await signer.sendTransaction(rawTx);

return {
Expand Down
25 changes: 24 additions & 1 deletion packages/checkout/sdk/src/utils/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { isMatchingAddress } from './utils';
import { ChainId } from '../types';
import { isMatchingAddress, isZkEvmChainId } from './utils';

describe('utils', () => {
it('should return true if addresses are the same', () => {
Expand All @@ -15,4 +16,26 @@ describe('utils', () => {
const address = isMatchingAddress('0x123', '0x1234');
expect(address).toBeFalsy();
});

describe('isZkEvmChainId', () => {
it('should return true if devnet zkEVM chain', () => {
const chainId = isZkEvmChainId(ChainId.IMTBL_ZKEVM_DEVNET);
expect(chainId).toBeTruthy();
});

it('should return true if testnet zkEVM chain', () => {
const chainId = isZkEvmChainId(ChainId.IMTBL_ZKEVM_TESTNET);
expect(chainId).toBeTruthy();
});

it('should return true if mainnet zkEVM chain', () => {
const chainId = isZkEvmChainId(ChainId.IMTBL_ZKEVM_MAINNET);
expect(chainId).toBeTruthy();
});

it('should return false if not zkEVM chain', () => {
const chainId = isZkEvmChainId(ChainId.SEPOLIA);
expect(chainId).toBeFalsy();
});
});
});
6 changes: 6 additions & 0 deletions packages/checkout/sdk/src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import { ChainId } from '../types';

export const isMatchingAddress = (addressA: string = '', addressB: string = '') => (
addressA.toLowerCase() === addressB.toLowerCase()
);

export const isZkEvmChainId = (chainId: ChainId) => chainId === ChainId.IMTBL_ZKEVM_DEVNET
|| chainId === ChainId.IMTBL_ZKEVM_TESTNET
|| chainId === ChainId.IMTBL_ZKEVM_MAINNET;

0 comments on commit 18ab3af

Please sign in to comment.