@optifi/optifi-sdk
TypeScript icon, indicating that this package has built-in type declarations

1.4.27 • Public • Published

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.ts

it 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.ts

it 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.ts

It 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.ts

How to run AMMs

  • create an AMM
npx ts-node scripts/amm/initializeAMMs.ts

It 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 address and 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.ts

After 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

Package Sidebar

Install

npm i @optifi/optifi-sdk

Weekly Downloads

284

Version

1.4.27

License

Apache-2.0

Unpacked Size

2.39 MB

Total Files

500

Last publish

Collaborators

  • leo_optifi
  • 0xpk
  • waynekuo