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

Sdk 372 preparesettlement should calculate needed atas create atas #240

Merged
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
252658e
need to fix mintAmericanOptions
Nagaprasadvr Aug 29, 2023
6ad852c
add logs to find error
Nagaprasadvr Aug 29, 2023
b4140d4
testing
Nagaprasadvr Aug 31, 2023
c1e9a55
add mintOptions logic to prepareSettlement operation
Nagaprasadvr Sep 4, 2023
abebda0
american mintOptions not working
Nagaprasadvr Sep 4, 2023
76fe7a8
testing
Nagaprasadvr Sep 5, 2023
f54300f
fix build errors
Nagaprasadvr Sep 5, 2023
4c0c659
fix more build errors
Nagaprasadvr Sep 5, 2023
743b8bd
Merge branch 'sdk-408-fix-cli-tests' of https://github.com/convergenc…
Nagaprasadvr Sep 5, 2023
9c3c6bc
add cvgWallet
Nagaprasadvr Sep 5, 2023
5f8f6f3
fix circular deps
Nagaprasadvr Sep 5, 2023
32ab68a
change defaultFeePayer to identity in CvgWallet
Nagaprasadvr Sep 5, 2023
d498542
add convergence.identity() as signer
Nagaprasadvr Sep 5, 2023
22e1227
fixed
Nagaprasadvr Sep 5, 2023
a3e4ac6
reduce expiry
Nagaprasadvr Sep 5, 2023
7fbacd8
replace sol with btc mint
Nagaprasadvr Sep 5, 2023
cc61a70
add long and short legs to straddle test
Nagaprasadvr Sep 5, 2023
5b27ca6
Merge branch 'dev' of https://github.com/convergence-rfq/convergence-…
Nagaprasadvr Sep 5, 2023
85e9a71
add more randomness to expiry
Nagaprasadvr Sep 5, 2023
ba41ad9
use Promise.all
Nagaprasadvr Sep 5, 2023
3b384c6
fix promise.all
Nagaprasadvr Sep 5, 2023
d627b60
cleanup
Nagaprasadvr Sep 5, 2023
ae5ae1c
optimise and cleanup
Nagaprasadvr Sep 7, 2023
2dfa4f7
fix createEuropeanProgram
Nagaprasadvr Sep 7, 2023
c769c1f
cleanup
Nagaprasadvr Sep 7, 2023
fa7b6d3
fix settlementResult tests
Nagaprasadvr Sep 7, 2023
6937fb7
resolve comments
Nagaprasadvr Sep 8, 2023
9dd996d
pull dev
Nagaprasadvr Sep 11, 2023
85aa453
resolve comments
Nagaprasadvr Sep 11, 2023
df69c67
add sendAndConfirmTx fn when signing individual txs
Nagaprasadvr Sep 11, 2023
ba5afa6
remove convergence.identity() as explicit signer
Nagaprasadvr Sep 11, 2023
d28abd9
fix signature verification error
Nagaprasadvr Sep 11, 2023
c7c16dc
add more randomness to expiry
Nagaprasadvr Sep 11, 2023
344a832
simplify UniqueInstructionTracker logic
Nagaprasadvr Sep 14, 2023
b655a68
optimize UnqueInstructionTracker code
Nagaprasadvr Sep 14, 2023
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
223 changes: 111 additions & 112 deletions packages/js/src/plugins/psyoptionsAmericanInstrumentModule/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,100 +1,17 @@
import * as psyoptionsAmerican from '@mithraic-labs/psy-american';

import { BN } from 'bn.js';
import { PublicKey } from '@solana/web3.js';
import { PublicKey, Transaction } from '@solana/web3.js';
import { Convergence } from '../../Convergence';

import { ATAExistence, getOrCreateATA } from '../../utils/ata';
import { getOrCreateATAtxBuilder } from '../../utils/ata';
import { Mint } from '../tokenModule/models';
import { TransactionBuilder } from '../../utils/TransactionBuilder';
import { CvgWallet } from '../../utils/Wallets';
import {
InstructionWithSigners,
TransactionBuilder,
} from '../../utils/TransactionBuilder';
import { InstructionUniquenessTracker } from '../../utils/classes';
import { PsyoptionsAmericanInstrument } from './types';
import { createAmericanProgram } from './instrument';

