From 7813a984d8f3b9939984df4ae97a3b56deea537d Mon Sep 17 00:00:00 2001 From: Nagaprasadvr Date: Thu, 14 Mar 2024 18:19:38 +0530 Subject: [PATCH 1/3] few more changes --- .../getHxroCollateralForSettlement.ts | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 packages/js/src/plugins/hxroPrintTradeProviderModule/operations/getHxroCollateralForSettlement.ts diff --git a/packages/js/src/plugins/hxroPrintTradeProviderModule/operations/getHxroCollateralForSettlement.ts b/packages/js/src/plugins/hxroPrintTradeProviderModule/operations/getHxroCollateralForSettlement.ts new file mode 100644 index 000000000..bb20436d2 --- /dev/null +++ b/packages/js/src/plugins/hxroPrintTradeProviderModule/operations/getHxroCollateralForSettlement.ts @@ -0,0 +1,107 @@ +import { VersionedTransaction, TransactionMessage } from '@solana/web3.js'; + +import { Convergence } from '../../../Convergence'; +import { + Operation, + OperationHandler, + OperationScope, + useOperation, +} from '../../../types'; +import { HxroContextHelper } from '../printTrade'; +import { lockHxroCollateralBuilder } from './lockHxroCollateral'; +import { + AuthoritySide, + PrintTradeResponse, + PrintTradeRfq, +} from '@/plugins/rfqModule'; + +const Key = 'GetRequiredHxroCollateralForSettlementOperation' as const; + +/** + * @group Operations + * @category Constructors + */ +export const GetRequiredHxroCollateralForSettlementOperation = + useOperation(Key); + +/** + * @group Operations + * @category Types + */ +export type GetRequiredHxroCollateralForSettlementOperation = Operation< + typeof Key, + GetRequiredHxroCollateralForSettlementInput, + GetRequiredHxroCollateralForSettlementOutput +>; + +/** + * @group Operations + * @category Inputs + */ +export type GetRequiredHxroCollateralForSettlementInput = { + /** Rfq Account. */ + rfq: PrintTradeRfq; + /** The response to prepare settlement for. */ + response: PrintTradeResponse; + /** The side of the authority. */ + side: AuthoritySide; + /** The hxroContext. */ + hxroContext: HxroContextHelper; +}; + +/** + * @group Operations + * @category Outputs + */ +export type GetRequiredHxroCollateralForSettlementOutput = { + excessCollateral: number; +}; + +/** + * @group Operations + * @category Handlers + */ +export const getRequiredHxroCollateralForSettlementOperationHandler: OperationHandler = + { + handle: async ( + operation: GetRequiredHxroCollateralForSettlementOperation, + convergence: Convergence, + scope: OperationScope + ): Promise => { + const payer = await convergence.identity().publicKey; + const txBuilder = await lockHxroCollateralBuilder( + convergence, + operation.input, + scope + ); + + const excessCollateral = 0; + + const lastValidBlockHeight = await convergence.rpc().getLatestBlockhash(); + const ixs = txBuilder.getInstructions(); + const txMessage = new TransactionMessage({ + payerKey: payer, + recentBlockhash: lastValidBlockHeight.blockhash, + instructions: ixs, + }).compileToV0Message(); + + const tx = new VersionedTransaction(txMessage); + const simulateTxResult = await convergence.connection.simulateTransaction( + tx, + { + sigVerify: false, + } + ); + + if (simulateTxResult.value.err) { + const { logs } = simulateTxResult.value; + if (!logs) { + throw new Error('Simulate transaction failed'); + } + for (const log of logs) { + } + } + scope.throwIfCanceled(); + return { excessCollateral }; + }, + }; From 093079d8d10d8a035a697852ec88d899813f1fac Mon Sep 17 00:00:00 2001 From: Nagaprasadvr Date: Tue, 2 Apr 2024 11:15:42 +0530 Subject: [PATCH 2/3] wip --- .../operations/fetchHxroProducts.ts | 3 ++ .../getHxroCollateralForSettlement.ts | 31 +++++++++++++------ .../printTrade.ts | 8 ++++- .../js/src/plugins/printTradeModule/types.ts | 9 ++++++ 4 files changed, 40 insertions(+), 11 deletions(-) diff --git a/packages/js/src/plugins/hxroPrintTradeProviderModule/operations/fetchHxroProducts.ts b/packages/js/src/plugins/hxroPrintTradeProviderModule/operations/fetchHxroProducts.ts index 650bc5c67..d6c3ffee7 100644 --- a/packages/js/src/plugins/hxroPrintTradeProviderModule/operations/fetchHxroProducts.ts +++ b/packages/js/src/plugins/hxroPrintTradeProviderModule/operations/fetchHxroProducts.ts @@ -60,6 +60,7 @@ export const fetchHxroProductsOperationHandler: OperationHandler => { const manifest = await hxroManifestCache.get(cvg); const baseProductData = await parseBaseProductData(cvg, manifest); + console.log('baseProductData', baseProductData); scope.throwIfCanceled(); const baseAssets = await cvg.protocol().getBaseAssets(); @@ -104,6 +105,7 @@ const expandProductData = async ( const metadata = await manifest.getDerivativeMetadata( baseData.productAddress ); + console.log('metadata', metadata); const { instrumentType: rawInstrumentType, strike, @@ -122,6 +124,7 @@ const expandProductData = async ( oracleType.pyth !== undefined && baseAsset.pythOracle.equals(priceOracle) ); + console.log('baseAsset', baseAsset); if (baseAsset === undefined) { return null; diff --git a/packages/js/src/plugins/hxroPrintTradeProviderModule/operations/getHxroCollateralForSettlement.ts b/packages/js/src/plugins/hxroPrintTradeProviderModule/operations/getHxroCollateralForSettlement.ts index bb20436d2..bb0912fa9 100644 --- a/packages/js/src/plugins/hxroPrintTradeProviderModule/operations/getHxroCollateralForSettlement.ts +++ b/packages/js/src/plugins/hxroPrintTradeProviderModule/operations/getHxroCollateralForSettlement.ts @@ -7,7 +7,7 @@ import { OperationScope, useOperation, } from '../../../types'; -import { HxroContextHelper } from '../printTrade'; +import { HxroContextHelper, HxroPrintTrade } from '../printTrade'; import { lockHxroCollateralBuilder } from './lockHxroCollateral'; import { AuthoritySide, @@ -45,8 +45,6 @@ export type GetRequiredHxroCollateralForSettlementInput = { response: PrintTradeResponse; /** The side of the authority. */ side: AuthoritySide; - /** The hxroContext. */ - hxroContext: HxroContextHelper; }; /** @@ -54,7 +52,7 @@ export type GetRequiredHxroCollateralForSettlementInput = { * @category Outputs */ export type GetRequiredHxroCollateralForSettlementOutput = { - excessCollateral: number; + remainingCollateral: number; }; /** @@ -69,13 +67,25 @@ export const getRequiredHxroCollateralForSettlementOperationHandler: OperationHa scope: OperationScope ): Promise => { const payer = await convergence.identity().publicKey; + const { printTrade } = operation.input.rfq; + + const hxroContext = await printTrade.getHxroContextHelper( + convergence, + operation.input.response, + operation.input.side + ); const txBuilder = await lockHxroCollateralBuilder( convergence, - operation.input, + { + hxroContext, + rfq: operation.input.rfq, + response: operation.input.response, + side: operation.input.side, + }, scope ); - const excessCollateral = 0; + const remainingCollateral = 0; const lastValidBlockHeight = await convergence.rpc().getLatestBlockhash(); const ixs = txBuilder.getInstructions(); @@ -96,12 +106,13 @@ export const getRequiredHxroCollateralForSettlementOperationHandler: OperationHa if (simulateTxResult.value.err) { const { logs } = simulateTxResult.value; if (!logs) { - throw new Error('Simulate transaction failed'); - } - for (const log of logs) { + return { remainingCollateral: 0 }; } + + // for (const log of logs) { + // } } scope.throwIfCanceled(); - return { excessCollateral }; + return { remainingCollateral }; }, }; diff --git a/packages/js/src/plugins/hxroPrintTradeProviderModule/printTrade.ts b/packages/js/src/plugins/hxroPrintTradeProviderModule/printTrade.ts index c50b47cf4..9bb9c8c12 100644 --- a/packages/js/src/plugins/hxroPrintTradeProviderModule/printTrade.ts +++ b/packages/js/src/plugins/hxroPrintTradeProviderModule/printTrade.ts @@ -52,7 +52,13 @@ export class HxroPrintTrade implements PrintTrade { public takerTrg: PublicKey, protected legsInfo: HxroLegInput[] ) {} - + getHxroContextHelper = async ( + cvg: Convergence, + response: PrintTradeResponse, + firstToPrepare: AuthoritySide + ) => { + return HxroContextHelper.create(cvg, this, response, firstToPrepare); + }; getPrintTradeProviderProgramId = () => this.cvg.programs().getHxroPrintTradeProvider().address; getLegs = () => this.legsInfo.map((legInfo) => new HxroLeg(legInfo)); diff --git a/packages/js/src/plugins/printTradeModule/types.ts b/packages/js/src/plugins/printTradeModule/types.ts index cc56c373e..dcfc1b7d3 100644 --- a/packages/js/src/plugins/printTradeModule/types.ts +++ b/packages/js/src/plugins/printTradeModule/types.ts @@ -11,6 +11,10 @@ import { PrintTradeResponse, PrintTradeRfq, } from '../rfqModule'; +import { + HxroContextHelper, + HxroPrintTrade, +} from '../hxroPrintTradeProviderModule'; import { Convergence } from '@/Convergence'; import { TransactionBuilder, TransactionBuilderOptions } from '@/utils'; @@ -25,6 +29,11 @@ export interface PrintTrade { side: AuthoritySide, options: TransactionBuilderOptions ) => Promise<{ accounts: AccountMeta[]; builders: TransactionBuilder[] }>; + getHxroContextHelper: ( + cvg: Convergence, + response: PrintTradeResponse, + firstToPrepare: AuthoritySide + ) => Promise; getSettlementAccounts: ( rfq: PrintTradeRfq, response: PrintTradeResponse From 829f0a8ace9fd918d29585f8850de3f6eae39a6d Mon Sep 17 00:00:00 2001 From: Nagaprasadvr Date: Wed, 3 Apr 2024 16:07:07 +0530 Subject: [PATCH 3/3] add getHxroCollateralForSettlement operation --- packages/js/src/constants.ts | 2 + .../hxroPrintTradeProviderModule/client.ts | 12 ++++++ .../operations/fetchHxroProducts.ts | 3 -- .../getHxroCollateralForSettlement.ts | 40 ++++++++++++++----- .../operations/index.ts | 1 + .../hxroPrintTradeProviderModule/plugin.ts | 7 ++++ .../js/src/plugins/printTradeModule/types.ts | 5 +-- 7 files changed, 53 insertions(+), 17 deletions(-) diff --git a/packages/js/src/constants.ts b/packages/js/src/constants.ts index 1a9658407..066a98d65 100644 --- a/packages/js/src/constants.ts +++ b/packages/js/src/constants.ts @@ -4,3 +4,5 @@ export const TRANSACTION_PRIORITY_FEE_MAP = { high: 10, turbo: 100, }; + +export const HXRO_COLLATERAL_LOG_INDEX = 7; diff --git a/packages/js/src/plugins/hxroPrintTradeProviderModule/client.ts b/packages/js/src/plugins/hxroPrintTradeProviderModule/client.ts index 2b2605277..a9caafca5 100644 --- a/packages/js/src/plugins/hxroPrintTradeProviderModule/client.ts +++ b/packages/js/src/plugins/hxroPrintTradeProviderModule/client.ts @@ -20,6 +20,9 @@ import { UnlockHxroCollateralByRecordInput, UnlockHxroCollateralByRecordOutput, modifyHxroConfigOperation, + GetRequiredHxroCollateralForSettlementInput, + GetRequiredHxroCollateralForSettlementOutput, + getRequiredHxroCollateralForSettlementOperation, } from './operations'; import { HxroPdasClient } from './pdas'; import { OperationOptions } from '@/types'; @@ -94,4 +97,13 @@ export class HxroClient { .operations() .execute(unlockHxroCollateralByRecordOperation(input), options); } + + getRequiredCollateralForSettlement( + input: GetRequiredHxroCollateralForSettlementInput, + options?: OperationOptions + ): Promise { + return this.cvg + .operations() + .execute(getRequiredHxroCollateralForSettlementOperation(input), options); + } } diff --git a/packages/js/src/plugins/hxroPrintTradeProviderModule/operations/fetchHxroProducts.ts b/packages/js/src/plugins/hxroPrintTradeProviderModule/operations/fetchHxroProducts.ts index d6c3ffee7..650bc5c67 100644 --- a/packages/js/src/plugins/hxroPrintTradeProviderModule/operations/fetchHxroProducts.ts +++ b/packages/js/src/plugins/hxroPrintTradeProviderModule/operations/fetchHxroProducts.ts @@ -60,7 +60,6 @@ export const fetchHxroProductsOperationHandler: OperationHandler => { const manifest = await hxroManifestCache.get(cvg); const baseProductData = await parseBaseProductData(cvg, manifest); - console.log('baseProductData', baseProductData); scope.throwIfCanceled(); const baseAssets = await cvg.protocol().getBaseAssets(); @@ -105,7 +104,6 @@ const expandProductData = async ( const metadata = await manifest.getDerivativeMetadata( baseData.productAddress ); - console.log('metadata', metadata); const { instrumentType: rawInstrumentType, strike, @@ -124,7 +122,6 @@ const expandProductData = async ( oracleType.pyth !== undefined && baseAsset.pythOracle.equals(priceOracle) ); - console.log('baseAsset', baseAsset); if (baseAsset === undefined) { return null; diff --git a/packages/js/src/plugins/hxroPrintTradeProviderModule/operations/getHxroCollateralForSettlement.ts b/packages/js/src/plugins/hxroPrintTradeProviderModule/operations/getHxroCollateralForSettlement.ts index bb0912fa9..20232ca5b 100644 --- a/packages/js/src/plugins/hxroPrintTradeProviderModule/operations/getHxroCollateralForSettlement.ts +++ b/packages/js/src/plugins/hxroPrintTradeProviderModule/operations/getHxroCollateralForSettlement.ts @@ -7,13 +7,13 @@ import { OperationScope, useOperation, } from '../../../types'; -import { HxroContextHelper, HxroPrintTrade } from '../printTrade'; import { lockHxroCollateralBuilder } from './lockHxroCollateral'; import { - AuthoritySide, PrintTradeResponse, PrintTradeRfq, + getAuthoritySide, } from '@/plugins/rfqModule'; +import { HXRO_COLLATERAL_LOG_INDEX } from '@/constants'; const Key = 'GetRequiredHxroCollateralForSettlementOperation' as const; @@ -21,7 +21,7 @@ const Key = 'GetRequiredHxroCollateralForSettlementOperation' as const; * @group Operations * @category Constructors */ -export const GetRequiredHxroCollateralForSettlementOperation = +export const getRequiredHxroCollateralForSettlementOperation = useOperation(Key); /** @@ -43,8 +43,6 @@ export type GetRequiredHxroCollateralForSettlementInput = { rfq: PrintTradeRfq; /** The response to prepare settlement for. */ response: PrintTradeResponse; - /** The side of the authority. */ - side: AuthoritySide; }; /** @@ -66,13 +64,19 @@ export const getRequiredHxroCollateralForSettlementOperationHandler: OperationHa convergence: Convergence, scope: OperationScope ): Promise => { - const payer = await convergence.identity().publicKey; + const payer = convergence.identity().publicKey; + const { rfq: rfqModel, response: responseModel } = operation.input; const { printTrade } = operation.input.rfq; + const caller = convergence.identity(); + const side = getAuthoritySide(caller.publicKey, rfqModel, responseModel); + if (!side) { + throw new Error('caller is not authorized to prepare settlement'); + } const hxroContext = await printTrade.getHxroContextHelper( convergence, operation.input.response, - operation.input.side + side ); const txBuilder = await lockHxroCollateralBuilder( convergence, @@ -80,7 +84,7 @@ export const getRequiredHxroCollateralForSettlementOperationHandler: OperationHa hxroContext, rfq: operation.input.rfq, response: operation.input.response, - side: operation.input.side, + side, }, scope ); @@ -108,9 +112,25 @@ export const getRequiredHxroCollateralForSettlementOperationHandler: OperationHa if (!logs) { return { remainingCollateral: 0 }; } + const logToParse = logs[HXRO_COLLATERAL_LOG_INDEX]; + const logsSplit = logToParse.split(','); + let totalVariance = Number(logsSplit[0].split(':')[2]); + let openOrdersVariance = Number(logsSplit[1].split(':')[1]); + let positionalValue = Number(logsSplit[2].split(':')[1]); + + if (totalVariance < 0 || isNaN(totalVariance)) { + totalVariance = 0; + } + if (openOrdersVariance < 0 || isNaN(openOrdersVariance)) { + openOrdersVariance = 0; + } + if (positionalValue < 0 || isNaN(positionalValue)) { + positionalValue = 0; + } - // for (const log of logs) { - // } + const remainingCollateralReq = + (Math.sqrt(positionalValue) + Math.sqrt(openOrdersVariance)) * 1.5; + return { remainingCollateral: remainingCollateralReq }; } scope.throwIfCanceled(); return { remainingCollateral }; diff --git a/packages/js/src/plugins/hxroPrintTradeProviderModule/operations/index.ts b/packages/js/src/plugins/hxroPrintTradeProviderModule/operations/index.ts index 6cff29ad4..428334296 100644 --- a/packages/js/src/plugins/hxroPrintTradeProviderModule/operations/index.ts +++ b/packages/js/src/plugins/hxroPrintTradeProviderModule/operations/index.ts @@ -7,3 +7,4 @@ export * from './lockHxroCollateral'; export * from './signHxroPrintTrade'; export * from './fetchUnusedCollateralLockRecords'; export * from './unlockHxroCollateralByRecord'; +export * from './getHxroCollateralForSettlement'; diff --git a/packages/js/src/plugins/hxroPrintTradeProviderModule/plugin.ts b/packages/js/src/plugins/hxroPrintTradeProviderModule/plugin.ts index ccc4f5ec0..ce50b540a 100644 --- a/packages/js/src/plugins/hxroPrintTradeProviderModule/plugin.ts +++ b/packages/js/src/plugins/hxroPrintTradeProviderModule/plugin.ts @@ -15,6 +15,8 @@ import { modifyHxroConfigOperationHandler, unlockHxroCollateralByRecordOperation, unlockHxroCollateralByRecordOperationHandler, + getRequiredHxroCollateralForSettlementOperation, + getRequiredHxroCollateralForSettlementOperationHandler, } from './operations'; import { hxroPrintTradeProviderProgram } from './program'; import { HxroPrintTradeParser } from './printTrade'; @@ -56,6 +58,11 @@ export const hxroModule = (): ConvergencePlugin => ({ unlockHxroCollateralByRecordOperationHandler ); + op.register( + getRequiredHxroCollateralForSettlementOperation, + getRequiredHxroCollateralForSettlementOperationHandler + ); + convergence.hxro = function () { return new HxroClient(this); }; diff --git a/packages/js/src/plugins/printTradeModule/types.ts b/packages/js/src/plugins/printTradeModule/types.ts index dcfc1b7d3..4fc6585f7 100644 --- a/packages/js/src/plugins/printTradeModule/types.ts +++ b/packages/js/src/plugins/printTradeModule/types.ts @@ -11,10 +11,7 @@ import { PrintTradeResponse, PrintTradeRfq, } from '../rfqModule'; -import { - HxroContextHelper, - HxroPrintTrade, -} from '../hxroPrintTradeProviderModule'; +import { HxroContextHelper } from '../hxroPrintTradeProviderModule'; import { Convergence } from '@/Convergence'; import { TransactionBuilder, TransactionBuilderOptions } from '@/utils';