TypeScript SDK
OptiFi SDK 
TypeScript interfaces for working with the on-chain OptiFi system:
Configuration
Configuration variables can be specified either through environment variables, or provided to initializeContext at runtime
Optional Environment Variables:
- OPTIFI_WALLET: the filepath of a Solana wallet
- OPTIFI_PROGRAM_ID: The ID of the on chain OptiFi program to interact with. If you don't want to deploy the OptiFi program by yourself, try using the program id deployed on devnet:- DeVoPfWrDn2UTA1MbSfagvpQxA91MpNFQnhHRw19dK34
How to initialize a new OptiFi Exchange
- Bootstrap a new exchange(do only once) 
npx ts-node scripts/bootstrap.tsIt will create a new exchange with the OPTIFI_EXCHANGE_ID set in ./constants.ts, (if the exchange id is already bootstrapped, just try using another id), also create OptiFi markets that users can trade on. Each OptiFi market will list one tradable instrument and using a seperate Serum orderbook for placing orders.
- Load the exchange info npx ts-node scripts/loadExchange.ts 
How to trade
- Create user account on the exchange 
npx ts-node scripts/user/createUserAccountIfNotExists.ts- Deposit fund (OptiFi USDC) to user's margin account (3000 usdc by default) 
npx ts-node scripts/user/deposit.ts- Find all available markets and select an OptiFi market to trade 
npx ts-node scripts/findOptifiMarkets.tsIt will print all the market info, find the address of any OptiFi market.
Copy the OptiFi market address you want to trade and paste it to variable market in scripts/constants.ts. It will be imported and used in the scripts of later steps
- Init the user on the OptiFi market (only for first time) 
npx ts-node scripts/user/initUserOnMarket.tsIt basically creates an open orders accounts for the Serum orderbook which is used the OptiFi market. Each user will have one open orders accounts for one OptiFi market.
- Check the current orderbook data 
npx ts-node scripts/loadMarketOrderbook.ts- Place ask/bid orders 
npx ts-node scripts/order/placeOrder.ts- Check the user's open account again after new order placed 
npx ts-node scripts/loadOpenOdersAccount.ts- Check the orderbook again after new order placed 
npx ts-node scripts/loadMarketOrderbook.tsHow to run OMM (AMM was rebranded to OMM in Nov 2022)
- Create an AMM 
npx ts-node scripts/amm/initializeAMMs.tsIt will create two AMMs, one for trading BTC options, another for trading ETH options
- To get the info of created BTC/ETH AMMs, run: 
npx ts-node scripts/amm/findAMMs.ts- Change the variable ammIndx in - scripts/amm/constants.ts, it will be imported and used in later steps. 1 is for BTC amm, 2 for ETH amm
- To deposit USDC into an AMM, set the - amm addressand- amount to deposit, and run:
npx ts-node scripts/amm/ammDeposit.ts- Add the optifi markets to an AMM so that it can trade on that market 
npx ts-node scripts/amm/addInstrumentToAmm.ts- Make sure you have done the last two steps for BOTH BTC and ETH AMMs 
- Now the amm is ready to work, run the following command to crank the AMM and let the AMM update orders on OptiFi markets contineously: 
npx ts-node scripts/amm/crankAMM.tsAfter the BTC or ETH AMM starts to run, orders should be placed on all BTC or ETH related OptiFi markets and updated contineously.
Debug
Error: failed to get info about account : FetchError: request to https://api.devnet.solana.com/ failed, reason: read ECONNRESET
Solution: It's because of the busy Solana network. just wait for a while and try again
Last updated

