ts: reuse tcs creation function

This commit is contained in:
Christian Kamm 2023-08-10 13:48:28 +02:00
parent 27dadbb6bc
commit f462c62816
2 changed files with 48 additions and 73 deletions

View File

@ -180,8 +180,8 @@ async function main() {
account,
MINTS.get('SOL')!,
MINTS.get('USDC')!,
100000000,
20000, // liqee only has 1k USDC-native, leverage does not go that far!
new BN(100000000),
new BN(20000), // liqee only has 1k USDC-native, leverage does not go that far!
null,
0.0,
1000000.0,
@ -204,8 +204,8 @@ async function main() {
account,
MINTS.get('SOL')!,
MINTS.get('USDC')!,
1000,
1000,
new BN(1000),
new BN(1000),
null,
0.0,
1000000.0,
@ -228,8 +228,8 @@ async function main() {
account,
MINTS.get('SOL')!,
MINTS.get('USDC')!,
1000,
1000,
new BN(1000),
new BN(1000),
Date.now() / 1000 + 15, // expire in 15s
0.0,
1000000.0,

View File

@ -3587,9 +3587,6 @@ export class MangoClient {
upperLimit = thresholdPrice;
}
const expiryTimestampBn =
expiryTimestamp !== null ? new BN(expiryTimestamp) : U64_MAX_BN;
if (!pricePremium) {
const buyTokenPriceImpact = group.getPriceImpactByTokenIndex(
buyBank.tokenIndex,
@ -3621,19 +3618,55 @@ export class MangoClient {
break;
}
return await this.tokenConditionalSwapCreateRaw(
group,
account,
buyBank.mint,
sellBank.mint,
maxBuy,
maxSell,
expiryTimestamp,
lowerLimit,
upperLimit,
pricePremiumRate,
allowCreatingDeposits,
allowCreatingBorrows,
thresholdPriceInSellPerBuyToken
? TokenConditionalSwapDisplayPriceStyle.sellTokenPerBuyToken
: TokenConditionalSwapDisplayPriceStyle.buyTokenPerSellToken,
intention,
);
}
public async tokenConditionalSwapCreateRaw(
group: Group,
account: MangoAccount,
buyMintPk: PublicKey,
sellMintPk: PublicKey,
maxBuy: BN,
maxSell: BN,
expiryTimestamp: number | null,
priceLowerLimit: number,
priceUpperLimit: number,
pricePremiumRate: number,
allowCreatingDeposits: boolean,
allowCreatingBorrows: boolean,
priceDisplayStyle: TokenConditionalSwapDisplayPriceStyle,
intention: TokenConditionalSwapIntention,
): Promise<TransactionSignature> {
const buyBank: Bank = group.getFirstBankByMint(buyMintPk);
const sellBank: Bank = group.getFirstBankByMint(sellMintPk);
const tcsIx = await this.program.methods
.tokenConditionalSwapCreateV2(
maxBuy,
maxSell,
expiryTimestampBn,
lowerLimit,
upperLimit,
expiryTimestamp !== null ? new BN(expiryTimestamp) : U64_MAX_BN,
priceLowerLimit,
priceUpperLimit,
pricePremiumRate,
allowCreatingDeposits,
allowCreatingBorrows,
thresholdPriceInSellPerBuyToken
? TokenConditionalSwapDisplayPriceStyle.sellTokenPerBuyToken
: TokenConditionalSwapDisplayPriceStyle.buyTokenPerSellToken,
priceDisplayStyle,
intention,
)
.accounts({
@ -3664,64 +3697,6 @@ export class MangoClient {
return await this.sendAndConfirmTransactionForGroup(group, ixs);
}
public async tokenConditionalSwapCreateRaw(
group: Group,
account: MangoAccount,
buyMintPk: PublicKey,
sellMintPk: PublicKey,
maxBuy: number,
maxSell: number,
expiryTimestamp: number | null,
priceLowerLimit: number,
priceUpperLimit: number,
pricePremiumRate: number,
allowCreatingDeposits: boolean,
allowCreatingBorrows: boolean,
priceDisplayStyle: TokenConditionalSwapDisplayPriceStyle,
intention: TokenConditionalSwapIntention,
): Promise<TransactionSignature> {
const buyBank: Bank = group.getFirstBankByMint(buyMintPk);
const sellBank: Bank = group.getFirstBankByMint(sellMintPk);
const ix = await this.program.methods
.tokenConditionalSwapCreateV2(
new BN(maxBuy),
new BN(maxSell),
expiryTimestamp !== null ? new BN(expiryTimestamp) : U64_MAX_BN,
priceLowerLimit,
priceUpperLimit,
pricePremiumRate,
allowCreatingDeposits,
allowCreatingBorrows,
priceDisplayStyle,
intention,
)
.accounts({
group: group.publicKey,
account: account.publicKey,
authority: (this.program.provider as AnchorProvider).wallet.publicKey,
buyBank: buyBank.publicKey,
sellBank: sellBank.publicKey,
})
.instruction();
const ixs = [ix];
if (account.tokenConditionalSwaps.length == 0) {
ixs.push(
await this.accountExpandV2Ix(
group,
account,
account.tokens.length,
account.serum3.length,
account.perps.length,
account.perpOpenOrders.length,
8,
),
);
}
return await this.sendAndConfirmTransactionForGroup(group, ixs);
}
public async tokenConditionalSwapCancel(
group: Group,
account: MangoAccount,