Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add onchainkit-context #1899

Merged
merged 4 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/api/buildMintTransaction.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { RequestContext } from '@/core/network/constants';
import { CDP_MINT_TOKEN } from '@/core/network/definitions/nft';
import { sendRequest } from '@/core/network/request';
import { type Mock, describe, expect, it, vi } from 'vitest';
Expand Down Expand Up @@ -35,7 +36,11 @@ describe('buildMintTransaction', () => {
const result = await buildMintTransaction(params);

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

it('should return error details when request fails with an error', async () => {
Expand All @@ -55,7 +60,11 @@ describe('buildMintTransaction', () => {
error: 'Error building mint transaction',
message: 'Not Found',
});
expect(mockSendRequest).toHaveBeenCalledWith(CDP_MINT_TOKEN, [params]);
expect(mockSendRequest).toHaveBeenCalledWith(
CDP_MINT_TOKEN,
[params],
RequestContext.API,
);
});

it('should return uncaught error details when an exception is thrown', async () => {
Expand All @@ -68,6 +77,10 @@ describe('buildMintTransaction', () => {
error: 'Something went wrong',
message: 'Error building mint transaction',
});
expect(mockSendRequest).toHaveBeenCalledWith(CDP_MINT_TOKEN, [params]);
expect(mockSendRequest).toHaveBeenCalledWith(
CDP_MINT_TOKEN,
[params],
RequestContext.API,
);
});
});
36 changes: 20 additions & 16 deletions src/api/buildMintTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { RequestContext } from '@/core/network/constants';
import { CDP_MINT_TOKEN } from '../core/network/definitions/nft';
import { sendRequest } from '../core/network/request';
import type {
Expand All @@ -8,26 +9,29 @@ import type {
/**
* Retrieves contract to mint an NFT
*/
export async function buildMintTransaction({
mintAddress,
tokenId,
network = '',
quantity,
takerAddress,
}: BuildMintTransactionParams): Promise<BuildMintTransactionResponse> {
export async function buildMintTransaction(
params: BuildMintTransactionParams,
_context: RequestContext = RequestContext.API,
): Promise<BuildMintTransactionResponse> {
const { mintAddress, tokenId, network = '', quantity, takerAddress } = params;

try {
const res = await sendRequest<
BuildMintTransactionParams,
BuildMintTransactionResponse
>(CDP_MINT_TOKEN, [
{
mintAddress,
network,
quantity,
takerAddress,
tokenId,
},
]);
>(
CDP_MINT_TOKEN,
[
{
mintAddress,
network,
quantity,
takerAddress,
tokenId,
},
],
RequestContext.API,
);
if (res.error) {
return {
code: `${res.error.code}`,
Expand Down
25 changes: 16 additions & 9 deletions src/api/buildPayTransaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
CHECKOUT_INVALID_CHARGE_ERROR_MESSAGE,
UNCAUGHT_CHECKOUT_ERROR_MESSAGE,
} from '@/checkout/constants';
import { RequestContext } from '@/core/network/constants';
import {
CDP_CREATE_PRODUCT_CHARGE,
CDP_HYDRATE_CHARGE,
Expand Down Expand Up @@ -50,9 +51,11 @@ describe('buildPayTransaction', () => {
const payTransaction = await buildPayTransaction(mockParams);
expect(payTransaction).toEqual(MOCK_HYDRATE_CHARGE_SUCCESS_RESPONSE.result);
expect(sendRequest).toHaveBeenCalledTimes(1);
expect(sendRequest).toHaveBeenCalledWith(CDP_HYDRATE_CHARGE, [
mockAPIParams,
]);
expect(sendRequest).toHaveBeenCalledWith(
CDP_HYDRATE_CHARGE,
[mockAPIParams],
RequestContext.API,
);
});

it('should return a Pay Transaction with productId', async () => {
Expand All @@ -72,9 +75,11 @@ describe('buildPayTransaction', () => {
MOCK_CREATE_PRODUCT_CHARGE_SUCCESS_RESPONSE.result,
);
expect(sendRequest).toHaveBeenCalledTimes(1);
expect(sendRequest).toHaveBeenCalledWith(CDP_CREATE_PRODUCT_CHARGE, [
mockAPIParams,
]);
expect(sendRequest).toHaveBeenCalledWith(
CDP_CREATE_PRODUCT_CHARGE,
[mockAPIParams],
RequestContext.API,
);
});

it('should return an error if neither chargeId nor productId is provided', async () => {
Expand Down Expand Up @@ -126,8 +131,10 @@ describe('buildPayTransaction', () => {
message: CHECKOUT_INVALID_CHARGE_ERROR_MESSAGE,
});
expect(sendRequest).toHaveBeenCalledTimes(1);
expect(sendRequest).toHaveBeenCalledWith(CDP_HYDRATE_CHARGE, [
mockAPIParams,
]);
expect(sendRequest).toHaveBeenCalledWith(
CDP_HYDRATE_CHARGE,
[mockAPIParams],
RequestContext.API,
);
});
});
44 changes: 27 additions & 17 deletions src/api/buildPayTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { RequestContext } from '@/core/network/constants';
import {
CDP_CREATE_PRODUCT_CHARGE,
CDP_HYDRATE_CHARGE,
Expand All @@ -11,33 +12,42 @@ import type {
} from './types';
import { getPayErrorMessage } from './utils/getPayErrorMessage';

export async function buildPayTransaction({
address,
chargeId,
productId,
}: BuildPayTransactionParams): Promise<BuildPayTransactionResponse> {
export async function buildPayTransaction(
params: BuildPayTransactionParams,
_context: RequestContext = RequestContext.API,
): Promise<BuildPayTransactionResponse> {
const { address, chargeId, productId } = params;

try {
let res: JSONRPCResult<BuildPayTransactionResponse>;
if (chargeId) {
res = await sendRequest<
HydrateChargeAPIParams,
BuildPayTransactionResponse
>(CDP_HYDRATE_CHARGE, [
{
sender: address,
chargeId,
},
]);
>(
CDP_HYDRATE_CHARGE,
[
{
sender: address,
chargeId,
},
],
_context,
);
} else if (productId) {
res = await sendRequest<
CreateProductChargeParams,
BuildPayTransactionResponse
>(CDP_CREATE_PRODUCT_CHARGE, [
{
sender: address,
productId,
},
]);
>(
CDP_CREATE_PRODUCT_CHARGE,
[
{
sender: address,
productId,
},
],
_context,
);
} else {
return {
code: 'AmBPTa01', // Api Module Build Pay Transaction Error 01
Expand Down
63 changes: 40 additions & 23 deletions src/api/buildSwapTransaction.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { RequestContext } from '@/core/network/constants';
import { CDP_GET_SWAP_TRADE } from '@/core/network/definitions/swap';
import { sendRequest } from '@/core/network/request';
import { SwapMessage } from '@/swap/constants';
Expand Down Expand Up @@ -115,9 +116,11 @@ describe('buildSwapTransaction', () => {
expect(quote.fee).toEqual(expectedResponse.fee);
expect(quote.warning).toEqual(expectedResponse.warning);
expect(sendRequest).toHaveBeenCalledTimes(1);
expect(sendRequest).toHaveBeenCalledWith(CDP_GET_SWAP_TRADE, [
mockApiParams,
]);
expect(sendRequest).toHaveBeenCalledWith(
CDP_GET_SWAP_TRADE,
[mockApiParams],
RequestContext.API,
);
});

it('should return a swap with useAggregator=false', async () => {
Expand Down Expand Up @@ -197,9 +200,11 @@ describe('buildSwapTransaction', () => {
expect(quote.fee).toEqual(expectedResponse.fee);
expect(quote.warning).toEqual(expectedResponse.warning);
expect(sendRequest).toHaveBeenCalledTimes(1);
expect(sendRequest).toHaveBeenCalledWith(CDP_GET_SWAP_TRADE, [
mockApiParams,
]);
expect(sendRequest).toHaveBeenCalledWith(
CDP_GET_SWAP_TRADE,
[mockApiParams],
RequestContext.API,
);
});

it('should return an error for an unsupported amount reference', async () => {
Expand Down Expand Up @@ -301,12 +306,16 @@ describe('buildSwapTransaction', () => {
expect(quote.fee).toEqual(expectedResponse.fee);
expect(quote.warning).toEqual(expectedResponse.warning);
expect(sendRequest).toHaveBeenCalledTimes(1);
expect(sendRequest).toHaveBeenCalledWith(CDP_GET_SWAP_TRADE, [
{
slippagePercentage: '30',
...mockApiParams,
},
]);
expect(sendRequest).toHaveBeenCalledWith(
CDP_GET_SWAP_TRADE,
[
{
slippagePercentage: '30',
...mockApiParams,
},
],
RequestContext.API,
);
});

it('should return an error if sendRequest fails', async () => {
Expand All @@ -330,9 +339,11 @@ describe('buildSwapTransaction', () => {
message: '',
});
expect(sendRequest).toHaveBeenCalledTimes(1);
expect(sendRequest).toHaveBeenCalledWith(CDP_GET_SWAP_TRADE, [
mockApiParams,
]);
expect(sendRequest).toHaveBeenCalledWith(
CDP_GET_SWAP_TRADE,
[mockApiParams],
RequestContext.API,
);
});

it('should return an error object from buildSwapTransaction', async () => {
Expand Down Expand Up @@ -361,9 +372,11 @@ describe('buildSwapTransaction', () => {
message: '',
});
expect(sendRequest).toHaveBeenCalledTimes(1);
expect(sendRequest).toHaveBeenCalledWith(CDP_GET_SWAP_TRADE, [
mockApiParams,
]);
expect(sendRequest).toHaveBeenCalledWith(
CDP_GET_SWAP_TRADE,
[mockApiParams],
RequestContext.API,
);
});

it('should return an error object from buildSwapTransaction for invalid `amount` input', async () => {
Expand Down Expand Up @@ -396,10 +409,14 @@ describe('buildSwapTransaction', () => {
};
await buildSwapTransaction(mockParams);
expect(sendRequest).toHaveBeenCalledTimes(1);
expect(sendRequest).toHaveBeenCalledWith(CDP_GET_SWAP_TRADE, [
expect.objectContaining({
slippagePercentage: '30',
}),
]);
expect(sendRequest).toHaveBeenCalledWith(
CDP_GET_SWAP_TRADE,
[
expect.objectContaining({
slippagePercentage: '30',
}),
],
RequestContext.API,
);
});
});
3 changes: 3 additions & 0 deletions src/api/buildSwapTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { RequestContext } from '@/core/network/constants';
import { SwapMessage } from '@/swap/constants';
import { UNSUPPORTED_AMOUNT_REFERENCE_ERROR_CODE } from '@/swap/constants';
import { CDP_GET_SWAP_TRADE } from '../core/network/definitions/swap';
Expand All @@ -17,6 +18,7 @@ import { getSwapTransaction } from './utils/getSwapTransaction';
*/
export async function buildSwapTransaction(
params: BuildSwapTransactionParams,
_context: RequestContext = RequestContext.API,
): Promise<BuildSwapTransactionResponse> {
// Default parameters
const defaultParams = {
Expand Down Expand Up @@ -64,6 +66,7 @@ export async function buildSwapTransaction(
const res = await sendRequest<SwapAPIParams, SwapAPIResponse>(
CDP_GET_SWAP_TRADE,
[apiParams],
_context,
);
if (res.error) {
return {
Expand Down
25 changes: 16 additions & 9 deletions src/api/getMintDetails.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { RequestContext } from '@/core/network/constants';
import { CDP_GET_MINT_DETAILS } from '@/core/network/definitions/nft';
import { sendRequest } from '@/core/network/request';
import { type Mock, describe, expect, it, vi } from 'vitest';
Expand Down Expand Up @@ -43,9 +44,11 @@ describe('getMintDetails', () => {
const result = await getMintDetails(params);

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

it('should return error details when request fails with an error', async () => {
Expand All @@ -65,9 +68,11 @@ describe('getMintDetails', () => {
error: 'Error fetching mint details',
message: 'Not Found',
});
expect(mockSendRequest).toHaveBeenCalledWith(CDP_GET_MINT_DETAILS, [
params,
]);
expect(mockSendRequest).toHaveBeenCalledWith(
CDP_GET_MINT_DETAILS,
[params],
RequestContext.API,
);
});

it('should return uncaught error details when an exception is thrown', async () => {
Expand All @@ -80,8 +85,10 @@ describe('getMintDetails', () => {
error: 'Something went wrong',
message: 'Error fetching mint details',
});
expect(mockSendRequest).toHaveBeenCalledWith(CDP_GET_MINT_DETAILS, [
params,
]);
expect(mockSendRequest).toHaveBeenCalledWith(
CDP_GET_MINT_DETAILS,
[params],
RequestContext.API,
);
});
});
Loading