All files / src openOrders.ts

15.15% Statements 5/33
0% Branches 0/5
0% Functions 0/15
15.15% Lines 5/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 2371x   1x 1x 1x           1x                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
import { Connection, PublicKey } from "@solana/web3.js";
import BN from "bn.js";
import { DEX_ID } from "./ids";
import { MarketState, Order, UserAccount } from "./state";
import { closeAccount, initializeAccount } from "./bindings";
import { BuiltInParserName } from "prettier";
 
/**
 * Open Orders class
 */
export class OpenOrders {
  /** Address of the open orders account
   * @private
   */
  private _address: PublicKey;
 
  /**
   * Address of the market of the open order account
   * @private
   */
  private _market: PublicKey;
 
  /**
   * Address of the owner of the open order account
   * @private
   */
  private _owner: PublicKey;
 
  /**
   * Amount of free base tokens
   * @private
   */
  private _baseTokenFree: BN;
 
  /**
   * Total amount of base tokens
   * @private
   */
  private _baseTokenTotal: BN;
 
  /**
   * Amount of free quote tokens
   * @private
   */
  private _quoteTokenFree: BN;
 
  /**
   * Total amount of quote tokens
   * @private
   */
  private _quoteTokenTotal: BN;
 
  /**
   * List of orders of the open order account
   * @private
   */
  private _orders: Order[];
 
  /**
   * Amount of accumulated rebates
   * @private
   */
  private _accumulatedRebates: BN;
 
  constructor(
    address: PublicKey,
    market: PublicKey,
    owner: PublicKey,
    baseTokenFree: BN,
    baseTokenTotal: BN,
    quoteTokenFree: BN,
    quoteTokenTotal: BN,
    orders: Order[],
    accumulatedRebates: BN
  ) {
    this._address = address;
    this._market = market;
    this._owner = owner;
    this._baseTokenFree = baseTokenFree;
    this._baseTokenTotal = baseTokenTotal;
    this._quoteTokenFree = quoteTokenFree;
    this._quoteTokenTotal = quoteTokenTotal;
    this._orders = orders;
    this._accumulatedRebates = accumulatedRebates;
  }
 
  /**
   * Returns the address of the open order account
   */
  get address(): PublicKey {
    return this._address;
  }
 
  /**
   * Returns the market address of the open order account
   */
  get market(): PublicKey {
    return this._market;
  }
 
  /**
   * Returns the owner of the open order account
   */
  get owner(): PublicKey {
    return this._owner;
  }
 
  /**
   * Returns the amount of free base tokens
   */
  get baseTokenFree(): BN {
    return this._baseTokenFree;
  }
 
  /**
   * Returns the total amount of base tokens
   */
  get baseTokenTotal(): BN {
    return this._baseTokenTotal;
  }
 
  /**
   * Returns the amount of free quote tokens
   */
  get quoteTokenFree(): BN {
    return this._quoteTokenFree;
  }
  /**
   * Returns the total amount of quote tokens
   */
  get quoteTokenTotal(): BN {
    return this._quoteTokenTotal;
  }
 
  /**
   * Returns the list of orders of the open orders account
   */
  get orders(): Order[] {
    return this._orders;
  }
 
  /**
   * Returns the amount of accumulated fee rebates
   */
  get accumulatedRebates(): BN {
    return this._accumulatedRebates;
  }
 
  /**
   * Loads the open orders account
   * @param connection The solana connection object to the RPC node
   * @param market The market address of the open orders account
   * @param owner The owner of the open orders account
   * @returns An OpenOrders object
   */
  static async load(
    connection: Connection,
    market: PublicKey,
    owner: PublicKey,
    marketState: MarketState
  ) {
    const [address] = await PublicKey.findProgramAddress(
      [market.toBuffer(), owner.toBuffer()],
      DEX_ID
    );
 
    const userAccount = await UserAccount.retrieve(
      connection,
      address,
      marketState
    );
 
    return new OpenOrders(
      address,
      market,
      owner,
      userAccount.baseTokenFree,
      userAccount.baseTokenLocked.add(userAccount.baseTokenFree),
      userAccount.quoteTokenFree,
      userAccount.quoteTokenFree.add(userAccount.quoteTokenLocked),
      userAccount.orders,
      userAccount.accumulatedRebates
    );
  }
 
  /**
   * Static method to make the transaction instruction that initializes an open order account
   * @param market The market address of the open orders account to initialize
   * @param owner The owner of the open orders account to initialize
   * @param maxOrders The max number of open orders the account will be able to hold
   * @returns A TransactionInstruction object
   */
  static async makeCreateAccountTransaction(
    market: PublicKey,
    owner: PublicKey,
    maxOrders = 20
  ) {
    return await initializeAccount(market, owner, maxOrders);
  }
 
  /**
   *
   * @returns Returns a TransactionInstruction object to close the OpenOrder account
   */
  async makeCloseAccountTransaction() {
    return await closeAccount(this.market, this.owner);
  }
 
  /**
   * Checks if an open order account exists for the owner on the given market
   * @param connection The solana connection object to the RPC node
   * @param market The market address of the open orders account
   * @param owner The owner of the open orders account
   * @returns A boolean
   */
  static async exists(
    connection: Connection,
    market: PublicKey,
    owner: PublicKey
  ) {
    const [address] = await PublicKey.findProgramAddress(
      [market.toBuffer(), owner.toBuffer()],
      DEX_ID
    );
    const info = await connection.getAccountInfo(address);
    return !!info?.data;
  }
 
  static async addressForOwner(market: PublicKey, owner: PublicKey) {
    const [address] = await PublicKey.findProgramAddress(
      [market.toBuffer(), owner.toBuffer()],
      DEX_ID
    );
    return address;
  }
}