Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update README TypeScript example #174

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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({
// Build an order packet for a market swap
const orderPacket = marketState.getSwapOrderPacket({
side: Phoenix.Side.Bid,
inAmount: 100,
trader: trader.publicKey,
});

// 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);
}
Expand Down