diff --git a/packages/payment-processor/src/payment/index.ts b/packages/payment-processor/src/payment/index.ts index 88f2dbfbc..2f384e87e 100644 --- a/packages/payment-processor/src/payment/index.ts +++ b/packages/payment-processor/src/payment/index.ts @@ -276,7 +276,7 @@ export async function isSolvent({ const ethBalance = await provider.getBalance(fromAddress); if (currency.type === 'ETH') { - return ethBalance.gt(amount); + return needsGas ? ethBalance.gt(amount) : ethBalance.gte(amount); } else { const balance = await getCurrencyBalance(fromAddress, currency, provider); return (ethBalance.gt(0) || !needsGas) && BigNumber.from(balance).gte(amount); diff --git a/packages/payment-processor/test/payment/index.test.ts b/packages/payment-processor/test/payment/index.test.ts index 9dfff0014..574544f3d 100644 --- a/packages/payment-processor/test/payment/index.test.ts +++ b/packages/payment-processor/test/payment/index.test.ts @@ -34,6 +34,12 @@ const nearCurrency: RequestLogicTypes.ICurrency = { value: 'near', }; +const ethCurrency: RequestLogicTypes.ICurrency = { + type: RequestLogicTypes.CURRENCY.ETH, + network: 'mainnet', + value: 'ETH', +}; + describe('payRequest', () => { afterEach(() => { jest.resetAllMocks(); @@ -516,7 +522,7 @@ describe('hasSufficientFunds', () => { expect(solvency).toBeFalsy(); }); - it('should skip ETH balance checks when needsGas is false', async () => { + it('should skip ETH balance checks when needsGas is false - ERC20 payment', async () => { const mock = jest .spyOn(erc20Module, 'getAnyErc20Balance') .mockReturnValue(Promise.resolve(BigNumber.from('200'))); @@ -534,6 +540,34 @@ describe('hasSufficientFunds', () => { expect(mock).toHaveBeenCalledTimes(1); }); + it('should require only the given ETH amount when needsGas is false - ETH payment', async () => { + // eslint-disable-next-line no-magic-numbers + const solvency = await isSolvent({ + fromAddress: 'any', + currency: ethCurrency, + amount: 200, + providerOptions: { + provider: fakeProvider as any, + }, + needsGas: false, + }); + expect(solvency).toBeTruthy(); + }); + + it('should require an excess of ETH when needsGas is true - ETH payment', async () => { + // eslint-disable-next-line no-magic-numbers + const solvency = await isSolvent({ + fromAddress: 'any', + currency: ethCurrency, + amount: 200, + providerOptions: { + provider: fakeProvider as any, + }, + needsGas: true, + }); + expect(solvency).toBeFalsy(); + }); + it('should check ETH balance checks by default', async () => { const mock = jest .spyOn(erc20Module, 'getAnyErc20Balance')