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 29, 2025
1 parent 797b7f0 commit 62a3e7c
Show file tree
Hide file tree
Showing 22 changed files with 263 additions and 145 deletions.
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
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
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
14 changes: 8 additions & 6 deletions src/api/getMintDetails.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { CDP_GET_MINT_DETAILS } from '../core/network/definitions/nft';
import { sendRequest } from '../core/network/request';
import { type JSONRPCReferrer, sendRequest } from '../core/network/request';
import type { GetMintDetailsParams, GetMintDetailsResponse } from './types';

/**
* Retrieves mint details for an NFT contract and token ID
*/
export async function getMintDetails({
contractAddress,
takerAddress,
tokenId,
}: GetMintDetailsParams): Promise<GetMintDetailsResponse> {
export async function getMintDetails(
params: GetMintDetailsParams,
_referrer: JSONRPCReferrer = 'api',
): Promise<GetMintDetailsResponse> {
const { contractAddress, takerAddress, tokenId } = params;

try {
const res = await sendRequest<GetMintDetailsParams, GetMintDetailsResponse>(
CDP_GET_MINT_DETAILS,
Expand All @@ -20,6 +21,7 @@ export async function getMintDetails({
tokenId,
},
],
_referrer,
);
if (res.error) {
return {
Expand Down
16 changes: 13 additions & 3 deletions src/api/getPortfolios.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
import { CDP_GET_PORTFOLIO_TOKEN_BALANCES } from '@/core/network/definitions/wallet';
import { sendRequest } from '@/core/network/request';
import type { GetPortfoliosParams, GetPortfoliosResponse } from './types';
import { type JSONRPCReferrer, sendRequest } from '@/core/network/request';
import type {
APIError,
GetPortfoliosParams,
GetPortfoliosResponse,
} from './types';

/**
* Retrieves the portfolios for the provided addresses
*/
export async function getPortfolios({ addresses }: GetPortfoliosParams) {
export async function getPortfolios(
params: GetPortfoliosParams,
_referrer: JSONRPCReferrer = 'api',
): Promise<GetPortfoliosResponse | APIError> {
const { addresses } = params;

try {
const res = await sendRequest<GetPortfoliosParams, GetPortfoliosResponse>(
CDP_GET_PORTFOLIO_TOKEN_BALANCES,
[{ addresses }],
_referrer,
);
if (res.error) {
return {
Expand Down
4 changes: 3 additions & 1 deletion src/api/getSwapQuote.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_QUOTE } from '../core/network/definitions/swap';
import { sendRequest } from '../core/network/request';
import { type JSONRPCReferrer, sendRequest } from '../core/network/request';
import type { SwapQuote } from '../swap/types';
import { getSwapErrorCode } from '../swap/utils/getSwapErrorCode';
import type {
Expand All @@ -16,6 +16,7 @@ import { getAPIParamsForToken } from './utils/getAPIParamsForToken';
*/
export async function getSwapQuote(
params: GetSwapQuoteParams,
_referrer: JSONRPCReferrer = 'api',
): Promise<GetSwapQuoteResponse> {
// Default parameters
const defaultParams = {
Expand Down Expand Up @@ -62,6 +63,7 @@ export async function getSwapQuote(
const res = await sendRequest<SwapAPIParams, SwapQuote>(
CDP_GET_SWAP_QUOTE,
[apiParams],
_referrer,
);
if (res.error) {
return {
Expand Down
28 changes: 17 additions & 11 deletions src/api/getTokenDetails.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
import { CDP_GET_TOKEN_DETAILS } from '../core/network/definitions/nft';
import { sendRequest } from '../core/network/request';
import { type JSONRPCReferrer, sendRequest } from '../core/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> {
export async function getTokenDetails(
params: GetTokenDetailsParams,
_referrer: JSONRPCReferrer = 'api',
): Promise<GetTokenDetailsResponse> {
const { contractAddress, tokenId } = params;

try {
const res = await sendRequest<
GetTokenDetailsParams,
GetTokenDetailsResponse
>(CDP_GET_TOKEN_DETAILS, [
{
contractAddress,
tokenId,
},
]);
>(
CDP_GET_TOKEN_DETAILS,
[
{
contractAddress,
tokenId,
},
],
_referrer,
);
if (res.error) {
return {
code: `${res.error.code}`,
Expand Down
4 changes: 3 additions & 1 deletion src/api/getTokens.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CDP_LIST_SWAP_ASSETS } from '../core/network/definitions/swap';
import { sendRequest } from '../core/network/request';
import { type JSONRPCReferrer, sendRequest } from '../core/network/request';
import type { Token } from '../token/types';
import type { GetTokensOptions, GetTokensResponse } from './types';

Expand All @@ -8,6 +8,7 @@ import type { GetTokensOptions, GetTokensResponse } from './types';
*/
export async function getTokens(
options?: GetTokensOptions,
_referrer: JSONRPCReferrer = 'api',
): Promise<GetTokensResponse> {
// Default filter values
const defaultFilter: GetTokensOptions = {
Expand All @@ -20,6 +21,7 @@ export async function getTokens(
const res = await sendRequest<GetTokensOptions, Token[]>(
CDP_LIST_SWAP_ASSETS,
[filters],
_referrer,
);
if (res.error) {
return {
Expand Down
19 changes: 11 additions & 8 deletions src/buy/components/BuyProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -351,14 +351,17 @@ export function BuyProvider({

try {
const maxSlippage = lifecycleStatus.statusData.maxSlippage;
const response = await buildSwapTransaction({
amount: from.amount,
fromAddress: address,
from: from.token,
maxSlippage: String(maxSlippage),
to: to.token,
useAggregator,
});
const response = await buildSwapTransaction(
{
amount: from.amount,
fromAddress: address,
from: from.token,
maxSlippage: String(maxSlippage),
to: to.token,
useAggregator,
},
'buy',
);
if (isSwapError(response)) {
updateLifecycleStatus({
statusName: 'error',
Expand Down
19 changes: 11 additions & 8 deletions src/buy/utils/getBuyQuote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,17 @@ export async function getBuyQuote({
// switching to and from here
// instead of getting a quote for how much of X do we need to sell to get the input token amount
// we can get a quote for how much of X we will recieve if we sell the input token amount
response = await getSwapQuote({
amount,
amountReference: 'from',
from: to,
maxSlippage,
to: from,
useAggregator,
});
response = await getSwapQuote(
{
amount,
amountReference: 'from',
from: to,
maxSlippage,
to: from,
useAggregator,
},
'buy',
);
}

let formattedFromAmount = '';
Expand Down
5 changes: 4 additions & 1 deletion src/checkout/utils/handlePayRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ export const handlePayRequest = async ({
buildPayTransactionParams.productId = productId;
}

const response = await buildPayTransaction(buildPayTransactionParams);
const response = await buildPayTransaction(
buildPayTransactionParams,
'checkout',
);

if ('error' in response) {
throw new Error(response.error);
Expand Down
9 changes: 9 additions & 0 deletions src/core/network/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,12 @@ export const JSON_HEADERS = {
};
export const JSON_RPC_VERSION = '2.0';
export const ANALYTICS_API_URL = 'https://api.developer.coinbase.com/analytics';
export const ALLOWABLE_REFERRERS = [
'api',
'buy',
'checkout',
'hook',
'nft',
'swap',
'wallet',
];
Loading

0 comments on commit 62a3e7c

Please sign in to comment.