nara-sdk 1.0.44 → 1.0.45
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.ts +1 -1
- package/package.json +1 -1
- package/src/quest.ts +5 -1
- package/src/tx.ts +45 -3
package/index.ts
CHANGED
|
@@ -18,7 +18,7 @@ export {
|
|
|
18
18
|
} from "./src/constants";
|
|
19
19
|
|
|
20
20
|
// Export transaction helper
|
|
21
|
-
export { sendTx, setAltAddress, getAltAddress } from "./src/tx";
|
|
21
|
+
export { sendTx, setAltAddress, getAltAddress, getRecentPriorityFee } from "./src/tx";
|
|
22
22
|
|
|
23
23
|
// Export quest functions and types
|
|
24
24
|
export {
|
package/package.json
CHANGED
package/src/quest.ts
CHANGED
|
@@ -423,7 +423,11 @@ export async function submitAnswer(
|
|
|
423
423
|
ixs.push(logIx);
|
|
424
424
|
}
|
|
425
425
|
|
|
426
|
-
const signature = await sendTx(connection, wallet, ixs, [], {
|
|
426
|
+
const signature = await sendTx(connection, wallet, ixs, [], {
|
|
427
|
+
skipPreflight: true,
|
|
428
|
+
computeUnitLimit: 500_000,
|
|
429
|
+
computeUnitPrice: "auto",
|
|
430
|
+
});
|
|
427
431
|
return { signature };
|
|
428
432
|
}
|
|
429
433
|
|
package/src/tx.ts
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
|
|
8
8
|
import {
|
|
9
9
|
AddressLookupTableAccount,
|
|
10
|
+
ComputeBudgetProgram,
|
|
10
11
|
Connection,
|
|
11
12
|
Keypair,
|
|
12
13
|
PublicKey,
|
|
@@ -57,11 +58,30 @@ async function loadAlt(
|
|
|
57
58
|
return _cachedAlt;
|
|
58
59
|
}
|
|
59
60
|
|
|
61
|
+
/**
|
|
62
|
+
* Get the recent average priority fee (in micro-lamports per CU).
|
|
63
|
+
* Samples the last few slots via getRecentPrioritizationFees.
|
|
64
|
+
*/
|
|
65
|
+
export async function getRecentPriorityFee(
|
|
66
|
+
connection: Connection,
|
|
67
|
+
): Promise<number> {
|
|
68
|
+
const fees = await connection.getRecentPrioritizationFees();
|
|
69
|
+
if (!fees.length) return 0;
|
|
70
|
+
const nonZero = fees.filter((f) => f.prioritizationFee > 0);
|
|
71
|
+
if (!nonZero.length) return 0;
|
|
72
|
+
const avg = nonZero.reduce((s, f) => s + f.prioritizationFee, 0) / nonZero.length;
|
|
73
|
+
return Math.ceil(avg);
|
|
74
|
+
}
|
|
75
|
+
|
|
60
76
|
/**
|
|
61
77
|
* Send a transaction with optional ALT support.
|
|
62
78
|
* If DEFAULT_ALT_ADDRESS is configured, uses VersionedTransaction.
|
|
63
79
|
* Otherwise, uses legacy Transaction.
|
|
64
80
|
*
|
|
81
|
+
* opts.computeUnitLimit - set CU limit (ComputeBudgetProgram.setComputeUnitLimit)
|
|
82
|
+
* opts.computeUnitPrice - set CU price in micro-lamports (ComputeBudgetProgram.setComputeUnitPrice)
|
|
83
|
+
* opts.computeUnitPrice = "auto" - auto-fetch recent average priority fee
|
|
84
|
+
*
|
|
65
85
|
* @returns transaction signature
|
|
66
86
|
*/
|
|
67
87
|
export async function sendTx(
|
|
@@ -69,8 +89,30 @@ export async function sendTx(
|
|
|
69
89
|
payer: Keypair,
|
|
70
90
|
instructions: TransactionInstruction[],
|
|
71
91
|
signers?: Keypair[],
|
|
72
|
-
opts?: { skipPreflight?: boolean }
|
|
92
|
+
opts?: { skipPreflight?: boolean; computeUnitLimit?: number; computeUnitPrice?: number | "auto" }
|
|
73
93
|
): Promise<string> {
|
|
94
|
+
// Prepend compute budget instructions
|
|
95
|
+
const budgetIxs: TransactionInstruction[] = [];
|
|
96
|
+
if (opts?.computeUnitLimit) {
|
|
97
|
+
budgetIxs.push(
|
|
98
|
+
ComputeBudgetProgram.setComputeUnitLimit({ units: opts.computeUnitLimit })
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
if (opts?.computeUnitPrice !== undefined) {
|
|
102
|
+
let price: number;
|
|
103
|
+
if (opts.computeUnitPrice === "auto") {
|
|
104
|
+
price = await getRecentPriorityFee(connection);
|
|
105
|
+
} else {
|
|
106
|
+
price = opts.computeUnitPrice;
|
|
107
|
+
}
|
|
108
|
+
if (price > 0) {
|
|
109
|
+
budgetIxs.push(
|
|
110
|
+
ComputeBudgetProgram.setComputeUnitPrice({ microLamports: price })
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
const allInstructions = [...budgetIxs, ...instructions];
|
|
115
|
+
|
|
74
116
|
const alt = await loadAlt(connection);
|
|
75
117
|
const { blockhash, lastValidBlockHeight } =
|
|
76
118
|
await connection.getLatestBlockhash("confirmed");
|
|
@@ -81,7 +123,7 @@ export async function sendTx(
|
|
|
81
123
|
const message = new TransactionMessage({
|
|
82
124
|
payerKey: payer.publicKey,
|
|
83
125
|
recentBlockhash: blockhash,
|
|
84
|
-
instructions,
|
|
126
|
+
instructions: allInstructions,
|
|
85
127
|
}).compileToV0Message([alt]);
|
|
86
128
|
|
|
87
129
|
const tx = new VersionedTransaction(message);
|
|
@@ -103,7 +145,7 @@ export async function sendTx(
|
|
|
103
145
|
tx.recentBlockhash = blockhash;
|
|
104
146
|
tx.lastValidBlockHeight = lastValidBlockHeight;
|
|
105
147
|
tx.feePayer = payer.publicKey;
|
|
106
|
-
for (const ix of
|
|
148
|
+
for (const ix of allInstructions) tx.add(ix);
|
|
107
149
|
const allSigners = [payer, ...(signers ?? [])];
|
|
108
150
|
const seen = new Set<string>();
|
|
109
151
|
const uniqueSigners = allSigners.filter((s) => {
|