Skip to content

Commit

Permalink
Add support for print trade settlement
Browse files Browse the repository at this point in the history
  • Loading branch information
EquilateralDelta committed Sep 18, 2023
1 parent 84adc2f commit 6c2c3d0
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ export class HxroPrintTrade implements PrintTrade {
},
];
};
getSettlementAccounts = async () => {
return [];
};

// after an rfq is parsed from an on-chain data, as much data is possible is parsed from there
// but some product info is missing in the rfq on-chain data
Expand Down
33 changes: 5 additions & 28 deletions packages/js/src/plugins/printTradeModule/methods.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { Leg, QuoteAsset, legBeet } from '@convergence-rfq/rfq';
import { AccountMeta } from '@solana/web3.js';
import {
AuthoritySide,
toSolitaLegSide,
PrintTradeResponse,
PrintTradeRfq,
} from '../rfqModule/models';
import { toSolitaLegSide } from '../rfqModule/models';
import { toNumberInstrumentType } from '../riskEngineModule/models';
import { PrintTrade, PrintTradeLeg, PrintTradeQuote } from './types';
import { addDecimals } from '@/utils';
Expand Down Expand Up @@ -53,27 +48,9 @@ export function getPrintTradeProgramAccount(
};
}

export async function getPrintTradeValidationAccounts(
printTrade: PrintTrade
): Promise<AccountMeta[]> {
return [getPrintTradeProgramAccount(printTrade)].concat(
await printTrade.getValidationAccounts()
);
}

export async function getSettlementPreparationAccounts(
export function prependWithProviderProgram(
printTrade: PrintTrade,
rfq: PrintTradeRfq,
response: PrintTradeResponse,
side: AuthoritySide,
additionalInfo: any
): Promise<AccountMeta[]> {
return [getPrintTradeProgramAccount(printTrade)].concat(
await printTrade.getSettlementPreparationAccounts(
rfq,
response,
side,
additionalInfo
)
);
accounts: AccountMeta[]
): AccountMeta[] {
return [getPrintTradeProgramAccount(printTrade)].concat(accounts);
}
4 changes: 4 additions & 0 deletions packages/js/src/plugins/printTradeModule/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export interface PrintTrade {
side: AuthoritySide,
additionalParams: any
) => Promise<AccountMeta[]>;
getSettlementAccounts: (
rfq: PrintTradeRfq,
response: PrintTradeResponse
) => Promise<AccountMeta[]>;
}

export interface PrintTradeLeg {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import {
serializePrintTradeAsSolitaLeg,
printTradeToSolitaLeg,
printTradetoSolitaQuote,
getPrintTradeValidationAccounts,
prependWithProviderProgram,
} from '@/plugins/printTradeModule';

const Key = 'CreatePrintTradeRfqOperation' as const;
Expand Down Expand Up @@ -307,7 +307,10 @@ export const validateRfqByPrintTradeProviderBuilder = async (

const rfqProgram = convergence.programs().getRfq(programs);

const validationAccounts = await getPrintTradeValidationAccounts(printTrade);
const validationAccounts = prependWithProviderProgram(
printTrade,
await printTrade.getValidationAccounts()
);

return TransactionBuilder.make()
.setFeePayer(payer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
makeConfirmOptionsFinalizedOnMainnet,
} from '../../../types';
import { SendAndConfirmTransactionResponse } from '../../rpcModule';
import { settleBuilder } from './settle';
import { settleEscrowBuilder } from './settle';
import { partiallySettleLegsBuilder } from './partiallySettleLegs';

const Key = 'PartiallySettleLegsAndSettleOperation' as const;
Expand Down Expand Up @@ -107,10 +107,19 @@ export const partiallySettleLegsAndSettleOperationHandler: OperationHandler<Part
scope.confirmOptions
);

let settleRfqBuilder = await settleBuilder(
const rfqModel = await convergence
.rfqs()
.findRfqByAddress({ address: rfq });

if (rfqModel.model !== 'escrowRfq') {
throw new Error('Only escrow rfqs allowed!');
}

let settleRfqBuilder = await settleEscrowBuilder(
convergence,
{
...operation.input,
response: operation.input.response,
rfq: rfqModel,
},
scope
);
Expand All @@ -120,21 +129,18 @@ export const partiallySettleLegsAndSettleOperationHandler: OperationHandler<Part
.rpc()
.getTransactionSize(settleRfqBuilder, []);

const rfqModel = await convergence
.rfqs()
.findRfqByAddress({ address: rfq });

let slicedIndex = rfqModel.legs.length;

while (settleTxSize == -1 || settleTxSize + 193 > MAX_TX_SIZE) {
const index = Math.trunc(slicedIndex / 2);
// const startIndex = rfqModel.legs.length - index;
const startIndex = rfqModel.legs.length - index + 3;

settleRfqBuilder = await settleBuilder(
settleRfqBuilder = await settleEscrowBuilder(
convergence,
{
...operation.input,
response: operation.input.response,
rfq: rfqModel,
startIndex,
},
scope
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from '../../../utils/TransactionBuilder';
import { getAuthoritySide } from '../helpers';
import { toSolitaAuthoritySide } from '../models';
import { getSettlementPreparationAccounts } from '@/plugins/printTradeModule';
import { prependWithProviderProgram } from '@/plugins/printTradeModule';

const Key = 'PreparePrintTradeSettlementOperation' as const;

Expand Down Expand Up @@ -141,12 +141,14 @@ export const preparePrintTradeSettlementBuilder = async (
}

const { printTrade } = rfqModel;
const printTradeAccounts = await getSettlementPreparationAccounts(
const printTradeAccounts = prependWithProviderProgram(
printTrade,
rfqModel,
responseModel,
side,
additionalPrintTradeInfo
await printTrade.getSettlementPreparationAccounts(
rfqModel,
responseModel,
side,
additionalPrintTradeInfo
)
);

return TransactionBuilder.make()
Expand Down
Loading

0 comments on commit 6c2c3d0

Please sign in to comment.