export const mintAmericanOptions = async (
convergence: Convergence,
responseAddress: PublicKey,
caller: PublicKey,
americanProgram: any
) => {
const response = await convergence
.rfqs()
.findResponseByAddress({ address: responseAddress });
const rfq = await convergence
.rfqs()
.findRfqByAddress({ address: response.rfq });

const callerSide = caller.equals(rfq.taker) ? 'taker' : 'maker';
const instructionWithSigners: InstructionWithSigners[] = [];
const { legs } = await convergence.rfqs().getSettlementResult({
response,
rfq,
});
for (const [index, leg] of rfq.legs.entries()) {
if (leg instanceof PsyoptionsAmericanInstrument) {
const { receiver } = legs[index];
if (receiver !== callerSide) {
const { amount } = legs[index];

const optionMarket = await psyoptionsAmerican.getOptionByKey(
americanProgram,
leg.optionMetaPubKey
);
if (optionMarket) {
const optionToken = await getOrCreateATA(
convergence,
optionMarket.optionMint,
caller
);

const writerToken = await getOrCreateATA(
convergence,
optionMarket!.writerTokenMint,
caller
);

const underlyingToken = await getOrCreateATA(
convergence,
optionMarket!.underlyingAssetMint,
caller
);

const ixWithSigners =
await psyoptionsAmerican.instructions.mintOptionV2Instruction(
americanProgram,
optionToken,
writerToken,
underlyingToken,
new BN(amount!),
optionMarket as psyoptionsAmerican.OptionMarketWithKey
);
ixWithSigners.ix.keys[0] = {
pubkey: caller,
isSigner: true,
isWritable: false,
};
instructionWithSigners.push({
instruction: ixWithSigners.ix,
signers: ixWithSigners.signers,
});
}
}
}
}
if (instructionWithSigners.length > 0) {
const payer = convergence.rpc().getDefaultFeePayer();
const txBuilder = TransactionBuilder.make().setFeePayer(payer);

txBuilder.add(...instructionWithSigners);
const sig = await txBuilder.sendAndConfirm(convergence);
return sig;
}
return null;
};

