Skip to content

Commit 640c961

Browse files
Make imports be able to use a program name or a program string
1 parent c1514db commit 640c961

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

sdk/src/network-client.ts

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,40 +1026,49 @@ class AleoNetworkClient {
10261026
* programImports = await networkClient.getProgramImports(double_test);
10271027
* assert.deepStrictEqual(programImports, expectedImports);
10281028
*/
1029-
async getProgramImports(
1030-
inputProgram: Program | string,
1031-
): Promise<ProgramImports> {
1029+
async getProgramImports(inputProgram: Program | string): Promise<ProgramImports> {
10321030
try {
10331031
this.ctx = { "X-ALEO-METHOD": "getProgramImports" };
10341032
const imports: ProgramImports = {};
10351033

1036-
// Get the program object or fail if the program is not valid or does not exist
1037-
const program =
1038-
inputProgram instanceof Program
1039-
? inputProgram
1040-
: <Program>await this.getProgramObject(inputProgram);
1034+
// Normalize input to a Program object
1035+
let program: Program;
1036+
if (inputProgram instanceof Program) {
1037+
program = inputProgram;
1038+
} else {
1039+
try {
1040+
program = Program.fromString(inputProgram);
1041+
} catch {
1042+
try {
1043+
program = await this.getProgramObject(inputProgram);
1044+
} catch (error2) {
1045+
throw new Error(
1046+
`${inputProgram} is neither a program name nor a valid program: ${error2}`,
1047+
);
1048+
}
1049+
}
1050+
}
10411051

10421052
// Get the list of programs that the program imports
10431053
const importList = program.getImports();
10441054

1045-
// Recursively get any imports that the imported programs have in a depth first search order
1055+
// Recursively get any imports that the imported programs have in a depth-first search
10461056
for (let i = 0; i < importList.length; i++) {
10471057
const import_id = importList[i];
10481058
if (!imports.hasOwnProperty(import_id)) {
1049-
const programSource = <string>(
1050-
await this.getProgram(import_id)
1051-
);
1052-
const nestedImports = <ProgramImports>(
1053-
await this.getProgramImports(import_id)
1054-
);
1059+
const programSource = <string>await this.getProgram(import_id);
1060+
const nestedImports = <ProgramImports>await this.getProgramImports(import_id);
1061+
10551062
for (const key in nestedImports) {
10561063
if (!imports.hasOwnProperty(key)) {
10571064
imports[key] = nestedImports[key];
10581065
}
10591066
}
1067+
10601068
imports[import_id] = programSource;
10611069
}
10621070
}
1071+
10631072
return imports;
10641073
} catch (error: any) {
10651074
logAndThrow("Error fetching program imports: " + error.message);
@@ -1068,6 +1077,7 @@ class AleoNetworkClient {
10681077
}
10691078
}
10701079

1080+
10711081
/**
10721082
* Get a list of the program names that a program imports.
10731083
*
@@ -1135,7 +1145,7 @@ class AleoNetworkClient {
11351145
);
11361146
} catch (error) {
11371147
throw new Error(
1138-
`Error fetching mappings for program ${programId} - ensure the program exists on chain before trying again`,
1148+
`Error fetching mappings for program ${programId} - ensure the program exists on chain before trying again: ${error}`,
11391149
);
11401150
} finally {
11411151
this.ctx = {};
@@ -1174,7 +1184,7 @@ class AleoNetworkClient {
11741184
);
11751185
} catch (error) {
11761186
throw new Error(
1177-
`Error fetching value for key '${key}' in mapping '${mappingName}' in program '${programId}' - ensure the mapping exists and the key is correct`,
1187+
`Error fetching value for key '${key}' in mapping '${mappingName}' in program '${programId}' - ensure the mapping exists and the key is correct: ${error}`,
11781188
);
11791189
} finally {
11801190
this.ctx = {};

0 commit comments

Comments
 (0)