Skip to content

Commit

Permalink
Upgrade compute budget instruction (#434)
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotkennedy authored Dec 2, 2023
1 parent 6c11221 commit f544827
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 57 deletions.
21 changes: 4 additions & 17 deletions packages/kamino-sdk/src/utils/tokenUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,12 @@ export function createAssociatedTokenAccountInstruction(
});
}

export function createAddExtraComputeUnitsTransaction(owner: PublicKey, units: number): TransactionInstruction {
const p = new PublicKey('ComputeBudget111111111111111111111111111111');
const params = { instruction: 0, units, fee: 0 };
const layout = struct([u8('instruction'), u32('units'), u32('fee')]);
const data = Buffer.alloc(layout.span);
layout.encode(params, data);
const keys = [{ pubkey: owner, isSigner: false, isWritable: false }];
return new TransactionInstruction({
keys,
programId: p,
data,
});
export function createAddExtraComputeUnitsIx(units: number): TransactionInstruction {
return ComputeBudgetProgram.setComputeUnitLimit({ units });
}

export function createTransactionWithExtraBudget(payer: PublicKey, extraUnits: number = 400000) {
const tx = new Transaction();
const increaseBudgetIx = createAddExtraComputeUnitsTransaction(payer, extraUnits);
tx.add(increaseBudgetIx);
return tx;
export function createTransactionWithExtraBudget(extraUnits: number = 400000) {
return new Transaction().add(createAddExtraComputeUnitsIx(extraUnits));
}

export async function assignBlockInfoToTransaction(connection: Connection, transaction: Transaction, payer: PublicKey) {
Expand Down
68 changes: 34 additions & 34 deletions packages/kamino-sdk/tests/kamino.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from '@solana/web3.js';
import {
collToLamportsDecimal,
createAddExtraComputeUnitsTransaction,
createAddExtraComputeUnitsIx,
DepositAmountsForSwap,
Dex,
getReadOnlyWallet,
Expand Down Expand Up @@ -195,7 +195,7 @@ describe('Kamino SDK Tests', () => {
fixtures.newTokenMintB
);
fixtures.newRaydiumPool = raydiumPool.pool;
let createRaydiumTx = createTransactionWithExtraBudget(signer.publicKey);
let createRaydiumTx = createTransactionWithExtraBudget();
const newRaydiumStrategy = Keypair.generate();
const createRaydiumStrategyAccountIx = await kamino.createStrategyAccount(
signer.publicKey,
Expand Down Expand Up @@ -223,7 +223,7 @@ describe('Kamino SDK Tests', () => {
fixtures.newWhirlpool = whirlpool.pool;
console.log('whilrpool is ', whirlpool.pool.toString());

let tx = createTransactionWithExtraBudget(signer.publicKey);
let tx = createTransactionWithExtraBudget();
const newOrcaStrategy = Keypair.generate();
const createStrategyAccountIx = await kamino.createStrategyAccount(signer.publicKey, newOrcaStrategy.publicKey);
tx.add(createStrategyAccountIx);
Expand Down Expand Up @@ -506,7 +506,7 @@ describe('Kamino SDK Tests', () => {
throw new Error(`Could not fetch strategy for pubkey ${fixtures.newOrcaStrategy.toString()}`);
}

let tx = createTransactionWithExtraBudget(signer.publicKey, 12000000);
let tx = createTransactionWithExtraBudget(12000000);
const [sharesAta, sharesMintData] = await getAssociatedTokenAddressAndData(
connection,
strategyState.sharesMint,
Expand Down Expand Up @@ -547,14 +547,14 @@ describe('Kamino SDK Tests', () => {
await sleep(5000);

let depositIx = await kamino.deposit(fixtures.newOrcaStrategy, usdcDeposit, usdhDeposit, signer.publicKey);
let depositTx = createTransactionWithExtraBudget(signer.publicKey, 1200000);
let depositTx = createTransactionWithExtraBudget(1200000);
depositTx.add(depositIx);
await sendTransactionWithLogs(connection, depositTx, signer.publicKey, [signer]);

const strategy = (await kamino.getStrategyByAddress(fixtures.newOrcaStrategy))!;
const strategyWithAddress = { strategy, address: fixtures.newOrcaStrategy };

let withdrawTx = createTransactionWithExtraBudget(signer.publicKey);
let withdrawTx = createTransactionWithExtraBudget();

//@ts-ignore
let shares = await kamino.getTokenAccountBalance(sharesAta);
Expand Down Expand Up @@ -737,7 +737,7 @@ describe('Kamino SDK Tests', () => {
swapper
);

const increaseBudgetIx = createAddExtraComputeUnitsTransaction(signer.publicKey, 1_000_000);
const increaseBudgetIx = createAddExtraComputeUnitsIx(1_000_000);

const tx = await kamino.getTransactionV2Message(
signer.publicKey,
Expand Down Expand Up @@ -801,7 +801,7 @@ describe('Kamino SDK Tests', () => {
strategyState.tokenBMint,
signer.publicKey
);
let tx = createTransactionWithExtraBudget(signer.publicKey);
let tx = createTransactionWithExtraBudget();
const ataInstructions = await kamino.getCreateAssociatedTokenAccountInstructionsIfNotExist(
signer.publicKey,
strategyWithAddress,
Expand All @@ -822,7 +822,7 @@ describe('Kamino SDK Tests', () => {
await mintTo(connection, signer, strategyState.tokenBMint, tokenBAta, 9000000);
await sleep(5000);

let withdrawTx = createTransactionWithExtraBudget(signer.publicKey);
let withdrawTx = createTransactionWithExtraBudget();
const withdrawIxns = await kamino.withdrawShares(strategyWithAddress, new Decimal(0.02), signer.publicKey);
tx.add(...withdrawIxns.prerequisiteIxs, withdrawIxns.withdrawIx);

Expand Down Expand Up @@ -861,7 +861,7 @@ describe('Kamino SDK Tests', () => {
usdhAirdropAmount
);

let tx = createTransactionWithExtraBudget(user.owner.publicKey, 1200000);
let tx = createTransactionWithExtraBudget(1200000);

const depositIx = await kamino.deposit(strategyWithAddress, new Decimal(1), new Decimal(2), user.owner.publicKey);
tx.add(depositIx);
Expand All @@ -873,7 +873,7 @@ describe('Kamino SDK Tests', () => {
skipPreflight: true,
});

let withdrawTx = createTransactionWithExtraBudget(user.owner.publicKey);
let withdrawTx = createTransactionWithExtraBudget();
const withdrawIxns = await kamino.withdrawAllShares(strategyWithAddress, user.owner.publicKey);
if (withdrawIxns) {
tx.add(...withdrawIxns.prerequisiteIxs, withdrawIxns.withdrawIx);
Expand Down Expand Up @@ -916,7 +916,7 @@ describe('Kamino SDK Tests', () => {
usdhAirdropAmount
);

let tx = createTransactionWithExtraBudget(user.owner.publicKey, 1000000);
let tx = createTransactionWithExtraBudget(1000000);

const depositIx = await kamino.deposit(strategyWithAddress, new Decimal(1), new Decimal(2), user.owner.publicKey);
tx.add(depositIx);
Expand All @@ -928,7 +928,7 @@ describe('Kamino SDK Tests', () => {
skipPreflight: true,
});

let withdrawTx = createTransactionWithExtraBudget(user.owner.publicKey, 1000000);
let withdrawTx = createTransactionWithExtraBudget(1000000);

const withdrawIxns = await kamino.withdrawAllShares(strategyWithAddress, user.owner.publicKey);
if (withdrawIxns) {
Expand Down Expand Up @@ -972,7 +972,7 @@ describe('Kamino SDK Tests', () => {
usdhAirdropAmount
);

let tx = createTransactionWithExtraBudget(user.owner.publicKey, 1200000);
let tx = createTransactionWithExtraBudget(1200000);

const depositIx = await kamino.deposit(strategyWithAddress, new Decimal(1), new Decimal(2), user.owner.publicKey);
tx.add(depositIx);
Expand Down Expand Up @@ -1012,7 +1012,7 @@ describe('Kamino SDK Tests', () => {
usdhAirdropAmount
);

let tx = createTransactionWithExtraBudget(user.owner.publicKey, 1000000);
let tx = createTransactionWithExtraBudget(1000000);

const depositIx = await kamino.deposit(strategyWithAddress, new Decimal(1), new Decimal(2), user.owner.publicKey);
tx.add(depositIx);
Expand Down Expand Up @@ -1052,7 +1052,7 @@ describe('Kamino SDK Tests', () => {
usdhAirdropAmount
);

let tx = createTransactionWithExtraBudget(user.owner.publicKey, 1000000);
let tx = createTransactionWithExtraBudget(1000000);

let amounts = await kamino.calculateAmountsToBeDeposited(fixtures.newOrcaStrategy, new Decimal(5400));
console.log('orca amounts', amounts);
Expand Down Expand Up @@ -1095,7 +1095,7 @@ describe('Kamino SDK Tests', () => {
usdhAirdropAmount
);

let tx = createTransactionWithExtraBudget(user.owner.publicKey, 1000000);
let tx = createTransactionWithExtraBudget(1000000);

let amounts = await kamino.calculateAmountsToBeDeposited(fixtures.newRaydiumStrategy, new Decimal(54));
console.log('amounts', amounts);
Expand Down Expand Up @@ -1126,14 +1126,14 @@ describe('Kamino SDK Tests', () => {
);

{
const increaseBudgetIx = createAddExtraComputeUnitsTransaction(signer.publicKey, 1_000_000);
const increaseBudgetIx = createAddExtraComputeUnitsIx(1_000_000);
let tx = new Transaction().add(increaseBudgetIx, collectFeesIx);
let sig = await sendTransactionWithLogs(connection, tx, signer.publicKey, [signer]);
expect(sig).to.not.be.null;
console.log('executive withdraw and collect fees have been executed ');
}
{
const increaseBudgetIx = createAddExtraComputeUnitsTransaction(signer.publicKey, 1_000_000);
const increaseBudgetIx = createAddExtraComputeUnitsIx(1_000_000);
let tx = new Transaction().add(increaseBudgetIx, openPositionIx);
let sig = await sendTransactionWithLogs(
connection,
Expand Down Expand Up @@ -1172,7 +1172,7 @@ describe('Kamino SDK Tests', () => {
usdhAirdropAmount
);

let tx = createTransactionWithExtraBudget(user.owner.publicKey, 1000000);
let tx = createTransactionWithExtraBudget(1000000);

const depositIx = await kamino.deposit(strategyWithAddress, new Decimal(10), new Decimal(10), user.owner.publicKey);
tx.add(depositIx);
Expand Down Expand Up @@ -1201,14 +1201,14 @@ describe('Kamino SDK Tests', () => {
} else {
openPositionIx = rebalanceIxns[1];

let tx = createTransactionWithExtraBudget(signer.publicKey, 1_000_000).add(rebalanceIxns[0]);
let tx = createTransactionWithExtraBudget(1_000_000).add(rebalanceIxns[0]);
let sig = await sendTransactionWithLogs(connection, tx, signer.publicKey, [signer]);
expect(sig).to.not.be.null;
console.log('executive withdraw and collect fees have been executed');
}

{
const increaseBudgetIx = createAddExtraComputeUnitsTransaction(signer.publicKey, 1_000_000);
const increaseBudgetIx = createAddExtraComputeUnitsIx(1_000_000);

const openPositionTx = await kamino.getTransactionV2Message(
signer.publicKey,
Expand All @@ -1226,7 +1226,7 @@ describe('Kamino SDK Tests', () => {

{
let invextIx = await kamino.invest(fixtures.newRaydiumStrategy, signer.publicKey);
let tx = createTransactionWithExtraBudget(signer.publicKey, 1000000).add(invextIx);
let tx = createTransactionWithExtraBudget(1000000).add(invextIx);
let sig = await sendTransactionWithLogs(connection, tx, signer.publicKey, [signer]);
expect(sig).not.to.be.null;
}
Expand All @@ -1242,7 +1242,7 @@ describe('Kamino SDK Tests', () => {
LOCAL_RAYDIUM_PROGRAM_ID
);

let createRaydiumTx = createTransactionWithExtraBudget(signer.publicKey);
let createRaydiumTx = createTransactionWithExtraBudget();
const newRaydiumStrategy = Keypair.generate();
const createRaydiumStrategyAccountIx = await kamino.createStrategyAccount(
signer.publicKey,
Expand Down Expand Up @@ -1281,7 +1281,7 @@ describe('Kamino SDK Tests', () => {
);

{
const increaseBudgetIx = createAddExtraComputeUnitsTransaction(signer.publicKey, 1_400_000);
const increaseBudgetIx = createAddExtraComputeUnitsIx(1_400_000);

const openPositionTx = await kamino.getTransactionV2Message(
signer.publicKey,
Expand Down Expand Up @@ -1765,7 +1765,7 @@ describe('Kamino SDK Tests', () => {
LOCAL_RAYDIUM_PROGRAM_ID
);
// Create a new strategy
let txStrategyCreate = createTransactionWithExtraBudget(signer.publicKey);
let txStrategyCreate = createTransactionWithExtraBudget();
const newOrcaStrategy = Keypair.generate();
const createStrategyAccountIx = await kamino.createStrategyAccount(signer.publicKey, newOrcaStrategy.publicKey);
txStrategyCreate.add(createStrategyAccountIx);
Expand All @@ -1780,7 +1780,7 @@ describe('Kamino SDK Tests', () => {
const strategyBefore = (await kamino.getStrategyByAddress(newOrcaStrategy.publicKey))!;

// Close it right after
let tx = createTransactionWithExtraBudget(signer.publicKey, 1000000);
let tx = createTransactionWithExtraBudget(1000000);
const closeIx = await kamino.closeStrategy(newOrcaStrategy.publicKey);
tx.add(closeIx);
tx = await assignBlockInfoToTransaction(connection, tx, signer.publicKey);
Expand Down Expand Up @@ -1813,7 +1813,7 @@ describe('Kamino SDK Tests', () => {
LOCAL_RAYDIUM_PROGRAM_ID
);
// Create a new strategy
let txStrategyCreate = createTransactionWithExtraBudget(signer.publicKey);
let txStrategyCreate = createTransactionWithExtraBudget();
const newOrcaStrategy = Keypair.generate();
const createStrategyAccountIx = await kamino.createStrategyAccount(signer.publicKey, newOrcaStrategy.publicKey);
txStrategyCreate.add(createStrategyAccountIx);
Expand All @@ -1832,7 +1832,7 @@ describe('Kamino SDK Tests', () => {
const strategyState = (await kamino.getStrategyByAddress(newOrcaStrategy.publicKey))!;

// Close it right after
let tx = createTransactionWithExtraBudget(signer.publicKey, 1000000);
let tx = createTransactionWithExtraBudget(1000000);
const closeIx = await kamino.closeStrategy(newOrcaStrategy.publicKey);
tx.add(closeIx);
tx = await assignBlockInfoToTransaction(connection, tx, signer.publicKey);
Expand Down Expand Up @@ -1865,7 +1865,7 @@ describe('Kamino SDK Tests', () => {
LOCAL_RAYDIUM_PROGRAM_ID
);
// Create a new strategy
let txStrategyCreate = createTransactionWithExtraBudget(signer.publicKey);
let txStrategyCreate = createTransactionWithExtraBudget();
const newOrcaStrategy = Keypair.generate();
const createStrategyAccountIx = await kamino.createStrategyAccount(signer.publicKey, newOrcaStrategy.publicKey);
txStrategyCreate.add(createStrategyAccountIx);
Expand Down Expand Up @@ -1900,7 +1900,7 @@ describe('Kamino SDK Tests', () => {

// Deposit some funds
await setupAta(connection, signer, strategyState.sharesMint, signer);
let depositTxn = createTransactionWithExtraBudget(signer.publicKey, 1000000);
let depositTxn = createTransactionWithExtraBudget(1000000);
const depositIx = await kamino.deposit(
newOrcaStrategy.publicKey,
new Decimal(100.0),
Expand All @@ -1915,7 +1915,7 @@ describe('Kamino SDK Tests', () => {
});

// Withdraw all shares and close it right after
let tx = createTransactionWithExtraBudget(signer.publicKey, 1000000);
let tx = createTransactionWithExtraBudget(1000000);
const withdrawAndClose = await kamino.withdrawAllAndCloseStrategy(newOrcaStrategy.publicKey);
expect(withdrawAndClose).to.not.be.null;
for (let ixn of withdrawAndClose!.withdrawIxns) {
Expand Down Expand Up @@ -1964,7 +1964,7 @@ export async function createStrategy(kamino: Kamino, owner: Keypair, pool: Publi
const createStrategyAccountIx = await kamino.createStrategyAccount(owner.publicKey, newStrategy.publicKey);
const createStrategyIx = await kamino.createStrategy(newStrategy.publicKey, pool, owner.publicKey, dex);

let tx = createTransactionWithExtraBudget(owner.publicKey).add(createStrategyAccountIx).add(createStrategyIx);
let tx = createTransactionWithExtraBudget().add(createStrategyAccountIx).add(createStrategyIx);

await sendTransactionWithLogs(kamino.getConnection(), tx, owner.publicKey, [owner, newStrategy]);
const strategy = await kamino.getStrategyByAddress(newStrategy.publicKey);
Expand All @@ -1985,7 +1985,7 @@ export async function openPosition(
{
const openPositionIx = await kamino.openPosition(strategy, positionMint.publicKey, priceLower, priceUpper);

let tx = createTransactionWithExtraBudget(owner.publicKey, 1000000);
let tx = createTransactionWithExtraBudget(1000000);
tx.add(openPositionIx);
let res = await sendTransactionWithLogs(
kamino.getConnection(),
Expand Down
6 changes: 2 additions & 4 deletions packages/kamino-sdk/tests/stratCreation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import { Manual, PricePercentage, PricePercentageWithReset } from '../src/kamino
import { createWsolAtaIfMissing, getComputeBudgetAndPriorityFeeIxns } from '../src/utils/transactions';
import { JupService } from '../src/services/JupService';
import { MAINNET_GLOBAL_LOOKUP_TABLE } from '../src/constants/pubkeys';
import { getPdaProtocolPositionAddress, i32ToBytes, TickMath, TickUtils } from '@raydium-io/raydium-sdk';
import { PoolState } from '../src/raydium_client';

describe('Kamino strategy creation SDK Tests', () => {
let connection: Connection;
Expand Down Expand Up @@ -628,7 +626,7 @@ describe('Kamino strategy creation SDK Tests', () => {
new Decimal(10.0),
new Decimal(24.0),
]);
let tx = createTransactionWithExtraBudget(signer.publicKey);
let tx = createTransactionWithExtraBudget();
tx.add(updateRebalanceParamsIx);
let updateRebalanceParamsTxHash = await sendTransactionWithLogs(connection, tx, signer.publicKey, [signer]);
console.log('update Rebalance Params Tx Hash ', updateRebalanceParamsTxHash);
Expand Down Expand Up @@ -776,7 +774,7 @@ describe('Kamino strategy creation SDK Tests', () => {
new Decimal(10.0),
new Decimal(24.0),
]);
let tx = createTransactionWithExtraBudget(signer.publicKey);
let tx = createTransactionWithExtraBudget();
tx.add(updateRebalanceParamsIx);
let updateRebalanceParamsTxHash = await sendTransactionWithLogs(connection, tx, signer.publicKey, [signer]);
console.log('update Rebalance Params Tx Hash ', updateRebalanceParamsTxHash);
Expand Down
3 changes: 1 addition & 2 deletions packages/kamino-sdk/tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ import { getTickArrayPubkeysFromRangeRaydium } from './raydium_utils';
import { getTickArrayPubkeysFromRangeOrca } from './orca_utils';
import { TokenInstructions } from '@project-serum/serum';
import { collateralTokenToNumber, CollateralToken } from './token_utils';
import { checkIfAccountExists, getAtasWithCreateIxnsIfMissing } from '../src/utils/transactions';
import { checkIfAccountExists } from '../src/utils/transactions';
import { FullBPS } from '../src/utils/CreationParameters';
import { U16_MAX } from '@hubbleprotocol/scope-sdk';

export const GlobalConfigMainnet = new PublicKey('GKnHiWh3RRrE1zsNzWxRkomymHc374TvJPSTv2wPeYdB');
export const KaminoProgramIdMainnet = new PublicKey('6LtLpnUFNByNXLyCoK9wA2MykKAmQNZKBdY8s47dehDc');
Expand Down

0 comments on commit f544827

Please sign in to comment.