Skip to content

Commit d23e2a8

Browse files
refactor(cli): generalize vault init messages for all account types
Synced from glamsystems/glam 99f7e10272f0082bb023b5cca67f878484284564
1 parent 3963a18 commit d23e2a8

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

src/cmds/vault.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,10 @@ export function installVaultCommands(program: Command, context: CliContext) {
198198
}
199199

200200
const initStateParams = parseStateJson(json);
201-
if (initStateParams.accountType === StateAccountType.VAULT) {
201+
const accountType = initStateParams.accountType;
202+
const accountTypeStr = Object.keys(accountType)[0];
203+
204+
if (accountType === StateAccountType.VAULT) {
202205
await executeTxWithErrorHandling(
203206
async () => {
204207
const txSig = await context.glamClient.state.initialize(
@@ -212,14 +215,14 @@ export function installVaultCommands(program: Command, context: CliContext) {
212215
},
213216
{
214217
skip: options?.yes,
215-
message: `Confirm initializing GLAM vault: ${charsToString(initStateParams.name)}`,
218+
message: `Confirm initializing ${accountTypeStr}: ${charsToString(initStateParams.name)}`,
216219
},
217-
(txSig) => `GLAM vault initialized: ${txSig}`,
220+
(txSig) => `Initialized ${accountTypeStr}: ${txSig}`,
218221
);
219222
return;
220223
}
221224

222-
const initMintParams = parseMintJson(json, initStateParams.accountType);
225+
const initMintParams = parseMintJson(json, accountType);
223226
await executeTxWithErrorHandling(
224227
async () => {
225228
// mint.initializeWithStateParams creates state with default setup
@@ -238,9 +241,9 @@ export function installVaultCommands(program: Command, context: CliContext) {
238241
},
239242
{
240243
skip: options?.yes,
241-
message: `Confirm initializing GLAM tokenized vault: ${charsToString(initMintParams.name)}`,
244+
message: `Confirm initializing ${accountTypeStr}: ${charsToString(initMintParams.name)}`,
242245
},
243-
(txSig) => `GLAM tokenized vault initialized: ${txSig}`,
246+
(txSig) => `Initialized ${accountTypeStr}: ${txSig}`,
244247
);
245248
});
246249

src/utils.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -194,15 +194,12 @@ export async function confirmOperation(message: string) {
194194
}
195195

196196
/**
197-
{
198-
"state": {
199-
"accountType": "vault",
200-
"name": "Example vault",
201-
"baseAssetMint": "So11111111111111111111111111111111111111112",
202-
"enabled": true, // optional
203-
"assets": [ "So11111111111111111111111111111111111111112" ] // optional
204-
}
205-
}
197+
* Parses a JSON object containing state initialization parameters.
198+
*
199+
* `json.state` must contain the following properties:
200+
* - `accountType`: The type of the state account.
201+
* - `name`: The name of the state account.
202+
* - `baseAssetMint`: The base asset mint of the vault.
206203
*/
207204
export function parseStateJson(json: any): InitStateParams {
208205
if (!json.state) {
@@ -229,6 +226,14 @@ export function parseStateJson(json: any): InitStateParams {
229226
};
230227
}
231228

229+
/**
230+
* Parses a JSON object containing mint initialization parameters.
231+
*
232+
* `json.mint` must contain the following properties:
233+
* - `name`: The name of the mint.
234+
* - `symbol`: The symbol of the mint.
235+
* - `uri`: The URI of the mint.
236+
*/
232237
export function parseMintJson(
233238
json: any,
234239
accountType: StateAccountType,
@@ -245,9 +250,7 @@ export function parseMintJson(
245250
}
246251

247252
const { mint } = json;
248-
249-
const requiredFields = ["name", "symbol", "uri"];
250-
requiredFields.forEach((field) => {
253+
["name", "symbol", "uri"].forEach((field) => {
251254
if (mint?.[field] === undefined) {
252255
throw new Error(
253256
`Account type ${accountType} missing required mint field: ${field}`,
@@ -257,7 +260,7 @@ export function parseMintJson(
257260
const baseAssetMint = json?.state?.baseAssetMint;
258261
if (!baseAssetMint) {
259262
throw new Error(
260-
"Invalid JSON file: must contain 'baseAssetMint' property for tokenized vault",
263+
"Invalid JSON file: missing required state field `baseAssetMint`",
261264
);
262265
}
263266

0 commit comments

Comments
 (0)