Skip to content

Commit c3209e5

Browse files
authored
🐛 Fix wrong handling of expected errors (#39)
1 parent 07a4be9 commit c3209e5

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

script/hyli-wallet.js

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -97,21 +97,21 @@ async function registerAccount(username, password, inviteCode, salt, enableSessi
9797
const nodeService = new NodeApiHttpClient(CONFIG.NODE_BASE_URL);
9898

9999
// Check if account already exists
100+
let accountInfo;
100101
try {
101102
const response = await fetch(`${CONFIG.WALLET_API_BASE_URL}/v1/indexer/contract/${CONFIG.WALLET_CONTRACT_NAME}/account/${username}`);
102-
103103
if (!response.ok) {
104104
throw new Error(`Failed to check account existence: ${response.statusText}`);
105105
}
106-
107-
const accountInfo = await response.json();
108-
109-
if (accountInfo) {
110-
throw new Error(`Account with username "${username}" already exists.`);
111-
}
106+
accountInfo = await response.json();
112107
} catch (error) {
113108
// If error, assume account does not exist and continue
114-
console.log("Account does not exist, proceeding with registration...");
109+
console.log(`Account ${username} does not exist, proceeding with registration...`);
110+
}
111+
112+
if (accountInfo) {
113+
console.log(`Account ${username} already exists`);
114+
return { success: true, wallet: accountInfo };
115115
}
116116

117117
// Validate password
@@ -178,7 +178,7 @@ async function registerAccount(username, password, inviteCode, salt, enableSessi
178178
console.log(`Blob transaction hash: ${txHash}`);
179179

180180
// Send the actual blob transaction
181-
console.log("Sending blob transaction...", blobTx);
181+
console.log("Sending blob transaction...");
182182
await nodeService.sendBlobTx(blobTx);
183183
console.log("Blob transaction sent successfully");
184184

@@ -259,18 +259,20 @@ async function transferFunds(username, password, amount, token, destination) {
259259
const identity = `${username}@${CONFIG.WALLET_CONTRACT_NAME}`;
260260
const salted_password = `${password}:${accountInfo.salt}`;
261261

262-
263262
// Check that account has enough balance
264263
console.log("Checking balance...");
264+
let balance;
265265
try {
266-
const balance = await indexerService.get(`v1/indexer/contract/${token}/balance/${identity}`, "Checking balance");
267-
console.log("Balance...", balance);
268-
if (balance < parsedAmount) {
269-
throw new Error(`Account "${username}" does not have enough balance to transfer ${amount} ${token}`);
270-
}
266+
const result = await indexerService.get(`v1/indexer/contract/${token}/balance/${identity}`, "Checking balance");
267+
balance = result.balance;
271268
} catch (error) {
272269
throw new Error(`Failed to get balance: ${error.message}. User might have no balance for this token.`);
273270
}
271+
console.log(`Balance for ${username} is`, balance);
272+
273+
if (balance < parsedAmount) {
274+
throw new Error(`Account "${username}" does not have enough balance to transfer ${amount} ${token}`);
275+
}
274276

275277
// Create blobs for transfer
276278
console.log("Creating transfer blobs...");
@@ -289,7 +291,7 @@ async function transferFunds(username, password, amount, token, destination) {
289291
console.log(`Blob transaction hash: ${txHash}`);
290292

291293
// Send the actual blob transaction
292-
console.log("Sending blob transaction...", blobTx);
294+
console.log("Sending blob transaction...");
293295
await nodeService.sendBlobTx(blobTx);
294296
console.log("Blob transaction sent successfully");
295297

script/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hyli-wallet-cli",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"type": "module",
55
"license": "MIT",
66
"repository": {

0 commit comments

Comments
 (0)