diff --git a/packages/js/src/plugins/instrumentModule/plugin.ts b/packages/js/src/plugins/instrumentModule/plugin.ts index 23c3a7f6a..8073daead 100644 --- a/packages/js/src/plugins/instrumentModule/plugin.ts +++ b/packages/js/src/plugins/instrumentModule/plugin.ts @@ -48,7 +48,7 @@ declare module '../../Convergence' { programAddress: PublicKey, factory: LegInstrumentParser ): void; - parseLegInstrument(leg: SolitaLeg): LegInstrument; + parseLegInstrument(leg: SolitaLeg): Promise; } } diff --git a/packages/js/src/plugins/instrumentModule/types.ts b/packages/js/src/plugins/instrumentModule/types.ts index 600ec3a28..648e8a872 100644 --- a/packages/js/src/plugins/instrumentModule/types.ts +++ b/packages/js/src/plugins/instrumentModule/types.ts @@ -6,7 +6,7 @@ import { Convergence } from '../../Convergence'; import { LegSide } from '../rfqModule/models/LegSide'; export interface LegInstrumentParser { - parseFromLeg(convergence: Convergence, leg: Leg): LegInstrument; + parseFromLeg(convergence: Convergence, leg: Leg): Promise; } export interface LegInstrument { @@ -17,6 +17,7 @@ export interface LegInstrument { getSide: () => LegSide; serializeInstrumentData: () => Buffer; getValidationAccounts(): Promise; + getBaseAssetMint(): PublicKey; } // TODO add registration of quote instruments diff --git a/packages/js/src/plugins/psyoptionsAmericanInstrumentModule/helpers.ts b/packages/js/src/plugins/psyoptionsAmericanInstrumentModule/helpers.ts index 3e6499227..82030b985 100644 --- a/packages/js/src/plugins/psyoptionsAmericanInstrumentModule/helpers.ts +++ b/packages/js/src/plugins/psyoptionsAmericanInstrumentModule/helpers.ts @@ -2,6 +2,7 @@ import * as psyoptionsAmerican from '@mithraic-labs/psy-american'; import { BN } from 'bn.js'; import { PublicKey } from '@solana/web3.js'; +import { Leg } from '@convergence-rfq/rfq'; import { Convergence } from '../../Convergence'; import { ATAExistence, getOrCreateATA } from '../../utils/ata'; @@ -13,6 +14,7 @@ import { } from '../../utils/TransactionBuilder'; import { PsyoptionsAmericanInstrument } from './types'; import { createAmericanProgram } from './instrument'; +import { psyoptionsAmericanInstrumentProgram } from './programs'; export const mintAmericanOptions = async ( convergence: Convergence, @@ -185,3 +187,9 @@ export const getOrCreateAmericanOptionATAs = async ( } return ATAExistence.NOTEXISTS; }; + +export function hasPsyoptionsAmericanLeg(legs: Leg[]): boolean { + return legs.some((leg) => + leg.instrumentProgram.equals(psyoptionsAmericanInstrumentProgram.address) + ); +} diff --git a/packages/js/src/plugins/psyoptionsAmericanInstrumentModule/instrument.ts b/packages/js/src/plugins/psyoptionsAmericanInstrumentModule/instrument.ts index ad226fc03..a988fb285 100644 --- a/packages/js/src/plugins/psyoptionsAmericanInstrumentModule/instrument.ts +++ b/packages/js/src/plugins/psyoptionsAmericanInstrumentModule/instrument.ts @@ -59,13 +59,14 @@ export class PsyoptionsAmericanInstrument implements LegInstrument { readonly baseAssetIndex: BaseAssetIndex, readonly amount: number, readonly side: LegSide, - private optionMeta?: OptionMarketWithKey + public optionMeta: OptionMarketWithKey ) {} getBaseAssetIndex = () => this.baseAssetIndex; getAmount = () => this.amount; getDecimals = () => PsyoptionsAmericanInstrument.decimals; getSide = () => this.side; + getBaseAssetMint = () => this.optionMeta.underlyingAssetMint; static async create( convergence: Convergence, @@ -196,10 +197,10 @@ export class PsyoptionsAmericanInstrument implements LegInstrument { } export const psyoptionsAmericanInstrumentParser = { - parseFromLeg( + async parseFromLeg( convergence: Convergence, leg: Leg - ): PsyoptionsAmericanInstrument { + ): Promise { const { side, instrumentAmount, instrumentData, baseAssetIndex } = leg; const [ { @@ -215,7 +216,10 @@ export const psyoptionsAmericanInstrumentParser = { ] = psyoptionsAmericanInstrumentDataSerializer.deserialize( Buffer.from(instrumentData) ); - + const optionMeta = await PsyoptionsAmericanInstrument.fetchMeta( + convergence, + metaKey + ); return new PsyoptionsAmericanInstrument( convergence, optionType, @@ -231,7 +235,8 @@ export const psyoptionsAmericanInstrumentParser = { metaKey, baseAssetIndex, removeDecimals(instrumentAmount, PsyoptionsAmericanInstrument.decimals), - fromSolitaLegSide(side) + fromSolitaLegSide(side), + optionMeta ); }, }; diff --git a/packages/js/src/plugins/psyoptionsEuropeanInstrumentModule/helpers.ts b/packages/js/src/plugins/psyoptionsEuropeanInstrumentModule/helpers.ts index d3c021a2a..5b6649a4d 100644 --- a/packages/js/src/plugins/psyoptionsEuropeanInstrumentModule/helpers.ts +++ b/packages/js/src/plugins/psyoptionsEuropeanInstrumentModule/helpers.ts @@ -2,12 +2,14 @@ import * as psyoptionsEuropean from '@mithraic-labs/tokenized-euros'; import * as anchor from '@project-serum/anchor'; import { Keypair, PublicKey } from '@solana/web3.js'; import { BN } from 'bn.js'; +import { Leg } from '@convergence-rfq/rfq'; import { Mint } from '../tokenModule'; import { ATAExistence, getOrCreateATA } from '../../utils/ata'; import { addDecimals } from '../../utils/conversions'; import { TransactionBuilder } from '../../utils/TransactionBuilder'; import { Convergence } from '../../Convergence'; import { PsyoptionsEuropeanInstrument } from './instrument'; +import { psyoptionsEuropeanInstrumentProgram } from './programs'; import { Pda } from '@/types/Pda'; import { makeConfirmOptionsFinalizedOnMainnet } from '@/types/Operation'; import { toBigNumber } from '@/types/BigNumber'; @@ -264,3 +266,9 @@ export const getOrCreateEuropeanOptionATAs = async ( } return ATAExistence.NOTEXISTS; }; + +export function hasPsyoptionsEuropeanLeg(legs: Leg[]): boolean { + return legs.some((leg) => + leg.instrumentProgram.equals(psyoptionsEuropeanInstrumentProgram.address) + ); +} diff --git a/packages/js/src/plugins/psyoptionsEuropeanInstrumentModule/instrument.ts b/packages/js/src/plugins/psyoptionsEuropeanInstrumentModule/instrument.ts index 18ac0a928..d331660ec 100644 --- a/packages/js/src/plugins/psyoptionsEuropeanInstrumentModule/instrument.ts +++ b/packages/js/src/plugins/psyoptionsEuropeanInstrumentModule/instrument.ts @@ -95,13 +95,14 @@ export class PsyoptionsEuropeanInstrument implements LegInstrument { readonly baseAssetIndex: BaseAssetIndex, readonly amount: number, readonly side: LegSide, - private optionMeta?: EuroMeta + public optionMeta: EuroMeta ) {} getBaseAssetIndex = () => this.baseAssetIndex; getAmount = () => this.amount; getDecimals = () => PsyoptionsEuropeanInstrument.decimals; getSide = () => this.side; + getBaseAssetMint = () => this.optionMeta.underlyingMint; static async create( convergence: Convergence, @@ -214,10 +215,10 @@ export class PsyoptionsEuropeanInstrument implements LegInstrument { } export const psyoptionsEuropeanInstrumentParser = { - parseFromLeg( + async parseFromLeg( convergence: Convergence, leg: Leg - ): PsyoptionsEuropeanInstrument { + ): Promise { const { side, instrumentAmount, instrumentData, baseAssetIndex } = leg; const [ { @@ -233,7 +234,10 @@ export const psyoptionsEuropeanInstrumentParser = { ] = psyoptionsEuropeanInstrumentDataSerializer.deserialize( Buffer.from(instrumentData) ); - + const optionMeta = await PsyoptionsEuropeanInstrument.fetchMeta( + convergence, + metaKey + ); return new PsyoptionsEuropeanInstrument( convergence, optionType, @@ -249,7 +253,8 @@ export const psyoptionsEuropeanInstrumentParser = { metaKey, baseAssetIndex, removeDecimals(instrumentAmount, PsyoptionsEuropeanInstrument.decimals), - fromSolitaLegSide(side) + fromSolitaLegSide(side), + optionMeta ); }, }; diff --git a/packages/js/src/plugins/rfqModule/models/Rfq.ts b/packages/js/src/plugins/rfqModule/models/Rfq.ts index d53e04e06..23f5cda19 100644 --- a/packages/js/src/plugins/rfqModule/models/Rfq.ts +++ b/packages/js/src/plugins/rfqModule/models/Rfq.ts @@ -104,6 +104,11 @@ export const toRfq = async ( ); const collateralMint = await collateralMintCache.get(convergence); const collateralDecimals = collateralMint.decimals; + const { legs } = account.data; + const parsedLegs = await Promise.all( + legs.map((leg) => convergence.parseLegInstrument(leg)) + ); + return { model: 'rfq', address: account.publicKey, @@ -130,6 +135,6 @@ export const toRfq = async ( totalResponses: account.data.totalResponses, clearedResponses: account.data.clearedResponses, confirmedResponses: account.data.confirmedResponses, - legs: account.data.legs.map((leg) => convergence.parseLegInstrument(leg)), + legs: parsedLegs, }; }; diff --git a/packages/js/src/plugins/spotInstrumentModule/instruments.ts b/packages/js/src/plugins/spotInstrumentModule/instruments.ts index b4f0070ad..76b69ba8a 100644 --- a/packages/js/src/plugins/spotInstrumentModule/instruments.ts +++ b/packages/js/src/plugins/spotInstrumentModule/instruments.ts @@ -42,7 +42,7 @@ export class SpotLegInstrument implements LegInstrument { getDecimals = () => this.decimals; getAmount = () => this.amount; getBaseAssetIndex = () => this.baseAssetIndex; - + getBaseAssetMint = () => this.mintAddress; static async create( convergence: Convergence, mint: Mint, @@ -92,7 +92,10 @@ export class SpotLegInstrument implements LegInstrument { } export const spotLegInstrumentParser = { - parseFromLeg(convergence: Convergence, leg: Leg): SpotLegInstrument { + async parseFromLeg( + convergence: Convergence, + leg: Leg + ): Promise { const { side, instrumentAmount, diff --git a/packages/js/tests/integration/psyoptionsAmerican.spec.ts b/packages/js/tests/integration/psyoptionsAmerican.spec.ts index 0e32836a9..7037c1a72 100644 --- a/packages/js/tests/integration/psyoptionsAmerican.spec.ts +++ b/packages/js/tests/integration/psyoptionsAmerican.spec.ts @@ -36,7 +36,6 @@ describe('integration.psyoptionsAmerican', () => { quoteMint ); expect(rfq).toHaveProperty('address'); - const { rfqResponse } = await respondToRfq(makerCvg, rfq, 12.1); expect(rfqResponse).toHaveProperty('address'); diff --git a/packages/js/tests/integration/psyoptionsEuropean.spec.ts b/packages/js/tests/integration/psyoptionsEuropean.spec.ts index 54ee6ea70..56be54a9b 100644 --- a/packages/js/tests/integration/psyoptionsEuropean.spec.ts +++ b/packages/js/tests/integration/psyoptionsEuropean.spec.ts @@ -35,7 +35,6 @@ describe('integration.psyoptionsEuropean', () => { baseMint, quoteMint ); - expect(rfq).toHaveProperty('address'); expect(response.signature).toBeDefined(); diff --git a/packages/js/tests/unit/rfq.spec.ts b/packages/js/tests/unit/rfq.spec.ts index 9fd4b571f..ae04508d6 100644 --- a/packages/js/tests/unit/rfq.spec.ts +++ b/packages/js/tests/unit/rfq.spec.ts @@ -37,6 +37,7 @@ describe('unit.rfq', () => { quoteAsset: await SpotQuoteInstrument.create(takerCvg, quoteMint), fixedSize, }); + // @ts-ignore expect(fixedSize.amount).toBeCloseTo(rfq.size.amount); });