Skip to content

Commit c28b040

Browse files
authored
fix: safe eth requirement (#1624)
1 parent d999342 commit c28b040

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

packages/payment-processor/src/payment/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ export async function isSolvent({
276276
const ethBalance = await provider.getBalance(fromAddress);
277277

278278
if (currency.type === 'ETH') {
279-
return ethBalance.gt(amount);
279+
return needsGas ? ethBalance.gt(amount) : ethBalance.gte(amount);
280280
} else {
281281
const balance = await getCurrencyBalance(fromAddress, currency, provider);
282282
return (ethBalance.gt(0) || !needsGas) && BigNumber.from(balance).gte(amount);

packages/payment-processor/test/payment/index.test.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ const nearCurrency: RequestLogicTypes.ICurrency = {
3434
value: 'near',
3535
};
3636

37+
const ethCurrency: RequestLogicTypes.ICurrency = {
38+
type: RequestLogicTypes.CURRENCY.ETH,
39+
network: 'mainnet',
40+
value: 'ETH',
41+
};
42+
3743
describe('payRequest', () => {
3844
afterEach(() => {
3945
jest.resetAllMocks();
@@ -516,7 +522,7 @@ describe('hasSufficientFunds', () => {
516522
expect(solvency).toBeFalsy();
517523
});
518524

519-
it('should skip ETH balance checks when needsGas is false', async () => {
525+
it('should skip ETH balance checks when needsGas is false - ERC20 payment', async () => {
520526
const mock = jest
521527
.spyOn(erc20Module, 'getAnyErc20Balance')
522528
.mockReturnValue(Promise.resolve(BigNumber.from('200')));
@@ -534,6 +540,34 @@ describe('hasSufficientFunds', () => {
534540
expect(mock).toHaveBeenCalledTimes(1);
535541
});
536542

543+
it('should require only the given ETH amount when needsGas is false - ETH payment', async () => {
544+
// eslint-disable-next-line no-magic-numbers
545+
const solvency = await isSolvent({
546+
fromAddress: 'any',
547+
currency: ethCurrency,
548+
amount: 200,
549+
providerOptions: {
550+
provider: fakeProvider as any,
551+
},
552+
needsGas: false,
553+
});
554+
expect(solvency).toBeTruthy();
555+
});
556+
557+
it('should require an excess of ETH when needsGas is true - ETH payment', async () => {
558+
// eslint-disable-next-line no-magic-numbers
559+
const solvency = await isSolvent({
560+
fromAddress: 'any',
561+
currency: ethCurrency,
562+
amount: 200,
563+
providerOptions: {
564+
provider: fakeProvider as any,
565+
},
566+
needsGas: true,
567+
});
568+
expect(solvency).toBeFalsy();
569+
});
570+
537571
it('should check ETH balance checks by default', async () => {
538572
const mock = jest
539573
.spyOn(erc20Module, 'getAnyErc20Balance')

0 commit comments

Comments
 (0)