nara-sdk 1.0.48 → 1.0.49
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/package.json +1 -1
- package/src/constants.ts +4 -3
- package/src/tx.ts +52 -33
package/package.json
CHANGED
package/src/constants.ts
CHANGED
|
@@ -39,8 +39,9 @@ export const DEFAULT_AGENT_REGISTRY_PROGRAM_ID =
|
|
|
39
39
|
process.env.AGENT_REGISTRY_PROGRAM_ID || "AgentRegistry111111111111111111111111111111";
|
|
40
40
|
|
|
41
41
|
/**
|
|
42
|
-
* Address Lookup Table
|
|
43
|
-
*
|
|
42
|
+
* Address Lookup Table addresses for transaction optimization.
|
|
43
|
+
* Supports comma-separated list for multiple ALTs.
|
|
44
|
+
* When set, all SDK transactions use VersionedTransaction with these ALTs.
|
|
44
45
|
* When empty, uses legacy transactions.
|
|
45
46
|
*/
|
|
46
|
-
export const DEFAULT_ALT_ADDRESS = process.env.ALT_ADDRESS || "";
|
|
47
|
+
export const DEFAULT_ALT_ADDRESS = process.env.ALT_ADDRESS || "7u3uwQof8YnTrdYE2ZXgAYQLsDxW9cEJjqC3zJHrZXGo";
|
package/src/tx.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Shared transaction sending utility with optional Address Lookup Table support.
|
|
3
3
|
*
|
|
4
|
-
* When
|
|
5
|
-
* with
|
|
4
|
+
* When ALT addresses are configured, transactions are sent as VersionedTransaction
|
|
5
|
+
* with ALTs for smaller on-chain size. Otherwise, legacy Transaction is used.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import {
|
|
@@ -18,44 +18,63 @@ import {
|
|
|
18
18
|
} from "@solana/web3.js";
|
|
19
19
|
import { DEFAULT_ALT_ADDRESS } from "./constants";
|
|
20
20
|
|
|
21
|
-
let
|
|
22
|
-
let
|
|
23
|
-
let
|
|
21
|
+
let _cachedAlts: AddressLookupTableAccount[] = [];
|
|
22
|
+
let _cachedAltKey: string = "";
|
|
23
|
+
let _overrideAltAddresses: string[] | null = null;
|
|
24
24
|
|
|
25
25
|
/**
|
|
26
|
-
* Set
|
|
27
|
-
* Pass empty
|
|
26
|
+
* Set global ALT addresses at runtime (overrides DEFAULT_ALT_ADDRESS / env).
|
|
27
|
+
* Pass empty array or null to disable ALT.
|
|
28
28
|
*/
|
|
29
|
-
export function setAltAddress(
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
29
|
+
export function setAltAddress(addresses: string | string[] | null): void {
|
|
30
|
+
if (addresses === null) {
|
|
31
|
+
_overrideAltAddresses = [];
|
|
32
|
+
} else if (typeof addresses === "string") {
|
|
33
|
+
_overrideAltAddresses = addresses ? [addresses] : [];
|
|
34
|
+
} else {
|
|
35
|
+
_overrideAltAddresses = addresses.filter(Boolean);
|
|
36
|
+
}
|
|
37
|
+
// Invalidate cache when addresses change
|
|
38
|
+
_cachedAlts = [];
|
|
39
|
+
_cachedAltKey = "";
|
|
34
40
|
}
|
|
35
41
|
|
|
36
42
|
/**
|
|
37
|
-
* Get the current effective ALT
|
|
43
|
+
* Get the current effective ALT addresses.
|
|
38
44
|
*/
|
|
39
|
-
export function getAltAddress(): string {
|
|
40
|
-
|
|
45
|
+
export function getAltAddress(): string[] {
|
|
46
|
+
if (_overrideAltAddresses !== null) return _overrideAltAddresses;
|
|
47
|
+
if (!DEFAULT_ALT_ADDRESS) return [];
|
|
48
|
+
// env supports comma-separated list
|
|
49
|
+
return DEFAULT_ALT_ADDRESS.split(",").map((s) => s.trim()).filter(Boolean);
|
|
41
50
|
}
|
|
42
51
|
|
|
43
|
-
async function
|
|
52
|
+
async function loadAlts(
|
|
44
53
|
connection: Connection
|
|
45
|
-
): Promise<AddressLookupTableAccount
|
|
46
|
-
const
|
|
47
|
-
if (!
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
if (
|
|
51
|
-
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
54
|
+
): Promise<AddressLookupTableAccount[]> {
|
|
55
|
+
const addrs = getAltAddress();
|
|
56
|
+
if (!addrs.length) return [];
|
|
57
|
+
|
|
58
|
+
const key = addrs.join(",");
|
|
59
|
+
if (_cachedAlts.length && _cachedAltKey === key) return _cachedAlts;
|
|
60
|
+
|
|
61
|
+
const results: AddressLookupTableAccount[] = [];
|
|
62
|
+
for (const addr of addrs) {
|
|
63
|
+
try {
|
|
64
|
+
const result = await connection.getAddressLookupTable(new PublicKey(addr));
|
|
65
|
+
if (result.value) {
|
|
66
|
+
results.push(result.value);
|
|
67
|
+
} else {
|
|
68
|
+
console.warn(`[nara-sdk] ALT not found: ${addr}, skipping`);
|
|
69
|
+
}
|
|
70
|
+
} catch (e) {
|
|
71
|
+
console.warn(`[nara-sdk] Failed to load ALT ${addr}: ${e}, skipping`);
|
|
72
|
+
}
|
|
55
73
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
74
|
+
|
|
75
|
+
_cachedAlts = results;
|
|
76
|
+
_cachedAltKey = key;
|
|
77
|
+
return _cachedAlts;
|
|
59
78
|
}
|
|
60
79
|
|
|
61
80
|
/**
|
|
@@ -75,7 +94,7 @@ export async function getRecentPriorityFee(
|
|
|
75
94
|
|
|
76
95
|
/**
|
|
77
96
|
* Send a transaction with optional ALT support.
|
|
78
|
-
* If
|
|
97
|
+
* If ALT addresses are configured, uses VersionedTransaction.
|
|
79
98
|
* Otherwise, uses legacy Transaction.
|
|
80
99
|
*
|
|
81
100
|
* opts.computeUnitLimit - set CU limit (ComputeBudgetProgram.setComputeUnitLimit)
|
|
@@ -113,18 +132,18 @@ export async function sendTx(
|
|
|
113
132
|
}
|
|
114
133
|
const allInstructions = [...budgetIxs, ...instructions];
|
|
115
134
|
|
|
116
|
-
const
|
|
135
|
+
const alts = await loadAlts(connection);
|
|
117
136
|
const { blockhash, lastValidBlockHeight } =
|
|
118
137
|
await connection.getLatestBlockhash("confirmed");
|
|
119
138
|
|
|
120
139
|
let signature: string;
|
|
121
140
|
|
|
122
|
-
if (
|
|
141
|
+
if (alts.length) {
|
|
123
142
|
const message = new TransactionMessage({
|
|
124
143
|
payerKey: payer.publicKey,
|
|
125
144
|
recentBlockhash: blockhash,
|
|
126
145
|
instructions: allInstructions,
|
|
127
|
-
}).compileToV0Message(
|
|
146
|
+
}).compileToV0Message(alts);
|
|
128
147
|
|
|
129
148
|
const tx = new VersionedTransaction(message);
|
|
130
149
|
const allSigners = [payer, ...(signers ?? [])];
|