{"version":3,"file":"Response.cjs","sources":["../../../../../src/plugins/rfqModule/models/Response.ts"],"sourcesContent":["import { PublicKey } from '@solana/web3.js';\nimport { DefaultingParty as SolitaDefaultingParty } from '@convergence-rfq/rfq';\n\nimport { ResponseAccount } from '../accounts';\nimport {\n  convertTimestampToMilliSeconds,\n  removeDecimals,\n} from '../../../utils/conversions';\nimport { assert } from '../../../utils/assert';\nimport { Confirmation, fromSolitaConfirmation } from './Confirmation';\nimport { AuthoritySide, fromSolitaAuthoritySide } from './AuthoritySide';\nimport {\n  StoredResponseState,\n  fromSolitaStoredResponseState,\n} from './StoredResponseState';\nimport { fromSolitaQuote, Quote } from './Quote';\nimport { Rfq, isSettledAsPrintTrade } from './Rfq';\n\n/**\n * This model captures all the relevant information about a response\n * on the Solana blockchain.\n *\n * @group Models\n */\ntype CommonResponse = {\n  /** The address of the response. */\n  readonly address: PublicKey;\n\n  /** The maker pubkey address. */\n  readonly maker: PublicKey;\n\n  /** The address of the RFQ this response corresponds to. */\n  readonly rfq: PublicKey;\n\n  /** The timestamp at which this response was created. */\n  readonly creationTimestamp: number;\n\n  /** The timestamp at which this response will expire. */\n  readonly expirationTimestamp: number;\n\n  /** The bid required for sell and optionally two-way order types. */\n  readonly bid: Quote | null;\n\n  /** The ask required for buy and optionally two-way order types. */\n  readonly ask: Quote | null;\n\n  /** The amount of the maker collateral locked. */\n  readonly makerCollateralLocked: number;\n\n  /** The amount of the taker collateral locked. */\n  readonly takerCollateralLocked: number;\n\n  /** The current state of the response. */\n  readonly state: StoredResponseState;\n\n  // TODO: Should be a ResponseSide?\n  /** The optional confirmation of this response. */\n  readonly confirmed: Confirmation | null;\n\n  //\n  /** The optional defaulting party of this response. */\n  readonly defaultingParty: SolitaDefaultingParty | null;\n};\n\nexport type EscrowResponse = CommonResponse & {\n  /** A model identifier to distinguish models in the SDK. */\n  readonly model: 'escrowResponse';\n\n  /** The number of legs prepared by the taker. */\n  readonly takerPreparedLegs: number;\n\n  /** The number of legs prepared by the maker. */\n  readonly makerPreparedLegs: number;\n\n  /** The number of legs that have already been settled. */\n  readonly settledLegs: number;\n\n  /** Shows whether the maker or taker initialized preparation for each prepared leg. */\n  readonly legPreparationsInitializedBy: AuthoritySide[];\n};\n\nexport type PrintTradeResponse = CommonResponse & {\n  /** A model identifier to distinguish models in the SDK. */\n  readonly model: 'printTradeResponse';\n\n  /** The number of legs prepared by the taker. */\n  readonly takerPrepared: boolean;\n\n  /** The number of legs prepared by the maker. */\n  readonly makerPrepared: boolean;\n\n  /** Shows whether the maker or taker initialized the print trade. */\n  readonly printTradeInitializedBy: AuthoritySide | null;\n\n  readonly additionalData: Uint8Array;\n};\n\nexport type Response = EscrowResponse | PrintTradeResponse;\n\n/** @group Model Helpers */\nexport const isResponse = (value: any): value is Response =>\n  typeof value === 'object' &&\n  (value.model === 'escrowResponse' || value.model === 'printTradeResponse');\n\n/** @group Model Helpers */\nexport function assertResponse(value: any): asserts value is Response {\n  assert(isResponse(value), 'Expected Response model');\n}\n\n/** @group Model Helpers */\nexport const toResponse = (\n  account: ResponseAccount,\n  collateralDecimals: number,\n  rfq: Rfq\n): Response => {\n  if (!rfq.address.equals(account.data.rfq)) {\n    throw new Error('Passed rfq does not match the one stored in response');\n  }\n\n  const commonResponse: CommonResponse = {\n    address: account.publicKey,\n    maker: account.data.maker,\n    rfq: account.data.rfq,\n    creationTimestamp: convertTimestampToMilliSeconds(\n      account.data.creationTimestamp\n    ),\n    expirationTimestamp: convertTimestampToMilliSeconds(\n      account.data.expirationTimestamp\n    ),\n    makerCollateralLocked: removeDecimals(\n      account.data.makerCollateralLocked,\n      collateralDecimals\n    ),\n    takerCollateralLocked: removeDecimals(\n      account.data.takerCollateralLocked,\n      collateralDecimals\n    ),\n    state: fromSolitaStoredResponseState(account.data.state),\n    bid:\n      account.data.bid &&\n      fromSolitaQuote(account.data.bid, rfq.quoteAsset.getDecimals()),\n    ask:\n      account.data.ask &&\n      fromSolitaQuote(account.data.ask, rfq.quoteAsset.getDecimals()),\n    confirmed:\n      account.data.confirmed && fromSolitaConfirmation(account.data.confirmed),\n    defaultingParty: account.data.defaultingParty,\n  };\n\n  if (isSettledAsPrintTrade(rfq)) {\n    return {\n      model: 'printTradeResponse',\n      ...commonResponse,\n      takerPrepared: account.data.takerPreparedCounter > 0,\n      makerPrepared: account.data.makerPreparedCounter > 0,\n      printTradeInitializedBy:\n        account.data.printTradeInitializedBy !== null\n          ? fromSolitaAuthoritySide(account.data.printTradeInitializedBy)\n          : null,\n      additionalData: account.data.additionalData,\n    };\n  }\n\n  return {\n    model: 'escrowResponse',\n    ...commonResponse,\n    takerPreparedLegs: account.data.takerPreparedCounter,\n    makerPreparedLegs: account.data.makerPreparedCounter,\n    settledLegs: account.data.settledEscrowLegs,\n    legPreparationsInitializedBy:\n      account.data.escrowLegPreparationsInitializedBy.map(\n        fromSolitaAuthoritySide\n      ),\n  };\n};\n"],"names":["isResponse","value","_typeof","model","assertResponse","assert","toResponse","account","collateralDecimals","rfq","address","equals","data","Error","commonResponse","publicKey","maker","creationTimestamp","convertTimestampToMilliSeconds","expirationTimestamp","makerCollateralLocked","removeDecimals","takerCollateralLocked","state","fromSolitaStoredResponseState","bid","fromSolitaQuote","quoteAsset","getDecimals","ask","confirmed","fromSolitaConfirmation","defaultingParty","isSettledAsPrintTrade","_objectSpread","takerPrepared","takerPreparedCounter","makerPrepared","makerPreparedCounter","printTradeInitializedBy","fromSolitaAuthoritySide","additionalData","takerPreparedLegs","makerPreparedLegs","settledLegs","settledEscrowLegs","legPreparationsInitializedBy","escrowLegPreparationsInitializedBy","map"],"mappings":";;;;;;;;;;;;;AAkBA;AACA;AACA;AACA;AACA;AACA;;AA4EA;IACaA,UAAU,GAAG,SAAbA,UAAUA,CAAIC,KAAU,EAAA;AAAA,EAAA,OACnCC,mCAAA,CAAOD,KAAK,CAAK,KAAA,QAAQ,KACxBA,KAAK,CAACE,KAAK,KAAK,gBAAgB,IAAIF,KAAK,CAACE,KAAK,KAAK,oBAAoB,CA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