ts client for max buyback of fees with mngo (#480)
* ts client for max buyback of fees with mngo Signed-off-by: microwavedcola1 <microwavedcola@gmail.com> * Fixes from review Signed-off-by: microwavedcola1 <microwavedcola@gmail.com> --------- Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
This commit is contained in:
parent
46c79af430
commit
73b626efff
|
@ -466,6 +466,14 @@ export class Group {
|
|||
return banks[0];
|
||||
}
|
||||
|
||||
public getFirstBankForMngo(): Bank {
|
||||
return this.getFirstBankByTokenIndex(this.mngoTokenIndex);
|
||||
}
|
||||
|
||||
public getFirstBankForPerpSettlement(): Bank {
|
||||
return this.getFirstBankByTokenIndex(0 as TokenIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param mintPk
|
||||
|
|
|
@ -964,6 +964,32 @@ export class MangoAccount {
|
|||
);
|
||||
}
|
||||
|
||||
public getBuybackFeesAccrued(): BN {
|
||||
return this.buybackFeesAccruedCurrent.add(this.buybackFeesAccruedPrevious);
|
||||
}
|
||||
|
||||
public getBuybackFeesAccruedUi(): number {
|
||||
return toUiDecimalsForQuote(this.getBuybackFeesAccrued());
|
||||
}
|
||||
|
||||
public getMaxFeesBuyback(group: Group): BN {
|
||||
const mngoBalanceValueWithBonus = new BN(
|
||||
this.getTokenBalance(group.getFirstBankForMngo())
|
||||
.mul(group.getFirstBankForMngo().price)
|
||||
.mul(I80F48.fromNumber(group.buybackFeesMngoBonusFactor))
|
||||
.floor()
|
||||
.toNumber(),
|
||||
);
|
||||
return BN.max(
|
||||
BN.min(this.getBuybackFeesAccrued(), mngoBalanceValueWithBonus),
|
||||
new BN(0),
|
||||
);
|
||||
}
|
||||
|
||||
public getMaxFeesBuybackUi(group: Group): number {
|
||||
return toUiDecimalsForQuote(this.getMaxFeesBuyback(group));
|
||||
}
|
||||
|
||||
toString(group?: Group, onlyTokens = false): string {
|
||||
let res = 'MangoAccount';
|
||||
res = res + '\n pk: ' + this.publicKey.toString();
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
import { AnchorProvider, BN, Program, Provider } from '@coral-xyz/anchor';
|
||||
import { TOKEN_PROGRAM_ID, NATIVE_MINT } from './utils/spl';
|
||||
import {
|
||||
createCloseAccountInstruction,
|
||||
createInitializeAccount3Instruction,
|
||||
} from '@solana/spl-token';
|
||||
import {
|
||||
AccountMeta,
|
||||
AddressLookupTableAccount,
|
||||
|
@ -60,10 +63,7 @@ import {
|
|||
toNative,
|
||||
} from './utils';
|
||||
import { sendTransaction } from './utils/rpc';
|
||||
import {
|
||||
createCloseAccountInstruction,
|
||||
createInitializeAccount3Instruction,
|
||||
} from '@solana/spl-token';
|
||||
import { NATIVE_MINT, TOKEN_PROGRAM_ID } from './utils/spl';
|
||||
|
||||
export enum AccountRetriever {
|
||||
Scanning,
|
||||
|
@ -993,6 +993,39 @@ export class MangoClient {
|
|||
return await this.sendAndConfirmTransactionForGroup(group, instructions);
|
||||
}
|
||||
|
||||
public async accountBuybackFeesWithMngoIx(
|
||||
group: Group,
|
||||
mangoAccount: MangoAccount,
|
||||
maxBuyback?: number,
|
||||
): Promise<TransactionInstruction> {
|
||||
maxBuyback = maxBuyback ?? mangoAccount.getMaxFeesBuybackUi(group);
|
||||
return await this.program.methods
|
||||
.accountBuybackFeesWithMngo(new BN(maxBuyback))
|
||||
.accounts({
|
||||
group: group.publicKey,
|
||||
account: mangoAccount.publicKey,
|
||||
daoAccount: group.buybackFeesSwapMangoAccount,
|
||||
mngoBank: group.getFirstBankForMngo().publicKey,
|
||||
mngoOracle: group.getFirstBankForMngo().oracle,
|
||||
feesBank: group.getFirstBankByTokenIndex(0 as TokenIndex).publicKey,
|
||||
feesOracle: group.getFirstBankByTokenIndex(0 as TokenIndex).oracle,
|
||||
})
|
||||
.instruction();
|
||||
}
|
||||
|
||||
public async accountBuybackFeesWithMngo(
|
||||
group: Group,
|
||||
mangoAccount: MangoAccount,
|
||||
maxBuyback?: number,
|
||||
): Promise<TransactionSignature> {
|
||||
const ix = await this.accountBuybackFeesWithMngoIx(
|
||||
group,
|
||||
mangoAccount,
|
||||
maxBuyback,
|
||||
);
|
||||
return await this.sendAndConfirmTransactionForGroup(group, [ix]);
|
||||
}
|
||||
|
||||
public async tokenDeposit(
|
||||
group: Group,
|
||||
mangoAccount: MangoAccount,
|
||||
|
@ -2035,7 +2068,7 @@ export class MangoClient {
|
|||
group,
|
||||
[mangoAccount],
|
||||
// Settlement token bank, because a position for it may be created
|
||||
[group.getFirstBankByTokenIndex(0 as TokenIndex)],
|
||||
[group.getFirstBankForPerpSettlement()],
|
||||
[perpMarket],
|
||||
);
|
||||
return await this.program.methods
|
||||
|
@ -2127,7 +2160,7 @@ export class MangoClient {
|
|||
group,
|
||||
[mangoAccount],
|
||||
// Settlement token bank, because a position for it may be created
|
||||
[group.getFirstBankByTokenIndex(0 as TokenIndex)],
|
||||
[group.getFirstBankForPerpSettlement()],
|
||||
[perpMarket],
|
||||
);
|
||||
return await this.program.methods
|
||||
|
@ -2250,7 +2283,7 @@ export class MangoClient {
|
|||
AccountRetriever.Scanning,
|
||||
group,
|
||||
[profitableAccount, unprofitableAccount],
|
||||
[group.getFirstBankByTokenIndex(0 as TokenIndex)],
|
||||
[group.getFirstBankForPerpSettlement()],
|
||||
[perpMarket],
|
||||
);
|
||||
const bank = group.banksMapByTokenIndex.get(0 as TokenIndex)![0];
|
||||
|
@ -2291,7 +2324,7 @@ export class MangoClient {
|
|||
AccountRetriever.Fixed,
|
||||
group,
|
||||
[account], // Account must be unprofitable
|
||||
[group.getFirstBankByTokenIndex(0 as TokenIndex)],
|
||||
[group.getFirstBankForPerpSettlement()],
|
||||
[perpMarket],
|
||||
);
|
||||
const bank = group.banksMapByTokenIndex.get(0 as TokenIndex)![0];
|
||||
|
|
Loading…
Reference in New Issue