Skip to content

Commit 8d663fd

Browse files
committed
nft api
1 parent d93de53 commit 8d663fd

File tree

8 files changed

+440
-0
lines changed

8 files changed

+440
-0
lines changed

src/api/getMintDetails.test.ts

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { type Mock, describe, expect, it, vi } from 'vitest';
2+
import { getMintDetails } from './getMintDetails';
3+
import { sendRequest } from '../network/request';
4+
import { CDP_GET_MINT_DETAILS } from '../network/definitions/nft';
5+
import type { GetMintDetailsParams } from './types';
6+
7+
vi.mock('../network/request', () => ({
8+
sendRequest: vi.fn(),
9+
}));
10+
11+
describe('getMintDetails', () => {
12+
const mockSendRequest = sendRequest as Mock;
13+
14+
const params: GetMintDetailsParams = {
15+
contractAddress: '0x123',
16+
takerAddress: '0x456',
17+
};
18+
19+
it('should return mint details when request is successful', async () => {
20+
const mockResponse = {
21+
result: {
22+
price: {
23+
amount: '1',
24+
currency: 'ETH',
25+
amountUsd: '2000',
26+
},
27+
fee: {
28+
amount: '0.1',
29+
currency: 'ETH',
30+
amountUsd: '200',
31+
},
32+
maxMintsPerWallet: 3,
33+
isEligibleToMint: true,
34+
eligibleForCollection: true,
35+
mintCount: 2000,
36+
network: 'networks/base-mainnet',
37+
},
38+
};
39+
40+
mockSendRequest.mockResolvedValueOnce(mockResponse);
41+
42+
const result = await getMintDetails(params);
43+
44+
expect(result).toEqual(mockResponse.result);
45+
expect(mockSendRequest).toHaveBeenCalledWith(CDP_GET_MINT_DETAILS, [
46+
params,
47+
]);
48+
});
49+
50+
it('should return error details when request fails with an error', async () => {
51+
const mockErrorResponse = {
52+
error: {
53+
code: '404',
54+
message: 'Not Found',
55+
},
56+
};
57+
58+
mockSendRequest.mockResolvedValueOnce(mockErrorResponse);
59+
60+
const result = await getMintDetails(params);
61+
62+
expect(result).toEqual({
63+
code: '404',
64+
error: 'Error fetching mint details',
65+
message: 'Not Found',
66+
});
67+
expect(mockSendRequest).toHaveBeenCalledWith(CDP_GET_MINT_DETAILS, [
68+
params,
69+
]);
70+
});
71+
72+
it('should return uncaught error details when an exception is thrown', async () => {
73+
mockSendRequest.mockRejectedValue(new Error('Network Error'));
74+
75+
const result = await getMintDetails(params);
76+
77+
expect(result).toEqual({
78+
code: 'uncaught-nft',
79+
error: 'Something went wrong',
80+
message: 'Error fetching mint details',
81+
});
82+
expect(mockSendRequest).toHaveBeenCalledWith(CDP_GET_MINT_DETAILS, [
83+
params,
84+
]);
85+
});
86+
});

src/api/getMintDetails.ts

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { CDP_GET_MINT_DETAILS } from '../network/definitions/nft';
2+
import { sendRequest } from '../network/request';
3+
import type { GetMintDetailsParams, GetMintDetailsResponse } from './types';
4+
5+
/**
6+
* Retrieves mint details for an NFT contract and token ID
7+
*/
8+
export async function getMintDetails({
9+
contractAddress,
10+
takerAddress,
11+
}: GetMintDetailsParams): Promise<GetMintDetailsResponse> {
12+
try {
13+
const res = await sendRequest<GetMintDetailsParams, GetMintDetailsResponse>(
14+
CDP_GET_MINT_DETAILS,
15+
[
16+
{
17+
contractAddress,
18+
takerAddress,
19+
},
20+
],
21+
);
22+
if (res.error) {
23+
return {
24+
code: `${res.error.code}`,
25+
error: 'Error fetching mint details',
26+
message: res.error.message,
27+
};
28+
}
29+
30+
return res.result;
31+
} catch (_error) {
32+
return {
33+
code: 'uncaught-nft',
34+
error: 'Something went wrong',
35+
message: 'Error fetching mint details',
36+
};
37+
}
38+
}

