交易对
交易对定义了 AI 代理将交易的资产组合。ONE SDK 提供了用于获取支持的交易对的 API 方法,以及适用于 React Native UI 的现成 OnePairSelector 组件。
getTradingPairs API
从引擎获取完整的支持交易对列表。
import { OneEngineClient } from '@one_deploy/sdk';
const engine = new OneEngineClient({
apiKey: process.env.ONE_API_KEY!,
projectId: process.env.ONE_PROJECT_ID!,
});
const pairs = await engine.getTradingPairs();
pairs.forEach((pair) => {
console.log(`${pair.symbol} -- ${pair.baseAsset}/${pair.quoteAsset}`);
console.log(` Chain: ${pair.chainId}`);
console.log(` Active: ${pair.isActive}`);
});
方法签名
getTradingPairs(): Promise<TradingPair[]>
TradingPair 类型
interface TradingPair {
/** Unique pair identifier, e.g. "BTC_USDT". */
symbol: string;
/** Base asset symbol, e.g. "BTC". */
baseAsset: string;
/** Quote asset symbol, e.g. "USDT". */
quoteAsset: string;
/** Chain ID where this pair is traded. */
chainId: number;
/** Whether the pair is currently available for new orders. */
isActive: boolean;
/** Minimum order size in quote currency. */
minOrderSize: number;
/** Maximum order size in quote currency, or null for unlimited. */
maxOrderSize: number | null;
/** Price precision (decimal places). */
pricePrecision: number;
/** Quantity precision (decimal places). */
quantityPrecision: number;
}
PAIR_ICONS 常量
PAIR_ICONS 常量将交易对符号映射到对应的图标 URI,便于在 UI 中展示交易对 logo。
import { PAIR_ICONS } from '@one_deploy/sdk';
// PAIR_ICONS is a Record<string, string>
const btcIcon = PAIR_ICONS['BTC']; // URI to BTC icon asset
const ethIcon = PAIR_ICONS['ETH']; // URI to ETH icon asset
const usdtIcon = PAIR_ICONS['USDT']; // URI to USDT icon asset
const PAIR_ICONS: Record<string, string> = {
BTC: 'https://assets.one23.io/icons/btc.png',
ETH: 'https://assets.one23.io/icons/eth.png',
USDT: 'https://assets.one23.io/icons/usdt.png',
USDC: 'https://assets.one23.io/icons/usdc.png',
SOL: 'https://assets.one23.io/icons/sol.png',
BNB: 'https://assets.one23.io/icons/bnb.png',
// ... additional assets
};
OnePairSelector 组件
OnePairSelector 是一个 React Native 组件,用于渲染可选择的交易对列表,并附带图标和交易对详情。
仅限 React Native
OnePairSelector 从 @one_deploy/sdk 的 React Native 入口导出,不适用于 Web。
快速开始
import { useState } from 'react';
import { OnePairSelector } from '@one_deploy/sdk';
import type { TradingPair } from '@one_deploy/sdk';
function PairSelection() {
const [selectedPair, setSelectedPair] = useState<TradingPair | null>(null);
return (
<OnePairSelector
strategyId="strat_abc123"
chainId={8453}
onSelect={(pair) => {
setSelectedPair(pair);
console.log('Selected pair:', pair.symbol);
}}
/>
);
}
OnePairSelectorProps
interface OnePairSelectorProps {
/** Strategy ID to filter pairs by supported pairs. */
strategyId: string;
/** Chain ID to filter pairs by chain. */
chainId: number;
/** Currently selected pair symbol. */
selectedPair?: string;
/** Callback fired when the user selects a pair. */
onSelect: (pair: TradingPair) => void;
/** Whether to show pair icons. Defaults to true. */
showIcons?: boolean;
/** Whether to show inactive pairs (greyed out). Defaults to false. */
showInactive?: boolean;
/** Additional style applied to the container. */
style?: ViewStyle;
/** Style applied to each pair item. */
itemStyle?: ViewStyle;
/** Test ID for testing frameworks. */
testID?: string;
}
属性表
| 属性 | 类型 | 必填 | 默认值 | 描述 |
|---|---|---|---|---|
strategyId | string | 是 | -- | 用于筛选交易对的策略 ID |
chainId | number | 是 | -- | 用于筛选可用交易对的链 ID |
selectedPair | string | 否 | -- | 当前选中的交易对符号 |
onSelect | (pair: TradingPair) => void | 是 | -- | 选择回调 |
showIcons | boolean | 否 | true | 显示资产图标 |
showInactive | boolean | 否 | false | 显示非活跃交易对 |
style | ViewStyle | 否 | -- | 容器样式 |
itemStyle | ViewStyle | 否 | -- | 单个交易对项样式 |
testID | string | 否 | -- | 测试 ID |
自定义样式
import { OnePairSelector } from '@one_deploy/sdk';
function StyledPairSelector() {
return (
<OnePairSelector
strategyId="strat_abc123"
chainId={8453}
showIcons={true}
showInactive={false}
style={{
backgroundColor: '#1a1a2e',
borderRadius: 12,
padding: 16,
}}
itemStyle={{
paddingVertical: 12,
borderBottomWidth: 1,
borderBottomColor: '#2d2d44',
}}
onSelect={(pair) => console.log('Pair:', pair.symbol)}
/>
);
}
在自定义列表中使用交易对图标
如果需要完全控制 UI,可以通过 API 获取交易对,并使用 PAIR_ICONS 显示图标。
import { useState, useEffect } from 'react';
import { OneEngineClient, PAIR_ICONS } from '@one_deploy/sdk';
import type { TradingPair } from '@one_deploy/sdk';
function CustomPairList({ engine }: { engine: OneEngineClient }) {
const [pairs, setPairs] = useState<TradingPair[]>([]);
useEffect(() => {
engine.getTradingPairs().then(setPairs);
}, []);
return (
<FlatList
data={pairs.filter((p) => p.isActive)}
keyExtractor={(item) => item.symbol}
renderItem={({ item }) => (
<View style={styles.row}>
<Image source={{ uri: PAIR_ICONS[item.baseAsset] }} style={styles.icon} />
<Text style={styles.symbol}>{item.baseAsset}/{item.quoteAsset}</Text>
<Text style={styles.detail}>
Min: {item.minOrderSize} {item.quoteAsset}
</Text>
</View>
)}
/>
);
}
后续步骤
- 投资周期 -- 选择交易对后选择锁定期。
- 创建订单 -- 使用所选交易对创建订单。
- OnePairSelector 参考 -- 完整的组件 API 参考。