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

Conversation

Nagaprasadvr
Copy link
Contributor

@Nagaprasadvr Nagaprasadvr commented Sep 4, 2023

Fix :
Move MintoOptions logic to prepareSettlement Operation and implement signAllTransaction

@linear
Copy link

linear bot commented Sep 4, 2023

@changeset-bot
Copy link

changeset-bot bot commented Sep 4, 2023

⚠️ No Changeset found

Latest commit: b655a68

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@Nagaprasadvr Nagaprasadvr changed the base branch from main to dev September 4, 2023 11:14
@Nagaprasadvr Nagaprasadvr marked this pull request as draft September 4, 2023 11:14
convergence,
optionMarket.optionMint,
caller
);

const writerToken = await getOrCreateATA(
if (optionToken.instruction) {
Copy link

Choose a reason for hiding this comment

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

Avoid deeply nested control flow statements.

convergence,
optionMarket!.writerTokenMint,
caller
);

const underlyingToken = await getOrCreateATA(
if (writerToken.instruction) {
Copy link

Choose a reason for hiding this comment

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

Avoid deeply nested control flow statements.

convergence,
optionMarket!.underlyingAssetMint,
caller
);

if (underlyingToken.instruction) {
Copy link

Choose a reason for hiding this comment

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

Avoid deeply nested control flow statements.

@@ -166,28 +184,41 @@ export const mintEuropeanOptions = async (
? stableMintToken
: underlyingMintToken;

const optionDestination = await getOrCreateATA(
const {
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 2 locations. Consider refactoring.

if (optionDestinationAtaIx) {
instructions.push(optionDestinationAtaIx);
}
const {
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 2 locations. Consider refactoring.

convergence,
optionMarket.optionMint,
caller
);

const writerToken = await getOrCreateATA(
if (optionToken.txBuilder) {
Copy link

Choose a reason for hiding this comment

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

Avoid deeply nested control flow statements.

convergence,
optionMarket!.writerTokenMint,
caller
);

const underlyingToken = await getOrCreateATA(
if (writerToken.txBuilder) {
Copy link

Choose a reason for hiding this comment

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

Avoid deeply nested control flow statements.

convergence,
optionMarket!.underlyingAssetMint,
caller
);

if (underlyingToken.txBuilder) {
Copy link

Choose a reason for hiding this comment

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

Avoid deeply nested control flow statements.

});
});
let signedTxs: Transaction[] = [];
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.

}
}
let signedTxs: Transaction[] = [];
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 sig = await txBuilder.sendAndConfirm(convergence, confirmOptions);
return sig;
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.

.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.

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.

.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.

}
}
if (mintInstructions.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 2 locations. Consider refactoring.

}
}
}
if (mintInstructions.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 2 locations. Consider refactoring.


const sig = await txBuilder.sendAndConfirm(convergence, confirmOptions);
return sig;
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 3 locations. Consider refactoring.

)
);
}
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 3 locations. Consider refactoring.

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 3 locations. Consider refactoring.

@Nagaprasadvr Nagaprasadvr marked this pull request as ready for review September 5, 2023 16:06
);
};

const doesRfqLegContainsPsyoptionsEuropean = (rfq: Rfq) => {
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 2 locations. Consider refactoring.

@@ -117,150 +99,140 @@ export const createEuropeanProgram = async (convergence: Convergence) => {
);
};

export const mintEuropeanOptions = async (
// create European Option ATAs and mint options
export const prepareEuropeanOptions = async (
Copy link

Choose a reason for hiding this comment

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

Function prepareEuropeanOptions has 121 lines of code (exceeds 25 allowed). Consider refactoring.

@@ -308,3 +357,15 @@ export const prepareSettlementBuilder = async (
}
);
};

const doesRfqLegContainsPsyoptionsAmerican = (rfq: Rfq) => {
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 2 locations. Consider refactoring.

// 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.

)
);
}
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.

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.

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.

// 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 a Cognitive Complexity of 21 (exceeds 5 allowed). Consider refactoring.

new PublicKey(psyoptionsEuropean.programId)
);
};

