From f4fa3fbe45fc61449f91c6f94698b9c9fa1b80f8 Mon Sep 17 00:00:00 2001 From: Samuel Vanderwaal Date: Wed, 10 Jul 2024 10:01:15 -0800 Subject: [PATCH 1/2] update README TypeScript example --- README.md | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 9fa8711..62e1342 100644 --- a/README.md +++ b/README.md @@ -16,8 +16,7 @@ $ cargo run --bin sample -- -r $YOUR_DEVNET_RPC_ENDPOINT ## TypeScript ```TypeScript -import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID } from "@solana/spl-token"; -import { Connection, Keypair, PublicKey, Transaction } from "@solana/web3.js"; +import { Connection, Keypair, Transaction } from "@solana/web3.js"; import base58 from "bs58"; import * as Phoenix from "@ellipsis-labs/phoenix-sdk"; @@ -27,28 +26,36 @@ async function simpleSwap() { // DO NOT USE THIS KEYPAIR IN PRODUCTION const trader = Keypair.fromSecretKey( base58.decode( - "2PKwbVQ1YMFEexCmUDyxy8cuwb69VWcvoeodZCLegqof84DJSTiEd89Ak3so9CiHycZwynesTt1JUDFAPFWEzvVs" - ) + "2PKwbVQ1YMFEexCmUDyxy8cuwb69VWcvoeodZCLegqof84DJSTiEd89Ak3so9CiHycZwynesTt1JUDFAPFWEzvVs", + ), ); // Create a Phoenix client and select a market const phoenix = await Phoenix.Client.create(connection); - const marketConfig = phoenix.marketConfigs.find((market) => market.name === "SOL/USDC"); + + const marketConfig = Array.from(phoenix.marketConfigs.values()).find( + (market) => market.name === "SOL/USDC", + ); if (!marketConfig) { throw new Error("Market config not found"); } + const marketState = phoenix.marketStates.get(marketConfig.marketId); if (!marketState) { throw new Error("Market state not found"); } - // Submit a market order buying 100 USDC worth of SOL - const tx = marketState.getSwapTransaction({ - side: Phoenix.Side.Bid, - inAmount: 100, - trader: trader.publicKey, + // Build an order packet for a market swap + const orderPacket = marketState.getSwapOrderPacket({ + side: Phoenix.Side.Ask, + inAmount: 1, }); + // Submit a market order buying 100 USDC worth of SOL + const tx = new Transaction().add( + marketState.createSwapInstruction(orderPacket, trader.publicKey), + ); + const txId = await connection.sendTransaction(tx, [trader]); console.log("Transaction ID: ", txId); } From 33b9c05da4c5a22024e94853a56939ca21615a1b Mon Sep 17 00:00:00 2001 From: Samuel Vanderwaal Date: Wed, 10 Jul 2024 10:04:45 -0800 Subject: [PATCH 2/2] match original example side and amount --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 62e1342..5712cea 100644 --- a/README.md +++ b/README.md @@ -47,8 +47,8 @@ async function simpleSwap() { // Build an order packet for a market swap const orderPacket = marketState.getSwapOrderPacket({ - side: Phoenix.Side.Ask, - inAmount: 1, + side: Phoenix.Side.Bid, + inAmount: 100, }); // Submit a market order buying 100 USDC worth of SOL