-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
855 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
packages/common/evmUtils/src/dataTypes/Erc20Mint/Erc20Mint.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
182
packages/common/evmUtils/src/dataTypes/Erc20Mint/Erc20Mint.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './Erc20Mint'; | ||
export * from './types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.