Skip to content

fix: safe eth requirement #1624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/payment-processor/src/payment/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
36 changes: 35 additions & 1 deletion packages/payment-processor/test/payment/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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')));
Expand All @@ -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')
Expand Down