src/api/getTokenDetails.test.ts

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { type Mock, describe, expect, it, vi } from 'vitest';
2+
import { getTokenDetails } from './getTokenDetails';
3+
import { sendRequest } from '../network/request';
4+
import {
5+
CDP_GET_MINT_DETAILS,
6+
CDP_GET_TOKEN_DETAILS,
7+
} from '../network/definitions/nft';
8+
import type { GetTokenDetailsParams } from './types';
9+
10+
vi.mock('../network/request', () => ({
11+
sendRequest: vi.fn(),
12+
}));
13+
14+
describe('getTokenDetails', () => {
15+
const mockSendRequest = sendRequest as Mock;
16+
17+
const params: GetTokenDetailsParams = {
18+
contractAddress: '0x123',
19+
tokenId: '1',
20+
chainId: 1,
21+
includeFloorPrice: false,
22+
};
23+
24+
it('should return token details when request is successful', async () => {
25+
const mockResponse = {
26+
result: {
27+
name: 'NFT Name',
28+
description: 'NFT Description',
29+
imageUrl: 'https://nft-image-url.com',
30+
animationUrl: 'https://nft-animation-url.com',
31+
mimeType: 'image/png',
32+
ownerAddress: '0x123',
33+
lastSoldPrice: {
34+
amount: '1',
35+
currency: 'ETH',
36+
amountUsd: '2000',
37+
},
38+
contractType: 'ERC721',
39+
},
40+
};
41+
42+
mockSendRequest.mockResolvedValueOnce(mockResponse);
43+
44+
const result = await getTokenDetails(params);
45+
46+
expect(result).toEqual(mockResponse.result);
47+
expect(mockSendRequest).toHaveBeenCalledWith(CDP_GET_TOKEN_DETAILS, [
48+
params,
49+
]);
50+
});
51+
52+
it('should return error details when request fails with an error', async () => {
53+
const mockErrorResponse = {
54+
error: {
55+
code: '404',
56+
message: 'Not Found',
57+
},
58+
};
59+
60+
mockSendRequest.mockResolvedValueOnce(mockErrorResponse);
61+
62+
const result = await getTokenDetails(params);
63+
64+
expect(result).toEqual({
65+
code: '404',
66+
error: 'Error fetching token details',
67+
message: 'Not Found',
68+
});
69+
expect(mockSendRequest).toHaveBeenCalledWith(CDP_GET_TOKEN_DETAILS, [
70+
params,
71+
]);
72+
});
73+
74+
it('should return uncaught error details when an exception is thrown', async () => {
75+
mockSendRequest.mockRejectedValue(new Error('Network Error'));
76+
77+
const result = await getTokenDetails(params);
78+
79+
expect(result).toEqual({
80+
code: 'uncaught-nft',
81+
error: 'Something went wrong',
82+
message: 'Error fetching token details',
83+
});
84+
expect(mockSendRequest).toHaveBeenCalledWith(CDP_GET_MINT_DETAILS, [
85+
params,
86+
]);
87+
});
88+
});

src/api/getTokenDetails.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { CDP_GET_TOKEN_DETAILS } from '../network/definitions/nft';
2+
import { sendRequest } from '../network/request';
3+
import type { GetTokenDetailsParams, GetTokenDetailsResponse } from './types';
4+
5+
/**
6+
* Retrieves token details for an NFT contract and token ID
7+
*/
8+
export async function getTokenDetails({
9+
contractAddress,
10+
tokenId = '1',
11+
chainId,
12+
includeFloorPrice = false,
13+
}: GetTokenDetailsParams): Promise<GetTokenDetailsResponse> {
14+
try {
15+
const res = await sendRequest<
16+
GetTokenDetailsParams,
17+
GetTokenDetailsResponse
18+
>(CDP_GET_TOKEN_DETAILS, [
19+
{
20+
contractAddress,
21+
tokenId,
22+
chainId,
23+
includeFloorPrice,
24+
},
25+
]);
26+
if (res.error) {
27+
return {
28+
code: `${res.error.code}`,
29+
error: 'Error fetching token details',
30+
message: res.error.message,
31+
};
32+
}
33+
34+
return res.result;
35+
} catch (_error) {
36+
return {
37+
code: 'uncaught-nft',
38+
error: 'Something went wrong',
39+
message: 'Error fetching token details',
40+
};
41+
}
42+
}

