Reference guide for using Orderly React SDK hooks - useOrderEntry, usePositionStream, useOrderbookStream, useCollateral, and more
技能名称: orderly-sdk-react-hooks
详细描述:
@orderly.network/hooks 提供的所有钩子的完整参考。
bash
npm install @orderly.network/hooks @orderly.network/types
typescript
import { OrderlyAppProvider } from @orderly.network/react;
import { QueryClient, QueryClientProvider } from @tanstack/react-query;
const queryClient = new QueryClient();
function App() {
return (
chainFilter={[42161, 421614]}
>
);
}
访问账户状态和操作。
typescript
import { useAccount } from @orderly.network/hooks;
const { account, state } = useAccount();
// 状态
state.status: notConnected | connecting | connected | disconnecting
state.address: string | null
// 账户
account.accountId: string
account.address: string
account.connect(): Promise
account.disconnect(): Promise
account.setAddress(address, options): void
// 示例
function AccountInfo() {
const { account, state } = useAccount();
if (state.status !== connected) {
return ;
}
return (
账户: {account.accountId}
地址: {account.address}
管理钱包连接。
typescript
import { useWalletConnector } from @orderly.network/hooks;
const wallet = useWalletConnector();
// 状态
wallet.connected: boolean
wallet.connecting: boolean
wallet.connectedChain: { id: string } | null
wallet.address: string | null
// 操作
wallet.connect(): Promise
wallet.disconnect(): Promise
wallet.setChain(options): Promise
// 示例
function WalletButton() {
const wallet = useWalletConnector();
if (wallet.connecting) {
return 连接中...;
}
if (wallet.connected) {
return (
return ;
}
创建和提交订单。
typescript
import { useOrderEntry, OrderSide, OrderType } from @orderly.network/hooks;
const {
submit,
setValue,
getValue,
helper,
reset,
isSubmitting,
errors,
} = useOrderEntry(symbol, options);
// 选项
interface OrderEntryOptions {
initialOrder?: {
side?: OrderSide;
order_type?: OrderType;
price?: string;
order_quantity?: string;
};
onSuccess?: (result) => void;
onError?: (error) => void;
}
// 方法
setValue(field: string, value: any): void
getValue(field: string): any
helper.validate(): Promise
submit(): Promise
reset(): void
// 示例
function OrderForm({ symbol }: { symbol: string }) {
const { submit, setValue, getValue, helper, isSubmitting } = useOrderEntry(symbol, {
initialOrder: {
side: OrderSide.BUY,
order_type: OrderType.LIMIT,
},
});
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
const valid = await helper.validate();
if (valid) {
await submit();
}
};
return (
实时流式传输订单。
typescript
import { useOrderStream, OrderStatus } from @orderly.network/hooks;
const [orders, actions] = useOrderStream(options);
// 选项
interface OrderStreamOptions {
status?: OrderStatus | OrderStatus[];
symbol?: string;
side?: OrderSide;
}
// 订单
orders: Order[]
// 订单类型
interface Order {
order_id: number;
symbol: string;
side: BUY | SELL;
order_type: string;
price: string;
order_qty: string;
filled_qty: string;
status: string;
created_at: number;
updated_at: number;
}
// 操作
actions.cancelOrder(orderId: number | string): Promise
actions.cancelAllOrders(options?): Promise
actions.editOrder(orderId, updates): Promise
// 示例
function OpenOrders() {
const [orders, { cancelOrder, cancelAllOrders }] = useOrderStream({
status: OrderStatus.INCOMPLETE,
});
return (
实时流式传输持仓及盈亏。
typescript
import { usePositionStream } from @orderly.network/hooks;
const { rows, aggregated, totalUnrealizedROI, isLoading } = usePositionStream();
// 返回值
rows: Position[]
aggregated: {
totalUnrealizedPnl: number;
totalNotional: number;
totalCollateral: number;
}
totalUnrealizedROI: number
isLoading: boolean
// 持仓类型
interface Position {
symbol: string;
position_qty: number;
averageopenprice: number;
mark_price: number;
unrealized_pnl: number;
unrealizedpnlroi: number;
leverage: number;
liq_price: number;
mmr: number;
imr: number;
notional: number;
}
// 示例
function PositionsSummary() {
const { rows, aggregated, totalUnrealizedROI } = usePositionStream();
return (
收益率: {(totalUnrealizedROI * 100).toFixed(2)}%
| {pos.symbol} | {pos.position_qty} | ${pos.unrealized_pnl.toFixed(2)} |
管理止盈止损订单。
typescript
import { useTPSLOrder } from @orderly.network/hooks;
const [computed, actions] = useTPSLOrder(position);
// 包含 positionqty 和 averageopen_price 的持仓
interface PositionForTPSL {
symbol: string;
position_qty: number;
averageopenprice: number;
}
// 计算值
computed: {
tpTriggerPrice?: string;
slTriggerPrice?: string;
tpOffsetPercentage?: number;
slOffsetPercentage?: number;
}
// 操作
actions.setValue(field:
该技能支持在以下平台通过对话安装:
帮我安装 SkillHub 和 orderly-sdk-react-hooks-1776195741 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 orderly-sdk-react-hooks-1776195741 技能
skillhub install orderly-sdk-react-hooks-1776195741
文件大小: 4.87 KB | 发布时间: 2026-4-15 10:59