본문으로 건너뛰기

주문 SDK

margin manager를 통해 주문을 배치하고 관리하면 DeepBook에서 leveraged trading을 수행할 수 있다. Orders SDK는 지정가 주문와 시장가 주문을 배치하고, 포지션을 관리하며, governance에 참여하기 위한 함수를 제공한다.

주문 함수

DeepBook Margin SDK는 margin manager를 통해 주문을 관리하기 위한 다음 함수를 제공한다.

placeLimitOrder

margin manager를 통해 지정가 주문을 배치하려면 placeLimitOrder를 사용한다. 이 호출은 Transaction 객체를 받는 함수를 반환한다.

매개변수

  • params: 다음을 포함하는 PlaceMarginLimitOrderParams 객체이다:
    • poolKey: DeepBook 풀을 식별하는 String이다
    • marginManagerKey: margin manager를 식별하는 String이다
    • clientOrderId: 클라이언트-side 주문 ID를 위한 String이다
    • price: 주문 price를 나타내는 Number이다
    • quantity: 주문 quantity를 나타내는 Number이다
    • isBid: buy 주문인지 여부를 나타내는 Boolean이다
    • expiration: 주문 expiration 타임스탬프를 위한 optional Number이다
    • orderType: optional OrderType enum이다
    • selfMatchingOption: optional SelfMatchingOptions enum이다
    • payWithDeep: DEEP 토큰으로 fee를 지불하는지 여부를 나타내는 optional boolean이다

placeMarketOrder

margin manager를 통해 시장가 주문을 배치하려면 placeMarketOrder를 사용한다. 이 호출은 Transaction 객체를 받는 함수를 반환한다.

매개변수

  • params: 지정가 주문와 유사한 파라미터를 포함하는 PlaceMarginMarketOrderParams 객체이다(price 및 expiration은 제외한다).

placeReduceOnlyLimitOrder

기존 debt 포지션만 줄일 수 있는 지정가 주문을 배치하려면 placeReduceOnlyLimitOrder를 사용한다. margin trading이 비활성화되어 있고 포지션을 종료해야 할 때 유용하다. 이 호출은 Transaction 객체를 받는 함수를 반환한다.

매개변수

  • params: PlaceMarginLimitOrderParams 객체이다(placeLimitOrder와 같다).

placeReduceOnlyMarketOrder

기존 debt 포지션만 줄일 수 있는 시장가 주문을 배치하려면 placeReduceOnlyMarketOrder를 사용한다. 이 호출은 Transaction 객체를 받는 함수를 반환한다.

매개변수

  • params: PlaceMarginMarketOrderParams 객체이다(placeMarketOrder와 같다).

modifyOrder

기존 주문의 quantity를 수정하려면 modifyOrder를 사용한다. 이 호출은 Transaction 객체를 받는 함수를 반환한다.

경고

orderId는 주문 배치 중 생성되는 프로토콜 orderId이며, 클라이언트 orderId와는 다르다.

매개변수

  • marginManagerKey: margin manager를 식별하는 String이다.
  • orderId: 프로토콜 주문 ID의 String이다.
  • newQuantity: 새 주문 quantity를 나타내는 Number이다.

cancelOrder, cancelOrders, cancelAllOrders

이 함수를 사용해 margin manager의 주문을 취소한다. 이 호출은 Transaction 객체를 받는 함수를 반환한다.

매개변수

  • marginManagerKey: margin manager를 식별하는 String이다.
  • orderId (cancelOrder only): 프로토콜 주문 ID의 String이다.
  • orderIds (cancelOrders only): 프로토콜 주문 ID의 Array이다.

withdrawSettledAmounts

완료된 trade에서 settled amount를 인출하려면 withdrawSettledAmounts를 사용한다. 이 호출은 Transaction 객체를 받는 함수를 반환한다.

매개변수

  • marginManagerKey: margin manager를 식별하는 String이다.

stake, unstake

이 함수를 사용해 거래 수수료 혜택을 위해 margin manager를 통해 DEEP 토큰을 stake 및 unstake한다. 이 호출은 Transaction 객체를 받는 함수를 반환한다.

매개변수

  • marginManagerKey: margin manager를 식별하는 String이다.
  • stakeAmount (stake only): stake할 amount를 나타내는 Number이다.

submitProposal, vote

이 함수를 사용해 margin manager를 통해 풀 governance에 참여한다. 이 호출은 Transaction 객체를 받는 함수를 반환한다.

매개변수

  • marginManagerKey: margin manager를 식별하는 String이다.
  • params (submitProposal): MarginProposalParams, takerFee, 그리고 makerFee가 포함된 stakeRequired 객체이다.
  • proposalId (vote): proposal ID를 나타내는 String이다.

claimRebate

margin manager를 통해 획득한 trading rebate를 claim하려면 claimRebate를 사용한다. 이 호출은 Transaction 객체를 받는 함수를 반환한다.

매개변수

  • marginManagerKey: margin manager를 식별하는 String이다.

