0.0.1 • Published 2 years ago

@jgensler8_2/zo-client v0.0.1

Weekly downloads
-
License
Apache-2.0
Repository
-
Last release
2 years ago

01 TypeScript Client SDK

TypeScript SDK for interacting with the 01 Solana program, built on top of Project Serum.

SDK Docs

Program

DevnetMainnet
Program IDZo1ThtSHMh9tZGECwBDL81WJRL6s3QTHf733Tyko7KQZo1ggzTUKMY5bYnDvT5mtVeZxzf2FaLTbKkmvGUhUQk
StateKwcWW7WvgSXLJcyjKZJBHLbfriErggzYHpjS9qjVD5F71yykwxq1zQqy99PgRsgZJXi2HHK2UDx9G4va7pH6qRv

Note

  • 01 is in active development so all APIs and protocols are subject to change.
  • The code is unaudited. Use at your own risk.

Getting Started

Installation

Using npm

npm install

Devnet token faucet

You can use the Devnet token faucet to get Devnet only tokens to test on the Devnet program. Replace <WALLET> , <MINT>, and <AMOUNT>

curl -XPOST 'https://devnet-faucet.01.xyz?owner=<WALLET>&mint=<MINT>&amount=<AMOUNT>'

SOL is deposited directly using native lamports. You can get SOL either through Solana cli airdrop or at any airdrop faucet.

Derivatives Markets

For markets specs on Mainnet, please visit docs.01.xyz/trading/perpetual-futures/perpetuals-specs

SymbolClusterBase LotsQuote LotsBase DecimalsNormalization Factor
BTC-PERPDevnet100106n/a
SOL-PERPDevnet1000000001009n/a
SOL-SQUAREDevnet1000010091

Collaterals

For collateral specs on Mainnet, please visit docs.01.xyz/margin/collateral

SymbolClusterMintDecimals
USDCDevnet7UT1javY6X1M9R2UrPGrwcZ78SX3huaXyETff5hm5YdX6
SOLDevnetSo111111111111111111111111111111111111111129
BTCDevnet3n3sMJMnZhgNDaxp6cfywvjHLrV1s34ndfa6xAaYvpRs6

Spot Swap Markets

SymbolClusterSerum Market Key
BTC-USDCDevnet9vNzQmmG7c3aXuTdKKULQW2oGrYsfGZ1uRsMtgZ2APJF
SOL-USDCDevnetE5gEK3WUVHzGDB6JSWZEwemJ7JZDZqHmG5gGX6NjgW2v

Usage examples

Setup | Deposit/ Withdraw/ Swap | Trading

Setup

The following example shows how to run basic setup instructions.

// Setup provider (see anchor docs for more instructions on setting up a provider using your wallet)
import * as anchor from "@project-serum/anchor";

const provider = anchor.Provider.local("https://api.devnet.solana.com"); // or your own endpoint

// Setup the program
const program = createProgram(provider, Cluster.Mainnet); // or Devnet

// Load the state
const state: State = await State.load(program, stateKey);

// Create a margin account (which also creates a control account)
const margin: Margin = await Margin.create(program, state);

// Create a PerpOpenOrders account (to be created for every market)
// This step is optional. Placing an order on a new market will also create the account automatically.
const MARKET_SYMBOL = "BTC-PERP";
await margin.createPerpOpenOrders(MARKET_SYMBOL);

Deposit/ Withdraw/ Swap

The following example shows how to deposit, withdraw and swap from a margin account.

// Deposit
const depositSize = 50_250.25; // $50,250.25 USDC
const repayOnly = false;
await margin.deposit(usdcMintKey, depositSize, repayOnly);

// Withdraw
const withdrawSize = 0.001; // 0.001 BTC
const allowWithdrawBorrow = false;
await margin.withdraw(btcMintKey, withdrawSize, allowWithdrawBorrow);

// Swap USDC to Token B (or vice versa)
const buy = true;
const fromSize = 50_000; // $50,000.00 USDC
const toSize = 1; // 1 BTC
const slippage = 0.1; // 10% slippage tolerance
const allowSwapBorrow = false;
await margin.swap({
  buy,
  tokenMint: btcMintKey,
  fromSize,
  toSize,
  slippage,
  allowBorrow: allowSwapBorrow,
  serumMarket: btcUsdcSerumMarketKey,
});

Trading

The following example shows to perform trading actions.

// Place an order (automatically creates an openOrders account for you if placing on a new market)
const price = 50_000; // $50,000.00 per BTC
const size = 0.004; // 0.004 BTC
const isLong = true;
const clientId = 12; // optional clientId to uniquely tag orders (used in CancelPerpOrderByClientId)
const orderType: OrderType = { limit: {} };

await margin.placePerpOrder({
  symbol: MARKET_SYMBOL,
  orderType,
  isLong,
  price,
  size,
  clientId, // optional arg
});

// Fetch Market
const marketKey = state.getMarketKeyBySymbol(MARKET_SYMBOL);
const market = await ZoMarket.load(
  program.provider.connection,
  marketKey,
  {},
  DEX_PROGRAM_ID,
);

// Fetch orders per user
const orders = await market.loadOrdersForOwner(
  program.provider.connection,
  margin.control.pubkey,
);

// Fetch orderbook
const bids = await market.loadBids(program.provider.connection);
const asks = await market.loadAsks(program.provider.connection);

// L2 orderbook data
for (const [price, size] of bids.getL2(20)) {
  console.log(price, size);
}

// Full orderbook data
for (const order of asks) {
  console.log(
    order.orderId,
    order.price,
    order.size,
    order.side, // 'buy' or 'sell'
  );
}

// Fetch funding info (daily, hourly)
const fundingInfo = state.getFundingInfo(MARKET_SYMBOL);

// Cancel an order by order id
const orderIsLong = true;
const orderId = orders[0]; // or whichever order that is being cancelled
await margin.cancelPerpOrder({ symbol: MARKET_SYMBOL, isLong: orderIsLong, orderId: orderId });

// Cancel an order by client id
await margin.cancelPerpOrder({ symbol: MARKET_SYMBOL, clientId: clientId });

// Settle funds
await margin.settleFunds(MARKET_SYMBOL);

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate. _ _ _ _