-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [CM-521] Clears the swap quote gas fees for Passport gas free u…
…sers (#1743)
- Loading branch information
1 parent
2d39f81
commit 34e2f06
Showing
2 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
packages/checkout/widgets-lib/src/widgets/swap/functions/processGasFree.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
26 changes: 26 additions & 0 deletions
26
packages/checkout/widgets-lib/src/widgets/swap/functions/processGasFree.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |