-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from 16 commits
252658e
6ad852c
b4140d4
c1e9a55
abebda0
76fe7a8
f54300f
4c0c659
743b8bd
9c3c6bc
5f8f6f3
32ab68a
d498542
22e1227
a3e4ac6
7fbacd8
cc61a70
5b27ca6
85e9a71
ba41ad9
3b384c6
d627b60
ae5ae1c
2dfa4f7
c769c1f
fa7b6d3
6937fb7
9dd996d
85aa453
df69c67
ba5afa6
d28abd9
c7c16dc
344a832
b655a68
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,31 @@ | ||
import * as psyoptionsAmerican from '@mithraic-labs/psy-american'; | ||
|
||
import { BN } from 'bn.js'; | ||
import { PublicKey } from '@solana/web3.js'; | ||
import { | ||
PublicKey, | ||
Transaction, | ||
TransactionInstruction, | ||
} from '@solana/web3.js'; | ||
import { Convergence } from '../../Convergence'; | ||
|
||
import { ATAExistence, getOrCreateATA } from '../../utils/ata'; | ||
import { | ||
ATAExistence, | ||
getOrCreateATA, | ||
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 { PsyoptionsAmericanInstrument } from './types'; | ||
import { createAmericanProgram } from './instrument'; | ||
|
||
export const mintAmericanOptions = async ( | ||
convergence: Convergence, | ||
responseAddress: PublicKey, | ||
caller: PublicKey, | ||
americanProgram: any | ||
caller: PublicKey | ||
) => { | ||
const cvgWallet = new CvgWallet(convergence); | ||
const americanProgram = createAmericanProgram(convergence, cvgWallet); | ||
const response = await convergence | ||
.rfqs() | ||
.findResponseByAddress({ address: responseAddress }); | ||
|
@@ -28,46 +34,51 @@ export const mintAmericanOptions = async ( | |
.findRfqByAddress({ address: response.rfq }); | ||
|
||
const callerSide = caller.equals(rfq.taker) ? 'taker' : 'maker'; | ||
const instructionWithSigners: InstructionWithSigners[] = []; | ||
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()) { | ||
const mintInstructions: TransactionInstruction[] = []; | ||
if (leg instanceof PsyoptionsAmericanInstrument) { | ||
const { receiver } = legs[index]; | ||
const { receiver, amount } = legs[index]; | ||
if (receiver !== callerSide) { | ||
const { amount } = legs[index]; | ||
|
||
const optionMarket = await psyoptionsAmerican.getOptionByKey( | ||
americanProgram, | ||
leg.optionMetaPubKey | ||
); | ||
const optionMarket = await leg.getOptionMeta(); | ||
if (optionMarket) { | ||
const optionToken = await getOrCreateATA( | ||
const optionToken = await getOrCreateATAtxBuilder( | ||
convergence, | ||
optionMarket.optionMint, | ||
caller | ||
); | ||
|
||
const writerToken = await getOrCreateATA( | ||
if (optionToken.txBuilder) { | ||
ataTxBuilderArray.push(optionToken.txBuilder); | ||
} | ||
const writerToken = await getOrCreateATAtxBuilder( | ||
convergence, | ||
optionMarket!.writerTokenMint, | ||
caller | ||
); | ||
|
||
const underlyingToken = await getOrCreateATA( | ||
if (writerToken.txBuilder) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid deeply nested control flow statements. |
||
ataTxBuilderArray.push(writerToken.txBuilder); | ||
} | ||
const underlyingToken = await getOrCreateATAtxBuilder( | ||
convergence, | ||
optionMarket!.underlyingAssetMint, | ||
caller | ||
); | ||
|
||
if (underlyingToken.txBuilder) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid deeply nested control flow statements. |
||
ataTxBuilderArray.push(underlyingToken.txBuilder); | ||
} | ||
const ixWithSigners = | ||
await psyoptionsAmerican.instructions.mintOptionV2Instruction( | ||
americanProgram, | ||
optionToken, | ||
writerToken, | ||
underlyingToken, | ||
optionToken.ataPubKey, | ||
writerToken.ataPubKey, | ||
underlyingToken.ataPubKey, | ||
new BN(amount!), | ||
optionMarket as psyoptionsAmerican.OptionMarketWithKey | ||
); | ||
|
@@ -76,23 +87,55 @@ export const mintAmericanOptions = async ( | |
isSigner: true, | ||
isWritable: false, | ||
}; | ||
instructionWithSigners.push({ | ||
instruction: ixWithSigners.ix, | ||
signers: ixWithSigners.signers, | ||
}); | ||
mintInstructions.push(ixWithSigners.ix); | ||
} | ||
} | ||
} | ||
if (mintInstructions.length > 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar blocks of code found in 2 locations. Consider refactoring. |
||
const txBuilder = TransactionBuilder.make().setFeePayer( | ||
convergence.rpc().getDefaultFeePayer() | ||
); | ||
mintInstructions.forEach((ins) => { | ||
txBuilder.add({ | ||
instruction: ins, | ||
signers: [convergence.identity()], | ||
}); | ||
}); | ||
mintTxBuilderArray.push(txBuilder); | ||
} | ||
} | ||
let signedTxs: Transaction[] = []; | ||
if (ataTxBuilderArray.length > 0 || mintTxBuilderArray.length > 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
const lastValidBlockHeight = await convergence.rpc().getLatestBlockhash(); | ||
signedTxs = await convergence | ||
.identity() | ||
.signAllTransactions( | ||
mergedTxBuilderArray.map((b) => b.toTransaction(lastValidBlockHeight)) | ||
); | ||
} | ||
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; | ||
const ataSignedTx = signedTxs.slice(0, ataTxBuilderArray.length); | ||
const mintSignedTx = signedTxs.slice(ataTxBuilderArray.length); | ||
|
||
if (ataSignedTx.length > 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar blocks of code found in 4 locations. Consider refactoring. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar blocks of code found in 3 locations. Consider refactoring. |
||
const lastValidBlockHeight = await convergence.rpc().getLatestBlockhash(); | ||
ataSignedTx.map( | ||
async (signedTx) => | ||
await convergence | ||
.rpc() | ||
.serializeAndSendTransaction(signedTx, lastValidBlockHeight) | ||
); | ||
} | ||
if (mintSignedTx.length > 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar blocks of code found in 4 locations. Consider refactoring. |
||
const lastValidBlockHeight = await convergence.rpc().getLatestBlockhash(); | ||
mintSignedTx.map( | ||
async (signedTx) => | ||
await convergence | ||
.rpc() | ||
.serializeAndSendTransaction(signedTx, lastValidBlockHeight) | ||
); | ||
} | ||
return null; | ||
}; | ||
|
||
export const initializeNewAmericanOption = async ( | ||
|
@@ -112,10 +155,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, { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { AccountMeta, TransactionInstruction } from '@solana/web3.js'; | ||
|
||
export class InstructionUniquenessTracker { | ||
constructor(public readonly IxArray: TransactionInstruction[]) { | ||
this.IxArray = IxArray; | ||
} | ||
|
||
private matchKeys = ( | ||
keys: AccountMeta[], | ||
keysToMatch: AccountMeta[] | ||
): boolean => { | ||
let matching = true; | ||
if (keys.length !== keysToMatch.length) { | ||
return false; | ||
} | ||
for (let i = 0; i < keys.length; i++) { | ||
if ( | ||
keys[i].isSigner !== keysToMatch[i].isSigner || | ||
keys[i].isWritable !== keysToMatch[i].isWritable || | ||
!keys[i].pubkey.equals(keysToMatch[i].pubkey) | ||
) { | ||
matching = false; | ||
break; | ||
} | ||
} | ||
return matching; | ||
}; | ||
private matchInstruction = (ixToBeAdded: TransactionInstruction): boolean => { | ||
let match = false; | ||
this.IxArray.forEach((ix) => { | ||
if ( | ||
this.matchKeys(ix.keys, ixToBeAdded.keys) && | ||
ix.programId.equals(ixToBeAdded.programId) && | ||
ix.data.equals(ixToBeAdded.data) | ||
) { | ||
match = true; | ||
return match; | ||
} | ||
}); | ||
return match; | ||
}; | ||
checkedAdd(ix: TransactionInstruction): boolean { | ||
if (!this.matchInstruction(ix)) { | ||
this.IxArray.push(ix); | ||
return true; | ||
} | ||
return false; | ||
} | ||
} |
There was a problem hiding this comment.
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.