-
Notifications
You must be signed in to change notification settings - Fork 205
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
9 changed files
with
436 additions
and
0 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,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]); | ||
}); | ||
}); |
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 { 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', | ||
}; | ||
} | ||
} |
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,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, | ||
]); | ||
}); | ||
}); |
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,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', | ||
}; | ||
} | ||
} |
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,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, | ||
]); | ||
}); | ||
}); |
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,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', | ||
}; | ||
} | ||
} |
Oops, something went wrong.