This repository has been archived by the owner on Sep 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
useTx.ts
240 lines (214 loc) · 6 KB
/
useTx.ts
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import { useCallback } from "react";
import { Fee, MsgExecuteContract } from "@terra-money/terra.js";
import {
UserDenied,
CreateTxFailed,
TxFailed,
Timeout,
TxUnspecifiedError,
TxResult,
} from "@terra-money/wallet-types";
import { useAstroswap, useTokenInfo } from "modules/common";
import { useWallet } from "@terra-money/wallet-provider";
export enum TxPostError {
UserDenied,
CreateTxFailed,
TxFailed,
Timeout,
TxUnspecifiedError,
UnknownError,
InsufficientFunds,
InsufficientFee,
}
export type TxErrorHandler = (
errorType: TxPostError,
originalError: Error
) => void;
export type UseTxNotificationDetails =
| {
type: "swap" | "provide" | "withdraw" | "createPool";
data: {
token1: string | null;
token2: string | null;
};
}
| {
type: "unstakeLp" | "lockdropUnlockLp";
data: {
token: string;
};
}
| {
type: "govVote";
data: {
proposal_id: string;
action: string;
};
}
| {
type:
| "govStake"
| "govUnstake"
| "auctionUnlockLp"
| "stakeLp"
| "claimRewards"
| "createProposal";
};
type Params = {
onPosting?: () => void;
onBroadcasting?: (txHash: string) => void;
// originalErrors: https://github.com/terra-money/wallet-provider/blob/v3.7.1/packages/src/%40terra-dev/wallet-types/errors.ts
onError?: TxErrorHandler;
notification: UseTxNotificationDetails;
};
const enumForTxPostError = (error: Error) => {
if (error instanceof UserDenied) {
return TxPostError.UserDenied;
} else if (error instanceof CreateTxFailed) {
if (/^timeout of \d+ms exceeded$/.test(error.message)) {
// Treat CreateTxFailed for timeouts as Timeout errors
return TxPostError.Timeout;
} else if (error.message.endsWith(": insufficient funds")) {
return TxPostError.InsufficientFunds;
} else if (error.message.endsWith(": insufficient fee")) {
return TxPostError.InsufficientFee;
} else {
return TxPostError.CreateTxFailed;
}
} else if (error instanceof TxFailed) {
return TxPostError.TxFailed;
} else if (error instanceof Timeout) {
return TxPostError.Timeout;
} else if (error instanceof TxUnspecifiedError) {
return TxPostError.TxUnspecifiedError;
} else {
return TxPostError.UnknownError;
}
};
export const useTx = ({
onPosting,
onBroadcasting,
onError,
notification,
}: Params) => {
const { post } = useWallet();
const { addNotification } = useAstroswap();
const { getSymbol } = useTokenInfo();
const errorNotificationTitle = (
notification: UseTxNotificationDetails
): string => {
switch (notification.type) {
case "swap": {
const { token1, token2 } = notification.data;
return `Swap from ${getSymbol(token1 || "")} to ${getSymbol(
token2 || ""
)} failed`;
}
case "claimRewards":
return "Failed to claim rewards";
case "auctionUnlockLp":
return "Failed to unlock LP token";
case "stakeLp":
return "Stake LP tokens failed";
case "unstakeLp":
return "Unstake LP tokens failed";
case "provide": {
const { token1, token2 } = notification.data;
return `Provide liquidity for ${getSymbol(
token1 || ""
)} and ${getSymbol(token2 || "")} failed`;
}
case "withdraw": {
const { token1, token2 } = notification.data;
return `Withdraw liquidity for ${getSymbol(
token1 || ""
)} and ${getSymbol(token2 || "")} failed`;
}
case "createPool": {
const { token1, token2 } = notification.data;
return `Failed to create pool ${getSymbol(token1 || "")} - ${getSymbol(
token2 || ""
)}`;
}
case "createProposal":
return "Failed to submit an Assembly proposal";
case "govVote": {
const { proposal_id } = notification.data;
return `Failed to vote on proposal ${proposal_id}`;
}
}
return "Failed";
};
const errorNotificationDescription = (
errorEnum: TxPostError,
originalError: Error
) => {
switch (errorEnum) {
case TxPostError.Timeout:
return "Timed out. Please try again.";
case TxPostError.InsufficientFunds:
return "We're sorry, you don't have enough funds to complete this request. Please try again when you have more funds available.";
case TxPostError.InsufficientFee:
return "Sorry, the specified fee was not enough to cover the cost of this transaction. Please try again.";
case TxPostError.CreateTxFailed:
return originalError.message;
default:
return "There was an unexpected error.";
}
};
const addErrorNotification = (
errorEnum: TxPostError,
originalError: Error
) => {
// Ignore UserDenied errors
if (errorEnum !== TxPostError.UserDenied) {
addNotification({
notification: {
type: "error",
title: errorNotificationTitle(notification),
description: errorNotificationDescription(errorEnum, originalError),
},
});
}
};
const submit = useCallback(
async ({
msgs,
fee,
}: {
msgs: MsgExecuteContract[];
fee?: Fee | undefined;
}) => {
if (fee == null || msgs == null || msgs.length < 1) {
return;
}
onPosting?.();
try {
const res: TxResult = await post({
msgs,
fee,
// @ts-ignore
isClassic: true,
});
onBroadcasting?.(res.result.txhash);
addNotification({
notification: {
type: "started",
txHash: res.result.txhash,
txType: notification.type,
// @ts-ignore
data: notification.data,
},
});
} catch (e: any) {
const errorEnum = enumForTxPostError(e);
addErrorNotification(errorEnum, e);
onError?.(errorEnum, e);
}
},
[onPosting, onBroadcasting, onError]
);
return {
submit,
};
};