Skip to content

Commit

Permalink
feat: add getErc20Burns
Browse files Browse the repository at this point in the history
  • Loading branch information
ErnoW committed Mar 21, 2023
1 parent 24af111 commit 2d99b05
Show file tree
Hide file tree
Showing 19 changed files with 829 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changeset/pretty-moons-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@moralisweb3/common-evm-utils': minor
'@moralisweb3/evm-api': minor
---

Add `getErc20Burns` endpoint at `Moralis.EvmApi.token.getErc20Burns()`
92 changes: 92 additions & 0 deletions packages/common/evmUtils/src/dataTypes/Erc20Burn/Erc20Burn.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { Core } from '@moralisweb3/common-core';
import { Erc20Burn } from './Erc20Burn';
import { setupEvmUtils } from '../../test/setup';
import { Erc20BurnInput } from './types';

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

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

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

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

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

expect(erc20Mint.chain.hex).toBe('0x1');
expect(erc20Mint.fromWallet.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 = Erc20Burn.create(exampleInput);

const value = erc20Mint.toJSON();

expect(value).toStrictEqual({
chain: '0x1',
fromWallet: '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 = Erc20Burn.create(exampleInput);
const erc20MintB = Erc20Burn.create(exampleInput);

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

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

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

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

expect(erc20MintA.equals(erc20MintB)).toBeFalsy();
});
});
182 changes: 182 additions & 0 deletions packages/common/evmUtils/src/dataTypes/Erc20Burn/Erc20Burn.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 { Erc20BurnInput, Erc20BurnData } from './types';

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

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

private _data: Erc20BurnData;

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

static parse = (data: Erc20BurnInput, core: Core): Erc20BurnData => ({
...data,
chain: EvmChain.create(data.chain, core),
contractAddress: EvmAddress.create(data.contractAddress, core),
fromWallet: EvmAddress.create(data.fromWallet, 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 burns
* @param dataA - The first burn to compare
* @param dataB - The second burn to compare
* @example Erc20Burn.equals(dataA, dataB)
* @returns true if the burns are equal, false otherwise
*/
static equals(dataA: Erc20Burn | Erc20BurnInput, dataB: Erc20Burn | Erc20BurnInput) {
const burnA = Erc20Burn.create(dataA);
const burnB = Erc20Burn.create(dataB);

return JSON.stringify(burnA.toJSON()) === JSON.stringify(burnB.toJSON());
}

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

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

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

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

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

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

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

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

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

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

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

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

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

/**
* @returns the logIndex of the burn
* @example burn.logIndex // 2
*/
get logIndex() {
return this._data.logIndex;
}
}
2 changes: 2 additions & 0 deletions packages/common/evmUtils/src/dataTypes/Erc20Burn/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './Erc20Burn';
export * from './types';
50 changes: 50 additions & 0 deletions packages/common/evmUtils/src/dataTypes/Erc20Burn/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,
* fromWallet: "0x09f4fc6081026c85070886599e83f599ecf82405",
* contractAddress: "0xa0e8fed3426391fdb446516799c4d6248e2b2860",
* blockHash: "0xa5f87d4341642b89e3ccb81449e3083032c36fface2c2042941b8bd9afe83f79",
* blockNumber: "16868690",
* blockTimestamp: "2023-03-20T11:48:59.000Z",
* transactionHash: "0xb7b4d321e2ab26c1cde1a2ef49413e21b65dcc663d6de8f75ddbdd868b98b4bf",
* transactionIndex: "4",
* logIndex: "25",
* value: "100000000000000000000000000000"
* }
* ```
*/
export interface Erc20BurnInput {
chain: EvmChainish;
fromWallet: 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 Erc20BurnData {
chain: EvmChain;
fromWallet: EvmAddress;
contractAddress: EvmAddress;
blockHash: string;
blockNumber: BigNumber;
blockTimestamp: Date;
transactionHash: string;
transactionIndex: number;
logIndex: number;
value: BigNumber;
}
1 change: 1 addition & 0 deletions packages/common/evmUtils/src/dataTypes/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './Erc20';
export * from './Erc20Burn';
export * from './Erc20Mint';
export * from './Erc20Transfer';
export * from './Erc20Value';
Expand Down
Loading

0 comments on commit 2d99b05

Please sign in to comment.