TypeScript 示例
以下示例展示最小调用方式。
参考报价
export async function getIndicativeQuote(args: {
baseUrl: string;
jwtToken: string;
srcChainId: number;
dstChainId: number;
tokenIn: string;
tokenOut: string;
amountInWei: string;
}) {
const res = await fetch(`${args.baseUrl}/v1/quote/indicative`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${args.jwtToken}`,
},
body: JSON.stringify({
src_chain_id: args.srcChainId,
dst_chain_id: args.dstChainId,
token_in: args.tokenIn,
token_out: args.tokenOut,
amount_in: args.amountInWei,
}),
});
return res.json();
}
确定性报价
export async function getFirmQuote(args: {
baseUrl: string;
jwtToken: string;
srcChainId: number;
dstChainId: number;
fromAddress: string;
toAddress: string;
tokenIn: string;
tokenOut: string;
amountInWei: string;
slippagePct: number;
expiryTimeSec: number;
indicativeAmountOutWei?: string;
}) {
const res = await fetch(`${args.baseUrl}/v1/quote/firm`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${args.jwtToken}`,
},
body: JSON.stringify({
src_chain_id: args.srcChainId,
dst_chain_id: args.dstChainId,
from_address: args.fromAddress,
to_address: args.toAddress,
token_in: args.tokenIn,
token_out: args.tokenOut,
amount_in: args.amountInWei,
indicative_amount_out: args.indicativeAmountOutWei,
slippage: args.slippagePct,
expiry_time_sec: args.expiryTimeSec,
}),
});
return res.json();
}
业务错误处理(示例)
export async function callWithBusinessErrorHandling<T>(fn: () => Promise<any>): Promise<T> {
const res = await fn();
if (res?.code === 10000) {
return res.data as T;
}
const code = res?.code;
const message = res?.message ?? "Unknown error";
throw new Error(`${code}: ${message}`);
}