-
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 34 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,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, | ||
|
@@ -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, { | ||
|
@@ -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 ( | ||
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. Function 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. Function 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. Function 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. Function 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. Function |
||
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 }); | ||
|
@@ -160,28 +75,104 @@ 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( | ||
americanProgram, | ||
leg.optionMetaPubKey | ||
); | ||
if (optionMarket) { | ||
await getOrCreateATA(convergence, optionMarket.optionMint, caller); | ||
} | ||
} | ||
const optionMarket = await leg.getOptionMeta(); | ||
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. Why do we skip all the logic if this returns null? 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. 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, | ||
optionToken.ataPubKey, | ||
writerToken.ataPubKey, | ||
underlyingToken.ataPubKey, | ||
new BN(amount!), | ||
optionMarket as psyoptionsAmerican.OptionMarketWithKey | ||
); | ||
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); | ||
} | ||
let signedTxs: Transaction[] = []; | ||
const lastValidBlockHeight = await convergence.rpc().getLatestBlockhash(); | ||
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); | ||
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) { | ||
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. |
||
await Promise.all( | ||
ataSignedTx.map((signedTx) => | ||
convergence | ||
.rpc() | ||
.serializeAndSendTransaction(signedTx, lastValidBlockHeight) | ||
) | ||
); | ||
} | ||
if (flag === true) { | ||
return ATAExistence.EXISTS; | ||
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. |
||
await Promise.all( | ||
mintSignedTx.map((signedTx) => | ||
convergence | ||
.rpc() | ||
.serializeAndSendTransaction(signedTx, lastValidBlockHeight) | ||
) | ||
); | ||
} | ||
return ATAExistence.NOTEXISTS; | ||
}; |
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.
Function
prepareAmericanOptions
has 113 lines of code (exceeds 25 allowed). Consider refactoring.