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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nara-sdk",
3
- "version": "1.0.48",
3
+ "version": "1.0.49",
4
4
  "description": "SDK for the Nara chain (Solana-compatible)",
5
5
  "module": "index.ts",
6
6
  "main": "index.ts",
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 address for transaction optimization.
43
- * When set, all SDK transactions use VersionedTransaction with this ALT.
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 DEFAULT_ALT_ADDRESS is set, transactions are sent as VersionedTransaction
5
- * with the ALT for smaller on-chain size. Otherwise, legacy Transaction is used.
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 _cachedAlt: AddressLookupTableAccount | null = null;
22
- let _cachedAltAddress: string = "";
23
- let _overrideAltAddress: string | null = null;
21
+ let _cachedAlts: AddressLookupTableAccount[] = [];
22
+ let _cachedAltKey: string = "";
23
+ let _overrideAltAddresses: string[] | null = null;
24
24
 
25
25
  /**
26
- * Set a global ALT address at runtime (overrides DEFAULT_ALT_ADDRESS / env).
27
- * Pass empty string or null to disable ALT.
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(address: string | null): void {
30
- _overrideAltAddress = address;
31
- // Invalidate cache when address changes
32
- _cachedAlt = null;
33
- _cachedAltAddress = "";
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 address.
43
+ * Get the current effective ALT addresses.
38
44
  */
39
- export function getAltAddress(): string {
40
- return _overrideAltAddress ?? DEFAULT_ALT_ADDRESS;
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 loadAlt(
52
+ async function loadAlts(
44
53
  connection: Connection
45
- ): Promise<AddressLookupTableAccount | null> {
46
- const addr = getAltAddress();
47
- if (!addr) return null;
48
-
49
- // Cache the ALT account to avoid repeated fetches
50
- if (_cachedAlt && _cachedAltAddress === addr) return _cachedAlt;
51
-
52
- const result = await connection.getAddressLookupTable(new PublicKey(addr));
53
- if (!result.value) {
54
- throw new Error(`Address Lookup Table not found: ${addr}`);
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
- _cachedAlt = result.value;
57
- _cachedAltAddress = addr;
58
- return _cachedAlt;
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 DEFAULT_ALT_ADDRESS is configured, uses VersionedTransaction.
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 alt = await loadAlt(connection);
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 (alt) {
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([alt]);
146
+ }).compileToV0Message(alts);
128
147
 
129
148
  const tx = new VersionedTransaction(message);
130
149
  const allSigners = [payer, ...(signers ?? [])];