Skip to content

Commit

Permalink
chore: add onchainkit-referrer
Browse files Browse the repository at this point in the history
  • Loading branch information
alessey committed Jan 30, 2025
1 parent 797b7f0 commit 1261517
Show file tree
Hide file tree
Showing 36 changed files with 566 additions and 272 deletions.
18 changes: 15 additions & 3 deletions src/api/buildMintTransaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,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],
'api',
);
});

it('should return error details when request fails with an error', async () => {
Expand All @@ -55,7 +59,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],
'api',
);
});

it('should return uncaught error details when an exception is thrown', async () => {
Expand All @@ -68,6 +76,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],
'api',
);
});
});
37 changes: 20 additions & 17 deletions src/api/buildMintTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CDP_MINT_TOKEN } from '../core/network/definitions/nft';
import { sendRequest } from '../core/network/request';
import { type JSONRPCReferrer, sendRequest } from '../core/network/request';
import type {
BuildMintTransactionParams,
BuildMintTransactionResponse,
Expand All @@ -8,26 +8,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,
_referrer: JSONRPCReferrer = '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,
},
],
_referrer,
);
if (res.error) {
return {
code: `${res.error.code}`,
Expand Down
24 changes: 15 additions & 9 deletions src/api/buildPayTransaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,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],
'api',
);
});

it('should return a Pay Transaction with productId', async () => {
Expand All @@ -72,9 +74,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],
'api',
);
});

it('should return an error if neither chargeId nor productId is provided', async () => {
Expand Down Expand Up @@ -126,8 +130,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],
'api',
);
});
});
49 changes: 31 additions & 18 deletions src/api/buildPayTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import {
CDP_CREATE_PRODUCT_CHARGE,
CDP_HYDRATE_CHARGE,
} from '../core/network/definitions/pay';
import { type JSONRPCResult, sendRequest } from '../core/network/request';
import {
type JSONRPCReferrer,
type JSONRPCResult,
sendRequest,
} from '../core/network/request';
import type {
BuildPayTransactionParams,
BuildPayTransactionResponse,
Expand All @@ -11,33 +15,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,
_referrer: JSONRPCReferrer = '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,
},
],
_referrer,
);
} else if (productId) {
res = await sendRequest<
CreateProductChargeParams,
BuildPayTransactionResponse
>(CDP_CREATE_PRODUCT_CHARGE, [
{
sender: address,
productId,
},
]);
>(
CDP_CREATE_PRODUCT_CHARGE,
[
{
sender: address,
productId,
},
],
_referrer,
);
} else {
return {
code: 'AmBPTa01', // Api Module Build Pay Transaction Error 01
Expand Down
62 changes: 39 additions & 23 deletions src/api/buildSwapTransaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,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],
'api',
);
});

it('should return a swap with useAggregator=false', async () => {
Expand Down Expand Up @@ -197,9 +199,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],
'api',
);
});

it('should return an error for an unsupported amount reference', async () => {
Expand Down Expand Up @@ -301,12 +305,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,
},
],
'api',
);
});

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

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

it('should return an error object from buildSwapTransaction for invalid `amount` input', async () => {
Expand Down Expand Up @@ -396,10 +408,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',
}),
],
'api',
);
});
});
4 changes: 3 additions & 1 deletion src/api/buildSwapTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SwapMessage } from '@/swap/constants';
import { UNSUPPORTED_AMOUNT_REFERENCE_ERROR_CODE } from '@/swap/constants';
import { CDP_GET_SWAP_TRADE } from '../core/network/definitions/swap';
import { sendRequest } from '../core/network/request';
import { type JSONRPCReferrer, sendRequest } from '../core/network/request';
import type { SwapAPIResponse } from '../swap/types';
import { getSwapErrorCode } from '../swap/utils/getSwapErrorCode';
import type {
Expand All @@ -17,6 +17,7 @@ import { getSwapTransaction } from './utils/getSwapTransaction';
*/
export async function buildSwapTransaction(
params: BuildSwapTransactionParams,
_referrer: JSONRPCReferrer = 'api',
): Promise<BuildSwapTransactionResponse> {
// Default parameters
const defaultParams = {
Expand Down Expand Up @@ -64,6 +65,7 @@ export async function buildSwapTransaction(
const res = await sendRequest<SwapAPIParams, SwapAPIResponse>(
CDP_GET_SWAP_TRADE,
[apiParams],
_referrer,
);
if (res.error) {
return {
Expand Down
24 changes: 15 additions & 9 deletions src/api/getMintDetails.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,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],
'api',
);
});

it('should return error details when request fails with an error', async () => {
Expand All @@ -65,9 +67,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],
'api',
);
});

it('should return uncaught error details when an exception is thrown', async () => {
Expand All @@ -80,8 +84,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],
'api',
);
});
});
Loading

0 comments on commit 1261517

Please sign in to comment.