export const initializeNewAmericanOption = async (
convergence: Convergence,
underlyingMint: Mint,
Expand All @@ -112,10 +29,8 @@ export const initializeNewAmericanOption = async (
Number(underlyingAmountPerContract) * Math.pow(10, underlyingMint.decimals)
);

const americanProgram = createAmericanProgram(
convergence,
new CvgWallet(convergence)
);
const cvgWallet = new CvgWallet(convergence);
const americanProgram = createAmericanProgram(convergence, cvgWallet);

const { optionMarketKey, optionMintKey, writerMintKey } =
await psyoptionsAmerican.instructions.initializeMarket(americanProgram, {
Expand Down Expand Up @@ -143,15 +58,15 @@ export const initializeNewAmericanOption = async (
optionMint,
};
};

// used in UI
export const getOrCreateAmericanOptionATAs = async (
//create American Options ATAs and mint Options
export const prepareAmericanOptions = async (
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function prepareAmericanOptions has 113 lines of code (exceeds 25 allowed). Consider refactoring.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function prepareAmericanOptions has a Cognitive Complexity of 21 (exceeds 5 allowed). Consider refactoring.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function prepareAmericanOptions has 108 lines of code (exceeds 25 allowed). Consider refactoring.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function prepareAmericanOptions has a Cognitive Complexity of 18 (exceeds 5 allowed). Consider refactoring.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function prepareAmericanOptions has 105 lines of code (exceeds 25 allowed). Consider refactoring.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function prepareAmericanOptions has a Cognitive Complexity of 16 (exceeds 5 allowed). Consider refactoring.

convergence: Convergence,
responseAddress: PublicKey,
caller: PublicKey,
americanProgram: any
): Promise<ATAExistence> => {
let flag = false;
caller: PublicKey
) => {
const ixTracker = new InstructionUniquenessTracker([]);
const cvgWallet = new CvgWallet(convergence);
const americanProgram = createAmericanProgram(convergence, cvgWallet);
const response = await convergence
.rfqs()
.findResponseByAddress({ address: responseAddress });
Expand All @@ -160,28 +75,112 @@ export const getOrCreateAmericanOptionATAs = async (
.findRfqByAddress({ address: response.rfq });

const callerSide = caller.equals(rfq.taker) ? 'taker' : 'maker';
const { legs } = await convergence.rfqs().getSettlementResult({

const { legs } = convergence.rfqs().getSettlementResult({
response,
rfq,
});

const ataTxBuilderArray: TransactionBuilder[] = [];
const mintTxBuilderArray: TransactionBuilder[] = [];
for (const [index, leg] of rfq.legs.entries()) {
if (leg instanceof PsyoptionsAmericanInstrument) {
const { receiver } = legs[index];
if (receiver !== callerSide) {
flag = true;
const { receiver, amount } = legs[index];
if (
!(leg instanceof PsyoptionsAmericanInstrument) ||
receiver === callerSide
) {
continue;
}

const optionMarket = await psyoptionsAmerican.getOptionByKey(
const optionMarket = await leg.getOptionMeta();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we skip all the logic if this returns null?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved

if (optionMarket) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can reverse this logic to remove the amount of nesting

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved

const optionToken = await getOrCreateATAtxBuilder(
convergence,
optionMarket.optionMint,
caller
);
if (
optionToken.txBuilder &&
ixTracker.checkedAdd(optionToken.txBuilder)
) {
ataTxBuilderArray.push(optionToken.txBuilder);
}
const writerToken = await getOrCreateATAtxBuilder(
convergence,
optionMarket!.writerTokenMint,
caller
);
if (
writerToken.txBuilder &&
ixTracker.checkedAdd(writerToken.txBuilder)
) {
ataTxBuilderArray.push(writerToken.txBuilder);
}
const underlyingToken = await getOrCreateATAtxBuilder(
convergence,
optionMarket!.underlyingAssetMint,
caller
);
if (
underlyingToken.txBuilder &&
ixTracker.checkedAdd(underlyingToken.txBuilder)
) {
ataTxBuilderArray.push(underlyingToken.txBuilder);
}
const ixWithSigners =
await psyoptionsAmerican.instructions.mintOptionInstruction(
americanProgram,
leg.optionMetaPubKey
optionToken.ataPubKey,
writerToken.ataPubKey,
underlyingToken.ataPubKey,
new BN(amount!),
optionMarket as psyoptionsAmerican.OptionMarketWithKey
);
if (optionMarket) {
await getOrCreateATA(convergence, optionMarket.optionMint, caller);
}
}
ixWithSigners.ix.keys[0] = {
pubkey: caller,
isSigner: true,
isWritable: false,
};
const mintTxBuilder = TransactionBuilder.make().setFeePayer(
convergence.rpc().getDefaultFeePayer()
);
mintTxBuilder.add({
instruction: ixWithSigners.ix,
signers: [convergence.identity()],
});
mintTxBuilderArray.push(mintTxBuilder);
}
}
if (flag === true) {
return ATAExistence.EXISTS;
let signedTxs: Transaction[] = [];
const lastValidBlockHeight = await convergence.rpc().getLatestBlockhash();
if (ataTxBuilderArray.length > 0 || mintTxBuilderArray.length > 0) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Identical blocks of code found in 2 locations. Consider refactoring.

const mergedTxBuilderArray = ataTxBuilderArray.concat(mintTxBuilderArray);
signedTxs = await convergence
.identity()
.signAllTransactions(
mergedTxBuilderArray.map((b) => b.toTransaction(lastValidBlockHeight))
);
}

const ataSignedTx = signedTxs.slice(0, ataTxBuilderArray.length);
const mintSignedTx = signedTxs.slice(ataTxBuilderArray.length);

if (ataSignedTx.length > 0) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 4 locations. Consider refactoring.

await Promise.all(
ataSignedTx.map((signedTx) =>
convergence
.rpc()
.serializeAndSendTransaction(signedTx, lastValidBlockHeight)
)
);
}
if (mintSignedTx.length > 0) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 4 locations. Consider refactoring.

await Promise.all(
mintSignedTx.map((signedTx) =>
convergence
.rpc()
.serializeAndSendTransaction(signedTx, lastValidBlockHeight)
)
);
}
return ATAExistence.NOTEXISTS;
};
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ export class PsyoptionsAmericanInstrument implements LegInstrument {
convergence: Convergence,
metaKey: PublicKey
): Promise<OptionMarketWithKey> {
const americanProgram = createAmericanProgram(convergence);
const cvgWallet = new CvgWallet(convergence);
const americanProgram = createAmericanProgram(convergence, cvgWallet);
const optionMarket = (await psyoptionsAmerican.getOptionByKey(
americanProgram,
metaKey
Expand Down
Loading