Skip to content

Commit

Permalink
feat: [CM-521] Clears the swap quote gas fees for Passport gas free u…
Browse files Browse the repository at this point in the history
…sers (#1743)
  • Loading branch information
dreamoftrees authored May 8, 2024
1 parent 2d39f81 commit 34e2f06
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { BigNumber } from 'ethers';
import { Web3Provider } from '@ethersproject/providers';
import {
Amount,
Fee,
Quote,
TransactionResponse,
} from '@imtbl/dex-sdk';
import { isPassportProvider } from '../../../lib/provider';
import { processGasFree } from './processGasFree';

jest.mock('../../../lib/provider');

describe('processGasFree', () => {
let mockPassportProvider: Web3Provider;
let mockMetaMaskProvider: Web3Provider;
const mockQuote = {
quote: {
amount: {} as Amount,
amountWithMaxSlippage: {} as Amount,
slippage: 0,
fees: [{
recipient: '0x123',
basisPoints: 100,
amount: {
value: BigNumber.from(100),
token: {
symbol: 'ETH',
address: '0x123',
chainId: 1,
decimals: 18,
},
},
} as Fee],
} as Quote,
swap: {
gasFeeEstimate: {
value: BigNumber.from(100),
},
},
approval: {
gasFeeEstimate: {
value: BigNumber.from(50),
},
},
} as TransactionResponse;

beforeEach(() => {
jest.clearAllMocks();

mockPassportProvider = {
provider: {
isPassport: true,
},
getSigner: jest.fn().mockReturnValue({
getAddress: jest.fn().mockResolvedValue('0xADDRESS'),
}),
} as unknown as Web3Provider;

mockMetaMaskProvider = {
provider: {
isMetaMask: true,
},
getSigner: jest.fn().mockReturnValue({
getAddress: jest.fn().mockResolvedValue('0xADDRESS'),
}),
} as unknown as Web3Provider;
});

it('should return the unmodified quote if the provider is not a passport provider', () => {
(isPassportProvider as jest.Mock).mockReturnValue(false);
const result = processGasFree(mockMetaMaskProvider, mockQuote);
expect(result).toEqual(mockQuote);
});

it('should set gas fees to zero if the provider is a passport provider', () => {
(isPassportProvider as jest.Mock).mockReturnValue(true);
const result = processGasFree(mockPassportProvider, mockQuote);
expect(result.swap.gasFeeEstimate!.value).toEqual(BigNumber.from(0));
expect(result.approval!.gasFeeEstimate!.value).toEqual(BigNumber.from(0));
});

it('should handle quotes without swap or approval gas fees gracefully', () => {
(isPassportProvider as jest.Mock).mockReturnValue(true);
const incompleteQuote = { ...mockQuote, swap: undefined, approval: undefined } as unknown as TransactionResponse;

const result = processGasFree(mockPassportProvider, incompleteQuote);
expect(result).toEqual(incompleteQuote);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Web3Provider } from '@ethersproject/providers';
import { TransactionResponse } from '@imtbl/dex-sdk';
import { BigNumber } from 'ethers';
import { isPassportProvider } from '../../../lib/provider';

/**
* Adjusts the quote for gas free txn so we don't have to adjust it everywhere
* @param checkProvider
* @param currentQuote
*/
export const processGasFree = (checkProvider: Web3Provider, currentQuote: TransactionResponse) => {
if (!isPassportProvider(checkProvider)) {
return currentQuote;
}

// Remove the quote gas fees as they are being covered by Relayer
const adjustedQuote = { ...currentQuote };
if (adjustedQuote.swap?.gasFeeEstimate) {
adjustedQuote.swap.gasFeeEstimate.value = BigNumber.from(0);
}
if (adjustedQuote.approval?.gasFeeEstimate) {
adjustedQuote.approval.gasFeeEstimate.value = BigNumber.from(0);
}

return adjustedQuote;
};

0 comments on commit 34e2f06

Please sign in to comment.