@swapper-finance/sdk
Version:
JavaScript SDK form Swapper
150 lines (134 loc) • 3.52 kB
text/typescript
import { IS_PRODUCTION_ENV } from "@src/config";
import { Chain } from "@src/models";
import { useCallback } from "react";
export interface QuoteRequestPayload {
cryptoCurrencyCode: string;
fiatCurrencyCode: string;
walletAddress: string;
fiatAmount: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
contractExecution?: any;
}
// Define the structure of the quote inside the response
export interface Quote {
quoteId: string;
expireTs: number;
side: string;
cryptoCurrencyCode: string;
cryptoAmount: string;
networkFeeCryptoAmount: string;
fiatCurrencyCode: string;
fiatAmount: string;
processingFeeFiatAmount: string;
partnerFeeFiatAmount: string;
networkFeeFiatAmount: string;
cryptoCostFiatAmount: string;
cryptoPrice: string;
}
// Define the structure of the API response
export interface QuoteResponse {
data: {
quote: Quote;
};
requestId: string;
}
// Define the structure of assets API response
export interface Shift4Asset {
minAmount: string;
symbol: string;
name: string;
chainCode: string;
type: string;
stablecoin: boolean;
precision: number;
tagName: string | null;
contractAddress: string;
addressRegexp: string;
iconUrl: string;
cryptoNetworkIconUrl: string;
nyAllowed: boolean;
displayDecimals: number;
}
export interface AssetsResponse {
data: {
assets: Shift4Asset[];
};
requestId: string;
}
interface ErrorResponse {
status: number;
statusText: string;
data?: string;
}
const SHIFT4_API_URL = IS_PRODUCTION_ENV
? "https://api.crypto.shift4.com/v1"
: "https://api.sandbox.crypto.shift4.com/v1";
export const useShift4Api = () => {
async function _sendRequest<T>(
urlPath: string,
options?: RequestInit,
): Promise<T> {
const response = await fetch(`${SHIFT4_API_URL}${urlPath}`, {
method: options?.method || "GET",
headers: {
"Content-Type": "application/json",
"Cache-Control": "no-cache",
...options?.headers,
},
body: options?.body,
});
if (!response.ok) {
const errorResponse: ErrorResponse = {
status: response.status,
statusText: response.statusText,
};
try {
const errorBody = await response.json();
if (errorBody) {
errorResponse.data = errorBody;
}
} catch (e) {
// ignore parsing error, fall back to status message
}
throw new Error(JSON.stringify(errorResponse));
}
return response.json();
}
const getQuote = useCallback(
async (
payload: QuoteRequestPayload,
srcChain: Chain,
): Promise<QuoteResponse> => {
const urlPath = "/quotes";
const options: RequestInit = {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
"x-public-key": srcChain.shift4Pk,
},
body: JSON.stringify(payload), // Use the payload directly
};
return _sendRequest<QuoteResponse>(urlPath, options);
},
[],
);
const getSupportedAssets = useCallback(
async (chain: Chain): Promise<AssetsResponse> => {
const urlPath = "/assets";
const options: RequestInit = {
method: "GET",
headers: {
Accept: "application/json",
"x-public-key": chain.shift4Pk,
},
};
return _sendRequest<AssetsResponse>(urlPath, options);
},
[],
);
return {
getQuote,
getSupportedAssets,
};
};