1
- import type { SwapActionTypes , SwapQueueContext , SwapStorage } from '../types' ;
2
- import type { ExecuterActions } from '@rango-dev/queue-manager-core' ;
1
+ import type {
2
+ SwapActionTypes ,
3
+ SwapQueueContext ,
4
+ SwapStorage ,
5
+ } from '../../types' ;
6
+ import type {
7
+ BlockedReason ,
8
+ ExecuterActions ,
9
+ } from '@rango-dev/queue-manager-core' ;
3
10
4
11
import { PendingSwapNetworkStatus } from 'rango-types' ;
12
+ import { Err , Ok , type Result } from 'ts-results' ;
5
13
6
14
import {
7
15
ERROR_MESSAGE_DEPENDS_ON_OTHER_QUEUES ,
8
16
ERROR_MESSAGE_WAIT_FOR_CHANGE_NETWORK ,
9
17
ERROR_MESSAGE_WAIT_FOR_WALLET_DESCRIPTION ,
10
18
ERROR_MESSAGE_WAIT_FOR_WALLET_DESCRIPTION_WRONG_WALLET ,
11
- } from '../constants' ;
19
+ } from '../../ constants' ;
12
20
import {
13
21
claimQueue ,
14
22
getCurrentStep ,
@@ -18,44 +26,72 @@ import {
18
26
isRequiredWalletConnected ,
19
27
isWalletNull ,
20
28
resetNetworkStatus ,
21
- signTransaction ,
22
29
updateNetworkStatus ,
23
- } from '../helpers' ;
24
- import { getCurrentNamespaceOf } from '../shared' ;
25
- import { BlockReason } from '../types' ;
30
+ } from '../../helpers' ;
31
+ import { getCurrentNamespaceOf } from '../../shared' ;
32
+ import { BlockReason } from '../../types' ;
33
+
34
+ import { isClaimedByCurrentQueue } from './utils' ;
26
35
27
36
/**
28
- * Excecute a created transaction.
29
- *
30
- * This function implemented the parallel mode by `claim` mechanism which means
31
- * All the queues the meet certain situation (like multiple evm transaction) will go through
32
- * a `claim` mechanims that decides which queue should be run and it blocks other ones.
33
- *
34
- * A queue will be go to sign process, if the wallet and network is matched.
37
+ * Check for network & address be matched and queue to not be blocked and update the swap accordingly.
35
38
*/
36
- export async function executeTransaction (
39
+ export async function checkEnvironmentBeforeExecuteTransaction (
37
40
actions : ExecuterActions < SwapStorage , SwapActionTypes , SwapQueueContext >
38
- ) : Promise < void > {
41
+ ) : Promise < Result < true , BlockedReason > > {
39
42
const { getStorage, context } = actions ;
40
- const { meta, wallets, providers } = context ;
41
- const { claimedBy } = claimQueue ( ) ;
42
-
43
- const isClaimed = context . claimedBy === context . _queue ?. id ;
44
- const requestBlock : typeof actions . block = ( blockedFor ) => {
45
- actions . block ( blockedFor ) ;
46
- if ( isClaimed && actions . context . resetClaimedBy ) {
47
- actions . context . resetClaimedBy ( ) ;
48
- }
49
- } ;
50
43
51
44
const swap = getStorage ( ) . swapDetails ;
52
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
45
+
53
46
const currentStep = getCurrentStep ( swap ) ! ;
54
47
55
48
// Resetting network status, so we will set it again during the running of this task.
56
49
resetNetworkStatus ( actions ) ;
57
50
58
51
/* Make sure wallet is connected and also the connected wallet is matched with tx by checking address. */
52
+ const addressCheckResult = ensureRequiredWalletIsConnected ( actions ) ;
53
+ if ( addressCheckResult . err ) {
54
+ return addressCheckResult ;
55
+ }
56
+
57
+ /* Wallet should be on correct network */
58
+ const networkResult = await ensureWalletIsOnCorrectNetwork ( actions ) ;
59
+ if ( networkResult . err ) {
60
+ return networkResult ;
61
+ }
62
+
63
+ // Update network to mark it as network changed successfully.
64
+ updateNetworkStatus ( actions , {
65
+ message : '' ,
66
+ details : 'Wallet network changed successfully' ,
67
+ status : PendingSwapNetworkStatus . NetworkChanged ,
68
+ } ) ;
69
+
70
+ /*
71
+ *For avoiding conflict by making too many requests to wallet, we need to make sure
72
+ *We only run one request at a time (In parallel mode).
73
+ */
74
+ const needsToBlockQueue = isNeedBlockQueueForParallel ( currentStep ) ;
75
+ const isClaimed = isClaimedByCurrentQueue ( context ) ;
76
+ if ( needsToBlockQueue && ! isClaimed ) {
77
+ const blockedFor = {
78
+ reason : BlockReason . DEPENDS_ON_OTHER_QUEUES ,
79
+ description : ERROR_MESSAGE_DEPENDS_ON_OTHER_QUEUES ,
80
+ details : { } ,
81
+ } ;
82
+ return new Err ( blockedFor ) ;
83
+ }
84
+
85
+ return new Ok ( true ) ;
86
+ }
87
+
88
+ function ensureRequiredWalletIsConnected (
89
+ actions : ExecuterActions < SwapStorage , SwapActionTypes , SwapQueueContext >
90
+ ) : Result < true , BlockedReason > {
91
+ const { getStorage, context } = actions ;
92
+ const { wallets } = context ;
93
+ const swap = getStorage ( ) . swapDetails ;
94
+
59
95
const isWrongAddress = ! isRequiredWalletConnected ( swap , context . state ) . ok ;
60
96
if ( isWrongAddress ) {
61
97
const { type, address } = getRequiredWallet ( swap ) ;
@@ -71,29 +107,40 @@ export async function executeTransaction(
71
107
reason : BlockReason . WAIT_FOR_CONNECT_WALLET ,
72
108
description,
73
109
} ;
74
- requestBlock ( blockedFor ) ;
75
- return ;
110
+ return new Err ( blockedFor ) ;
76
111
}
77
112
78
- /* Wallet should be on correct network */
113
+ return new Ok ( true ) ;
114
+ }
115
+
116
+ async function ensureWalletIsOnCorrectNetwork (
117
+ actions : ExecuterActions < SwapStorage , SwapActionTypes , SwapQueueContext >
118
+ ) : Promise < Result < true , BlockedReason > > {
119
+ const { getStorage, context } = actions ;
120
+ const { meta, wallets, providers } = context ;
121
+ const swap = getStorage ( ) . swapDetails ;
122
+ const currentStep = getCurrentStep ( swap ) ! ;
123
+
79
124
const networkMatched = await isNetworkMatchedForTransaction (
80
125
swap ,
81
126
currentStep ,
82
127
wallets ,
83
128
meta ,
84
129
providers
85
130
) ;
131
+
132
+ const { claimedBy } = claimQueue ( ) ;
86
133
const claimerId = claimedBy ( ) ;
87
- const isClaimedByAnyQueue = ! ! claimerId && ! isClaimed ;
134
+ const isClaimedByAnyQueue = ! ! claimerId && ! isClaimedByCurrentQueue ( context ) ;
135
+
88
136
if ( isClaimedByAnyQueue && ! networkMatched ) {
89
137
const details = ERROR_MESSAGE_DEPENDS_ON_OTHER_QUEUES ;
90
138
91
139
const blockedFor = {
92
140
reason : BlockReason . DEPENDS_ON_OTHER_QUEUES ,
93
141
details : details ,
94
142
} ;
95
- requestBlock ( blockedFor ) ;
96
- return ;
143
+ return new Err ( blockedFor ) ;
97
144
} else if ( ! networkMatched ) {
98
145
const fromNamespace = getCurrentNamespaceOf ( swap , currentStep ) ;
99
146
const details = ERROR_MESSAGE_WAIT_FOR_CHANGE_NETWORK (
@@ -104,32 +151,8 @@ export async function executeTransaction(
104
151
reason : BlockReason . WAIT_FOR_NETWORK_CHANGE ,
105
152
details : details ,
106
153
} ;
107
- requestBlock ( blockedFor ) ;
108
- return ;
109
- }
110
- // Update network to mark it as network changed successfully.
111
- updateNetworkStatus ( actions , {
112
- message : '' ,
113
- details : 'Wallet network changed successfully' ,
114
- status : PendingSwapNetworkStatus . NetworkChanged ,
115
- } ) ;
116
-
117
- /*
118
- *For avoiding conflict by making too many requests to wallet, we need to make sure
119
- *We only run one request at a time (In parallel mode).
120
- */
121
- const needsToBlockQueue = isNeedBlockQueueForParallel ( currentStep ) ;
122
-
123
- if ( needsToBlockQueue && ! isClaimed ) {
124
- const blockedFor = {
125
- reason : BlockReason . DEPENDS_ON_OTHER_QUEUES ,
126
- description : ERROR_MESSAGE_DEPENDS_ON_OTHER_QUEUES ,
127
- details : { } ,
128
- } ;
129
- requestBlock ( blockedFor ) ;
130
- return ;
154
+ return new Err ( blockedFor ) ;
131
155
}
132
156
133
- // All the conditions are met. We can safely send the tx to wallet for sign.
134
- await signTransaction ( actions ) ;
157
+ return new Ok ( true ) ;
135
158
}
0 commit comments