Skip to content

Commit

Permalink
fix: Smart checkout balance token information (#1612)
Browse files Browse the repository at this point in the history
Co-authored-by: Mimi Immutable <[email protected]>
  • Loading branch information
dreamoftrees and mimi-imtbl authored Mar 21, 2024
1 parent 0fc0ed3 commit 4aa9a47
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,17 @@ export const balanceCheck = async (

// Wait for all balances and calculate the requirements
const promisesResponses = await Promise.all(balancePromises);
const balanceRequirements: BalanceRequirement[] = [];
const erc721BalanceRequirements: BalanceRequirement[] = [];
const tokenBalanceRequirementPromises: Promise<BalanceRequirement>[] = [];

// Get all ERC20 and NATIVE balances
if (requiredToken.length > 0 && promisesResponses.length > 0) {
const result = promisesResponses.shift();
if (result) {
requiredToken.forEach((item) => {
balanceRequirements.push(getTokenBalanceRequirement(item as (NativeItem | ERC20Item), result));
tokenBalanceRequirementPromises.push(
getTokenBalanceRequirement(item as (NativeItem | ERC20Item), result, provider),
);
});
}
}
Expand All @@ -175,10 +178,14 @@ export const balanceCheck = async (
const result = promisesResponses.shift();
if (result) {
requiredERC721.forEach((item) => {
balanceRequirements.push(getERC721BalanceRequirement(item as (ERC721Item), result));
erc721BalanceRequirements.push(getERC721BalanceRequirement(item as (ERC721Item), result));
});
}
}
const balanceRequirements = [
...erc721BalanceRequirements,
...(await Promise.all(tokenBalanceRequirementPromises)),
];

// Find if there are any requirements that aren't sufficient.
// If there is not item with sufficient === false then the requirements
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { BigNumber } from 'ethers';
import { Web3Provider } from '@ethersproject/providers';
import {
ERC20Item, ERC721Item, ItemBalance, ItemRequirement, ItemType, NativeItem,
ChainId,
ERC20Item,
ERC721Item,
ItemBalance,
ItemRequirement,
ItemType,
NativeItem,
} from '../../types';
import {
getERC721BalanceRequirement,
Expand Down Expand Up @@ -63,30 +70,28 @@ describe('balanceRequirement', () => {
},
];
const result = getERC721BalanceRequirement(itemRequirement, balances);
expect(result).toEqual(
{
sufficient: true,
expect(result).toEqual({
sufficient: true,
type: ItemType.ERC721,
delta: {
balance: BigNumber.from(0),
formattedBalance: '0',
},
required: {
type: ItemType.ERC721,
delta: {
balance: BigNumber.from(0),
formattedBalance: '0',
},
required: {
type: ItemType.ERC721,
balance: BigNumber.from(1),
formattedBalance: '1',
contractAddress: '0xERC721',
id: '0',
},
current: {
type: ItemType.ERC721,
balance: BigNumber.from(1),
formattedBalance: '1',
contractAddress: '0xERC721',
id: '0',
},
balance: BigNumber.from(1),
formattedBalance: '1',
contractAddress: '0xERC721',
id: '0',
},
);
current: {
type: ItemType.ERC721,
balance: BigNumber.from(1),
formattedBalance: '1',
contractAddress: '0xERC721',
id: '0',
},
});
});

it('should return sufficient false if does not meet requirement for ERC721', () => {
Expand All @@ -110,35 +115,46 @@ describe('balanceRequirement', () => {
},
];
const result = getERC721BalanceRequirement(itemRequirement, balances);
expect(result).toEqual(
{
sufficient: false,
expect(result).toEqual({
sufficient: false,
type: ItemType.ERC721,
delta: {
balance: BigNumber.from(1),
formattedBalance: '1',
},
required: {
type: ItemType.ERC721,
delta: {
balance: BigNumber.from(1),
formattedBalance: '1',
},
required: {
type: ItemType.ERC721,
balance: BigNumber.from(1),
formattedBalance: '1',
contractAddress: '0xERC721',
id: '0',
},
current: {
type: ItemType.ERC721,
balance: BigNumber.from(0),
formattedBalance: '0',
contractAddress: '0xERC721',
id: '0',
},
balance: BigNumber.from(1),
formattedBalance: '1',
contractAddress: '0xERC721',
id: '0',
},
);
current: {
type: ItemType.ERC721,
balance: BigNumber.from(0),
formattedBalance: '0',
contractAddress: '0xERC721',
id: '0',
},
});
});
});

describe('getTokenBalanceRequirement', () => {
it('should return sufficient true if meets requirements for NATIVE', () => {
let mockProvider: Web3Provider;

beforeEach(() => {
mockProvider = {
getSigner: jest.fn().mockReturnValue({
getAddress: jest.fn().mockResolvedValue('0xADDRESS'),
}),
network: {
chainId: ChainId.ETHEREUM,
},
} as unknown as Web3Provider;
});

it('should return sufficient true if meets requirements for NATIVE', async () => {
const itemRequirement: NativeItem = {
type: ItemType.NATIVE,
amount: BigNumber.from('1000000000000000000'),
Expand All @@ -156,7 +172,11 @@ describe('balanceRequirement', () => {
},
];

const result = getTokenBalanceRequirement(itemRequirement, balances);
const result = await getTokenBalanceRequirement(
itemRequirement,
balances,
mockProvider,
);
expect(result).toEqual({
sufficient: true,
type: ItemType.NATIVE,
Expand Down Expand Up @@ -187,7 +207,7 @@ describe('balanceRequirement', () => {
});
});

it('should return sufficient true if meets requirements for ERC20', () => {
it('should return sufficient true if meets requirements for ERC20', async () => {
const itemRequirement: ERC20Item = {
type: ItemType.ERC20,
tokenAddress: '0xERC20',
Expand All @@ -208,7 +228,11 @@ describe('balanceRequirement', () => {
},
];

const result = getTokenBalanceRequirement(itemRequirement, balances);
const result = await getTokenBalanceRequirement(
itemRequirement,
balances,
mockProvider,
);
expect(result).toEqual({
sufficient: true,
type: ItemType.ERC20,
Expand Down Expand Up @@ -241,7 +265,7 @@ describe('balanceRequirement', () => {
});
});

it('should return sufficient false if requirements not met for NATIVE', () => {
it('should return sufficient false if requirements not met for NATIVE', async () => {
const itemRequirement: NativeItem = {
type: ItemType.NATIVE,
amount: BigNumber.from('1000000000000000000'),
Expand Down Expand Up @@ -270,7 +294,11 @@ describe('balanceRequirement', () => {
},
];

const result = getTokenBalanceRequirement(itemRequirement, balances);
const result = await getTokenBalanceRequirement(
itemRequirement,
balances,
mockProvider,
);
expect(result).toEqual({
sufficient: false,
type: ItemType.NATIVE,
Expand Down Expand Up @@ -301,7 +329,7 @@ describe('balanceRequirement', () => {
});
});

it('should return sufficient false if requirements not met for ERC20', () => {
it('should return sufficient false if requirements not met for ERC20', async () => {
const itemRequirement: ERC20Item = {
type: ItemType.ERC20,
tokenAddress: '0xERC20',
Expand Down Expand Up @@ -332,7 +360,11 @@ describe('balanceRequirement', () => {
},
];

const result = getTokenBalanceRequirement(itemRequirement, balances);
const result = await getTokenBalanceRequirement(
itemRequirement,
balances,
mockProvider,
);
expect(result).toEqual({
sufficient: false,
type: ItemType.ERC20,
Expand Down
Loading

0 comments on commit 4aa9a47

Please sign in to comment.