01 TypeScript Client SDK
TypeScript SDK for interacting with the 01 Solana program, built on top
of Project Serum.
SDK Docs
Program
| Devnet | Mainnet |
---|
Program ID | Zo1ThtSHMh9tZGECwBDL81WJRL6s3QTHf733Tyko7KQ | Zo1ggzTUKMY5bYnDvT5mtVeZxzf2FaLTbKkmvGUhUQk |
State | KwcWW7WvgSXLJcyjKZJBHLbfriErggzYHpjS9qjVD5F | 71yykwxq1zQqy99PgRsgZJXi2HHK2UDx9G4va7pH6qRv |
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
Symbol | Cluster | Base Lots | Quote Lots | Base Decimals | Normalization Factor |
---|
BTC-PERP | Devnet | 100 | 10 | 6 | n/a |
SOL-PERP | Devnet | 100000000 | 100 | 9 | n/a |
SOL-SQUARE | Devnet | 10000 | 100 | 9 | 1 |
Collaterals
For collateral specs on Mainnet, please visit docs.01.xyz/margin/collateral
Symbol | Cluster | Mint | Decimals |
---|
USDC | Devnet | 7UT1javY6X1M9R2UrPGrwcZ78SX3huaXyETff5hm5YdX | 6 |
SOL | Devnet | So11111111111111111111111111111111111111112 | 9 |
BTC | Devnet | 3n3sMJMnZhgNDaxp6cfywvjHLrV1s34ndfa6xAaYvpRs | 6 |
Spot Swap Markets
Symbol | Cluster | Serum Market Key |
---|
BTC-USDC | Devnet | 9vNzQmmG7c3aXuTdKKULQW2oGrYsfGZ1uRsMtgZ2APJF |
SOL-USDC | Devnet | E5gEK3WUVHzGDB6JSWZEwemJ7JZDZqHmG5gGX6NjgW2v |
Usage examples
Setup | Deposit/ Withdraw/ Swap | Trading
Setup
The following example shows how to run basic setup instructions.
import * as anchor from "@project-serum/anchor";
const provider = anchor.Provider.local("https://api.devnet.solana.com");
const program = createProgram(provider, Cluster.Mainnet);
const state: State = await State.load(program, stateKey);
const margin: Margin = await Margin.create(program, state);
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.
const depositSize = 50_250.25;
const repayOnly = false;
await margin.deposit(usdcMintKey, depositSize, repayOnly);
const withdrawSize = 0.001;
const allowWithdrawBorrow = false;
await margin.withdraw(btcMintKey, withdrawSize, allowWithdrawBorrow);
const buy = true;
const fromSize = 50_000;
const toSize = 1;
const slippage = 0.1;
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.
const price = 50_000;
const size = 0.004;
const isLong = true;
const clientId = 12;
const orderType: OrderType = { limit: {} };
await margin.placePerpOrder({
symbol: MARKET_SYMBOL,
orderType,
isLong,
price,
size,
clientId,
});
const marketKey = state.getMarketKeyBySymbol(MARKET_SYMBOL);
const market = await ZoMarket.load(
program.provider.connection,
marketKey,
{},
DEX_PROGRAM_ID,
);
const orders = await market.loadOrdersForOwner(
program.provider.connection,
margin.control.pubkey,
);
const bids = await market.loadBids(program.provider.connection);
const asks = await market.loadAsks(program.provider.connection);
for (const [price, size] of bids.getL2(20)) {
console.log(price, size);
}
for (const order of asks) {
console.log(
order.orderId,
order.price,
order.size,
order.side,
);
}
const fundingInfo = state.getFundingInfo(MARKET_SYMBOL);
const orderIsLong = true;
const orderId = orders[0];
await margin.cancelPerpOrder({ symbol: MARKET_SYMBOL, isLong: orderIsLong, orderId: orderId });
await margin.cancelPerpOrder({ symbol: MARKET_SYMBOL, clientId: clientId });
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. _ _
_
_