import { BigNumber, providers, utils } from 'ethers'; import { eEthereumTxType, EthereumTransactionTypeExtended, GasType, transactionType, } from '../commons/types'; import { ERC721Service, IERC721ServiceInterface } from './index'; jest.mock('../commons/gasStation', () => { return { __esModule: true, estimateGasByNetwork: jest .fn() .mockImplementation(async () => Promise.resolve(BigNumber.from(1))), estimateGas: jest.fn(async () => Promise.resolve(BigNumber.from(1))), }; }); describe('ERC721Service', () => { const provider: providers.Provider = new providers.JsonRpcProvider(); jest .spyOn(provider, 'getGasPrice') .mockImplementation(async () => Promise.resolve(BigNumber.from(1))); describe('Initialize', () => { it('Expects to be initialized correctly', () => { const instance: IERC721ServiceInterface = new ERC721Service(provider); expect(instance instanceof ERC721Service).toEqual(true); }); }); describe('approve', () => { const erc721Service: IERC721ServiceInterface = new ERC721Service(provider); const user = '0x0000000000000000000000000000000000000001'; const token = '0x0000000000000000000000000000000000000002'; const spender = '0x0000000000000000000000000000000000000003'; const token_id = '0'; it('Expects to get the approval txObj with correct params', async () => { const erc721Service: IERC721ServiceInterface = new ERC721Service( provider, ); const txObj: EthereumTransactionTypeExtended = erc721Service.approve({ user, token, spender, token_id, }); expect(txObj.txType).toEqual(eEthereumTxType.ERC721_APPROVAL); const tx: transactionType = await txObj.tx(); expect(tx.to).toEqual(token); expect(tx.from).toEqual(user); expect(tx.gasLimit).toEqual(BigNumber.from(1)); const decoded = utils.defaultAbiCoder.decode( ['address', 'uint256'], utils.hexDataSlice(tx.data ?? '', 4), ); expect(decoded[0]).toEqual(spender); expect(decoded[1]).toEqual(BigNumber.from(token_id)); // gas price const gasPrice: GasType | null = await txObj.gas(); expect(gasPrice).not.toBeNull(); expect(gasPrice?.gasLimit).toEqual('1'); expect(gasPrice?.gasPrice).toEqual('1'); }); it('Expects to fail when user is not address', () => { const user = 'asdf'; expect(() => erc721Service.approve({ user, token, spender, token_id, }), ).toThrowError( new Error(`Address: ${user} is not a valid ethereum Address`), ); }); it('Expects to fail when token is not address', () => { const token = 'asdf'; expect(() => erc721Service.approve({ user, token, spender, token_id, }), ).toThrowError( new Error(`Address: ${token} is not a valid ethereum Address`), ); }); it('Expects to fail when spender is not address', () => { const spender = 'asdf'; expect(() => erc721Service.approve({ user, token, spender, token_id, }), ).toThrowError( new Error(`Address: ${spender} is not a valid ethereum Address`), ); }); it('Expects to fail when token_id is not positive > 0', () => { const token_id = '-1'; expect(() => erc721Service.approve({ user, token, spender, token_id, }), ).toThrowError( new Error(`TokenId: ${token_id} needs to be greater or equal than 0`), ); }); it('Expects to fail when amount is not a number', () => { const token_id = 'asdf'; expect(() => erc721Service.approve({ user, token, spender, token_id, }), ).toThrowError( new Error(`TokenId: ${token_id} needs to be greater or equal than 0`), ); }); }); });