Skip to content

Commit

Permalink
tokenId is uint256 so must be a BigNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
keithbro-imx committed Dec 21, 2023
1 parent a4a7bb2 commit 2c7ac99
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
20 changes: 10 additions & 10 deletions packages/checkout/sdk/src/smartCheckout/allowance/erc721.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jest.mock('ethers', () => ({
describe('erc721', () => {
const mockProvider = {} as unknown as Web3Provider;

describe('getERC20Allowance', () => {
describe('getERC721ApprovedAddress', () => {
it('should get the allowance from the contract', async () => {
const getApprovedMock = jest.fn().mockResolvedValue('0xSEAPORT');
(Contract as unknown as jest.Mock).mockReturnValue({
Expand All @@ -29,10 +29,10 @@ describe('erc721', () => {
const address = await getERC721ApprovedAddress(
mockProvider,
'0xERC721',
0,
BigNumber.from(0),
);
expect(address).toEqual('0xSEAPORT');
expect(getApprovedMock).toBeCalledWith(0);
expect(getApprovedMock).toBeCalledWith(BigNumber.from(0));
});

it('should throw checkout error when getApproved call errors', async () => {
Expand All @@ -49,7 +49,7 @@ describe('erc721', () => {
await getERC721ApprovedAddress(
mockProvider,
'0xERC721',
0,
BigNumber.from(0),
);
} catch (err: any) {
message = err.message;
Expand All @@ -63,7 +63,7 @@ describe('erc721', () => {
id: '0',
contractAddress: '0xERC721',
});
expect(getApprovedMock).toBeCalledWith(0);
expect(getApprovedMock).toBeCalledWith(BigNumber.from(0));
});
});

Expand All @@ -81,10 +81,10 @@ describe('erc721', () => {
'0xADDRESS',
'0xERC721',
'0xSEAPORT',
0,
BigNumber.from(0),
);
expect(approvalTransaction).toEqual({ from: '0xADDRESS', data: '0xDATA' });
expect(approveMock).toBeCalledWith('0xSEAPORT', 0);
expect(approveMock).toBeCalledWith('0xSEAPORT', BigNumber.from(0));
});

it('should throw checkout error if call to approve fails', async () => {
Expand All @@ -105,7 +105,7 @@ describe('erc721', () => {
'0xADDRESS',
'0xERC721',
'0xSEAPORT',
0,
BigNumber.from(0),
);
} catch (err: any) {
message = err.message;
Expand All @@ -121,7 +121,7 @@ describe('erc721', () => {
spenderAddress: '0xSEAPORT',
ownerAddress: '0xADDRESS',
});
expect(approveMock).toBeCalledWith('0xSEAPORT', 0);
expect(approveMock).toBeCalledWith('0xSEAPORT', BigNumber.from(0));
});
});

Expand Down Expand Up @@ -476,7 +476,7 @@ describe('erc721', () => {
it('should converts a valid string ID to a number', () => {
const id = '123';
const result = convertIdToNumber(id, '0xERC721');
expect(result).toBe(123);
expect(result.toNumber()).toBe(123);
});

it('should throw checkout error an error for invalid string ID', () => {
Expand Down
16 changes: 7 additions & 9 deletions packages/checkout/sdk/src/smartCheckout/allowance/erc721.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TransactionRequest, Web3Provider } from '@ethersproject/providers';
import { Contract } from 'ethers';
import { BigNumber, Contract } from 'ethers';
import { CheckoutError, CheckoutErrorType } from '../../errors';
import { ItemRequirement, ItemType } from '../../types';
import { Allowance, InsufficientERC721 } from './types';
Expand Down Expand Up @@ -34,7 +34,7 @@ export const getApproveTransaction = async (
ownerAddress: string,
contractAddress: string,
spenderAddress: string,
id: number,
id: BigNumber,
): Promise<TransactionRequest | undefined> => {
try {
const contract = new Contract(
Expand All @@ -61,7 +61,7 @@ export const getApproveTransaction = async (
export const getERC721ApprovedAddress = async (
provider: Web3Provider,
contractAddress: string,
id: number,
id: BigNumber,
): Promise<string> => {
try {
const contract = new Contract(
Expand All @@ -79,18 +79,16 @@ export const getERC721ApprovedAddress = async (
}
};

export const convertIdToNumber = (id: string, contractAddress: string): number => {
const parsedId = parseInt(id, 10);

if (Number.isNaN(parsedId)) {
export const convertIdToNumber = (id: string, contractAddress: string): BigNumber => {
try {
return BigNumber.from(id);
} catch (e) {
throw new CheckoutError(
'Invalid ERC721 ID',
CheckoutErrorType.GET_ERC721_ALLOWANCE_ERROR,
{ id, contractAddress },
);
}

return parsedId;
};

export const getApprovedCollections = async (
Expand Down

0 comments on commit 2c7ac99

Please sign in to comment.