Place Order
How to place a trading order via API
Place Order
Place a new trading order.
Endpoint: POST /v1/orders/place
Prerequisites: You must have registered a signer first (see Register Signer).
Example:
import axios from "axios";
import { apiClient } from "./risex-core";
import { MarketsId, OrderSide, STPMode, OrderType, TimeInForce, encodePlaceOrderData } from "./types";
import { createPermitParams } from "./create-permit-params";
const placeOrder = async (
orderParams: {
market_id: MarketsId;
size: string;
price: string;
side: OrderSide;
stp_mode: STPMode;
order_type: OrderType;
post_only: boolean;
reduce_only: boolean;
tif: TimeInForce;
expiry: number;
},
signingKey: `0x${string}`, // From registerSigner
signerAddress: string, // From registerSigner
account: string,
) => {
try {
// Contract addresses
const perpContractAddress = '0x68cAcD54a8c93A3186BF50bE6b78B761F728E1b4' as `0x${string}`;
const authContractAddress = '0x8d8708f9D87ef522c1f99DD579BF6A051e34C28E' as `0x${string}`;
// Encode order data
const encodedData = encodePlaceOrderData({
marketId: orderParams.market_id.toString(),
size: BigInt(orderParams.size),
price: BigInt(orderParams.price),
side: orderParams.side,
stpMode: orderParams.stp_mode,
orderType: orderParams.order_type,
postOnly: orderParams.post_only,
reduceOnly: orderParams.reduce_only,
timeInForce: orderParams.tif,
expiry: orderParams.expiry,
});
// Create permit params with signature
const permitParams = await createPermitParams(
encodedData,
signingKey,
account as `0x${string}`,
signerAddress as `0x${string}`,
perpContractAddress,
authContractAddress,
);
const response = await apiClient.post('/v1/orders/place', {
order_params: {
market_id: orderParams.market_id,
size: orderParams.size,
price: orderParams.price,
side: orderParams.side,
stp_mode: orderParams.stp_mode,
order_type: orderParams.order_type,
post_only: orderParams.post_only,
reduce_only: orderParams.reduce_only,
tif: orderParams.tif,
expiry: orderParams.expiry,
},
permit_params: {
account: permitParams.account,
signer: permitParams.signer,
deadline: permitParams.deadline,
signature: permitParams.signature,
nonce: permitParams.nonce,
},
});
const { data } = response.data;
console.log('Order placed successfully!');
console.log('Transaction Hash:', data.transaction_hash);
console.log('Order ID:', data.order_id);
return data;
} catch (error) {
if (axios.isAxiosError(error)) {
console.error('Order failed:', error.response?.data?.message || error.message);
} else {
console.error('Order failed:', (error as Error).message);
}
throw error;
}
};
// Example usage:
// await placeOrder(
// {
// market_id: MarketsId.BTC,
// size: '1000000000000000', // 0.001 BTC
// price: '87467000000000000000000', // 87,467 USDC (18 decimals)
// side: OrderSide.Long,
// stp_mode: STPMode.None,
// order_type: OrderType.Market,
// post_only: false,
// reduce_only: false,
// tif: TimeInForce.ImmediateOrCancel,
// expiry: 0,
// },
// signingKey,
// signerAddress,
// account
// );