-
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
14 changed files
with
525 additions
and
12 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,6 @@ | ||
--- | ||
'@moralisweb3/evm-api': minor | ||
'@moralisweb3/evm-utils': minor | ||
--- | ||
|
||
Add Moralis.EvmApi.nft.getWalletNFTCollections() to return all nft collections of a specified address |
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
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
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
42 changes: 42 additions & 0 deletions
42
packages/evmApi/src/resolvers/nft/getWalletNFTCollections.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,42 @@ | ||
import { Camelize, toCamelCase } from '@moralisweb3/core'; | ||
import { EvmChainish, EvmAddressish, EvmAddress, EvmNftCollection } from '@moralisweb3/evm-utils'; | ||
import { operations } from '../../generated/types'; | ||
import { createPaginatedEndpointFactory, createPaginatedEndpoint, PaginatedParams } from '@moralisweb3/api-utils'; | ||
import { EvmChainResolver } from '../EvmChainResolver'; | ||
|
||
type operation = 'getWalletNFTCollections'; | ||
|
||
type QueryParams = operations[operation]['parameters']['query']; | ||
type PathParams = operations[operation]['parameters']['path']; | ||
type ApiParams = QueryParams & PathParams; | ||
export interface Params extends Camelize<Omit<ApiParams, 'chain' | 'address'>>, PaginatedParams { | ||
chain?: EvmChainish; | ||
address: EvmAddressish; | ||
} | ||
|
||
type ApiResult = operations[operation]['responses']['200']['content']['application/json']; | ||
|
||
export const getWalletNFTCollections = createPaginatedEndpointFactory((core) => | ||
createPaginatedEndpoint({ | ||
name: 'getWalletNFTCollections', | ||
urlParams: ['address'], | ||
getUrl: (params: Params) => `/${params.address}/nft/collections`, | ||
apiToResult: (data: ApiResult, params: Params) => | ||
(data.result ?? []).map((collection) => | ||
EvmNftCollection.create( | ||
{ | ||
...toCamelCase(collection), | ||
chain: EvmChainResolver.resolve(params.chain, core), | ||
tokenAddress: EvmAddress.create(collection.token_address, core), | ||
}, | ||
core, | ||
), | ||
), | ||
resultToJson: (data) => data.map((transaction) => transaction.toJSON()), | ||
parseParams: (params: Params): ApiParams => ({ | ||
...params, | ||
chain: EvmChainResolver.resolve(params.chain, core).apiHex, | ||
address: EvmAddress.create(params.address, core).lowercase, | ||
}), | ||
}), | ||
); |
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
106 changes: 106 additions & 0 deletions
106
packages/evmUtils/src/dataTypes/EvmNftCollection/EvmNftCollection.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,106 @@ | ||
import { MoralisCore } from '@moralisweb3/core'; | ||
import { EvmNftCollection } from './EvmNftCollection'; | ||
import { setupEvmUtils } from '../../test/setup'; | ||
import { EvmNftCollectionInput } from './types'; | ||
|
||
const exampleInput: EvmNftCollectionInput = { | ||
chain: '0x1', | ||
tokenAddress: '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045', | ||
contractType: 'ERC721', | ||
name: 'Test NFT', | ||
symbol: 'TEST', | ||
}; | ||
|
||
describe('EvmNftCollection', () => { | ||
let core: MoralisCore; | ||
|
||
beforeAll(() => { | ||
core = setupEvmUtils(); | ||
}); | ||
|
||
beforeEach(() => { | ||
core.config.reset(); | ||
}); | ||
|
||
/** | ||
* Creation | ||
*/ | ||
it('should create a new EvmNftCollection', () => { | ||
const collection = EvmNftCollection.create(exampleInput); | ||
|
||
expect(collection.chain.hex).toBe('0x1'); | ||
expect(collection.tokenAddress.checksum).toBe('0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045'); | ||
expect(collection.contractType).toBe('ERC721'); | ||
expect(collection.name).toBe('Test NFT'); | ||
expect(collection.symbol).toBe('TEST'); | ||
}); | ||
|
||
it('should throw an error when creating with an invalid contractType', () => { | ||
expect(() => EvmNftCollection.create({ ...exampleInput, contractType: 'ERC100' })).toThrowErrorMatchingInlineSnapshot( | ||
`"[C0005] Invalid NFT contract type provided"`, | ||
); | ||
}); | ||
|
||
/** | ||
* Formatting | ||
*/ | ||
it('should return formatting in json', () => { | ||
const collection = EvmNftCollection.create(exampleInput); | ||
|
||
const value = collection.toJSON(); | ||
|
||
expect(value).toStrictEqual({ | ||
"chain": "0x1", | ||
"contractType": "ERC721", | ||
"name": "Test NFT", | ||
"symbol": "TEST", | ||
"tokenAddress": "0xd8da6bf26964af9d7eed9e03e53415d37aa96045", | ||
}); | ||
}); | ||
|
||
it('should return a result object', () => { | ||
const collection = EvmNftCollection.create(exampleInput); | ||
|
||
const value = collection.result; | ||
|
||
expect(value.chain.equals(exampleInput.chain)).toBeTruthy(); | ||
expect(value.contractType).toBe("ERC721"); | ||
expect(value.name).toBe('Test NFT'); | ||
expect(value.symbol).toBe('TEST'); | ||
expect(value.tokenAddress.equals(exampleInput.tokenAddress)).toBeTruthy(); | ||
}); | ||
|
||
/** | ||
* Methods | ||
*/ | ||
it('should check equality of 2 collections of the same value', () => { | ||
const collectionA = EvmNftCollection.create(exampleInput); | ||
const collectionB = EvmNftCollection.create(exampleInput); | ||
|
||
expect(collectionA.equals(collectionB)).toBeTruthy(); | ||
}); | ||
|
||
it('should check equality of 2 collectiones of the same value via a static method', () => { | ||
const collectionA = EvmNftCollection.create(exampleInput); | ||
const collectionB = EvmNftCollection.create(exampleInput); | ||
|
||
expect(EvmNftCollection.equals(collectionA, collectionB)).toBeTruthy(); | ||
}); | ||
|
||
it('should check inequality when chain is different', () => { | ||
const collectionA = EvmNftCollection.create(exampleInput); | ||
const collectionB = EvmNftCollection.create({ ...exampleInput, chain: '0x2' }); | ||
|
||
expect(collectionA.equals(collectionB)).toBeFalsy(); | ||
}); | ||
|
||
it('should check inequality when tokenAddress is different', () => { | ||
const collectionA = EvmNftCollection.create(exampleInput); | ||
const collectionB = EvmNftCollection.create({ | ||
...exampleInput, | ||
tokenAddress: '0xC969E2CBF0Be089d938256ebE3931c2416D8A109', | ||
}); | ||
|
||
expect(collectionA.equals(collectionB)).toBeFalsy(); | ||
}); | ||
}); |
Oops, something went wrong.