Skip to content

Commit

Permalink
feat: add getErc20Mints
Browse files Browse the repository at this point in the history
  • Loading branch information
ErnoW committed Mar 20, 2023
1 parent 24a2f3b commit e96c34a
Show file tree
Hide file tree
Showing 21 changed files with 855 additions and 34 deletions.
7 changes: 7 additions & 0 deletions .changeset/empty-chairs-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@moralisweb3/common-evm-utils': minor
'@moralisweb3/api-utils': minor
'@moralisweb3/evm-api': minor
---

Add `getErc20Mints` endpoint at `Moralis.EvmApi.token.getErc20Mints()`
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ const outputClassName = outputFilePath.split('/').pop().split('.')[0];
const sourcePackageName = process.argv[3];

/* eslint-disable @typescript-eslint/no-var-requires */
const package = require(sourcePackageName);
const sourcePackage = require(sourcePackageName);
const fs = require('fs');
const { determineOperationType } = require('@moralisweb3/common-core');

const uniqueGroupNames = new Set(package.operations.map((o) => o.groupName));
const uniqueGroupNames = new Set(sourcePackage.operations.map((o) => o.groupName));
const sourcePackageImports = new Set();
const apiUtilsPackageImports = new Set();
const corePackageImports = new Set();
Expand All @@ -28,7 +28,7 @@ for (const groupName of uniqueGroupNames) {
public readonly ${groupName} = {
`;

for (const operation of package.operations.filter((o) => o.groupName === groupName)) {
for (const operation of sourcePackage.operations.filter((o) => o.groupName === groupName)) {
const operationVarName = `${operation.name}Operation`;
const requestClassName = `${capitalizeFirst(operation.name)}Request`;
const responseClassName = `${capitalizeFirst(operation.name)}ResponseAdapter`;
Expand Down Expand Up @@ -76,13 +76,10 @@ for (const groupName of uniqueGroupNames) {

const output = `
// CAUTION: This file is automatically generated. Do not edit it manually!
import { ${[...sourcePackageImports].join(', ')} } from '${sourcePackageName}';
import { ${[...apiUtilsPackageImports].join(', ')} } from '@moralisweb3/api-utils';
import { ApiModule, ${[...corePackageImports].join(', ')} } from '@moralisweb3/common-core';
export abstract class ${outputClassName} extends ApiModule {
${bodyOutput}
}
`;
Expand Down
92 changes: 92 additions & 0 deletions packages/common/evmUtils/src/dataTypes/Erc20Mint/Erc20Mint.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { Core } from '@moralisweb3/common-core';
import { Erc20Mint } from './Erc20Mint';
import { setupEvmUtils } from '../../test/setup';
import { Erc20MintInput } from './types';

const exampleInput: Erc20MintInput = {
chain: '0x1',
toWallet: '0x09f4fc6081026c85070886599e83f599ecf82405',
contractAddress: '0xa0e8fed3426391fdb446516799c4d6248e2b2860',
blockHash: '0xa5f87d4341642b89e3ccb81449e3083032c36fface2c2042941b8bd9afe83f79',
blockNumber: '16868690',
blockTimestamp: '2023-03-20T11:48:59.000Z',
transactionHash: '0xb7b4d321e2ab26c1cde1a2ef49413e21b65dcc663d6de8f75ddbdd868b98b4bf',
transactionIndex: 4,
logIndex: 25,
value: '100000000000000000000000000000',
};

describe('Erc20Mint', () => {
let core: Core;

beforeAll(() => {
core = setupEvmUtils();
});

beforeEach(() => {
core.config.reset();
});

/**
* Creation
*/
it('should create a new Erc20Mint', () => {
const erc20Mint = Erc20Mint.create(exampleInput);

expect(erc20Mint.chain.hex).toBe('0x1');
expect(erc20Mint.toWallet.lowercase).toBe('0x09f4fc6081026c85070886599e83f599ecf82405');
expect(erc20Mint.contractAddress.lowercase).toBe('0xa0e8fed3426391fdb446516799c4d6248e2b2860');
expect(erc20Mint.blockNumber.toString()).toBe('16868690');
expect(erc20Mint.blockTimestamp.toISOString()).toBe('2023-03-20T11:48:59.000Z');
expect(erc20Mint.transactionHash).toBe('0xb7b4d321e2ab26c1cde1a2ef49413e21b65dcc663d6de8f75ddbdd868b98b4bf');
expect(erc20Mint.transactionIndex).toBe(4);
expect(erc20Mint.logIndex).toBe(25);
expect(erc20Mint.value.toString()).toBe('100000000000000000000000000000');
});

/**
* Formatting
*/
it('should return formatting in json', () => {
const erc20Mint = Erc20Mint.create(exampleInput);

const value = erc20Mint.toJSON();

expect(value).toStrictEqual({
chain: '0x1',
toWallet: '0x09f4fc6081026c85070886599e83f599ecf82405',
contractAddress: '0xa0e8fed3426391fdb446516799c4d6248e2b2860',
blockHash: '0xa5f87d4341642b89e3ccb81449e3083032c36fface2c2042941b8bd9afe83f79',
blockNumber: '16868690',
blockTimestamp: new Date('2023-03-20T11:48:59.000Z'),
transactionHash: '0xb7b4d321e2ab26c1cde1a2ef49413e21b65dcc663d6de8f75ddbdd868b98b4bf',
transactionIndex: 4,
logIndex: 25,
value: '100000000000000000000000000000',
});
});

/**
* Methods
*/
it('should check equality of 2 erc20Mints of the same value', () => {
const erc20MintA = Erc20Mint.create(exampleInput);
const erc20MintB = Erc20Mint.create(exampleInput);

expect(erc20MintA.equals(erc20MintB)).toBeTruthy();
});

it('should check equality of 2 erc20Mints of the same value via a static method', () => {
const erc20MintA = Erc20Mint.create(exampleInput);
const erc20MintB = Erc20Mint.create(exampleInput);

expect(Erc20Mint.equals(erc20MintA, erc20MintB)).toBeTruthy();
});

it('should check inequality when chain is different', () => {
const erc20MintA = Erc20Mint.create(exampleInput);
const erc20MintB = Erc20Mint.create({ ...exampleInput, chain: '0x2' });

expect(erc20MintA.equals(erc20MintB)).toBeFalsy();
});
});
182 changes: 182 additions & 0 deletions packages/common/evmUtils/src/dataTypes/Erc20Mint/Erc20Mint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
import Core, { MoralisDataObject, BigNumber, dateInputToDate, CoreProvider } from '@moralisweb3/common-core';
import { EvmAddress } from '../EvmAddress';
import { EvmChain } from '../EvmChain';
import { Erc20MintInput, Erc20MintData } from './types';

/**
* The Erc20Mint is a representation of an Erc20 token mint.
*
* @category DataType
*/
export class Erc20Mint implements MoralisDataObject {
/**
* Create a new instance of Erc20Mint from any valid input
* @param data - Erc20Mint instance or valid Erc20MintInput
* @example
* ```
* const mint = Erc20Mint.create(data);
*```
*/
static create(data: Erc20Mint | Erc20MintInput, core?: Core) {
if (data instanceof Erc20Mint) {
return data;
}

const finalCore = core ?? CoreProvider.getDefault();
return new Erc20Mint(data, finalCore);
}

private _data: Erc20MintData;

constructor(data: Erc20MintInput, core: Core) {
this._data = Erc20Mint.parse(data, core);
}

static parse = (data: Erc20MintInput, core: Core): Erc20MintData => ({
...data,
chain: EvmChain.create(data.chain, core),
contractAddress: EvmAddress.create(data.contractAddress, core),
toWallet: EvmAddress.create(data.toWallet, core),
blockTimestamp: dateInputToDate(data.blockTimestamp),
blockNumber: BigNumber.create(data.blockNumber),
value: BigNumber.create(data.value),
transactionIndex: Number(data.transactionIndex),
logIndex: Number(data.logIndex),
});

/**
* Check the equality between two Erc20 mints
* @param dataA - The first mint to compare
* @param dataB - The second mint to compare
* @example Erc20Mint.equals(dataA, dataB)
* @returns true if the mints are equal, false otherwise
*/
static equals(dataA: Erc20Mint | Erc20MintInput, dataB: Erc20Mint | Erc20MintInput) {
const mintA = Erc20Mint.create(dataA);
const mintB = Erc20Mint.create(dataB);

return JSON.stringify(mintA.toJSON()) === JSON.stringify(mintB.toJSON());
}

/**
* Checks the equality of the current mint with another erc20 mint
* @param data - the mint to compare with
* @example mint.equals(data)
* @returns true if the mints are equal, false otherwise
*/
equals(data: Erc20Mint | Erc20MintInput): boolean {
return Erc20Mint.equals(this, data);
}

/**
* @returns a JSON representation of the mint.
* @example mint.toJSON()
*/
toJSON() {
const data = this._data;
return {
...data,
chain: data.chain.format(),
contractAddress: data.contractAddress.format(),
blockNumber: data.blockNumber.toString(),
toWallet: data.toWallet.format(),
value: data.value.toString(),
};
}

/**
* @returns a JSON representation of the mint.
* @example mint.format()
*/
format() {
return this.toJSON();
}

/**
* @returns all the data without casting it to JSON.
* @example mint.result
*/
get result() {
return this._data;
}

/**
* @returns the toWallet of the mint
* @example mint.toWallet // EvmAddress
*/
get toWallet() {
return this._data.toWallet;
}

/**
* @returns the contractAddress of the mint
* @example mint.contractAddress // EvmAddress
*/
get contractAddress() {
return this._data.contractAddress;
}

/**
* @returns the block hash of the mint
* @example mint.blockHash // "0x0372c302e3c52e8f2e15d155e2c545e6d802e479236564af052759253b20fd86"
*/
get blockHash() {
return this._data.blockHash;
}

/**
* @returns the block number of the mint
* @example mint.blockNumber // BigNumber
*/
get blockNumber() {
return this._data.blockNumber;
}

/**
* @returns the block timestamp of the mint
* @example mint.blockTimestamp // Date
*/
get blockTimestamp() {
return this._data.blockTimestamp;
}

/**
* @returns the chain of the mint
* @example mint.chain // EvmChain
*/
get chain() {
return this._data.chain;
}

/**
* @returns the transaction hash of the mint
* @example mint.transactionHash // "0x0372c302e3c52e8f2e15d155e2c545e6d802e479236564af052759253b20fd86"
*/
get transactionHash() {
return this._data.transactionHash;
}

/**
* @returns the value of the mint
* @example mint.value // BigNumber
*/
get value() {
return this._data.value;
}

/**
* @returns the transactionIndex of the mint
* @example mint.transactionIndex // 3
*/
get transactionIndex() {
return this._data.transactionIndex;
}

/**
* @returns the logIndex of the mint
* @example mint.logIndex // 2
*/
get logIndex() {
return this._data.logIndex;
}
}
2 changes: 2 additions & 0 deletions packages/common/evmUtils/src/dataTypes/Erc20Mint/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './Erc20Mint';
export * from './types';
50 changes: 50 additions & 0 deletions packages/common/evmUtils/src/dataTypes/Erc20Mint/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { BigNumber, BigNumberish, DateInput } from '@moralisweb3/common-core';
import { EvmAddressish, EvmAddress } from '../EvmAddress';
import { EvmChain, EvmChainish } from '../EvmChain';

/**
* This can be any object with valid erc20 mint data.
* @example
* ```
* const input = {
* chain: 1,
* toWallet: "0x09f4fc6081026c85070886599e83f599ecf82405",
* contractAddress: "0xa0e8fed3426391fdb446516799c4d6248e2b2860",
* blockHash: "0xa5f87d4341642b89e3ccb81449e3083032c36fface2c2042941b8bd9afe83f79",
* blockNumber: "16868690",
* blockTimestamp: "2023-03-20T11:48:59.000Z",
* transactionHash: "0xb7b4d321e2ab26c1cde1a2ef49413e21b65dcc663d6de8f75ddbdd868b98b4bf",
* transactionIndex: "4",
* logIndex: "25",
* value: "100000000000000000000000000000"
* }
* ```
*/
export interface Erc20MintInput {
chain: EvmChainish;
toWallet: EvmAddressish;
contractAddress: EvmAddressish;
blockHash: string;
blockNumber: BigNumberish;
blockTimestamp: DateInput;
transactionHash: string;
transactionIndex: number;
logIndex: number;
value: BigNumberish;
}

/**
* This is the return type of Erc20Mint
*/
export interface Erc20MintData {
chain: EvmChain;
toWallet: EvmAddress;
contractAddress: EvmAddress;
blockHash: string;
blockNumber: BigNumber;
blockTimestamp: Date;
transactionHash: string;
transactionIndex: number;
logIndex: number;
value: BigNumber;
}
3 changes: 2 additions & 1 deletion packages/common/evmUtils/src/dataTypes/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export * from './Erc20';
export * from './Erc20Mint';
export * from './Erc20Transfer';
export * from './Erc20Value';
export * from './EvmAbiItem';
export * from './EvmAddress';
export * from './EvmBlock';
export * from './EvmBlockDate';
Expand All @@ -15,4 +17,3 @@ export * from './EvmNftTransfer';
export * from './EvmSignature';
export * from './EvmTransaction';
export * from './EvmTransactionLog';
export * from './EvmAbiItem';
Loading

0 comments on commit e96c34a

Please sign in to comment.