본문으로 건너뛰기

스왑

DeepBookV3는 automatic market maker (AMM)에서 흔히 볼 수 있는 스왑-like interface를 제공한다. DeepBookV3 SDK는 이 interface의 기능을 활용하는 함수를 제공한다. 자세한 내용은 API 섹션의 스왑를 참조하라.

스왑 함수​

SDK는 베이스 자산과 견적 자산 사이의 스왑을 수행하기 위한 다음 함수를 제공한다.

swapExactBaseForQuote​

swapExactBaseForQuote를 사용하여 정확한 base amount를 quote amount로 스왑한다. 이 호출은 Transaction 객체를 받는 함수를 반환한다.

매개변수

  • params: 스왑 파라미터를 나타내는 SwapParams 객체이다.
swapExactBaseForQuote({ params: SwapParams });

swapExactQuoteForBase​

swapExactQuoteForBase를 사용하여 정확한 quote amount를 base amount로 스왑한다. 이 호출은 Transaction 객체를 받는 함수를 반환한다.

매개변수

  • params: 스왑 파라미터를 나타내는 SwapParams 객체이다.
swapExactQuoteForBase({ params: SwapParams });

swapExactQuantity​

swapExactQuantity를 사용하여 BalanceManager를 사용하지 않고 어느 방향으로든(base to quote 또는 quote to base) 정확한 quantity를 스왑한다. 이 호출은 Transaction 객체를 받는 함수를 반환한다.

매개변수

  • params: 다음을 포함하는 SwapParams & { isBaseToCoin: boolean } 객체이다:
    • poolKey: 풀을 식별하는 String이다.
    • amount: 스왑할 amount를 나타내는 Number이다.
    • deepAmount: 수수료에 사용할 DEEP amount를 나타내는 Number이다.
    • minOut: 최소 출력 amount를 나타내는 Number이다.
    • isBaseToCoin: 스왑 방향을 나타내는 Boolean이다(true = base to quote).
    • baseCoin: base 코인 입력을 위한 optional TransactionArgument이다.
    • quoteCoin: quote 코인 입력을 위한 optional TransactionArgument이다.
    • deepCoin: DEEP 코인 입력을 위한 optional TransactionArgument이다.

swapExactQuantityWithManager​

swapExactQuantityWithManager를 사용하여 BalanceManager로 정확한 quantity를 스왑한다. 이 호출은 Transaction 객체를 받는 함수를 반환한다.

매개변수

  • params: 다음을 포함하는 SwapWithManagerParams & { isBaseToCoin: boolean } 객체이다:
    • poolKey: 풀을 식별하는 String이다.
    • balanceManagerKey: BalanceManager를 식별하는 String이다.
    • amount: 스왑할 amount를 나타내는 Number이다.
    • minOut: 최소 출력 amount를 나타내는 Number이다.
    • isBaseToCoin: 스왑 방향을 나타내는 Boolean이다(true = base to quote).
    • tradeCap: trade capability를 위한 optional TransactionArgument이다.
    • depositCap: 예치 capability를 위한 optional TransactionArgument이다.
    • withdrawCap: 출금 capability를 위한 optional TransactionArgument이다.
    • baseCoin: base 코인 입력을 위한 optional TransactionArgument이다.
    • quoteCoin: quote 코인 입력을 위한 optional TransactionArgument이다.

예시​

다음 예시는 DeepBookMarketMaker class에 넣을 수 있는 custom 스왑 함수를 보여준다. Base 코인, quote 코인, deep 코인은 인자로 명시적으로 전달하지 않는 한 사용자 주소에서 사용 가능한 코인을 기준으로 자동 결정된다. 사용자는 출력 코인을 자신의 주소로 전송하거나 그 출력을 사용해 다른 작업을 실행할 수 있다.

swapExactBaseForQuote = (tx: Transaction) => {
const [baseOut, quoteOut, deepOut] = this.deepBook.swapExactBaseForQuote({
poolKey: 'SUI_DBUSDC',
amount: 1, // amount of SUI to swap
deepAmount: 1, // amount of DEEP to pay as fees, excess is returned
minOut: 0.1, // minimum amount of DBUSDC to receive or transaction fails
})(tx);

// Transfer received coins to own address
tx.transferObjects([baseOut, quoteOut, deepOut], this.getActiveAddress());
};

swapExactQuoteForBase = (tx: Transaction) => {
const [baseOut, quoteOut, deepOut] = this.deepBook.swapExactQuoteForBase({
poolKey: 'SUI_DBUSDC',
amount: 1, // amount of DBUSDC to swap
deepAmount: 1, // amount of DEEP to pay as fees, excess is returned
minOut: 0.1, // minimum amount of SUI to receive or transaction fails
})(tx);

// Transfer received coins to own address
tx.transferObjects([baseOut, quoteOut, deepOut], this.getActiveAddress());
};