src/api/mintToken.test.ts

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { type Mock, describe, expect, it, vi } from 'vitest';
2+
import { mintToken } from './mintToken';
3+
import { sendRequest } from '../network/request';
4+
import { CDP_MINT_TOKEN } from '../network/definitions/nft';
5+
import type { MintTokenParams } from './types';
6+
7+
vi.mock('../network/request', () => ({
8+
sendRequest: vi.fn(),
9+
}));
10+
11+
describe('mintToken', () => {
12+
const mockSendRequest = sendRequest as Mock;
13+
14+
const params: MintTokenParams = {
15+
mintAddress: '0x123',
16+
network: 'networks/base-mainnet',
17+
quantity: 1,
18+
takerAddress: '0x456',
19+
};
20+
21+
it('should return call data when request is successful', async () => {
22+
const mockResponse = {
23+
result: {
24+
callData: {
25+
to: '0x123',
26+
from: '0x456',
27+
data: '0x789',
28+
value: '1',
29+
},
30+
},
31+
};
32+
33+
mockSendRequest.mockResolvedValueOnce(mockResponse);
34+
35+
const result = await mintToken(params);
36+
37+
expect(result).toEqual(mockResponse.result);
38+
expect(mockSendRequest).toHaveBeenCalledWith(CDP_MINT_TOKEN, [params]);
39+
});
40+
41+
it('should return error details when request fails with an error', async () => {
42+
const mockErrorResponse = {
43+
error: {
44+
code: '404',
45+
message: 'Not Found',
46+
},
47+
};
48+
49+
mockSendRequest.mockResolvedValueOnce(mockErrorResponse);
50+
51+
const result = await mintToken(params);
52+
53+
expect(result).toEqual({
54+
code: '404',
55+
error: 'Error minting token',
56+
message: 'Not Found',
57+
});
58+
expect(mockSendRequest).toHaveBeenCalledWith(CDP_MINT_TOKEN, [params]);
59+
});
60+
61+
it('should return uncaught error details when an exception is thrown', async () => {
62+
mockSendRequest.mockRejectedValue(new Error('Network Error'));
63+
64+
const result = await mintToken(params);
65+
66+
expect(result).toEqual({
67+
code: 'uncaught-nft',
68+
error: 'Something went wrong',
69+
message: 'Error minting token',
70+
});
71+
expect(mockSendRequest).toHaveBeenCalledWith(CDP_MINT_TOKEN, [params]);
72+
});
73+
});

src/api/mintToken.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { CDP_MINT_TOKEN } from '../network/definitions/nft';
2+
import { sendRequest } from '../network/request';
3+
import type { MintTokenParams, MintTokenResponse } from './types';
4+
5+
/**
6+
* Retrieves token details for an NFT contract and token ID
7+
*/
8+
export async function mintToken({
9+
mintAddress,
10+
network,
11+
quantity,
12+
takerAddress,
13+
}: MintTokenParams): Promise<MintTokenResponse> {
14+
try {
15+
const res = await sendRequest<MintTokenParams, MintTokenResponse>(
16+
CDP_MINT_TOKEN,
17+
[
18+
{
19+
mintAddress,
20+
network,
21+
quantity,
22+
takerAddress,
23+
},
24+
],
25+
);
26+
if (res.error) {
27+
return {
28+
code: `${res.error.code}`,
29+
error: 'Error minting token',
30+
message: res.error.message,
31+
};
32+
}
33+
34+
return res.result;
35+
} catch (_error) {
36+
return {
37+
code: 'uncaught-nft',
38+
error: 'Something went wrong',
39+
message: 'Error minting token',
40+
};
41+
}
42+
}

0 commit comments

Comments
 (0)