diff --git a/packages/js/src/plugins/hxroPrintTradeProviderModule/helpers.ts b/packages/js/src/plugins/hxroPrintTradeProviderModule/helpers.ts index 327919643..a9f299276 100644 --- a/packages/js/src/plugins/hxroPrintTradeProviderModule/helpers.ts +++ b/packages/js/src/plugins/hxroPrintTradeProviderModule/helpers.ts @@ -25,5 +25,5 @@ export const fetchValidHxroMpg = async (cvg: Convergence, manifest: any) => { const { validMpg } = await cvg.hxro().fetchConfig(); const mpg = await manifest.getMPG(validMpg); - return mpg; + return { pubkey: validMpg, ...mpg }; }; diff --git a/packages/js/src/plugins/hxroPrintTradeProviderModule/operations/initializeOperatorPrintTradeProvider.ts b/packages/js/src/plugins/hxroPrintTradeProviderModule/operations/initializeOperatorPrintTradeProvider.ts index 4cd0f7ca6..7a3247918 100644 --- a/packages/js/src/plugins/hxroPrintTradeProviderModule/operations/initializeOperatorPrintTradeProvider.ts +++ b/packages/js/src/plugins/hxroPrintTradeProviderModule/operations/initializeOperatorPrintTradeProvider.ts @@ -99,15 +99,17 @@ export const initializeOperatorTraderRiskGroupBuilder = async ( .getHxroPrintTradeProvider(); const systemProgram = cvg.programs().getSystem(programs); - const { validMpg } = await cvg.hxro().fetchConfig(); const { dexProgram } = manifest.fields; - const { feeModelProgramId, feeModelConfigurationAcct } = - await fetchValidHxroMpg(cvg, manifest); + const { + pubkey: mpgAddress, + feeModelProgramId, + feeModelConfigurationAcct, + } = await fetchValidHxroMpg(cvg, manifest); const [traderFeeStateAcct] = PublicKey.findProgramAddressSync( [ Buffer.from('trader_fee_acct'), trgAccount.publicKey.toBuffer(), - validMpg.toBuffer(), + mpgAddress.toBuffer(), ], feeModelProgramId ); @@ -142,11 +144,11 @@ export const initializeOperatorTraderRiskGroupBuilder = async ( authority: cvg.identity().publicKey, protocol: cvg.protocol().pdas().protocol(), config: cvg.hxro().pdas().config(), - marketProductGroup: validMpg, + marketProductGroup: mpgAddress, operator: cvg.hxro().pdas().operator(), dex: manifest.fields.dexProgram.programId, operatorTrg: trgAccount.publicKey, - riskAndFeeSigner: dexterity.Manifest.GetRiskAndFeeSigner(validMpg), + riskAndFeeSigner: dexterity.Manifest.GetRiskAndFeeSigner(mpgAddress), traderRiskStateAcct: riskStateAccount.publicKey, traderFeeStateAcct, riskEngineProgram, diff --git a/packages/js/src/plugins/hxroPrintTradeProviderModule/printTrade.ts b/packages/js/src/plugins/hxroPrintTradeProviderModule/printTrade.ts index 1348752ae..cf4db404c 100644 --- a/packages/js/src/plugins/hxroPrintTradeProviderModule/printTrade.ts +++ b/packages/js/src/plugins/hxroPrintTradeProviderModule/printTrade.ts @@ -4,6 +4,7 @@ import { } from '@convergence-rfq/risk-engine'; import BN from 'bn.js'; import { Leg as SolitaLeg } from '@convergence-rfq/rfq'; +import dexterity from '@hxronetwork/dexterity-ts'; import { PrintTrade, PrintTradeLeg, @@ -11,7 +12,12 @@ import { PrintTradeQuote, } from '../printTradeModule'; import { fromNumberInstrumentType } from '../riskEngineModule'; -import { AuthoritySide, Rfq, Response, fromSolitaLegSide } from '../rfqModule'; +import { + AuthoritySide, + fromSolitaLegSide, + PrintTradeRfq, + PrintTradeResponse, +} from '../rfqModule'; import { HXRO_LEG_DECIMALS, HXRO_QUOTE_DECIMALS } from './constants'; import { HxroLegInput } from './types'; import { fetchValidHxroMpg, getHxroManifest } from './helpers'; @@ -75,8 +81,8 @@ export class HxroPrintTrade implements PrintTrade { }; getSettlementPreparationAccounts = async ( - rfq: Rfq, - response: Response, + rfq: PrintTradeRfq, + response: PrintTradeResponse, side: AuthoritySide, additionalParams: any ) => { @@ -94,22 +100,54 @@ export class HxroPrintTrade implements PrintTrade { : [response.maker, rfq.taker]; const manifest = await getHxroManifest(this.cvg); - const [mpg, userTrg, counterpartyTrg, operatorTrgs] = await Promise.all([ + const [mpg, userTrgs, counterpartyTrgs, operatorTrgs] = await Promise.all([ fetchValidHxroMpg(this.cvg, manifest), - manifest.getTRG(additionalParams.userTrgAddress), - manifest.getTRG(additionalParams.counterpartyTrgAddress), + manifest.getTRGsOfOwner(user), + manifest.getTRGsOfOwner(counterparty), manifest.getTRGsOfOwner(this.cvg.hxro().pdas().operator()), ]); - // @ts-ignore - const operatorTrg = operatorTrgs[0]; - if (!user.equals(userTrg.owner)) { + const { pubkey: userTrgAddress, trg: userTrg } = userTrgs[0]; + const { pubkey: counterpartyTrgAddress, trg: counterpartyTrg } = + counterpartyTrgs[0]; + const { pubkey: operatorTrgAddress } = operatorTrgs[0]; + if (!user?.equals(userTrg.owner)) { throw new Error('Invalid user trg authority!'); } - if (!counterparty.equals(counterpartyTrg.owner)) { + if (!counterparty?.equals(counterpartyTrg.owner)) { throw new Error('Invalid counterparty trg authority!'); } + const dexProgramId = manifest.fields.dexProgram.programId; + const [firstToPrepare, secondToPrepare] = + response.printTradeInitializedBy === null + ? [userTrgAddress, counterpartyTrgAddress] + : [counterpartyTrgAddress, userTrgAddress]; + const [printTradeAddress] = PublicKey.findProgramAddressSync( + [ + Buffer.from('print_trade'), + firstToPrepare.toBuffer(), + secondToPrepare.toBuffer(), + ], + dexProgramId + ); + + const riskAndFeeSigner = dexterity.Manifest.GetRiskAndFeeSigner(mpg.pubkey); + const systemProgram = this.cvg.programs().getSystem(); + + const [covarianceAddress] = PublicKey.findProgramAddressSync( + [Buffer.from('s'), mpg.pubkey.toBuffer()], + mpg.riskEngineProgramId + ); + const [correlationAddress] = PublicKey.findProgramAddressSync( + [Buffer.from('r'), mpg.pubkey.toBuffer()], + mpg.riskEngineProgramId + ); + const [markPricesAddress] = PublicKey.findProgramAddressSync( + [Buffer.from('mark_prices'), mpg.pubkey.toBuffer()], + mpg.riskEngineProgramId + ); + return [ { pubkey: this.cvg.hxro().pdas().operator(), @@ -122,12 +160,12 @@ export class HxroPrintTrade implements PrintTrade { isWritable: false, }, { - pubkey: manifest.fields.dexProgram.programId, + pubkey: dexProgramId, isSigner: false, isWritable: false, }, { - pubkey: mpg.address, // TODO + pubkey: mpg.pubkey, isSigner: false, isWritable: true, }, @@ -137,10 +175,68 @@ export class HxroPrintTrade implements PrintTrade { isWritable: false, }, { - pubkey: userTrg.pubkey, // TODO - isSigner: true, + pubkey: userTrgAddress, + isSigner: false, + isWritable: true, + }, + { + pubkey: counterpartyTrgAddress, + isSigner: false, + isWritable: true, + }, + { + pubkey: operatorTrgAddress, + isSigner: false, + isWritable: true, + }, + { + pubkey: printTradeAddress, + isSigner: false, + isWritable: true, + }, + { pubkey: mpg.feeModelProgramId, isSigner: false, isWritable: false }, + { + pubkey: mpg.feeModelConfigurationAcct, + isSigner: false, + isWritable: false, + }, + { pubkey: mpg.feeOutputRegister, isSigner: false, isWritable: true }, + { pubkey: mpg.riskEngineProgramId, isSigner: false, isWritable: false }, + { + pubkey: mpg.riskModelConfigurationAcct, + isSigner: false, isWritable: false, }, + { pubkey: mpg.riskOutputRegister, isSigner: false, isWritable: true }, + { pubkey: riskAndFeeSigner, isSigner: false, isWritable: false }, + { pubkey: userTrg.feeStateAccount, isSigner: false, isWritable: true }, + { pubkey: userTrg.riskStateAccount, isSigner: false, isWritable: true }, + { + pubkey: counterpartyTrg.feeStateAccount, + isSigner: false, + isWritable: true, + }, + { + pubkey: counterpartyTrg.riskStateAccount, + isSigner: false, + isWritable: true, + }, + { pubkey: systemProgram.address, isSigner: false, isWritable: false }, + { + pubkey: covarianceAddress, + isSigner: false, + isWritable: true, + }, + { + pubkey: correlationAddress, + isSigner: false, + isWritable: true, + }, + { + pubkey: markPricesAddress, + isSigner: false, + isWritable: true, + }, ]; }; } @@ -255,10 +351,7 @@ class HxroLeg implements PrintTradeLeg { } export class AdditionalHxroSettlementPreparationParameters { - constructor( - public userTrgAddress: PublicKey, - public counterpartyTrgAddress: PublicKey - ) {} + constructor(public userTrgAddress: PublicKey) {} static verify( parameters: any diff --git a/packages/js/src/plugins/printTradeModule/methods.ts b/packages/js/src/plugins/printTradeModule/methods.ts index c43a0bfc6..683f40897 100644 --- a/packages/js/src/plugins/printTradeModule/methods.ts +++ b/packages/js/src/plugins/printTradeModule/methods.ts @@ -1,10 +1,10 @@ import { Leg, QuoteAsset, legBeet } from '@convergence-rfq/rfq'; import { AccountMeta } from '@solana/web3.js'; import { - Rfq, - Response, AuthoritySide, toSolitaLegSide, + PrintTradeResponse, + PrintTradeRfq, } from '../rfqModule/models'; import { toNumberInstrumentType } from '../riskEngineModule/models'; import { PrintTrade, PrintTradeLeg, PrintTradeQuote } from './types'; @@ -63,8 +63,8 @@ export async function getPrintTradeValidationAccounts( export async function getSettlementPreparationAccounts( printTrade: PrintTrade, - rfq: Rfq, - response: Response, + rfq: PrintTradeRfq, + response: PrintTradeResponse, side: AuthoritySide, additionalInfo: any ): Promise { diff --git a/packages/js/src/plugins/printTradeModule/types.ts b/packages/js/src/plugins/printTradeModule/types.ts index 19a147ccf..a5cb9c314 100644 --- a/packages/js/src/plugins/printTradeModule/types.ts +++ b/packages/js/src/plugins/printTradeModule/types.ts @@ -5,7 +5,12 @@ import { QuoteAsset as SolitaQuoteAsset, } from '@convergence-rfq/rfq'; import { InstrumentType } from '../riskEngineModule'; -import { LegSide, Rfq, Response, AuthoritySide } from '../rfqModule'; +import { + LegSide, + AuthoritySide, + PrintTradeResponse, + PrintTradeRfq, +} from '../rfqModule'; import { Convergence } from '@/Convergence'; export interface PrintTrade { @@ -14,8 +19,8 @@ export interface PrintTrade { getQuote: () => PrintTradeQuote; getValidationAccounts: () => Promise; getSettlementPreparationAccounts: ( - rfq: Rfq, - response: Response, + rfq: PrintTradeRfq, + response: PrintTradeResponse, side: AuthoritySide, additionalParams: any ) => Promise; diff --git a/packages/js/tests/unit/hxro.spec.ts b/packages/js/tests/unit/hxro.spec.ts index 4145c8ebd..ccd1f15ff 100644 --- a/packages/js/tests/unit/hxro.spec.ts +++ b/packages/js/tests/unit/hxro.spec.ts @@ -87,12 +87,26 @@ describe('unit.hxro', () => { .rfqs() .respond({ rfq: rfq.address, ask: { price: 123, legsMultiplier: 1 } }); + await cvgTaker.rfqs().confirmResponse({ + response: rfqResponse.address, + rfq: rfq.address, + side: 'ask', + }); + await cvgTaker.rfqs().preparePrintTradeSettlement({ rfq: rfq.address, response: rfqResponse.address, additionalPrintTradeInfo: new AdditionalHxroSettlementPreparationParameters( - new PublicKey(CTX.hxroTakerTrg), + new PublicKey(CTX.hxroTakerTrg) + ), + }); + + await cvgMaker.rfqs().preparePrintTradeSettlement({ + rfq: rfq.address, + response: rfqResponse.address, + additionalPrintTradeInfo: + new AdditionalHxroSettlementPreparationParameters( new PublicKey(CTX.hxroMakerTrg) ), });