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

Dev hxro add update mark price ix #307

Merged
merged 7 commits into from
Jun 3, 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
48 changes: 48 additions & 0 deletions packages/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,53 @@
# @convergence-rfq/cli

## 6.6.24

### Patch Changes

- add lastValidBlockhash to revertSettlementPreparation txs
- Updated dependencies
- @convergence-rfq/[email protected]

## 6.6.23

### Patch Changes

- split txs for revertPrepareSettlement
- Updated dependencies
- @convergence-rfq/[email protected]

## 6.6.22

### Patch Changes

- add updateMarkPriceIx to unlockHxroCollatera fn
- Updated dependencies
- @convergence-rfq/[email protected]

## 6.6.21

### Patch Changes

- add logs to check unlockhxroCollateralTxSize
- Updated dependencies
- @convergence-rfq/[email protected]

## 6.6.20

### Patch Changes

- fix tx size overflow for unlockHxroCollateralTx
- Updated dependencies
- @convergence-rfq/[email protected]

## 6.6.19

### Patch Changes

- modify unclockHxroCollateral and add markPriceUpdate ixs
- Updated dependencies
- @convergence-rfq/[email protected]

## 6.6.18

### 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": "6.6.18",
"version": "6.6.24",
"license": "MIT",
"publishConfig": {
"access": "public"
Expand Down Expand Up @@ -47,7 +47,7 @@
"cli": "ts-node src/index.ts"
},
"dependencies": {
"@convergence-rfq/sdk": "6.6.18",
"@convergence-rfq/sdk": "6.6.24",
"@solana/web3.js": "^1.87.6",
"@types/cookie": "^0.5.1",
"commander": "^10.0.0"
Expand Down
36 changes: 36 additions & 0 deletions packages/js/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,41 @@
# @convergence-rfq/sdk

## 6.6.24

### Patch Changes

- add lastValidBlockhash to revertSettlementPreparation txs

## 6.6.23

### Patch Changes

- split txs for revertPrepareSettlement

## 6.6.22

### Patch Changes

- add updateMarkPriceIx to unlockHxroCollatera fn

## 6.6.21

### Patch Changes

- add logs to check unlockhxroCollateralTxSize

## 6.6.20

### Patch Changes

- fix tx size overflow for unlockHxroCollateralTx

## 6.6.19

### Patch Changes

- modify unclockHxroCollateral and add markPriceUpdate ixs

## 6.6.18

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/js/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@convergence-rfq/sdk",
"description": "Official Convergence RFQ SDK",
"version": "6.6.18",
"version": "6.6.24",
"license": "MIT",
"publishConfig": {
"access": "public"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
import dexterity from '@hxronetwork/dexterity-ts';

import BN from 'bn.js';
import { Transaction } from '@solana/web3.js';
import * as solana from '@solana/web3.js';
import { getHxroProgramFromIDL } from '../program';
import { fetchValidHxroMpg } from '../helpers';
import { hxroManifestCache } from '../cache';
Expand All @@ -18,8 +20,8 @@ import {
PublicKey,
useOperation,
} from '@/types';
import { TransactionBuilder, TransactionBuilderOptions } from '@/utils';
import { SendAndConfirmTransactionResponse } from '@/plugins/rpcModule';
import { TransactionBuilder, TransactionBuilderOptions, sleep } from '@/utils';
import { isLocalEnv } from '@/utils/helpers';

const Key = 'unlockHxroCollateralByRecord' as const;

Expand Down Expand Up @@ -51,8 +53,7 @@ export type UnlockHxroCollateralByRecordInput = {
* @category Outputs
*/
export type UnlockHxroCollateralByRecordOutput =
SendAndConfirmTransactionResponse;

solana.RpcResponseAndContext<solana.SignatureResult>;
/**
* @group Operations
* @category Handlers
Expand All @@ -67,7 +68,7 @@ export const unlockHxroCollateralByRecordOperationHandler: OperationHandler<Unlo
const {
input: { lockRecord, action = 'unlock-and-remove-record' },
} = operation;

const payer = cvg.rpc().getDefaultFeePayer();
let lockRecordData: WithPubkey<LockedCollateralRecordArgs>;
if ('publicKey' in lockRecord) {
lockRecordData = lockRecord;
Expand All @@ -79,9 +80,13 @@ export const unlockHxroCollateralByRecordOperationHandler: OperationHandler<Unlo
lockRecordData = { ...accountData, publicKey: lockRecord };
}

const builder = TransactionBuilder.make().setFeePayer(cvg.identity());
const unlockHxroCollateralTxBuilder =
TransactionBuilder.make().setFeePayer(cvg.identity());
const removeRecordCollateralTxBuilder =
TransactionBuilder.make().setFeePayer(cvg.identity());

if (action == 'unlock' || action == 'unlock-and-remove-record') {
builder.add(
unlockHxroCollateralTxBuilder.add(
await unlockHxroCollateralBuilder(
cvg,
{
Expand All @@ -93,7 +98,7 @@ export const unlockHxroCollateralByRecordOperationHandler: OperationHandler<Unlo
}

if (action == 'remove-record' || action == 'unlock-and-remove-record') {
builder.add(
removeRecordCollateralTxBuilder.add(
await removeLockCollateralRecordBuilder(
cvg,
{
Expand All @@ -104,8 +109,131 @@ export const unlockHxroCollateralByRecordOperationHandler: OperationHandler<Unlo
);
}

const output = await builder.sendAndConfirm(cvg, scope.confirmOptions);
return output.response;
const slot = await cvg.connection.getSlot();

const [lookupTableInst, lookupTableAddress] =
solana.AddressLookupTableProgram.createLookupTable({
authority: payer.publicKey,
payer: payer.publicKey,
recentSlot: slot,
});
const lookUpTable = await cvg.connection.getAddressLookupTable(
lookupTableAddress
);
const lastValidBlockHeight = await cvg.rpc().getLatestBlockhash();
const addressLookupTableAcc = lookUpTable.value;
const lookUpTxs: Transaction[] = [];
const unlockHxroCollateralAccouts = unlockHxroCollateralTxBuilder
.getInstructions()
.map((ix) => ix.keys.map((k) => k.pubkey))
.flat();

if (addressLookupTableAcc && addressLookupTableAcc.isActive()) {
const remainingAddresses: PublicKey[] = [];
const addressesArray = addressLookupTableAcc.state.addresses.map((a) =>
a.toBase58()
);
for (const addr of unlockHxroCollateralAccouts) {
if (!addressesArray.includes(addr.toBase58())) {
remainingAddresses.push(addr);
break;
}
}
if (remainingAddresses.length > 0) {
const extendInstruction =
solana.AddressLookupTableProgram.extendLookupTable({
payer: payer.publicKey,
authority: payer.publicKey,
lookupTable: lookupTableAddress,
addresses: remainingAddresses,
});
const extendLookupTx = new Transaction().add(extendInstruction);
extendLookupTx.feePayer = payer.publicKey;
extendLookupTx.recentBlockhash = lastValidBlockHeight.blockhash;
extendLookupTx.lastValidBlockHeight =
lastValidBlockHeight.lastValidBlockHeight;
lookUpTxs.push(extendLookupTx);
}
} else {
const lookUpCreateTx = new Transaction().add(lookupTableInst);
lookUpCreateTx.feePayer = payer.publicKey;
lookUpCreateTx.recentBlockhash = lastValidBlockHeight.blockhash;
lookUpCreateTx.lastValidBlockHeight =
lastValidBlockHeight.lastValidBlockHeight;
lookUpTxs.push(lookUpCreateTx);

const extendInstruction =
solana.AddressLookupTableProgram.extendLookupTable({
payer: payer.publicKey,
authority: payer.publicKey,
lookupTable: lookupTableAddress,
addresses: unlockHxroCollateralAccouts,
});
const extendLookupTx = new Transaction().add(extendInstruction);
extendLookupTx.feePayer = payer.publicKey;
extendLookupTx.recentBlockhash = lastValidBlockHeight.blockhash;
extendLookupTx.lastValidBlockHeight =
lastValidBlockHeight.lastValidBlockHeight;
lookUpTxs.push(extendLookupTx);
}

if (lookUpTxs.length > 0) {
const signedLookUpTxs = await cvg
.identity()
.signAllTransactions(lookUpTxs);
for (const tx of signedLookUpTxs) {
await cvg.rpc().serializeAndSendTransaction(tx);
}
}

const txs: Transaction[] = [];

if (action == 'unlock' || action == 'unlock-and-remove-record') {
txs.push(
unlockHxroCollateralTxBuilder.toTransaction(lastValidBlockHeight)
);
}
if (action == 'remove-record' || action == 'unlock-and-remove-record') {
txs.push(
removeRecordCollateralTxBuilder.toTransaction(lastValidBlockHeight)
);
}

if (txs.length === 0) {
throw new Error('No transactions to send');
}
const signedTxs = await cvg.identity().signAllTransactions(txs);
let txResponse: solana.RpcResponseAndContext<solana.SignatureResult> | null =
null;

const newlookUpTable = await cvg.connection.getAddressLookupTable(
lookupTableAddress
);
const newAddressLookupTableAcc = newlookUpTable.value;
if (!newAddressLookupTableAcc) {
throw new Error('No lookup table');
}
const txMessages = signedTxs.map((tx) =>
new solana.TransactionMessage({
payerKey: payer.publicKey,
recentBlockhash: lastValidBlockHeight.blockhash,
instructions: tx.instructions.map((ix) => ix),
}).compileToV0Message([newAddressLookupTableAcc])
);

const versionedTxs = txMessages.map(
(msg) => new solana.VersionedTransaction(msg)
);
await sleep(1);
for (const tx of versionedTxs) {
tx.sign([cvg.identity() as solana.Signer]);
const sig = await cvg.connection.sendTransaction(tx);
txResponse = await cvg.connection.confirmTransaction(sig);
}
if (!txResponse) {
throw new Error('No transaction response');
}
return txResponse;
},
};

Expand Down Expand Up @@ -197,11 +325,26 @@ export const unlockHxroCollateralBuilder = async (
])
.instruction();

return TransactionBuilder.make<{}>()
const txBuilder = TransactionBuilder.make<{}>()
.setFeePayer(payer)
.add({
instruction,
signers: [payer],
key: 'unlockHxroCollateral',
});

if (!isLocalEnv(cvg)) {
const trader = new dexterity.Trader(manifest, lockRecord.trg, true);

await trader.connect(null, null);
const updateMarkPriceIx = trader.getUpdateMarkPricesIx();
updateMarkPriceIx.keys[0].pubkey = payer.publicKey;

txBuilder.prepend({
instruction: updateMarkPriceIx,
signers: [payer],
key: 'updateMarkPrices',
});
}
return txBuilder;
};
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,29 @@ export const revertSettlementPreparationOperationHandler: OperationHandler<Rever
convergence,
scope.confirmOptions
);

const output = await builder.sendAndConfirm(convergence, confirmOptions);
scope.throwIfCanceled();

return output;
if (builder.checkTransactionFits()) {
const output = await builder.sendAndConfirm(
convergence,
confirmOptions
);
scope.throwIfCanceled();

return output;
}
const builders = builder.divideToMultipleBuildersThatFit();
const latestBlockhash = await convergence.rpc().getLatestBlockhash();
const txs = builders.map((b) => b.toTransaction(latestBlockhash));
const signedTxs = await convergence.identity().signAllTransactions(txs);
let output: SendAndConfirmTransactionResponse | undefined;
for (const signedTx of signedTxs) {
output = await convergence
.rpc()
.serializeAndSendTransaction(signedTx, latestBlockhash);
}
if (!output) {
throw new Error('No transaction was sent');
}
return { response: output };
},
};

Expand Down
Loading