Skip to content

Commit a43e1c0

Browse files
committed
adds simulation for cus
1 parent db6b323 commit a43e1c0

File tree

1 file changed

+41
-5
lines changed

1 file changed

+41
-5
lines changed

scripts/v0.7/hurupay/setupLaunch.ts

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ import {
44
getLaunchAddr,
55
getLaunchSignerAddr,
66
} from "@metadaoproject/futarchy/v0.7";
7-
import { PublicKey, SystemProgram, Transaction } from "@solana/web3.js";
7+
import {
8+
ComputeBudgetProgram,
9+
PublicKey,
10+
SystemProgram,
11+
Transaction,
12+
} from "@solana/web3.js";
813
import BN from "bn.js";
914
import * as token from "@solana/spl-token";
1015

@@ -107,19 +112,50 @@ export const launch = async () => {
107112
})
108113
.instruction();
109114

115+
// Build transaction without compute budget first
110116
const tx = new Transaction().add(
111117
createTokenAccountIx,
112118
initializeMintIx,
113119
launchIx,
114120
);
115121

116-
tx.recentBlockhash = (
117-
await provider.connection.getLatestBlockhash()
118-
).blockhash;
122+
const { blockhash } = await provider.connection.getLatestBlockhash();
123+
tx.recentBlockhash = blockhash;
119124
tx.feePayer = payer.publicKey;
125+
126+
// Simulate transaction to get compute units used
120127
tx.sign(payer);
128+
const simulation = await provider.connection.simulateTransaction(tx);
129+
130+
if (simulation.value.err) {
131+
console.error("Transaction simulation failed:", simulation.value.err);
132+
throw new Error(
133+
`Simulation failed: ${JSON.stringify(simulation.value.err)}`,
134+
);
135+
}
136+
137+
const computeUnitsUsed = simulation.value.unitsConsumed || 200_000;
138+
// Add 20% buffer to the compute units
139+
const computeUnitsWithBuffer = Math.floor(computeUnitsUsed * 1.2);
140+
141+
console.log(`Simulated compute units: ${computeUnitsUsed}`);
142+
console.log(`Setting compute unit limit: ${computeUnitsWithBuffer}`);
143+
144+
// Rebuild transaction with compute budget
145+
const finalTx = new Transaction().add(
146+
ComputeBudgetProgram.setComputeUnitLimit({ units: computeUnitsWithBuffer }),
147+
createTokenAccountIx,
148+
initializeMintIx,
149+
launchIx,
150+
);
151+
152+
finalTx.recentBlockhash = blockhash;
153+
finalTx.feePayer = payer.publicKey;
154+
finalTx.sign(payer);
121155

122-
const txHash = await provider.connection.sendRawTransaction(tx.serialize());
156+
const txHash = await provider.connection.sendRawTransaction(
157+
finalTx.serialize(),
158+
);
123159
await provider.connection.confirmTransaction(txHash, "confirmed");
124160

125161
console.log("Launch initialized", txHash);

0 commit comments

Comments
 (0)