Skip to content

Commit

Permalink
Merge pull request #230 from convergence-rfq/sdk-279-add-hxro-integra…
Browse files Browse the repository at this point in the history
…tion-to-sdk

Print trade provider logic and Hxro
  • Loading branch information
pindaroso authored Mar 7, 2024
2 parents 174ca17 + b97465e commit c071d0b
Show file tree
Hide file tree
Showing 198 changed files with 16,743 additions and 3,152 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,12 @@ jobs:
shell: bash
run: yarn validator & sleep 3

- name: Logs
continue-on-error: false
shell: bash
run: yarn validator:logs & sleep 3

- name: Test
run: yarn test


25 changes: 25 additions & 0 deletions packages/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
# @convergence-rfq/cli

## 6.1.0

### Minor Changes

- Remove collateral requirements, add quote spot fees, add spot instrument config, remove operations to unlock collateral or settle defaults

### Patch Changes

- Updated dependencies
- @convergence-rfq/sdk@6.1.0

## 6.0.1

### Patch Changes

- Updated dependencies
- @convergence-rfq/sdk@6.0.1

## 6.0.0

### Patch Changes

- Updated dependencies
- @convergence-rfq/sdk@6.0.0

## 4.5.35

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@convergence-rfq/cli",
"description": "Official Convergence CLI",
"version": "4.5.35",
"version": "6.1.0",
"license": "MIT",
"publishConfig": {
"access": "public"
Expand Down Expand Up @@ -47,7 +47,7 @@
"cli": "ts-node src/index.ts"
},
"dependencies": {
"@convergence-rfq/sdk": "4.5.35",
"@convergence-rfq/sdk": "6.1.0",
"@solana/web3.js": "^1.87.6",
"@types/cookie": "^0.5.1",
"commander": "^10.0.0"
Expand Down
13 changes: 10 additions & 3 deletions packages/cli/scripts/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ set -e

export DEVNET="true"

export SPOT_INSTRUMENT="CjQCEjXtG3QNBuT5Z1sctaAYCo5Mt6edftqHQetEPo9w"
export PSYOPTIONS_EUROPEAN_INSTRUMENT="A86fhhdNVDdXV8pB48WXtPeM3EBkcBeJEdrx9xrUo9nF"
export PSYOPTIONS_AMERICAN_INSTRUMENT="6JG1tWK4w6LmjeXbmDZJsmUsPSjgnp74j2XPsTvjjTX8"
export SPOT_INSTRUMENT="4A9M7iojGDPc4n4YDGnTmsYsNKUohG1zM1nrAqVMMmrm"
export PSYOPTIONS_EUROPEAN_INSTRUMENT="6B7TdBNAF7tWWz5sZbbBZj8jH1ix7QWAchtkvMHveEuW"
export PSYOPTIONS_AMERICAN_INSTRUMENT="HGmSFSRfVAG8RC8Ae4G1JFSHK7Au5GrGskDxncG3JRok"
export HXRO_PRINT_TRADE_PROVIDER="4WbVwc5Edfo3oB1n16bVC9qrghYHSNh1qAECbSCyiT95"

# Same for devnet and mainnet
export BTC_ORACLE_ADDRESS="8SXvChNYFhRq4EZuZvnhjrB3jJRQCv4k3P4W6hesH3Ee"
Expand All @@ -31,6 +32,12 @@ convergence protocol initialize --collateral-mint=$USDC_MINT --rpc-endpoint=$RPC

convergence risk-engine initialize --rpc-endpoint=$RPC_ENDPOINT

convergence protocol add-print-trade-provider --print-trade-provider-program $HXRO_PRINT_TRADE_PROVIDER --settlement-can-expire false --validate-response-account-amount 2 --rpc-endpoint=$RPC_ENDPOINT
convergence hxro initialize-config --valid-mpg $HXRO_MPG --rpc-endpoint=$RPC_ENDPOINT
convergence hxro initialize-operator-trg --rpc-endpoint=$RPC_ENDPOINT

convergence spot-instrument initialize-config --fee-bps "0.01" --rpc-endpoint=$RPC_ENDPOINT

convergence protocol add-instrument --instrument-program=$SPOT_INSTRUMENT --can-be-used-as-quote=true --validate-data-account-amount=1 --prepare-to-settle-account-amount=7 --settle-account-amount=3 --revert-preparation-account-amount=3 --clean-up-account-amount=4 --rpc-endpoint=$RPC_ENDPOINT
convergence protocol add-instrument --instrument-program=$PSYOPTIONS_EUROPEAN_INSTRUMENT --can-be-used-as-quote=false --validate-data-account-amount=2 --prepare-to-settle-account-amount=7 --settle-account-amount=3 --revert-preparation-account-amount=3 --clean-up-account-amount=4 --rpc-endpoint=$RPC_ENDPOINT
convergence protocol add-instrument --instrument-program=$PSYOPTIONS_AMERICAN_INSTRUMENT --can-be-used-as-quote=false --validate-data-account-amount=3 --prepare-to-settle-account-amount=7 --settle-account-amount=3 --revert-preparation-account-amount=3 --clean-up-account-amount=4 --rpc-endpoint=$RPC_ENDPOINT
Expand Down
101 changes: 98 additions & 3 deletions packages/cli/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
PriceOracle,
SpotLegInstrument,
SpotQuoteInstrument,
isRiskCategory,
isOracleSource,
addBaseAssetBuilder,
registerMintBuilder,
TransactionBuilder,
Expand All @@ -16,7 +18,7 @@ import { createCvg, Opts } from './cvg';
import {
fetchBirdeyeTokenPrice,
fetchCoinGeckoTokenPrice,
getInstrumentType,
extractBooleanString,
getSigConfirmation,
getSize,
} from './helpers';
Expand Down Expand Up @@ -128,7 +130,7 @@ export const addInstrument = async (opts: Opts) => {
const { response } = await cvg.protocol().addInstrument({
authority: cvg.rpc().getDefaultFeePayer(),
instrumentProgram: new PublicKey(opts.instrumentProgram),
canBeUsedAsQuote: opts.canBeUsedAsQuote,
canBeUsedAsQuote: extractBooleanString(opts, 'canBeUsedAsQuote'),
validateDataAccountAmount: opts.validateDataAccountAmount,
prepareToSettleAccountAmount: opts.prepareToSettleAccountAmount,
settleAccountAmount: opts.settleAccountAmount,
Expand All @@ -141,6 +143,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,
});
logResponse(response);
} catch (e) {
logError(e);
}
};

