Skip to content

Commit

Permalink
nft api
Browse files Browse the repository at this point in the history
  • Loading branch information
alessey committed Oct 18, 2024
1 parent 3f33904 commit 028d701
Show file tree
Hide file tree
Showing 9 changed files with 436 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/api/buildMintTransaction.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { type Mock, describe, expect, it, vi } from 'vitest';
import { CDP_MINT_TOKEN } from '../network/definitions/nft';
import { sendRequest } from '../network/request';
import { buildMintTransaction } from './buildMintTransaction';
import type { BuildMintTransactionParams } from './types';

vi.mock('../network/request', () => ({
sendRequest: vi.fn(),
}));

describe('buildMintTransaction', () => {
const mockSendRequest = sendRequest as Mock;

const params: BuildMintTransactionParams = {
mintAddress: '0x123',
network: 'networks/base-mainnet',
quantity: 1,
takerAddress: '0x456',
};

it('should return call data when request is successful', async () => {
const mockResponse = {
result: {
callData: {
to: '0x123',
from: '0x456',
data: '0x789',
value: '1',
},
},
};

mockSendRequest.mockResolvedValueOnce(mockResponse);

const result = await buildMintTransaction(params);

expect(result).toEqual(mockResponse.result);
expect(mockSendRequest).toHaveBeenCalledWith(CDP_MINT_TOKEN, [params]);
});

it('should return error details when request fails with an error', async () => {
const mockErrorResponse = {
error: {
code: '404',
message: 'Not Found',
},
};

mockSendRequest.mockResolvedValueOnce(mockErrorResponse);

const result = await buildMintTransaction(params);

expect(result).toEqual({
code: '404',
error: 'Error minting token',
message: 'Not Found',
});
expect(mockSendRequest).toHaveBeenCalledWith(CDP_MINT_TOKEN, [params]);
});

it('should return uncaught error details when an exception is thrown', async () => {
mockSendRequest.mockRejectedValue(new Error('Network Error'));

const result = await buildMintTransaction(params);

expect(result).toEqual({
code: 'uncaught-nft',
error: 'Something went wrong',
message: 'Error minting token',
});
expect(mockSendRequest).toHaveBeenCalledWith(CDP_MINT_TOKEN, [params]);
});
});
42 changes: 42 additions & 0 deletions src/api/buildMintTransaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { CDP_MINT_TOKEN } from '../network/definitions/nft';
import { sendRequest } from '../network/request';
import type { BuildMintTransactionParams, BuildMintTransactionResponse } from './types';

/**
* Retrieves contract to mint an nft
*/
export async function buildMintTransaction({
mintAddress,
network,
quantity,
takerAddress,
}: BuildMintTransactionParams): Promise<BuildMintTransactionResponse> {
try {
const res = await sendRequest<BuildMintTransactionParams, BuildMintTransactionResponse>(
CDP_MINT_TOKEN,
[
{
mintAddress,
network,
quantity,
takerAddress,
},
],
);
if (res.error) {
return {
code: `${res.error.code}`,
error: 'Error minting token',
message: res.error.message,
};
}

return res.result;
} catch (_error) {
return {
code: 'uncaught-nft',
error: 'Something went wrong',
message: 'Error minting token',
};
}
}
87 changes: 87 additions & 0 deletions src/api/getMintDetails.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { type Mock, describe, expect, it, vi } from 'vitest';
import { CDP_GET_MINT_DETAILS } from '../network/definitions/nft';
import { sendRequest } from '../network/request';
import { getMintDetails } from './getMintDetails';
import type { GetMintDetailsParams } from './types';

vi.mock('../network/request', () => ({
sendRequest: vi.fn(),
}));

describe('getMintDetails', () => {
const mockSendRequest = sendRequest as Mock;

const params: GetMintDetailsParams = {
contractAddress: '0x123',
takerAddress: '0x456',
};

it('should return mint details when request is successful', async () => {
const mockResponse = {
result: {
price: {
amount: '1',
currency: 'ETH',
amountUsd: '2000',
},
fee: {
amount: '0.1',
currency: 'ETH',
amountUsd: '200',
},
maxMintsPerWallet: 3,
isEligibleToMint: true,
creatorAddress: '0x123',
totalTokens: '10',
totalOwners: '5',
network: 'networks/base-mainnet',
},
};

mockSendRequest.mockResolvedValueOnce(mockResponse);

const result = await getMintDetails(params);

expect(result).toEqual(mockResponse.result);
expect(mockSendRequest).toHaveBeenCalledWith(CDP_GET_MINT_DETAILS, [
params,
]);
});

it('should return error details when request fails with an error', async () => {
const mockErrorResponse = {
error: {
code: '404',
message: 'Not Found',
},
};

mockSendRequest.mockResolvedValueOnce(mockErrorResponse);

const result = await getMintDetails(params);

expect(result).toEqual({
code: '404',
error: 'Error fetching mint details',
message: 'Not Found',
});
expect(mockSendRequest).toHaveBeenCalledWith(CDP_GET_MINT_DETAILS, [
params,
]);
});

it('should return uncaught error details when an exception is thrown', async () => {
mockSendRequest.mockRejectedValue(new Error('Network Error'));

const result = await getMintDetails(params);

expect(result).toEqual({
code: 'uncaught-nft',
error: 'Something went wrong',
message: 'Error fetching mint details',
});
expect(mockSendRequest).toHaveBeenCalledWith(CDP_GET_MINT_DETAILS, [
params,
]);
});
});
38 changes: 38 additions & 0 deletions src/api/getMintDetails.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { CDP_GET_MINT_DETAILS } from '../network/definitions/nft';
import { sendRequest } from '../network/request';
import type { GetMintDetailsParams, GetMintDetailsResponse } from './types';

