토큰 스왑하기
개요(Overview)
이 가이드는 프론트엔드 인터페이스를 통해 Saros DLMM 풀에서 토큰을 교환하는 방법을 설명합니다. 단계는 다음과 같습니다:
스왑 파라미터 정의하기
온체인
swap명령 호출하기
스왑 유형 (Swap Types)
스왑에는 두 가지 유형이 있습니다:
정확한 인풋 (Exact Input): 교환할 토큰의 정확한 수량을 지정하면, 그에 상응하는 출력 토큰을 받습니다
정확한 아웃풋 (Exact Output): 받고자 하는 토큰의 정확한 수량을 지정하면, 그에 맞는 입력 토큰을 제공합니다
스왑 방향 (Swap Direction)
스왑은 두 가지 방향으로 수행될 수 있습니다:
Y로 스왑 (Swap for Y): 토큰 X를 토큰 Y로 교환
X로 스왑 (Swap for X): 토큰 Y를 토큰 X로 교환
코드 예시 (Example Code)
정확한 입력 스왑 (Exact Input Swap)
import { PublicKey } from "@solana/web3.js";
import { BN } from "@project-serum/anchor";
import { TOKEN_2022_PROGRAM_ID, TOKEN_PROGRAM_ID } from "@solana/spl-token";
// Define the amount of tokens to swap in
const amountIn = new BN(1000000); // Amount of token X or Y to swap in
// Define the minimum amount of tokens to receive (0 for no minimum)
const otherAmountThreshold = new BN(0);
// Set the swap direction (true for X to Y, false for Y to X)
const swapForY = true;
// Derive bin array PDAs
const [binArrayLowerPda] = PublicKey.findProgramAddressSync(
[Buffer.from("bin_array"), pairPda.toBuffer(), Buffer.from([0])],
program.programId
);
const [binArrayUpperPda] = PublicKey.findProgramAddressSync(
[Buffer.from("bin_array"), pairPda.toBuffer(), Buffer.from([1])],
program.programId
);
await program.methods
.swap(amountIn, otherAmountThreshold, swapForY, { exactInput: {} })
.accounts({
pair: pairPda,
binArrayLower: binArrayLowerPda,
binArrayUpper: binArrayUpperPda,
tokenMintX: tokenMintX,
tokenMintY: tokenMintY,
tokenVaultX: tokenVaultXPda,
tokenVaultY: tokenVaultYPda,
userVaultX: userVaultXPda,
userVaultY: userVaultYPda,
tokenProgramX: TOKEN_PROGRAM_ID,
tokenProgramY: TOKEN_PROGRAM_ID,
user: wallet.publicKey,
})
.signers([wallet.payer])
.rpc();
정확한 아웃풋 스왑 (Exact Output Swap)
스왑 실행 방식 (How the Swap Executes)
스왑을 수행하면 다음이 일어납니다:
사용자의 토큰이 풀의 볼트(vault)로 전송됩니다
스왑이 활성 빈(Active Bin)부터 시작하여 하나 이상의 빈(Bins)에서 실행됩니다
단일 빈에서 스왑을 완료할 수 없는 경우, 활성 빈이 이동하며 스왑이 계속됩니다
풀의 볼트에서 사용자에게 토큰이 전송됩니다
프로토콜 수수료가 징수되어 페어(Pair) 계정에 보관됩니다
Last updated