withdrawMarginSettledAmounts

Object ID로 임의의 margin manager에 대해 settled amounts를 permissionless하게 인출하려면 withdrawMarginSettledAmounts를 사용한다. Ownership이 필요한 withdrawSettledAmounts와 달리 누구나 호출할 수 있다. 이 호출은 Transaction 객체를 받는 함수를 반환한다.

매개변수

  • poolKey: DeepBook 풀을 식별하는 String이다.
  • marginManagerId: margin manager의 객체 ID를 나타내는 String이다.

updateCurrentPrice

Pyth oracle을 사용해 풀의 current price를 업데이트하려면 updateCurrentPrice를 사용한다. 이 호출은 Transaction 객체를 받는 함수를 반환한다.

매개변수

  • poolKey: DeepBook 풀을 식별하는 String이다.

예시

다음 예시는 일반적인 margin 주문 operation을 시연한다.

지정가 주문 배치

// Params for limit order
interface PlaceMarginLimitOrderParams {
poolKey: string;
marginManagerKey: string;
clientOrderId: string;
price: number;
quantity: number;
isBid: boolean;
expiration?: number | bigint;
orderType?: OrderType;
selfMatchingOption?: SelfMatchingOptions;
payWithDeep?: boolean;
}

// Example: Place a buy limit order for 10 SUI at $2.50
placeLimitOrder = (tx: Transaction) => {
const poolKey = 'SUI_DBUSDC';
const managerKey = 'MARGIN_MANAGER_1';
tx.add(
this.poolProxyContract.placeLimitOrder({
poolKey,
marginManagerKey: managerKey,
clientOrderId: '12345',
price: 2.5,
quantity: 10,
isBid: true,
payWithDeep: true,
}),
);
};

시장가 주문 배치

// Example: Place a market sell order for 5 SUI
placeMarketOrder = (tx: Transaction) => {
const poolKey = 'SUI_DBUSDC';
const managerKey = 'MARGIN_MANAGER_1';
tx.add(
this.poolProxyContract.placeMarketOrder({
poolKey,
marginManagerKey: managerKey,
clientOrderId: '12346',
quantity: 5,
isBid: false,
payWithDeep: true,
}),
);
};

reduce-only 주문 배치

// Example: Place a reduce-only limit order to close a position
placeReduceOnly = (tx: Transaction) => {
const poolKey = 'SUI_DBUSDC';
const managerKey = 'MARGIN_MANAGER_1';
tx.add(
this.poolProxyContract.placeReduceOnlyLimitOrder({
poolKey,
marginManagerKey: managerKey,
clientOrderId: '12347',
price: 2.6,
quantity: 10,
isBid: true, // Buying back to reduce short position
payWithDeep: true,
}),
);
};

주문 수정 및 cancel

// Example: Modify order quantity
modifyExistingOrder = (tx: Transaction) => {
const managerKey = 'MARGIN_MANAGER_1';
const orderId = '123456789'; // Protocol order ID
const newQuantity = 8; // Reduce from 10 to 8
tx.add(this.poolProxyContract.modifyOrder(managerKey, orderId, newQuantity));
};

// Example: Cancel a single order
cancelSingleOrder = (tx: Transaction) => {
const managerKey = 'MARGIN_MANAGER_1';
const orderId = '123456789';
tx.add(this.poolProxyContract.cancelOrder(managerKey, orderId));
};

// Example: Cancel multiple orders
cancelMultipleOrders = (tx: Transaction) => {
const managerKey = 'MARGIN_MANAGER_1';
const orderIds = ['123456789', '987654321'];
tx.add(this.poolProxyContract.cancelOrders(managerKey, orderIds));
};

// Example: Cancel all orders
cancelAll = (tx: Transaction) => {
const managerKey = 'MARGIN_MANAGER_1';
tx.add(this.poolProxyContract.cancelAllOrders(managerKey));
};

stake 및 governance 참여

// Example: Stake DEEP tokens
stakeDeep = (tx: Transaction) => {
const managerKey = 'MARGIN_MANAGER_1';
const stakeAmount = 1000; // Stake 1000 DEEP
tx.add(this.poolProxyContract.stake(managerKey, stakeAmount));
};

// Example: Submit a governance proposal
submitGovernanceProposal = (tx: Transaction) => {
const managerKey = 'MARGIN_MANAGER_1';
tx.add(
this.poolProxyContract.submitProposal(managerKey, {
takerFee: 0.0005, // 5 bps
makerFee: 0.0002, // 2 bps
stakeRequired: 1000,
}),
);
};

// Example: Vote on a proposal
voteOnProposal = (tx: Transaction) => {
const managerKey = 'MARGIN_MANAGER_1';
const proposalId = '0x...';
tx.add(this.poolProxyContract.vote(managerKey, proposalId));
};

// Example: Claim trading rebates
claimTradingRebate = (tx: Transaction) => {
const managerKey = 'MARGIN_MANAGER_1';
tx.add(this.poolProxyContract.claimRebate(managerKey));
};