-
Notifications
You must be signed in to change notification settings - Fork 415
/
tx-event-toast.ts
86 lines (77 loc) · 2.38 KB
/
tx-event-toast.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
import { ChainInfoInner } from "@osmosis-labs/keplr-stores";
import { DeliverTxResponse, isSlippageError } from "@osmosis-labs/stores";
import type { AppCurrency, ChainInfoWithExplorer } from "@osmosis-labs/types";
import { displayToast } from "~/components/alert/toast";
import { ToastType } from "~/components/alert/types";
import { prettifyTxError } from "./prettify";
// Error code for timeout height reached in Cosmos SDK.
// https://github.com/cosmos/cosmos-sdk/blob/8f6a94cd1f9f1c6bf1ad83a751da86270db92e02/types/errors/errors.go#L129
const txTimeoutHeightReachedErrorCode = 30;
export function toastOnBroadcastFailed(
getChain: (chainId: string) => ChainInfoInner<ChainInfoWithExplorer>
) {
return (chainId: string, e?: Error) => {
let caption: string = "unknownError";
if (typeof e === "string") {
caption = e;
} else if (e instanceof Error || (e && "message" in e)) {
caption = e.message;
}
displayToast(
{
message: "transactionFailed",
caption:
prettifyTxError(caption, getChain(chainId).currencies) ?? caption,
},
ToastType.ERROR
);
};
}
export function toastOnBroadcast() {
return () => {
displayToast(
{
message: "transactionBroadcasting",
caption: "waitingForTransaction",
},
ToastType.LOADING
);
};
}
export function toastOnFulfill(
getChain: (chainId: string) => ChainInfoInner<ChainInfoWithExplorer>
) {
return (chainId: string, tx: DeliverTxResponse) => {
const chainInfo = getChain(chainId);
if (tx.code) {
displayToast(
{
message: "transactionFailed",
caption: getErrorMessage(tx, chainInfo.currencies),
},
ToastType.ERROR
);
} else {
displayToast(
{
message: "transactionSuccessful",
learnMoreUrl: chainInfo.raw.explorerUrlToTx.replace(
"{txHash}",
tx.transactionHash.toUpperCase()
),
learnMoreUrlCaption: "viewExplorer",
},
ToastType.SUCCESS
);
}
};
}
// gets the error message depending on the transaction.
const getErrorMessage = (tx: DeliverTxResponse, currencies: AppCurrency[]) => {
if (tx.code === txTimeoutHeightReachedErrorCode) {
return "errors.txTimedOutError";
}
return isSlippageError(tx)
? "swapFailed"
: prettifyTxError(tx.rawLog ?? "", currencies) ?? tx.rawLog;
};