Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Commit

Permalink
chore: fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
javiergarciavera authored and salimtb committed Apr 7, 2023
1 parent bf6a132 commit 0653f67
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 27 deletions.
42 changes: 24 additions & 18 deletions src/lib/ContractTemplates/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,29 @@ import axios from 'axios';
import { GAS_LIMIT } from '../constants';

export default async function preparePolygonTransaction(nonce: number) {
const { data } = await axios({
method: 'get',
url: 'https://gasstation-mainnet.matic.network/v2',
});
try {
const { data } = await axios({
method: 'get',
url: 'https://gasstation-mainnet.matic.network/v2',
});
console.log('WORKED 2');
const gas = data.fast;

const gas = data.fast;

// convert priority fee and max fee from GWEI to WEI
const priority = Math.trunc(gas.maxPriorityFee * 10 ** 9);
const max = Math.trunc(gas.maxFee * 10 ** 9);
const maxFeePerGas = max.toString();
const maxPriorityFeePerGas = priority.toString();

return {
nonce,
maxFeePerGas,
maxPriorityFeePerGas,
gasLimit: GAS_LIMIT,
};
// convert priority fee and max fee from GWEI to WEI
const priority = Math.trunc(gas.maxPriorityFee * 10 ** 9);
const max = Math.trunc(gas.maxFee * 10 ** 9);
const maxFeePerGas = max.toString();
const maxPriorityFeePerGas = priority.toString();
console.log('WORKED');
return {
nonce,
maxFeePerGas,
maxPriorityFeePerGas,
gasLimit: GAS_LIMIT,
};
} catch (err) {
return {
gas: GAS_LIMIT,
};
}
}
3 changes: 3 additions & 0 deletions src/lib/Logger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ export enum ErrorLocation {
METADATA_TOKEN_CREATION = '[Metadata.tokenLevelMetadata]',
METADATA_CONTRACT_CREATION = '[Metadata.contractLevelMetadata]',
METADATA_FREE_CREATION = '[Metadata.freeLevelMetadata]',

// Utils
UTILS_PREPARE_POLYGON_TX = '[Utils.preparePolygonTransaction]',
}