export const addBaseAsset = async (opts: Opts) => {
const cvg = await createCvg(opts);
try {
Expand Down Expand Up @@ -173,6 +189,85 @@ export const addBaseAsset = async (opts: Opts) => {
}
};

export const changeBaseAssetParameters = async (opts: Opts) => {
const cvg = await createCvg(opts);
try {
const {
index,
enabled: enabledOpts,
riskCategory,
oracleSource,
switchboardOracle: switchboardOracleOpts,
pythOracle: pythOracleOpts,
inPlacePrice: inPlacePriceOpts,
}: {
index: number;
enabled?: string;
riskCategory?: string;
oracleSource?: string;
switchboardOracle?: string;
pythOracle?: string;
inPlacePrice?: number;
} = opts;

let enabled;
switch (enabledOpts) {
case undefined:
break;
case 'true':
enabled = true;
break;
case 'false':
enabled = false;
break;
default:
throw new Error('Unrecognized enabled parameter!');
}

if (riskCategory !== undefined && !isRiskCategory(riskCategory)) {
throw new Error('Unrecognized risk category parameter!');
}

if (oracleSource !== undefined && !isOracleSource(oracleSource)) {
throw new Error('Unrecognized oracle source parameter!');
}

let switchboardOracle;
if (switchboardOracleOpts === 'none') {
switchboardOracle = null;
} else if (typeof switchboardOracleOpts === 'string') {
switchboardOracle = new PublicKey(switchboardOracleOpts);
}

let pythOracle;
if (pythOracleOpts === 'none') {
pythOracle = null;
} else if (typeof pythOracleOpts === 'string') {
pythOracle = new PublicKey(pythOracleOpts);
}

let inPlacePrice;
if (inPlacePriceOpts === -1) {
inPlacePrice = null;
} else if (typeof inPlacePriceOpts === 'number') {
inPlacePrice = inPlacePriceOpts;
}

const { response } = await cvg.protocol().changeBaseAssetParameters({
index,
enabled,
riskCategory,
oracleSource,
switchboardOracle,
pythOracle,
inPlacePrice,
});
logResponse(response);
} catch (e) {
logError(e);
}
};

export const updateBaseAsset = async (opts: Opts) => {
const cvg = await createCvg(opts);
const {
Expand Down Expand Up @@ -443,7 +538,7 @@ export const setRiskEngineInstrumentType = async (opts: Opts) => {
try {
const { response } = await cvg.riskEngine().setInstrumentType({
instrumentProgram: new PublicKey(opts.program),
instrumentType: getInstrumentType(opts.type),
instrumentType: opts.type,
});
logResponse(response);
} catch (e) {
Expand Down
4 changes: 4 additions & 0 deletions packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
airdropGroup,
rfqGroup,
tokenGroup,
hxroGroup,
spotInstrumentGroup,
} from './groups';

export const makeCli = (): Command => {
Expand All @@ -17,6 +19,8 @@ export const makeCli = (): Command => {
riskEngineGroup,
collateralGroup,
rfqGroup,
hxroGroup,
spotInstrumentGroup,
];

const cli = new Command();
Expand Down
3 changes: 1 addition & 2 deletions packages/cli/src/cvg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ export const createCvg = async (opts: Opts): Promise<Convergence> => {
new Connection(opts.rpcEndpoint, {
commitment: 'confirmed',
}),

{ transactionPriority: opts?.txPriority ?? 'none', skipPreflight: false }
{ skipPreflight: opts.skipPreflight }
);
cvg.use(keypairIdentity(user));
return cvg;
Expand Down
120 changes: 120 additions & 0 deletions packages/cli/src/groups/hxro.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/* eslint-disable no-console */

import { Command } from 'commander';

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

const initializeConfigCmd = (c: Command) =>
addCmd(c, 'initialize-config', 'initializes hxro config', initializeConfig, [
{
flags: '--valid-mpg <string>',
description: 'Valid Hxro market product group',
},
]);

const initializeConfig = async (opts: Opts) => {
const cvg = await createCvg(opts);
try {
const response = await cvg
.hxro()
.initializeConfig({ validMpg: new PublicKey(opts.validMpg) });
logResponse(response);
} catch (e) {
logError(e);
}
};

const modifyConfigCmd = (c: Command) =>
addCmd(c, 'modify-config', 'modifiess hxro config', modifyConfig, [
{
flags: '--valid-mpg <string>',
description: 'Valid Hxro market product group',
},
]);

const modifyConfig = async (opts: Opts) => {
const cvg = await createCvg(opts);
try {
const response = await cvg
.hxro()
.modifyConfig({ validMpg: new PublicKey(opts.validMpg) });
logResponse(response);
} catch (e) {
logError(e);
}
};

const displayConfigCmd = (c: Command) =>
addCmd(c, 'display-config', 'displays hxro config', displayConfig, []);

const displayConfig = async (opts: Opts) => {
const cvg = await createCvg(opts);
try {
const config = await cvg.hxro().fetchConfig();
logHxroConfig(config);
} catch (e) {
logError(e);
}
};

const displayProductsCmd = (c: Command) =>
addCmd(c, 'display-products', 'displays hxro products', displayProducts, []);

const displayProducts = async (opts: Opts) => {
const cvg = await createCvg(opts);
try {
const products: HxroProductInfo[] = await cvg.hxro().fetchProducts();
console.log(`Products amount: ${products.length}`);
products.forEach((product) => {
console.log(JSON.stringify(product));
});
} catch (e) {
logError(e);
}
};

const initializeOperatorTRGCmd = (c: Command) =>
addCmd(
c,
'initialize-operator-trg',
'initialized a trg for an operator, which is required for hxro settlements',
initializeOperatorTRG,
[
{
flags: '--hxro-risk-engine <string>',
description:
'Overrides hxro risk engine address. Should be used primarely for testing purposes',
defaultValue: '',
},
]
);

const initializeOperatorTRG = async (opts: Opts) => {
const cvg = await createCvg(opts);

try {
const hxroRiskEngineAddress =
opts.hxroRiskEngine !== ''
? new PublicKey(opts.hxroRiskEngine)
: undefined;
const response = await cvg.hxro().initializeOperatorTraderRiskGroup({
hxroRiskEngineAddress,
});
logResponse(response);
} catch (e) {
logError(e);
}
};

export const hxroGroup = (c: Command) => {
const group = c.command('hxro');
initializeConfigCmd(group);
modifyConfigCmd(group);
displayConfigCmd(group);
initializeOperatorTRGCmd(group);
displayProductsCmd(group);
};
2 changes: 2 additions & 0 deletions packages/cli/src/groups/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ export * from './protocol';
export * from './rfq';
export * from './riskEngine';
export * from './token';
export * from './hxro';
export * from './spotInstrument';
Loading

0 comments on commit c071d0b

Please sign in to comment.