Skip to content

Commit 8ddc0ba

Browse files
committed
add fn to parse version string, check major version as numeric, remove commented out code
1 parent 6ce56f8 commit 8ddc0ba

File tree

2 files changed

+15
-21
lines changed

2 files changed

+15
-21
lines changed

solana/ts/lib/ntt.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import {
4949
BPF_LOADER_UPGRADEABLE_PROGRAM_ID,
5050
chainToBytes,
5151
derivePda,
52+
parseVersion,
5253
programDataAddress,
5354
programVersionLayout,
5455
} from "./utils.js";
@@ -266,8 +267,9 @@ export namespace NTT {
266267
},
267268
pdas?: Pdas
268269
) {
269-
// if the program is at version 1.0.0, we don't need to initialize the LUT
270-
if (program.idl.version === "1.0.0") return;
270+
// if the program is < major version 2.x.x, we don't need to initialize the LUT
271+
const [major, ,] = parseVersion(program.idl.version);
272+
if (major < 2) return;
271273

272274
pdas = pdas ?? NTT.pdas(program.programId);
273275

@@ -318,10 +320,8 @@ export namespace NTT {
318320
};
319321
const pubkeys = collectPubkeys(entries).map((pk) => pk.toBase58());
320322

321-
let existingLut: web3.AddressLookupTableAccount | null = null;
322-
try {
323-
existingLut = await getAddressLookupTable(program, pdas);
324-
} catch {}
323+
let existingLut: web3.AddressLookupTableAccount | null =
324+
await getAddressLookupTable(program, pdas);
325325

326326
if (existingLut !== null) {
327327
const existingPubkeys =
@@ -996,9 +996,9 @@ export namespace NTT {
996996
export async function getAddressLookupTable(
997997
program: Program<NttBindings.NativeTokenTransfer<IdlVersion>>,
998998
pdas?: Pdas
999-
): Promise<AddressLookupTableAccount> {
1000-
if (program.idl.version === "1.0.0")
1001-
throw new Error("Lookup tables not supported for this version");
999+
): Promise<AddressLookupTableAccount | null> {
1000+
const [major, ,] = parseVersion(program.idl.version);
1001+
if (major < 2) return null;
10021002

10031003
pdas = pdas ?? NTT.pdas(program.programId);
10041004
const lut = await program.account.lut.fetchNullable(pdas.lutAccount());

solana/ts/lib/utils.ts

+6-12
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ export function programDataAddress(programId: PublicKeyInitData) {
1818
)[0];
1919
}
2020

21+
export function parseVersion(version: string): [number, number, number] {
22+
const components = version.split(".");
23+
if (components.length !== 3) throw new Error("Invalid version string");
24+
return [Number(components[0]), Number(components[1]), Number(components[2])];
25+
}
26+
2127
export const pubKeyConversion = {
2228
to: (encoded: Uint8Array) => new PublicKey(encoded),
2329
from: (decoded: PublicKey) => decoded.toBytes(),
@@ -97,15 +103,3 @@ export const quoterAddresses = (programId: PublicKeyInitData) => {
97103
registeredNttAccount,
98104
};
99105
};
100-
101-
// // The `translateError` function expects this format, but the idl gives us a
102-
// // different one, so we preprocess the idl and store the expected format.
103-
// // NOTE: I'm sure there's a function within anchor that does this, but I
104-
// // couldn't find it.
105-
// private processErrors(): Map<number, string> {
106-
// const errors = this.program.idl.errors;
107-
// const result: Map<number, string> = new Map<number, string>();
108-
// errors.forEach((entry) => result.set(entry.code, entry.msg));
109-
// return result;
110-
// }
111-
// // View functions

0 commit comments

Comments
 (0)