/**
* Retrieves mint details for an NFT contract and token ID
*/
export async function getMintDetails({
contractAddress,
takerAddress,
}: GetMintDetailsParams): Promise<GetMintDetailsResponse> {
try {
const res = await sendRequest<GetMintDetailsParams, GetMintDetailsResponse>(
CDP_GET_MINT_DETAILS,
[
{
contractAddress,
takerAddress,
},
],
);
if (res.error) {
return {
code: `${res.error.code}`,
error: 'Error fetching mint details',
message: res.error.message,
};
}

return res.result;
} catch (_error) {
return {
code: 'uncaught-nft',
error: 'Something went wrong',
message: 'Error fetching mint details',
};
}
}
83 changes: 83 additions & 0 deletions src/api/getTokenDetails.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { type Mock, describe, expect, it, vi } from 'vitest';
import { CDP_GET_TOKEN_DETAILS } from '../network/definitions/nft';
import { sendRequest } from '../network/request';
import { getTokenDetails } from './getTokenDetails';
import type { GetTokenDetailsParams } from './types';

vi.mock('../network/request', () => ({
sendRequest: vi.fn(),
}));

describe('getTokenDetails', () => {
const mockSendRequest = sendRequest as Mock;

const params: GetTokenDetailsParams = {
contractAddress: '0x123',
tokenId: '1',
};

it('should return token details when request is successful', async () => {
const mockResponse = {
result: {
name: 'NFT Name',
description: 'NFT Description',
imageUrl: 'https://nft-image-url.com',
animationUrl: 'https://nft-animation-url.com',
mimeType: 'image/png',
ownerAddress: '0x123',
lastSoldPrice: {
amount: '1',
currency: 'ETH',
amountUUSD: '2000',
},
contractType: 'ERC721',
},
};

mockSendRequest.mockResolvedValueOnce(mockResponse);

const result = await getTokenDetails(params);

expect(result).toEqual(mockResponse.result);
expect(mockSendRequest).toHaveBeenCalledWith(CDP_GET_TOKEN_DETAILS, [
params,
]);
});

it('should return error details when request fails with an error', async () => {
const mockErrorResponse = {
error: {
code: '404',
message: 'Not Found',
},
};

mockSendRequest.mockResolvedValueOnce(mockErrorResponse);

const result = await getTokenDetails(params);

expect(result).toEqual({
code: '404',
error: 'Error fetching token details',
message: 'Not Found',
});
expect(mockSendRequest).toHaveBeenCalledWith(CDP_GET_TOKEN_DETAILS, [
params,
]);
});

it('should return uncaught error details when an exception is thrown', async () => {
mockSendRequest.mockRejectedValue(new Error('Network Error'));

const result = await getTokenDetails(params);

expect(result).toEqual({
code: 'uncaught-nft',
error: 'Something went wrong',
message: 'Error fetching token details',
});
expect(mockSendRequest).toHaveBeenCalledWith(CDP_GET_TOKEN_DETAILS, [
params,
]);
});
});
38 changes: 38 additions & 0 deletions src/api/getTokenDetails.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { CDP_GET_TOKEN_DETAILS } from '../network/definitions/nft';
import { sendRequest } from '../network/request';
import type { GetTokenDetailsParams, GetTokenDetailsResponse } from './types';

/**
* Retrieves token details for an NFT contract and token ID
*/
export async function getTokenDetails({
contractAddress,
tokenId,
}: GetTokenDetailsParams): Promise<GetTokenDetailsResponse> {
try {
const res = await sendRequest<
GetTokenDetailsParams,
GetTokenDetailsResponse
>(CDP_GET_TOKEN_DETAILS, [
{
contractAddress,
tokenId,
},
]);
if (res.error) {
return {
code: `${res.error.code}`,
error: 'Error fetching token details',
message: res.error.message,
};
}

return res.result;
} catch (_error) {
return {
code: 'uncaught-nft',
error: 'Something went wrong',
message: 'Error fetching token details',
};
}
}
Loading

0 comments on commit 028d701

Please sign in to comment.