|
| 1 | +import { |
| 2 | + BlockhashSubscriber, |
| 3 | + ConstituentMap, |
| 4 | + DriftClient, |
| 5 | + encodeName, |
| 6 | + getConstituentTargetBasePublicKey, |
| 7 | + getLpPoolPublicKey, |
| 8 | + LPPoolAccount, |
| 9 | + PriorityFeeMethod, |
| 10 | + PriorityFeeSubscriber, |
| 11 | +} from '@drift-labs/sdk'; |
| 12 | +import { ComputeBudgetProgram } from '@solana/web3.js'; |
| 13 | +import { simulateAndGetTxWithCUs } from '../../utils'; |
| 14 | + |
| 15 | +export class LpPoolTargetBaseCranker { |
| 16 | + interval: NodeJS.Timeout | null = null; |
| 17 | + lpPoolAccount?: LPPoolAccount; |
| 18 | + constituentMap: ConstituentMap; |
| 19 | + |
| 20 | + priorityFeeSubscriber: PriorityFeeSubscriber; |
| 21 | + blockhashSubscriber: BlockhashSubscriber; |
| 22 | + |
| 23 | + public constructor( |
| 24 | + private driftClient: DriftClient, |
| 25 | + private intervalMs: number, |
| 26 | + private lpPoolId: number |
| 27 | + ) { |
| 28 | + this.constituentMap = new ConstituentMap({ |
| 29 | + driftClient: this.driftClient, |
| 30 | + subscriptionConfig: { |
| 31 | + type: 'websocket', |
| 32 | + resubTimeoutMs: 30_000, |
| 33 | + }, |
| 34 | + lpPoolId: lpPoolId, |
| 35 | + }); |
| 36 | + this.priorityFeeSubscriber = new PriorityFeeSubscriber({ |
| 37 | + connection: this.driftClient.connection, |
| 38 | + frequencyMs: 30_000, |
| 39 | + addresses: [ |
| 40 | + getConstituentTargetBasePublicKey( |
| 41 | + this.driftClient.program.programId, |
| 42 | + getLpPoolPublicKey(this.driftClient.program.programId, this.lpPoolId) |
| 43 | + ), |
| 44 | + ], |
| 45 | + priorityFeeMethod: PriorityFeeMethod.SOLANA, |
| 46 | + slotsToCheck: 10, |
| 47 | + }); |
| 48 | + |
| 49 | + this.blockhashSubscriber = new BlockhashSubscriber({ |
| 50 | + connection: this.driftClient.connection, |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + async init() { |
| 55 | + await this.constituentMap.sync(); |
| 56 | + await this.constituentMap.subscribe(); |
| 57 | + this.lpPoolAccount = await this.driftClient.getLpPoolAccount(this.lpPoolId); |
| 58 | + await this.blockhashSubscriber.subscribe(); |
| 59 | + await this.priorityFeeSubscriber.subscribe(); |
| 60 | + await this.startInterval(); |
| 61 | + } |
| 62 | + |
| 63 | + public async healthCheck() { |
| 64 | + return true; |
| 65 | + } |
| 66 | + |
| 67 | + private async getBlockhashForTx(): Promise<string> { |
| 68 | + const cachedBlockhash = this.blockhashSubscriber.getLatestBlockhash(5); |
| 69 | + if (cachedBlockhash) { |
| 70 | + return cachedBlockhash.blockhash as string; |
| 71 | + } |
| 72 | + |
| 73 | + const recentBlockhash = |
| 74 | + await this.driftClient.connection.getLatestBlockhash({ |
| 75 | + commitment: 'confirmed', |
| 76 | + }); |
| 77 | + |
| 78 | + return recentBlockhash.blockhash; |
| 79 | + } |
| 80 | + |
| 81 | + async startInterval() { |
| 82 | + setInterval(async () => { |
| 83 | + this.lpPoolAccount = await this.driftClient.getLpPoolAccount( |
| 84 | + this.lpPoolId |
| 85 | + ); |
| 86 | + }, 10_000); |
| 87 | + |
| 88 | + this.interval = setInterval(async () => { |
| 89 | + const perpMarkets = this.driftClient.getPerpMarketAccounts(); |
| 90 | + const marketIndexes = perpMarkets |
| 91 | + .filter((market) => market.lpStatus == 1) |
| 92 | + .map((market) => market.marketIndex); |
| 93 | + if (marketIndexes.length === 0) { |
| 94 | + console.warn( |
| 95 | + `No markets found with LP status for pool ${this.lpPoolId}. Skipping update.` |
| 96 | + ); |
| 97 | + return; |
| 98 | + } |
| 99 | + if (!this.lpPoolAccount) { |
| 100 | + this.lpPoolAccount = await this.driftClient.getLpPoolAccount( |
| 101 | + this.lpPoolId |
| 102 | + ); |
| 103 | + } |
| 104 | + const ixs = [ |
| 105 | + ComputeBudgetProgram.setComputeUnitLimit({ |
| 106 | + units: 1_400_000, // will be overridden by simulateTx |
| 107 | + }), |
| 108 | + ]; |
| 109 | + ixs.push( |
| 110 | + ComputeBudgetProgram.setComputeUnitPrice({ |
| 111 | + microLamports: Math.floor( |
| 112 | + this.priorityFeeSubscriber.getCustomStrategyResult() * |
| 113 | + this.driftClient.txSender.getSuggestedPriorityFeeMultiplier() |
| 114 | + ), |
| 115 | + }) |
| 116 | + ); |
| 117 | + const updateIxs = |
| 118 | + await this.driftClient.getAllUpdateConstituentTargetBaseIxs( |
| 119 | + marketIndexes, |
| 120 | + this.lpPoolAccount, |
| 121 | + this.constituentMap, |
| 122 | + true |
| 123 | + ); |
| 124 | + ixs.push(...updateIxs); |
| 125 | + const simResult = await simulateAndGetTxWithCUs({ |
| 126 | + ixs, |
| 127 | + connection: this.driftClient.connection, |
| 128 | + payerPublicKey: this.driftClient.wallet.publicKey, |
| 129 | + lookupTableAccounts: |
| 130 | + await this.driftClient.fetchAllLookupTableAccounts(), |
| 131 | + cuLimitMultiplier: 1.5, |
| 132 | + doSimulation: true, |
| 133 | + recentBlockhash: await this.getBlockhashForTx(), |
| 134 | + }); |
| 135 | + |
| 136 | + if (simResult.simError) { |
| 137 | + console.log(simResult.simTxLogs); |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + this.driftClient.txSender |
| 142 | + .sendVersionedTransaction(simResult.tx) |
| 143 | + .then((response) => { |
| 144 | + console.log(response); |
| 145 | + }) |
| 146 | + .catch((error) => { |
| 147 | + console.log(error); |
| 148 | + }); |
| 149 | + }, this.intervalMs); |
| 150 | + } |
| 151 | +} |
0 commit comments