0
# Account and Balance Errors
1
2
Error classes for account management and balance validation including insufficient funds, account state issues, and transaction validation errors.
3
4
## Types
5
6
```typescript { .api }
7
interface Account {
8
balance: number;
9
delegatedBalance: number;
10
subAccounts: Account[];
11
deviceId?: string;
12
}
13
```
14
15
## Capabilities
16
17
### Account Validation Errors
18
19
Errors related to account requirements and validation.
20
21
```typescript { .api }
22
const AccountNameRequiredError: CustomErrorFunc;
23
const AccountNotSupported: CustomErrorFunc;
24
const NoAddressesFound: CustomErrorFunc;
25
const WrongDeviceForAccount: CustomErrorFunc;
26
```
27
28
**Usage Examples:**
29
30
```typescript
31
import {
32
AccountNameRequiredError,
33
AccountNotSupported,
34
NoAddressesFound,
35
WrongDeviceForAccount
36
} from "@ledgerhq/errors";
37
38
// Validate account name requirement
39
function createAccount(name?: string) {
40
if (!name || name.trim() === "") {
41
throw new AccountNameRequiredError("Account name is required");
42
}
43
}
44
45
// Handle unsupported account types
46
function validateAccountType(accountType: string) {
47
const supportedTypes = ["bitcoin", "ethereum", "litecoin"];
48
if (!supportedTypes.includes(accountType)) {
49
throw new AccountNotSupported(`Account type '${accountType}' is not supported`);
50
}
51
}
52
53
// Handle missing addresses
54
if (addresses.length === 0) {
55
throw new NoAddressesFound("No addresses found for this account");
56
}
57
58
// Validate device-account pairing
59
if (account.deviceId !== connectedDevice.id) {
60
throw new WrongDeviceForAccount("This account belongs to a different device");
61
}
62
```
63
64
### Balance Validation Errors
65
66
Errors related to insufficient balances and spending limitations.
67
68
```typescript { .api }
69
const NotEnoughBalance: CustomErrorFunc;
70
const NotEnoughBalanceToDelegate: CustomErrorFunc;
71
const NotEnoughBalanceInParentAccount: CustomErrorFunc;
72
const NotEnoughSpendableBalance: CustomErrorFunc;
73
const NotEnoughBalanceBecauseDestinationNotCreated: CustomErrorFunc;
74
```
75
76
**Usage Examples:**
77
78
```typescript
79
import {
80
NotEnoughBalance,
81
NotEnoughBalanceToDelegate,
82
NotEnoughBalanceInParentAccount,
83
NotEnoughSpendableBalance,
84
NotEnoughBalanceBecauseDestinationNotCreated
85
} from "@ledgerhq/errors";
86
87
// Validate sufficient balance for transaction
88
function validateTransactionAmount(balance: number, amount: number, fee: number) {
89
if (balance < amount + fee) {
90
throw new NotEnoughBalance(
91
`Insufficient balance: ${balance} < ${amount + fee} (amount + fee)`
92
);
93
}
94
}
95
96
// Validate delegation balance
97
function validateDelegation(balance: number, delegationAmount: number, minimumRequired: number) {
98
if (balance < minimumRequired) {
99
throw new NotEnoughBalanceToDelegate(
100
`Minimum balance of ${minimumRequired} required for delegation`
101
);
102
}
103
}
104
105
// Validate parent account for sub-accounts
106
function validateSubAccountOperation(parentBalance: number, requiredAmount: number) {
107
if (parentBalance < requiredAmount) {
108
throw new NotEnoughBalanceInParentAccount(
109
"Parent account has insufficient balance for this operation"
110
);
111
}
112
}
113
114
// Validate spendable balance (excluding locked/pending funds)
115
function validateSpendableAmount(totalBalance: number, lockedAmount: number, spendAmount: number) {
116
const spendableBalance = totalBalance - lockedAmount;
117
if (spendableBalance < spendAmount) {
118
throw new NotEnoughSpendableBalance(
119
`Only ${spendableBalance} available to spend (${lockedAmount} locked)`
120
);
121
}
122
}
123
124
// Handle destination account creation requirement
125
function validateDestinationAccount(destinationExists: boolean, amount: number, minimumCreation: number) {
126
if (!destinationExists && amount < minimumCreation) {
127
throw new NotEnoughBalanceBecauseDestinationNotCreated(
128
`Minimum ${minimumCreation} required to create destination account`
129
);
130
}
131
}
132
```
133
134
### Delegation and Staking Errors
135
136
Errors specific to delegation and staking operations.
137
138
```typescript { .api }
139
const RecommendSubAccountsToEmpty: CustomErrorFunc;
140
const RecommendUndelegation: CustomErrorFunc;
141
```
142
143
**Usage Examples:**
144
145
```typescript
146
import {
147
RecommendSubAccountsToEmpty,
148
RecommendUndelegation
149
} from "@ledgerhq/errors";
150
151
// Recommend emptying sub-accounts before main operation
152
function checkSubAccounts(subAccounts: Account[]) {
153
const hasBalance = subAccounts.some(account => account.balance > 0);
154
if (hasBalance) {
155
throw new RecommendSubAccountsToEmpty(
156
"Please empty sub-accounts before performing this operation"
157
);
158
}
159
}
160
161
// Recommend undelegation before account operations
162
function checkDelegatedBalance(delegatedAmount: number) {
163
if (delegatedAmount > 0) {
164
throw new RecommendUndelegation(
165
"Please undelegate your funds before closing the account"
166
);
167
}
168
}
169
170
// Example usage in account management
171
function closeAccount(account: Account) {
172
// Check for delegated funds
173
if (account.delegatedBalance > 0) {
174
throw new RecommendUndelegation(
175
`Account has ${account.delegatedBalance} delegated. Undelegate first.`
176
);
177
}
178
179
// Check sub-accounts
180
checkSubAccounts(account.subAccounts);
181
182
// Proceed with account closure
183
performAccountClosure(account);
184
}
185
```
186
187
### Tezos-Specific Account Errors
188
189
Errors specific to Tezos originated accounts and their limitations.
190
191
```typescript { .api }
192
const UnavailableTezosOriginatedAccountReceive: CustomErrorFunc;
193
const UnavailableTezosOriginatedAccountSend: CustomErrorFunc;
194
```
195
196
**Usage Examples:**
197
198
```typescript
199
import {
200
UnavailableTezosOriginatedAccountReceive,
201
UnavailableTezosOriginatedAccountSend
202
} from "@ledgerhq/errors";
203
204
// Handle Tezos originated account receive limitations
205
function validateTezosReceive(account: TezosAccount) {
206
if (account.type === "originated" && !account.canReceive) {
207
throw new UnavailableTezosOriginatedAccountReceive(
208
"This Tezos originated account cannot receive funds"
209
);
210
}
211
}
212
213
// Handle Tezos originated account send limitations
214
function validateTezosSend(account: TezosAccount) {
215
if (account.type === "originated" && !account.canSend) {
216
throw new UnavailableTezosOriginatedAccountSend(
217
"This Tezos originated account cannot send funds"
218
);
219
}
220
}
221
222
// Example usage in transaction validation
223
function validateTezosTransaction(fromAccount: TezosAccount, toAccount: TezosAccount) {
224
validateTezosSend(fromAccount);
225
validateTezosReceive(toAccount);
226
227
console.log("Tezos transaction validation passed");
228
}
229
```