@@ -36,56 +36,83 @@ export const onTransaction: OnTransactionHandler = async ({
36
36
transaction,
37
37
chainId,
38
38
} ) => {
39
- return renderTransactionUi (
40
- convertCAIP2ToHex ( chainId ) ,
41
- transaction . from ,
42
- transaction . to ,
43
- ) ;
39
+ try {
40
+ return renderTransactionUi (
41
+ convertCAIP2ToHex ( chainId ) ,
42
+ transaction . from ,
43
+ transaction . to ,
44
+ ) ;
45
+ } catch ( error ) {
46
+ const message = error instanceof Error ? error . message : 'Unknown error' ;
47
+
48
+ return { content : renderMainUiWithError ( message ) } ;
49
+ }
44
50
} ;
45
51
46
52
export const onUserInput : OnUserInputHandler = async ( { id, event } ) => {
47
- if ( event . name === 'calculate-score' ) {
48
- await snap . request ( {
49
- method : 'snap_updateInterface' ,
50
- params : {
51
- id,
52
- ui : renderMainUiWithLoading ( ) ,
53
- } ,
54
- } ) ;
55
-
56
- const [ account , chainId ] = await Promise . all ( [ getAccount ( ) , getChainId ( ) ] ) ;
57
-
58
- try {
59
- const { score, scoreName, url } = await calculateScore ( chainId , account ) ;
60
-
53
+ try {
54
+ if ( event . name === 'calculate-score' ) {
61
55
await snap . request ( {
62
56
method : 'snap_updateInterface' ,
63
57
params : {
64
58
id,
65
- ui : renderMainUiWithScore ( score , scoreName , url ) ,
59
+ ui : renderMainUiWithLoading ( ) ,
66
60
} ,
67
61
} ) ;
68
- } catch {
62
+
63
+ const [ account , chainId ] = await Promise . all ( [
64
+ getAccount ( ) ,
65
+ getChainId ( ) ,
66
+ ] ) ;
67
+
68
+ try {
69
+ const { score, scoreName, url } = await calculateScore (
70
+ chainId ,
71
+ account ,
72
+ ) ;
73
+
74
+ await snap . request ( {
75
+ method : 'snap_updateInterface' ,
76
+ params : {
77
+ id,
78
+ ui : renderMainUiWithScore ( score , scoreName , url ) ,
79
+ } ,
80
+ } ) ;
81
+ } catch {
82
+ await snap . request ( {
83
+ method : 'snap_updateInterface' ,
84
+ params : {
85
+ id,
86
+ ui : renderMainUiWithError ( 'Failed to calculate score' ) ,
87
+ } ,
88
+ } ) ;
89
+ }
90
+ }
91
+
92
+ if ( event . name === 'back' ) {
93
+ const [ account , chainId ] = await Promise . all ( [
94
+ getAccount ( ) ,
95
+ getChainId ( ) ,
96
+ ] ) ;
97
+
98
+ const ui = await renderMainUi ( account , chainId ) ;
99
+
69
100
await snap . request ( {
70
101
method : 'snap_updateInterface' ,
71
102
params : {
72
103
id,
73
- ui : renderMainUiWithError ( ) ,
104
+ ui,
74
105
} ,
75
106
} ) ;
76
107
}
77
- }
78
-
79
- if ( event . name === 'back' ) {
80
- const [ account , chainId ] = await Promise . all ( [ getAccount ( ) , getChainId ( ) ] ) ;
81
-
82
- const ui = await renderMainUi ( account , chainId ) ;
108
+ } catch ( error ) {
109
+ const message = error instanceof Error ? error . message : 'Unknown error' ;
83
110
84
111
await snap . request ( {
85
112
method : 'snap_updateInterface' ,
86
113
params : {
87
114
id,
88
- ui,
115
+ ui : renderMainUiWithError ( message ) ,
89
116
} ,
90
117
} ) ;
91
118
}
0 commit comments