Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modify logic #286

Merged
merged 2 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 106 additions & 65 deletions packages/cli/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
extractBooleanString,
getSigConfirmation,
getSize,
expirationRetry,
} from './helpers';
import {
logPk,
Expand Down Expand Up @@ -110,13 +111,17 @@ export const mintTo = async (opts: Opts) => {
export const initializeProtocol = async (opts: Opts) => {
const cvg = await createCvg(opts);
try {
const { response, protocol } = await cvg.protocol().initialize({
collateralMint: new PublicKey(opts.collateralMint),
protocolTakerFee: opts.protocolTakerFee,
protocolMakerFee: opts.protocolMakerFee,
settlementTakerFee: opts.settlementTakerFee,
settlementMakerFee: opts.settlementMakerFee,
});
const { response, protocol } = await expirationRetry(
() =>
cvg.protocol().initialize({
collateralMint: new PublicKey(opts.collateralMint),
protocolTakerFee: opts.protocolTakerFee,
protocolMakerFee: opts.protocolMakerFee,
settlementTakerFee: opts.settlementTakerFee,
settlementMakerFee: opts.settlementMakerFee,
}),
opts
);
logPk(protocol.address);
logResponse(response);
} catch (e) {
Expand All @@ -127,16 +132,21 @@ export const initializeProtocol = async (opts: Opts) => {
export const addInstrument = async (opts: Opts) => {
const cvg = await createCvg(opts);
try {
const { response } = await cvg.protocol().addInstrument({
authority: cvg.rpc().getDefaultFeePayer(),
instrumentProgram: new PublicKey(opts.instrumentProgram),
canBeUsedAsQuote: extractBooleanString(opts, 'canBeUsedAsQuote'),
validateDataAccountAmount: opts.validateDataAccountAmount,
prepareToSettleAccountAmount: opts.prepareToSettleAccountAmount,
settleAccountAmount: opts.settleAccountAmount,
revertPreparationAccountAmount: opts.revertPreparationAccountAmount,
cleanUpAccountAmount: opts.cleanUpAccountAmount,
});
const { response } = await expirationRetry(
() =>
cvg.protocol().addInstrument({
authority: cvg.rpc().getDefaultFeePayer(),
instrumentProgram: new PublicKey(opts.instrumentProgram),
canBeUsedAsQuote: extractBooleanString(opts, 'canBeUsedAsQuote'),
validateDataAccountAmount: opts.validateDataAccountAmount,
prepareToSettleAccountAmount: opts.prepareToSettleAccountAmount,
settleAccountAmount: opts.settleAccountAmount,
revertPreparationAccountAmount: opts.revertPreparationAccountAmount,
cleanUpAccountAmount: opts.cleanUpAccountAmount,
}),
opts
);

logResponse(response);
} catch (e) {
logError(e);
Expand All @@ -146,11 +156,20 @@ export const addInstrument = async (opts: Opts) => {
export const addPrintTradeProvider = async (opts: Opts) => {
const cvg = await createCvg(opts);
try {
const { response } = await cvg.protocol().addPrintTradeProvider({
printTradeProviderProgram: new PublicKey(opts.printTradeProviderProgram),
settlementCanExpire: extractBooleanString(opts, 'settlementCanExpire'),
validateResponseAccountAmount: opts.validateResponseAccountAmount,
});
const { response } = await expirationRetry(
() =>
cvg.protocol().addPrintTradeProvider({
printTradeProviderProgram: new PublicKey(
opts.printTradeProviderProgram
),
settlementCanExpire: extractBooleanString(
opts,
'settlementCanExpire'
),
validateResponseAccountAmount: opts.validateResponseAccountAmount,
}),
opts
);
logResponse(response);
} catch (e) {
logError(e);
Expand All @@ -160,7 +179,10 @@ export const addPrintTradeProvider = async (opts: Opts) => {
export const addBaseAsset = async (opts: Opts) => {
const cvg = await createCvg(opts);
try {
const baseAssets = await cvg.protocol().getBaseAssets();
const baseAssets = await expirationRetry(
() => cvg.protocol().getBaseAssets(),
opts
);
const { oracleSource } = opts;

let priceOracle: PriceOracle;
Expand Down Expand Up @@ -312,7 +334,10 @@ export const registerMint = async (opts: Opts) => {
};
const cvg = await createCvg(opts);
try {
const { response } = await cvg.protocol().registerMint(getMintArgs());
const { response } = await expirationRetry(
() => cvg.protocol().registerMint(getMintArgs()),
opts
);
logResponse(response);
} catch (e) {
logError(e);
Expand Down Expand Up @@ -477,17 +502,21 @@ export const getCollateral = async (opts: Opts) => {
export const initializeRiskEngine = async (opts: Opts) => {
const cvg = await createCvg(opts);
try {
const { response } = await cvg.riskEngine().initializeConfig({
collateralMintDecimals: opts.collateralMintDecimals,
minCollateralRequirement: opts.minCollateralRequirement,
collateralForFixedQuoteAmountRfqCreation:
opts.collateralForFixedQuoteAmountRfqCreation,
safetyPriceShiftFactor: opts.safetyPriceShiftFactor,
overallSafetyFactor: opts.overallSafetyFace,
acceptedOracleStaleness: opts.acceptedOracleStaleness,
acceptedOracleConfidenceIntervalPortion:
opts.acceptedOracleConfidenceIntervalPortion,
});
const { response } = await expirationRetry(
() =>
cvg.riskEngine().initializeConfig({
collateralMintDecimals: opts.collateralMintDecimals,
minCollateralRequirement: opts.minCollateralRequirement,
collateralForFixedQuoteAmountRfqCreation:
opts.collateralForFixedQuoteAmountRfqCreation,
safetyPriceShiftFactor: opts.safetyPriceShiftFactor,
overallSafetyFactor: opts.overallSafetyFace,
acceptedOracleStaleness: opts.acceptedOracleStaleness,
acceptedOracleConfidenceIntervalPortion:
opts.acceptedOracleConfidenceIntervalPortion,
}),
opts
);
logResponse(response);
} catch (e) {
logError(e);
Expand All @@ -497,17 +526,21 @@ export const initializeRiskEngine = async (opts: Opts) => {
export const updateRiskEngine = async (opts: Opts) => {
const cvg = await createCvg(opts);
try {
const { response } = await cvg.riskEngine().updateConfig({
collateralMintDecimals: opts.collateralMintDecimals,
minCollateralRequirement: opts.minCollateralRequirement,
collateralForFixedQuoteAmountRfqCreation:
opts.collateralForFixedQuoteAmountRfqCreation,
safetyPriceShiftFactor: opts.safetyPriceShiftFactor,
overallSafetyFactor: opts.overallSafetyFace,
acceptedOracleStaleness: opts.acceptedOracleStaleness,
acceptedOracleConfidenceIntervalPortion:
opts.acceptedOracleConfidenceIntervalPortion,
});
const { response } = await expirationRetry(
() =>
cvg.riskEngine().updateConfig({
collateralMintDecimals: opts.collateralMintDecimals,
minCollateralRequirement: opts.minCollateralRequirement,
collateralForFixedQuoteAmountRfqCreation:
opts.collateralForFixedQuoteAmountRfqCreation,
safetyPriceShiftFactor: opts.safetyPriceShiftFactor,
overallSafetyFactor: opts.overallSafetyFace,
acceptedOracleStaleness: opts.acceptedOracleStaleness,
acceptedOracleConfidenceIntervalPortion:
opts.acceptedOracleConfidenceIntervalPortion,
}),
opts
);
logResponse(response);
} catch (e) {
logError(e);
Expand Down Expand Up @@ -536,10 +569,14 @@ export const getRiskEngineConfig = async (opts: Opts) => {
export const setRiskEngineInstrumentType = async (opts: Opts) => {
const cvg = await createCvg(opts);
try {
const { response } = await cvg.riskEngine().setInstrumentType({
instrumentProgram: new PublicKey(opts.program),
instrumentType: opts.type,
});
const { response } = await expirationRetry(
() =>
cvg.riskEngine().setInstrumentType({
instrumentProgram: new PublicKey(opts.program),
instrumentType: opts.type,
}),
opts
);
logResponse(response);
} catch (e) {
logError(e);
Expand All @@ -550,21 +587,25 @@ export const setRiskEngineCategoriesInfo = async (opts: Opts) => {
const newValue = opts.newValue.split(',').map((x: string) => parseFloat(x));
const cvg = await createCvg(opts);
try {
const { response } = await cvg.riskEngine().setRiskCategoriesInfo({
changes: [
{
value: toRiskCategoryInfo(newValue[0], newValue[1], [
toScenario(newValue[2], newValue[3]),
toScenario(newValue[4], newValue[5]),
toScenario(newValue[6], newValue[7]),
toScenario(newValue[8], newValue[9]),
toScenario(newValue[10], newValue[11]),
toScenario(newValue[12], newValue[13]),
]),
category: opts.category,
},
],
});
const { response } = await expirationRetry(
() =>
cvg.riskEngine().setRiskCategoriesInfo({
changes: [
{
value: toRiskCategoryInfo(newValue[0], newValue[1], [
toScenario(newValue[2], newValue[3]),
toScenario(newValue[4], newValue[5]),
toScenario(newValue[6], newValue[7]),
toScenario(newValue[8], newValue[9]),
toScenario(newValue[10], newValue[11]),
toScenario(newValue[12], newValue[13]),
]),
category: opts.category,
},
],
}),
opts
);
logResponse(response);
} catch (e) {
logError(e);
Expand Down
5 changes: 1 addition & 4 deletions packages/cli/src/cvg.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { readFileSync } from 'fs';
import { Connection, Keypair } from '@solana/web3.js';
import { Convergence, keypairIdentity } from '@convergence-rfq/sdk';
import { resolveMaxRetriesArg, resolveTxPriorityArg } from './helpers';
import { resolveTxPriorityArg } from './helpers';

export type Opts = any;

export const createCvg = async (opts: Opts): Promise<Convergence> => {
const buffer = JSON.parse(readFileSync(opts.keypairFile, 'utf8'));
const user = Keypair.fromSecretKey(new Uint8Array(buffer));
const txPriorityString: string = opts.txPriorityFee;
const maxRetriesString = opts.maxRetries;
const maxRetries = resolveMaxRetriesArg(maxRetriesString);

const txPriority = resolveTxPriorityArg(txPriorityString);
const cvg = new Convergence(
Expand All @@ -20,7 +18,6 @@ export const createCvg = async (opts: Opts): Promise<Convergence> => {
{
skipPreflight: opts.skipPreflight,
transactionPriority: txPriority,
maxRetries,
}
);
cvg.use(keypairIdentity(user));
Expand Down
27 changes: 17 additions & 10 deletions packages/cli/src/groups/hxro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Command } from 'commander';

import { PublicKey } from '@solana/web3.js';
import { HxroProductInfo } from '@convergence-rfq/sdk';
import { addCmd } from '../helpers';
import { addCmd, expirationRetry } from '../helpers';
import { createCvg, Opts } from '../cvg';
import { logError, logHxroConfig, logResponse } from '../logger';

Expand All @@ -19,9 +19,11 @@ const initializeConfigCmd = (c: Command) =>
const initializeConfig = async (opts: Opts) => {
const cvg = await createCvg(opts);
try {
const response = await cvg
.hxro()
.initializeConfig({ validMpg: new PublicKey(opts.validMpg) });
const response = await expirationRetry(
() =>
cvg.hxro().initializeConfig({ validMpg: new PublicKey(opts.validMpg) }),
opts
);
logResponse(response);
} catch (e) {
logError(e);
Expand All @@ -39,9 +41,10 @@ const modifyConfigCmd = (c: Command) =>
const modifyConfig = async (opts: Opts) => {
const cvg = await createCvg(opts);
try {
const response = await cvg
.hxro()
.modifyConfig({ validMpg: new PublicKey(opts.validMpg) });
const response = await expirationRetry(
() => cvg.hxro().modifyConfig({ validMpg: new PublicKey(opts.validMpg) }),
opts
);
logResponse(response);
} catch (e) {
logError(e);
Expand Down Expand Up @@ -101,9 +104,13 @@ const initializeOperatorTRG = async (opts: Opts) => {
opts.hxroRiskEngine !== ''
? new PublicKey(opts.hxroRiskEngine)
: undefined;
const response = await cvg.hxro().initializeOperatorTraderRiskGroup({
hxroRiskEngineAddress,
});
const response = await expirationRetry(
() =>
cvg.hxro().initializeOperatorTraderRiskGroup({
hxroRiskEngineAddress,
}),
opts
);
logResponse(response);
} catch (e) {
logError(e);
Expand Down
9 changes: 5 additions & 4 deletions packages/cli/src/groups/spotInstrument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { Command } from 'commander';

import { addCmd } from '../helpers';
import { addCmd, expirationRetry } from '../helpers';
import { createCvg, Opts } from '../cvg';
import { logError, logResponse, logSpotInstrumentConfig } from '../logger';

Expand All @@ -23,9 +23,10 @@ const initializeConfigCmd = (c: Command) =>
const initializeConfig = async (opts: Opts) => {
const cvg = await createCvg(opts);
try {
const response = await cvg
.spotInstrument()
.initializeConfig({ feeBps: opts.feeBps });
const response = await expirationRetry(
() => cvg.spotInstrument().initializeConfig({ feeBps: opts.feeBps }),
opts
);
logResponse(response);
} catch (e) {
logError(e);
Expand Down
28 changes: 28 additions & 0 deletions packages/cli/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,31 @@ export const resolveMaxRetriesArg = (maxRetries: string): number => {
}
return maxRetriesInNumber;
};

export async function expirationRetry<T>(
fn: () => Promise<T>,
opts: Opts
): Promise<T> {
let { maxRetries } = opts;
maxRetries = resolveMaxRetriesArg(maxRetries);
if (maxRetries === 0) return await fn();
let retryCount = 0;

while (retryCount < maxRetries) {
try {
return await fn();
} catch (error) {
if (!isTransactionExpiredBlockheightExceededError(error)) throw error;
retryCount++;
console.error(`Attempt ${retryCount + 1} tx expired. Retrying...`);
}
}
throw new Error('Max Tx Expiration retries exceeded');
}

function isTransactionExpiredBlockheightExceededError(error: unknown) {
return (
error instanceof Error &&
error.message.includes('TransactionExpiredBlockheightExceededError')
);
}
3 changes: 0 additions & 3 deletions packages/js/src/Convergence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,19 @@ export type ConvergenceOptions = {
cluster?: Cluster;
skipPreflight?: boolean;
transactionPriority?: TransactionPriority;
maxRetries?: number;
};

export class Convergence {
public readonly connection: Connection;
public readonly cluster: Cluster;
public readonly skipPreflight: boolean;
public readonly transactionPriority: TransactionPriority;
public readonly maxRetries: number;

constructor(connection: Connection, options: ConvergenceOptions = {}) {
this.connection = connection;
this.cluster = options.cluster ?? resolveClusterFromConnection(connection);
this.skipPreflight = options.skipPreflight ?? false;
this.transactionPriority = options.transactionPriority ?? 'none';
this.maxRetries = options.maxRetries ?? 0;
this.use(corePlugins());
}

Expand Down
Loading
Loading