{"version":3,"file":"Rfq.cjs","sources":["../../../../../src/plugins/rfqModule/models/Rfq.ts"],"sourcesContent":["import { PublicKey } from '@solana/web3.js';\nimport { RfqAccount } from '../accounts';\nimport { assert } from '../../../utils/assert';\nimport {\n  convertTimestampToMilliSeconds,\n  removeDecimals,\n} from '../../../utils/conversions';\nimport {\n  SpotLegInstrument,\n  SpotQuoteInstrument,\n} from '../../../plugins/spotInstrumentModule';\nimport {\n  LegInstrument,\n  QuoteInstrument,\n} from '../../../plugins/instrumentModule';\nimport { Convergence } from '../../../Convergence';\nimport { collateralMintCache } from '../../../plugins/collateralModule';\nimport { FixedSize, fromSolitaFixedSize } from './FixedSize';\nimport { OrderType, fromSolitaOrderType } from './OrderType';\nimport { StoredRfqState, fromSolitaStoredRfqState } from './StoredRfqState';\nimport {\n  PrintTrade,\n  PrintTradeLeg,\n  PrintTradeQuote,\n} from '@/plugins/printTradeModule';\n\n/**\n * This model captures all the relevant information about an RFQ\n * on the Solana blockchain. That includes the Rfq's leg accounts.\n *\n * @group Models\n */\ntype CommonRfq = {\n  /** The address of the Rfq. */\n  readonly address: PublicKey;\n\n  /** The Taker's pubkey address. */\n  readonly taker: PublicKey;\n\n  /** The order type of the Rfq. */\n  readonly orderType: OrderType;\n\n  /** Whether this Rfq is open (no size specified), or a fixed amount of the base asset,\n   * or a fixed amount of the quote asset. */\n  readonly size: FixedSize;\n\n  /** The time at which this Rfq was created. */\n  readonly creationTimestamp: number;\n\n  /** The number of seconds during which this Rfq can be responded to. */\n  readonly activeWindow: number;\n\n  /** The number of seconds within which this Rfq must be settled\n   *  after starting the settlement process. */\n  readonly settlingWindow: number;\n\n  /** The combined size of all legs of Rfq. This must include the sizes\n   *  of any legs to be added in the future. */\n  readonly expectedLegsSize: number;\n\n  /** The state of the Rfq. */\n  readonly state: StoredRfqState;\n\n  /** The amount of Taker collateral locked at the time\n   *  of finalized construction of the Rfq. */\n  readonly nonResponseTakerCollateralLocked: number;\n\n  /** The total amount of Taker collateral locked.\n   * This includes collateral added when confirming a Response. */\n  readonly totalTakerCollateralLocked: number;\n\n  /** The total number of Responses to this Rfq. */\n  readonly totalResponses: number;\n\n  /** The number of Responses to this Rfq which have been\n   *  cleared during the Rfq cleanup process. */\n  readonly clearedResponses: number;\n\n  /** The number of confirmed Responses to the Rfq. */\n  readonly confirmedResponses: number;\n\n  /** The address of the Whitelist. */\n  readonly whitelist: PublicKey;\n};\n\nexport type EscrowRfq = CommonRfq & {\n  /** A model identifier to distinguish models in the SDK. */\n  readonly model: 'escrowRfq';\n\n  /** The quote asset of the Rfq. */\n  readonly quoteAsset: QuoteInstrument;\n\n  /** The quote asset mint. */\n  readonly quoteMint: PublicKey;\n\n  /** The legs of the Rfq. */\n  readonly legs: LegInstrument[];\n};\n\nexport type PrintTradeRfq = CommonRfq & {\n  /** A model identifier to distinguish models in the SDK. */\n  readonly model: 'printTradeRfq';\n\n  /** A model that stores legs, quote info and a settlement venue. */\n  readonly printTrade: PrintTrade;\n\n  /** The quote asset of the Rfq. */\n  readonly quoteAsset: PrintTradeQuote;\n\n  /** The legs of the Rfq. */\n  readonly legs: PrintTradeLeg[];\n};\n\nexport type Rfq = EscrowRfq | PrintTradeRfq;\n\n/** @group Model Helpers */\nexport const isEscrowRfq = (value: any): value is Rfq =>\n  typeof value === 'object' && value.model === 'escrowRfq';\n\nexport const isPrintTradeRfq = (value: any): value is Rfq =>\n  typeof value === 'object' && value.model === 'printTradeRfq';\n\n/** @group Model Helpers */\nexport function assertRfq(value: any): asserts value is Rfq {\n  assert(isEscrowRfq(value) || isPrintTradeRfq(value), 'Expected Rfq model');\n}\n\nexport function assertEscrowRfq(value: any): asserts value is EscrowRfq {\n  assert(isEscrowRfq(value), 'Expected Escrow Rfq model');\n}\n\nexport function assertPrintTradeRfq(\n  value: any\n): asserts value is PrintTradeRfq {\n  assert(isPrintTradeRfq(value), 'Expected Print Trade Rfq model');\n}\n\nexport function isSettledAsPrintTrade(rfq: Rfq): boolean {\n  return rfq.model === 'printTradeRfq';\n}\n\n/** @group Model Helpers */\nexport const toRfq = async (\n  cvg: Convergence,\n  account: RfqAccount\n): Promise<Rfq> => {\n  const protocol = await cvg.protocol().get();\n  const quoteAsset = await SpotQuoteInstrument.parseFromQuote(\n    cvg,\n    protocol,\n    account.data.quoteAsset\n  );\n  const collateralMint = await collateralMintCache.get(cvg);\n  const collateralDecimals = collateralMint.decimals;\n\n  const commonRfq: CommonRfq = {\n    address: account.publicKey,\n    taker: account.data.taker,\n    orderType: fromSolitaOrderType(account.data.orderType),\n    size: fromSolitaFixedSize(account.data.fixedSize, quoteAsset.getDecimals()),\n    creationTimestamp: convertTimestampToMilliSeconds(\n      account.data.creationTimestamp\n    ),\n    activeWindow: account.data.activeWindow,\n    settlingWindow: account.data.settlingWindow,\n    expectedLegsSize: account.data.expectedLegsSize,\n    state: fromSolitaStoredRfqState(account.data.state),\n    nonResponseTakerCollateralLocked: removeDecimals(\n      account.data.nonResponseTakerCollateralLocked,\n      collateralDecimals\n    ),\n    totalTakerCollateralLocked: removeDecimals(\n      account.data.totalTakerCollateralLocked,\n      collateralDecimals\n    ),\n    totalResponses: account.data.totalResponses,\n    clearedResponses: account.data.clearedResponses,\n    confirmedResponses: account.data.confirmedResponses,\n    whitelist: account.data.whitelist,\n  };\n\n  if (account.data.printTradeProvider === null) {\n    return {\n      model: 'escrowRfq',\n      ...commonRfq,\n      quoteAsset,\n      quoteMint: SpotLegInstrument.deserializeInstrumentData(\n        Buffer.from(account.data.quoteAsset.data)\n      ).mintAddress,\n      legs: account.data.legs.map((leg) =>\n        cvg.parseLegInstrument(leg, protocol)\n      ),\n    };\n  }\n\n  const printTrade = cvg.parsePrintTrade(\n    account.data.printTradeProvider,\n    account.data.legs,\n    account.data.quoteAsset\n  );\n  return {\n    model: 'printTradeRfq',\n    ...commonRfq,\n    printTrade,\n    legs: printTrade.getLegs(),\n    quoteAsset: printTrade.getQuote(),\n  };\n};\n"],"names":["isEscrowRfq","value","_typeof","model","isPrintTradeRfq","assertRfq","assert","assertEscrowRfq","assertPrintTradeRfq","isSettledAsPrintTrade","rfq","toRfq","_ref","_asyncToGenerator","_regeneratorRuntime","mark","_callee","cvg","account","protocol","quoteAsset","collateralMint","collateralDecimals","commonRfq","printTrade","wrap","_callee$","_context","prev","next","get","sent","SpotQuoteInstrument","parseFromQuote","data","collateralMintCache","decimals","address","publicKey","taker","orderType","fromSolitaOrderType","size","fromSolitaFixedSize","fixedSize","getDecimals","creationTimestamp","convertTimestampToMilliSeconds","activeWindow","settlingWindow","expectedLegsSize","state","fromSolitaStoredRfqState","nonResponseTakerCollateralLocked","removeDecimals","totalTakerCollateralLocked","totalResponses","clearedResponses","confirmedResponses","whitelist","printTradeProvider","abrupt","_objectSpread","quoteMint","SpotLegInstrument","deserializeInstrumentData","Buffer","from","mintAddress","legs","map","leg","parseLegInstrument","parsePrintTrade","getLegs","getQuote","stop","_x","_x2","apply","arguments"],"mappings":";;;;;;;;;;;;;AA0BA;AACA;AACA;AACA;AACA;AACA;;AAoFA;IACaA,WAAW,GAAG,SAAdA,WAAWA,CAAIC,KAAU,EAAA;EAAA,OACpCC,mCAAA,CAAOD,KAAK,CAAK,KAAA,QAAQ,IAAIA,KAAK,CAACE,KAAK,KAAK,WAAW,CAAA;AAAA,EAAA;IAE7CC,eAAe,GAAG,SAAlBA,eAAeA,CAAIH,KAAU,EAAA;EAAA,OACxCC,mCAAA,CAAOD,KAAK,CAAK,KAAA,QAAQ,IAAIA,KAAK,CAACE,KAAK,KAAK,eAAe,CAAA;AAAA,EAAA;;AAE9D;AACO,SAASE,SAASA,CAACJ,KAAU,EAAwB;AAC1DK,EAAAA,aAAM,CAACN,WAAW,CAACC,KAAK,CAAC,IAAIG,eAAe,CAACH,KAAK,CAAC,EAAE,oBAAoB,CAAC,CAAA;AAC5E,CAAA;AAEO,SAASM,eAAeA,CAACN,KAAU,EAA8B;AACtEK,EAAAA,aAAM,CAACN,WAAW,CAACC,KAAK,CAAC,EAAE,2BAA2B,CAAC,CA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