Skip to content

Commit

Permalink
Add a working hxro preparation for the settlement
Browse files Browse the repository at this point in the history
  • Loading branch information
EquilateralDelta committed Sep 15, 2023
1 parent a912f5e commit 528d295
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
};
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down Expand Up @@ -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,
Expand Down
129 changes: 111 additions & 18 deletions packages/js/src/plugins/hxroPrintTradeProviderModule/printTrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@ 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,
PrintTradeParser,
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';
Expand Down Expand Up @@ -75,8 +81,8 @@ export class HxroPrintTrade implements PrintTrade {
};

getSettlementPreparationAccounts = async (
rfq: Rfq,
response: Response,
rfq: PrintTradeRfq,
response: PrintTradeResponse,
side: AuthoritySide,
additionalParams: any
) => {
Expand All @@ -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(),
Expand All @@ -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,
},
Expand All @@ -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,
},
];
};
}
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions packages/js/src/plugins/printTradeModule/methods.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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<AccountMeta[]> {
Expand Down
11 changes: 8 additions & 3 deletions packages/js/src/plugins/printTradeModule/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -14,8 +19,8 @@ export interface PrintTrade {
getQuote: () => PrintTradeQuote;
getValidationAccounts: () => Promise<AccountMeta[]>;
getSettlementPreparationAccounts: (
rfq: Rfq,
response: Response,
rfq: PrintTradeRfq,
response: PrintTradeResponse,
side: AuthoritySide,
additionalParams: any
) => Promise<AccountMeta[]>;
Expand Down
16 changes: 15 additions & 1 deletion packages/js/tests/unit/hxro.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
),
});
Expand Down

0 comments on commit 528d295

Please sign in to comment.