Skip to content

Commit d8b1019

Browse files
nooxxloicttn
authored andcommitted
ada: custom errors
1 parent fd6e2ce commit d8b1019

File tree

2 files changed

+57
-10
lines changed

2 files changed

+57
-10
lines changed

src/errors/ada.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
export class CouldNotFetchStakeAddress extends Error {
2+
constructor(message: string) {
3+
super(message);
4+
this.name = 'CouldNotFetchStakeAddress';
5+
}
6+
}
7+
8+
export class CouldNotHashStakeKey extends Error {
9+
constructor(message: string) {
10+
super(message);
11+
this.name = 'CouldNotHashStakeKey';
12+
}
13+
}
14+
15+
export class CouldNotFetchSlot extends Error {
16+
constructor(message: string) {
17+
super(message);
18+
this.name = 'CouldNotFetchSlot';
19+
}
20+
}
21+
22+
export class NotEnoughFunds extends Error {
23+
constructor(message: string) {
24+
super(message);
25+
this.name = 'NotEnoughFunds';
26+
}
27+
}
28+
29+
export class NoStakeAddressFound extends Error {
30+
constructor(message: string) {
31+
super(message);
32+
this.name = 'NoStakeAddressFound';
33+
}
34+
}
35+
36+
export class NoRewardAddressFound extends Error {
37+
constructor(message: string) {
38+
super(message);
39+
this.name = 'NoRewardAddressFound';
40+
}
41+
}

src/services/ada.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ import {
3535
} from "@blockfrost/blockfrost-js";
3636
import { InvalidIntegration, InvalidSignature } from "../errors/integrations";
3737
import { ADDRESSES } from "../globals";
38+
import {
39+
CouldNotFetchSlot,
40+
CouldNotFetchStakeAddress,
41+
CouldNotHashStakeKey, NoRewardAddressFound,
42+
NoStakeAddressFound, NotEnoughFunds,
43+
} from "../errors/ada";
3844

3945
const CARDANO_PARAMS = {
4046
COINS_PER_UTXO_WORD: '34482',
@@ -75,12 +81,12 @@ export class AdaService extends Service {
7581
const utxos = await this.getUtxos(walletAddress);
7682
const address = await this.client.addresses(walletAddress);
7783
if (!address.stake_address) {
78-
throw Error('Could not fetch stake address');
84+
throw new CouldNotFetchStakeAddress('Could not fetch stake address');
7985
}
8086

8187
const stakeKeyHash = await this.getStakeKeyHash(address.stake_address);
8288
if (!stakeKeyHash) {
83-
throw Error('Could not hash stake key');
89+
throw new CouldNotHashStakeKey('Could not hash stake key');
8490
}
8591
const certificates = Certificates.new();
8692

@@ -136,19 +142,19 @@ export class AdaService extends Service {
136142
const utxos = await this.getUtxos(walletAddress);
137143
const address = await this.client.addresses(walletAddress);
138144
if (!address.stake_address) {
139-
throw Error('No stake address');
145+
throw new NoStakeAddressFound('No stake address');
140146
}
141147

142148
const stakeKeyHash = await this.getStakeKeyHash(address.stake_address);
143149
if (!stakeKeyHash) {
144-
throw Error('Could not hash stake key');
150+
throw new CouldNotHashStakeKey('Could not hash stake key');
145151
}
146152

147153
const withdrawals = Withdrawals.new();
148154
const rewardAddress = RewardAddress.from_address(Address.from_bech32(address.stake_address));
149155

150156
if (!rewardAddress) {
151-
throw Error('Could not retrieve rewards address');
157+
throw new NoRewardAddressFound('Could not retrieve rewards address');
152158
}
153159

154160
const availableRewards = await this.getAvailableRewards(address.stake_address);
@@ -178,19 +184,19 @@ export class AdaService extends Service {
178184
const utxos = await this.getUtxos(walletAddress);
179185
const address = await this.client.addresses(walletAddress);
180186
if (!address.stake_address) {
181-
throw Error('No stake address');
187+
throw new NoStakeAddressFound('No stake address');
182188
}
183189

184190
const stakeKeyHash = await this.getStakeKeyHash(address.stake_address);
185191
if (!stakeKeyHash) {
186-
throw Error('Could not hash stake key');
192+
throw new CouldNotHashStakeKey('Could not hash stake key');
187193
}
188194

189195
const withdrawals = Withdrawals.new();
190196
const rewardAddress = RewardAddress.from_address(Address.from_bech32(address.stake_address));
191197

192198
if (!rewardAddress) {
193-
throw Error('Could not retrieve rewards address');
199+
throw new NoRewardAddressFound('Could not retrieve rewards address');
194200
}
195201

196202

@@ -306,7 +312,7 @@ export class AdaService extends Service {
306312
const latestBlock = await this.client.blocksLatest();
307313
const currentSlot = latestBlock.slot;
308314
if (!currentSlot) {
309-
throw Error('Failed to fetch slot number');
315+
throw new CouldNotFetchSlot('Failed to fetch slot number');
310316
}
311317
// Current slot + 2h
312318
const ttl = currentSlot + 7200;
@@ -332,7 +338,7 @@ export class AdaService extends Service {
332338
utxo = await this.client.addressesUtxos(walletAddress);
333339
} catch (error) {
334340
if (error instanceof BlockfrostServerError && error.status_code === 404) {
335-
throw new Error(`You should send ADA to ${walletAddress} to have enough funds to sent a transaction`);
341+
throw new NotEnoughFunds(`You should send ADA to ${walletAddress} to have enough funds to sent a transaction`);
336342
} else {
337343
throw error;
338344
}

0 commit comments

Comments
 (0)