export const mintEuropeanOptions = async (
// create European Option ATAs and mint options
export const prepareEuropeanOptions = async (
Copy link

Choose a reason for hiding this comment

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

Function prepareEuropeanOptions has a Cognitive Complexity of 20 (exceeds 5 allowed). Consider refactoring.


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


const optionMarket = await psyoptionsAmerican.getOptionByKey(
const optionMarket = await leg.getOptionMeta();
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

);
};
private matchInstruction = (ixToBeAdded: TransactionInstruction): boolean => {
this.IxArray.forEach((ix) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not just return every as in the method above?

Copy link
Contributor Author

@Nagaprasadvr Nagaprasadvr Sep 8, 2023

Choose a reason for hiding this comment

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

bcos we are checking if one of the ix in IxArray match the passed ix and return true in that case

Copy link
Contributor

Choose a reason for hiding this comment

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

You can still use it. Just check if every instruction differs using every and return false in that case. Otherwise, return true.

checked++;
}
});
return checked === ixLength;
Copy link
Contributor

Choose a reason for hiding this comment

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

This wouldn't work correctly in a case when this returns false, but still adds some of the instruction to the inner tracking list

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 euroMeta = await leg.getOptionMeta();
const { stableMint } = euroMeta;
const { underlyingMint } = euroMeta;
Copy link
Contributor

Choose a reason for hiding this comment

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

Two deconstructions of the same object

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

new PublicKey(psyoptionsEuropean.programId)
);
};

export const mintEuropeanOptions = async (
// create European Option ATAs and mint options
export const prepareEuropeanOptions = async (
Copy link

Choose a reason for hiding this comment

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

Function prepareEuropeanOptions has 120 lines of code (exceeds 25 allowed). Consider refactoring.

// 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 108 lines of code (exceeds 25 allowed). Consider refactoring.

// 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 a Cognitive Complexity of 18 (exceeds 5 allowed). Consider refactoring.

});
return false;
};
checkedAdd(ix: TransactionInstruction | TransactionBuilder): boolean {
Copy link

Choose a reason for hiding this comment

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

Function checkedAdd has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.

}
}
const optionMarket = await leg.getOptionMeta();
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.

In which cases this is 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.

removing this check

);
};
private matchInstruction = (ixToBeAdded: TransactionInstruction): boolean => {
this.IxArray.forEach((ix) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

You can still use it. Just check if every instruction differs using every and return false in that case. Otherwise, return true.

const instructions = ix.getInstructions();
const ixLength = instructions.length;
let checked = 0;
instructions.forEach((ix) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not use every here also?

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

tx.recentBlockhash = latestBlockHash.blockhash;
tx.feePayer = convergence.rpc().getDefaultFeePayer().publicKey;
await convergence.identity().signTransaction(tx);
await convergence.rpc().serializeAndSendTransaction(tx, latestBlockHash);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't we use a regular sign and send tx rpc call here?

const signedTx = await convergence.identity().signTransaction(createTx);
await convergence
.rpc()
.serializeAndSendTransaction(signedTx, latestBlockHash);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't we use a regular sign and send tx rpc call here?

// 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 105 lines of code (exceeds 25 allowed). Consider refactoring.

// 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 a Cognitive Complexity of 16 (exceeds 5 allowed). Consider refactoring.

if (ix instanceof TransactionBuilder) {
const instructions = ix.getInstructions();
const ixLength = instructions.length;
let checked = 0;
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 need this variable? We can just use the return value of instructions.every in the next line

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 instructions = ix.getInstructions();
const uniqueIxs = instructions.every((ix) => {
return !this.matchInstruction(ix);
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's rework lambda from a block to a simple expression and rename a uniqueIxs because this name implies a list of unique instructions, but it's actually a boolean value

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

@codeclimate
Copy link

codeclimate bot commented Sep 14, 2023

Code Climate has analyzed commit b655a68 and detected 18 issues on this pull request.

Here's the issue category breakdown:

Category Count
Complexity 8
Duplication 10

View more on Code Climate.

@pindaroso pindaroso merged commit 9e94493 into dev Sep 20, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants