forked from Prompt-Hash-Stellar/prompt-hash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetworkDetection.ts
More file actions
104 lines (87 loc) · 3.08 KB
/
Copy pathnetworkDetection.ts
File metadata and controls
104 lines (87 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { stellarWalletNetwork } from "@/lib/env";
export type NetworkMismatchType =
| "correct"
| "wrong-network"
| "unavailable"
| "disconnected";
export interface NetworkMismatchState {
type: NetworkMismatchType;
message?: string;
recoveryInstructions?: string;
}
/**
* Detects wallet/network mismatch states and provides user-friendly recovery instructions
*/
export function detectNetworkMismatch(
walletConnected: boolean,
walletNetwork?: string,
walletStatus?: string,
): NetworkMismatchState {
// User hasn't connected wallet yet
if (!walletConnected || walletStatus === "idle" || walletStatus === "error") {
return {
type: "disconnected",
message: "Wallet not connected",
recoveryInstructions: "Connect your Stellar wallet to continue",
};
}
// Wallet is connecting or reconnecting
if (walletStatus === "connecting" || walletStatus === "reconnecting") {
return {
type: "unavailable",
message: "Connecting to wallet...",
recoveryInstructions: "Please wait while we establish connection",
};
}
// Wallet network is unavailable (some wallets don't expose network info)
if (!walletNetwork) {
return {
type: "correct",
message: "Network verification unavailable",
};
}
// Check if wallet network matches configured app network
const expectedNetwork = stellarWalletNetwork.toUpperCase();
const actualNetwork = walletNetwork.toUpperCase();
if (actualNetwork !== expectedNetwork) {
return {
type: "wrong-network",
message: `Wrong network: Connected to ${actualNetwork}`,
recoveryInstructions: `Please switch your wallet to ${expectedNetwork} network and reconnect`,
};
}
return {
type: "correct",
};
}
/**
* Maps common technical errors to user-facing messages
*/
export function mapTechnicalError(error: Error | string): string {
const errorMessage = typeof error === "string" ? error : error.message;
const lowerError = errorMessage.toLowerCase();
// Common Stellar/Soroban errors
if (lowerError.includes("op_underfunded")) {
return "Insufficient XLM balance. Please add funds to your wallet.";
}
if (lowerError.includes("op_no_trust")) {
return "Asset trustline not established. Please add the asset to your wallet.";
}
if (lowerError.includes("tx_bad_auth")) {
return "Transaction authorization failed. Please check your wallet signature.";
}
if (lowerError.includes("tx_failed") || lowerError.includes("transaction failed")) {
return "Transaction failed on the network. Please try again.";
}
if (lowerError.includes("timeout") || lowerError.includes("timed out")) {
return "Network request timed out. Please check your connection and try again.";
}
if (lowerError.includes("user rejected") || lowerError.includes("user denied")) {
return "Transaction was rejected in your wallet.";
}
if (lowerError.includes("network") || lowerError.includes("connection")) {
return "Network connection issue. Please check your internet and try again.";
}
// Return original message if no mapping found
return errorMessage;
}