Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

@jup-ag/dradex-sdk

Package Overview
Dependencies
Maintainers
5
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@jup-ag/dradex-sdk

> Note: This is an alpha release and in active development. Function names and signatures are subject to change.

latest
npmnpm
Version
0.2.3
Version published
Maintainers
5
Created
Source

Dradex SDK

Note: This is an alpha release and in active development. Function names and signatures are subject to change.

What is Dradex

Dradex is a unified Order Book & AMM exchange, where an AMM liquidity pool is seamlessly integrated with an order book.

Examples

Get market quotes

import { NATIVE_MINT } from "@solana/spl-token";
import { Connection, PublicKey, AccountInfo } from "@solana/web3.js";
import JSBI from "jsbi";
import { DexMarket, SwapMode } from "@dradex/sdk";

const MARKET_ID = new PublicKey("CJept8TLyG9r2GMhttqR98zohVq274XCMiy5oxjTKKBt");
const USDC_MINT = new PublicKey("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v");

async function main() {
  const connection = new Connection("https://api.mainnet-beta.solana.com", "confirmed");

  // create market
  const marketAccountInfo = await connection.getAccountInfo(MARKET_ID);
  const market = new DexMarket(MARKET_ID, marketAccountInfo!);

  // update data
  const accountKeys = market.getAccountsForUpdate();
  const accounts = await connection.getMultipleAccountsInfo(accountKeys);
  const accountMap = toAccountInfoMap(accountKeys, accounts as (AccountInfo<Buffer> | null)[]);
  market.update(accountMap);

  // get quotes
  const quotes = [
    [
      "BUY 1 USD of SOL",
      market.getQuote({
        amount: JSBI.BigInt(1000_000),
        sourceMint: USDC_MINT,
        destinationMint: NATIVE_MINT,
        swapMode: SwapMode.ExactIn,
      }),
    ],
    [
      "SELL 1 SOL",
      market.getQuote({
        amount: JSBI.BigInt(1000_000_000),
        sourceMint: NATIVE_MINT,
        destinationMint: USDC_MINT,
        swapMode: SwapMode.ExactIn,
      }),
    ],
    [
      "SELL 1 USD of SOL",
      market.getQuote({
        amount: JSBI.BigInt(1000_000),
        sourceMint: NATIVE_MINT,
        destinationMint: USDC_MINT,
        swapMode: SwapMode.ExactOut,
      }),
    ],
    [
      "BUY 1 SOL",
      market.getQuote({
        amount: JSBI.BigInt(1000_000_000),
        sourceMint: USDC_MINT,
        destinationMint: NATIVE_MINT,
        swapMode: SwapMode.ExactOut,
      }),
    ],
  ];
  quotes.forEach(([message, quote]) => {
    console.log(message, JSON.stringify(quote, undefined, 2), "\n");
  });
}

function toAccountInfoMap(keys: PublicKey[], accounts: (AccountInfo<Buffer> | null)[]) {
  const accountMap = new Map<string, AccountInfo<Buffer>>();
  accounts.forEach((account, index) => account && accountMap.set(keys[index].toString(), account));
  return accountMap;
}

main();

Create order

import { BN, Provider } from "@project-serum/anchor";
import NodeWallet from "@project-serum/anchor/dist/cjs/nodewallet";
import { Keypair, Connection, PublicKey } from "@solana/web3.js";
import base58 from "bs58";

import { DexProgram, Side } from "@dradex/sdk";

// ENTER YOUR SECRET KEY HERE
const SECRET_KEY = process.env.SECRET_KEY || "<your secret key>";
const KEYPAIR = Keypair.fromSecretKey(base58.decode(SECRET_KEY));

// GMT-USDC
const MARKET_ID = new PublicKey("4v57hiqDBBpmpBU3ta3vmrgE4d8AWdWHyykLzzcfukJp");

async function main() {
  const connection = new Connection(
    "https://api.mainnet-beta.solana.com",
    "confirmed"
  );
  const provider = new Provider(connection, new NodeWallet(KEYPAIR), {
    commitment: "confirmed",
  });
  const dex = new DexProgram({ provider });
  const publicKey = provider.wallet.publicKey;

  // fetch market
  const market = await dex.getMarket(MARKET_ID);
  await dex.loadMarket(market);

  // ensure that the MarketUser & DexUser exists
  const dexUserAddress = await dex.getDexUserAddress(publicKey);
  const marketUserAddress = await dex.getMarketUserAddress(
    market.address,
    publicKey
  );
  const [dexUser, marketUser] = await connection.getMultipleAccountsInfo([
    dexUserAddress,
    marketUserAddress,
  ]);
  const ix = dex.createInstructionSet([]);
  if (!dexUser) {
    ix.add(await dex.createDexUser(publicKey));
  }
  if (!marketUser) {
    ix.add(await dex.createMarketUser(market, publicKey));
  }

  // send a market order to buy 0.01 GMT
  // associated token accounts for both tokens must be available
  ix.add(
    await dex.createOrder(market, {
      amount: new BN(10_000_000),
      clientOrderId: new BN(0),
      limitPrice: new BN(0),
      limitTotal: null,
      minAmountOut: new BN(0),
      orderType: 0,
      side: Side.BID,
    })
  );
  const txId = await ix.exec({ skipPreflight: true });
  console.log("txId:", txId);
}

main();

FAQs

Package last updated on 05 Dec 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts