Skip to content

Commit

Permalink
Merge pull request #1310 from immutable/bug/tokenId-bignumber
Browse files Browse the repository at this point in the history
fix: tokenId is uint256 so must be a BigNumber
  • Loading branch information
ZacharyCouchman committed Apr 10, 2024
2 parents 67db8cf + 9b2d9f1 commit 263577f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
42 changes: 24 additions & 18 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 @@ -59,10 +59,12 @@ describe('erc721', () => {

expect(message).toEqual('Failed to get approved address for ERC721');
expect(type).toEqual(CheckoutErrorType.GET_ERC721_ALLOWANCE_ERROR);
expect(data.error).toBeDefined();
expect(data.id).toEqual('0');
expect(data.contractAddress).toEqual('0xERC721');
expect(getApprovedMock).toBeCalledWith(0);
expect(data).toEqual({
id: '0',
error: {},
contractAddress: '0xERC721',
});
expect(getApprovedMock).toBeCalledWith(BigNumber.from(0));
});
});

Expand All @@ -80,10 +82,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 @@ -104,7 +106,7 @@ describe('erc721', () => {
'0xADDRESS',
'0xERC721',
'0xSEAPORT',
0,
BigNumber.from(0),
);
} catch (err: any) {
message = err.message;
Expand All @@ -114,12 +116,16 @@ describe('erc721', () => {

expect(message).toEqual('Failed to get the approval transaction for ERC721');
expect(type).toEqual(CheckoutErrorType.GET_ERC721_ALLOWANCE_ERROR);
expect(data.error).toBeDefined();
expect(data.id).toEqual('0');
expect(data.contractAddress).toEqual('0xERC721');
expect(data.spenderAddress).toEqual('0xSEAPORT');
expect(data.ownerAddress).toEqual('0xADDRESS');
expect(approveMock).toBeCalledWith('0xSEAPORT', 0);
expect(data).toEqual({
id: '0',
error: {
from: '0xADDRESS',
},
contractAddress: '0xERC721',
spenderAddress: '0xSEAPORT',
ownerAddress: '0xADDRESS',
});
expect(approveMock).toBeCalledWith('0xSEAPORT', BigNumber.from(0));
});
});

Expand Down Expand Up @@ -472,7 +478,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 @@ -39,7 +39,7 @@ export const getApproveTransaction = async (
ownerAddress: string,
contractAddress: string,
spenderAddress: string,
id: number,
id: BigNumber,
): Promise<TransactionRequest | undefined> => {
try {
const contract = new Contract(
Expand Down Expand Up @@ -70,7 +70,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 @@ -92,18 +92,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 263577f

Please sign in to comment.