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

Commit

Permalink
NFTSDK:546 add tokenAddresses filter
Browse files Browse the repository at this point in the history
  • Loading branch information
sahar-fehri authored and salimtb committed Apr 7, 2023
1 parent 685dbe8 commit 45c7c68
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 1 deletion.
60 changes: 60 additions & 0 deletions e2e/sdk.ERC1155.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,66 @@ describe('SDK - ERC1155 - contract interaction (deploy, load and mint)', () => {
);
expect(createdToken.metadata).not.toBeNull();
});

it('Deploy - Get all nfts by owner address with filter', async () => {
const acc = new Auth(authInfo);
const sdk = new SDK(acc);
const response = await sdk.api.getNFTs({
publicAddress: ownerAddress,
includeMetadata: false,
tokenAddresses: ['0x3ed3894bccacb3de8cf1cd0bda5192f5fa1492ce'],
});
response.assets.forEach((asset: any) => {
expect(asset).not.toHaveProperty('metadata');
expect(asset).toHaveProperty('contract');
expect(asset.contract).toEqual('0x3ed3894bccacb3de8cf1cd0bda5192f5fa1492ce');
expect(asset).toHaveProperty('tokenId');
expect(asset).toHaveProperty('supply');
expect(asset).toHaveProperty('type');
});

const newContract = await sdk.deploy(contractInfo);
const mintHash = await newContract.mint({
to: ownerAddress,
id: 0,
quantity: 3,
});
const receipt = await mintHash.wait();
expect(receipt.status).toEqual(1);

await wait(
async () => {
const resp = await sdk.api.getNFTs({ publicAddress: ownerAddress, includeMetadata: false });
return resp.total > response.total;
},
120000,
1000,
'Waiting for new nft to be available',
);
const response2 = await sdk.api.getNFTs({
publicAddress: ownerAddress,
includeMetadata: false,
});
expect(response2.total).toBeGreaterThan(response.total);
expect(response2.assets[0].metadata).toEqual(undefined);
const response3 = await sdk.api.getNFTs({
publicAddress: ownerAddress,
includeMetadata: true,
tokenAddresses: [newContract.contractAddress],
});
const createdToken: any = await response3.assets.filter(
asset => asset.contract.toLowerCase() === newContract.contractAddress.toLowerCase(),
);
expect(createdToken.metadata).not.toBeNull();

response3.assets.forEach((asset: any) => {
expect(asset).toHaveProperty('contract');
expect(asset.contract).toEqual(newContract.contractAddress);
expect(asset).toHaveProperty('tokenId');
expect(asset).toHaveProperty('supply');
expect(asset).toHaveProperty('type');
});
});
it('Deploy - Get all nfts from a collection', async () => {
const acc = new Auth(authInfo);
const sdk = new SDK(acc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,38 @@ describe('E2E Test: Sdk (read)', () => {
expect(nftPage2.cursor).not.toBeNull();
expect(nftPage2.pageNumber).toEqual(2);
});

it('should return filtered list of NFTs by address', async () => {
const nfts: any = await sdk.api.getNFTs({
publicAddress: <string>process.env.WALLET_PUBLIC_ADDRESS,
tokenAddresses: ['0x8cf4237954b32fe05af80c589fdb815a2d95b4f1'],
});
expect(nfts.account).toEqual(process.env.WALLET_PUBLIC_ADDRESS);
expect(nfts.total).not.toBeNaN();
expect(nfts.pageNumber).toEqual(1);

// Checking that each element has the right data
nfts.assets.forEach((asset: any) => {
expect(asset).not.toHaveProperty('metadata');
expect(asset).toHaveProperty('contract');
expect(asset.contract).toEqual('0x8cf4237954b32fe05af80c589fdb815a2d95b4f1');
expect(asset).toHaveProperty('tokenId');
expect(asset).toHaveProperty('supply');
expect(asset).toHaveProperty('type');
});
});

it('should return error when tokenAddress is not valid', async () => {
await expect(
sdk.api.getNFTs({
publicAddress: <string>process.env.WALLET_PUBLIC_ADDRESS,
tokenAddresses: ['foo'],
}),
).rejects.toThrow(
`missing argument: Invalid token address (location=\"[SDK.getNFTs]\", code=MISSING_ARGUMENT, version=${version}`,
);
});

it('should return an error when using wrong cursor', async () => {
const nfts = async () =>
await sdk.api.getNFTs({
Expand Down
16 changes: 15 additions & 1 deletion src/lib/Api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type PublicAddressOptions = {
publicAddress: string;
includeMetadata?: boolean;
cursor?: string;
tokenAddresses?: string[];
};

type ContractAddressOptions = {
Expand Down Expand Up @@ -145,9 +146,22 @@ export default class Api {
});
}

if (opts.tokenAddresses) {
opts.tokenAddresses.forEach(item => {
if (!utils.isAddress(item)) {
log.throwMissingArgumentError(Logger.message.invalid_token_address, {
location: Logger.location.SDK_GETNFTS,
});
}
});
}

const apiUrl = `${this.apiPath}/accounts/${opts.publicAddress}/assets/nfts`;

const { data } = await this.httpClient.get(apiUrl, { cursor: opts.cursor });
const { data } = await this.httpClient.get(apiUrl, {
cursor: opts.cursor,
tokenAddresses: opts.tokenAddresses,
});

if (!opts.includeMetadata) {
return {
Expand Down
18 changes: 18 additions & 0 deletions test/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,29 @@ describe('Api', () => {

describe('getNFTs', () => {
it('should throw when "address" is not a valid address', async () => {
await expect(() =>
api.getNFTs({ publicAddress: CONTRACT_ADDRESS, tokenAddresses: ['foo'] }),
).rejects.toThrow(
`missing argument: Invalid token address (location="[SDK.getNFTs]", code=MISSING_ARGUMENT, version=${version})`,
);
});

it('should throw when "tokenAddress" is not a valid address', async () => {
await expect(() => api.getNFTs({ publicAddress: 'notAValidAddress' })).rejects.toThrow(
`missing argument: Invalid public address. (location="[SDK.getNFTs]", code=MISSING_ARGUMENT, version=${version})`,
);
});

it('should return the list of NFTs with filter addresses', async () => {
HttpServiceMock.mockResolvedValueOnce(accountNFTsMock as AxiosResponse<any, any>);
const accountNFTs = await api.getNFTs({
publicAddress: CONTRACT_ADDRESS,
tokenAddresses: [CONTRACT_ADDRESS],
});
expect(HttpServiceMock).toHaveBeenCalledTimes(1);
expect((accountNFTs as any).assets[0].contract).toBe(`ETHEREUM:${CONTRACT_ADDRESS}`);
});

it('should return the list of NFTs without metadata', async () => {
HttpServiceMock.mockResolvedValueOnce(accountNFTsMock as AxiosResponse<any, any>);
const accountNFTs = await api.getNFTs({ publicAddress: CONTRACT_ADDRESS });
Expand Down

0 comments on commit 45c7c68

Please sign in to comment.