export enum ErrorMessage {
Expand Down
35 changes: 32 additions & 3 deletions test/ERC1155Mintable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { faker } from '@faker-js/faker';
import { BigNumber, Contract, ContractFactory, ethers } from 'ethers';
import { ACCOUNT_ADDRESS, ACCOUNT_ADDRESS_2, CONTRACT_ADDRESS } from './__mocks__/utils';
import ERC1155Mintable from '../src/lib/ContractTemplates/ERC1155Mintable';
import * as templateUtils from '../src/lib/ContractTemplates/utils';
import version from '../src/_version';

let erc1155Mintable: ERC1155Mintable;
Expand Down Expand Up @@ -48,13 +49,15 @@ describe('ERC1155Mintable SDK', () => {
beforeAll(() => {
signer = {
getChainId: () => 80001,
getTransactionCount: () => 1,
};
});

afterEach(() => {
contractFactoryMock.mockClear();
signer = {
getChainId: () => 80001,
getTransactionCount: () => 1,
};
});

Expand Down Expand Up @@ -192,11 +195,37 @@ describe('ERC1155Mintable SDK', () => {

it('[Deploy] - should deploy when polygon mainnet', async () => {
erc1155Mintable = new ERC1155Mintable(signer as unknown as ethers.Wallet);
jest.spyOn(signer, 'getChainId' as any).mockResolvedValue(137);
jest.spyOn(signer, 'getTransactionCount' as any).mockResolvedValue(1);
jest.spyOn(templateUtils as any, 'default').mockResolvedValueOnce({
nonce: 1,
maxFeePerGas: '0.001',
maxPriorityFeePerGas: '0.001',
gasLimit: '6000000',
});
const baseURI = faker.internet.url();
const contractURI = faker.internet.url();
signer = {
getChainId: () => 137,
};

await erc1155Mintable.deploy({
baseURI,
contractURI,
ids: [],
gas: '250',
});

expect(ContractFactory.prototype.deploy).toHaveBeenCalledTimes(1);
});

it('[Deploy] - should deploy when polygon mainnet when axios failed', async () => {
erc1155Mintable = new ERC1155Mintable(signer as unknown as ethers.Wallet);
jest.spyOn(signer, 'getChainId' as any).mockResolvedValue(137);
jest.spyOn(signer, 'getTransactionCount' as any).mockResolvedValue(1);
jest.spyOn(templateUtils as any, 'default').mockResolvedValue({
gas: '6000000',
});
const baseURI = faker.internet.url();
const contractURI = faker.internet.url();

await erc1155Mintable.deploy({
baseURI,
contractURI,
Expand Down
28 changes: 25 additions & 3 deletions test/ERC721Mintable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Contract, ContractFactory, ethers } from 'ethers';
import ERC721Mintable from '../src/lib/ContractTemplates/ERC721Mintable';
import { ACCOUNT_ADDRESS, CONTRACT_ADDRESS, ACCOUNT_ADDRESS_2 } from './__mocks__/utils';
import version from '../src/_version';
import * as templateUtils from '../src/lib/ContractTemplates/utils';

let eRC721Mintable: ERC721Mintable;
let signer: Object;
Expand Down Expand Up @@ -39,13 +40,15 @@ describe('SDK', () => {
beforeAll(() => {
signer = {
getChainId: () => 80001,
getTransactionCount: () => 1,
};
});

afterEach(() => {
contractFactoryMock.mockClear();
signer = {
getChainId: () => 80001,
getTransactionCount: () => 1,
};
});

Expand Down Expand Up @@ -99,9 +102,28 @@ describe('SDK', () => {

it('[Deploy] - should deploy when polygon mainnet', async () => {
eRC721Mintable = new ERC721Mintable(signer as unknown as ethers.Wallet);
signer = {
getChainId: () => 137,
};
jest.spyOn(signer, 'getChainId' as any).mockResolvedValue(137);
jest.spyOn(signer, 'getTransactionCount' as any).mockResolvedValue(1);
jest.spyOn(templateUtils as any, 'default').mockResolvedValueOnce({
nonce: 1,
maxFeePerGas: '0.001',
maxPriorityFeePerGas: '0.001',
gasLimit: '6000000',
});

await eRC721Mintable.deploy({ name: 'name', symbol: 'symbol', contractURI: 'URI' });

expect(ContractFactory.prototype.deploy).toHaveBeenCalledTimes(1);
});

it('[Deploy] - should deploy when polygon mainnet when axios failed', async () => {
eRC721Mintable = new ERC721Mintable(signer as unknown as ethers.Wallet);
jest.spyOn(signer, 'getChainId' as any).mockResolvedValue(137);
jest.spyOn(signer, 'getTransactionCount' as any).mockResolvedValue(1);
jest.spyOn(templateUtils as any, 'default').mockResolvedValue({
gas: '6000000',
});

await eRC721Mintable.deploy({ name: 'name', symbol: 'symbol', contractURI: 'URI' });

expect(ContractFactory.prototype.deploy).toHaveBeenCalledTimes(1);
Expand Down
37 changes: 34 additions & 3 deletions test/ERC721UserMintable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { BigNumber, Contract, ContractFactory, ethers, utils } from 'ethers';
import ERC721UserMintable from '../src/lib/ContractTemplates/ERC721UserMintable';
import version from '../src/_version';
import { ACCOUNT_ADDRESS, CONTRACT_ADDRESS, ACCOUNT_ADDRESS_2 } from './__mocks__/utils';
import * as templateUtils from '../src/lib/ContractTemplates/utils';

let eRC721UserMintable: ERC721UserMintable;
let signer: Object;
Expand Down Expand Up @@ -59,13 +60,15 @@ describe('SDK', () => {
beforeAll(() => {
signer = {
getChainId: () => 80001,
getTransactionCount: () => 1,
};
});

afterEach(() => {
contractFactoryMock.mockClear();
signer = {
getChainId: () => 80001,
getTransactionCount: () => 1,
};
});

Expand Down Expand Up @@ -256,9 +259,37 @@ describe('SDK', () => {

it('[Deploy] - should deploy when polygon mainnet', async () => {
eRC721UserMintable = new ERC721UserMintable(signer as unknown as ethers.Wallet);
signer = {
getChainId: () => 137,
};
jest.spyOn(signer, 'getChainId' as any).mockResolvedValue(137);
jest.spyOn(signer, 'getTransactionCount' as any).mockResolvedValue(1);
jest.spyOn(templateUtils as any, 'default').mockResolvedValueOnce({
nonce: 1,
maxFeePerGas: '0.001',
maxPriorityFeePerGas: '0.001',
gasLimit: '6000000',
});

await eRC721UserMintable.deploy({
name: 'name',
symbol: 'symbol',
baseURI: 'URI',
contractURI: 'contractURI',
maxSupply: 10,
price: '1',
maxTokenRequest: 1,
gas: '250',
});

expect(ContractFactory.prototype.deploy).toHaveBeenCalledTimes(1);
});

it('[Deploy] - should deploy when polygon mainnet when axios failed', async () => {
eRC721UserMintable = new ERC721UserMintable(signer as unknown as ethers.Wallet);
jest.spyOn(signer, 'getChainId' as any).mockResolvedValue(137);
jest.spyOn(signer, 'getTransactionCount' as any).mockResolvedValue(1);
jest.spyOn(templateUtils as any, 'default').mockResolvedValue({
gas: '6000000',
});

await eRC721UserMintable.deploy({
name: 'name',
symbol: 'symbol',
Expand Down

0 comments on commit 0653f67

Please sign in to comment.