Skip to content

Commit 5ce253a

Browse files
committed
chore: refactor executeTransaction for rango-preset
1 parent 30eb4c8 commit 5ce253a

File tree

8 files changed

+449
-239
lines changed

8 files changed

+449
-239
lines changed

queue-manager/core/src/manager.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ import { Status, SYNC_POLLING_INTERVAL } from './types';
1010
export type ManagerContext = object;
1111
export type QueueName = string;
1212
export type QueueID = string;
13+
14+
export type BlockedReason = Record<string, unknown>;
1315
export type BlockedTask = {
1416
queue_id: string;
1517
task_id: string;
1618
action: string;
17-
reason: Record<string, unknown>;
19+
reason: BlockedReason;
1820
storage: {
1921
get: () => QueueStorage;
2022
set: (data: QueueStorage) => QueueStorage;
@@ -34,11 +36,13 @@ export interface ExecuterActions<
3436
schedule: (actionName: V) => void;
3537
setStorage: SetStorage<T>;
3638
getStorage: () => T;
37-
block: (reason: Record<string, unknown>) => void;
39+
block: (reason: BlockedReason) => void;
3840
unblock: () => void;
3941
context: C;
4042
}
4143

44+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
45+
type WhenTaskBlockedEvent = any;
4246
export interface QueueDef<
4347
T extends QueueStorage = QueueStorage,
4448
V extends string = string,
@@ -51,7 +55,7 @@ export interface QueueDef<
5155
events?: Partial<QueueEventHandlers>;
5256
run: V[];
5357
whenTaskBlocked?: (
54-
event: any,
58+
event: WhenTaskBlockedEvent,
5559
params: {
5660
queue_id: string;
5761
queue: Queue;
@@ -82,6 +86,8 @@ interface ManagerOptions {
8286
isPaused?: boolean;
8387
}
8488

89+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
90+
type _Storage = any;
8591
export interface QueueInfo {
8692
name: QueueName;
8793
createdAt: number;
@@ -90,8 +96,8 @@ export interface QueueInfo {
9096
actions: {
9197
run: () => void;
9298
cancel: () => void;
93-
setStorage: SetStorage<any>;
94-
getStorage: () => any;
99+
setStorage: SetStorage<_Storage>;
100+
getStorage: () => _Storage;
95101
};
96102
}
97103

@@ -240,6 +246,7 @@ class Manager {
240246
this.execute();
241247
return queue_id;
242248
} catch (e) {
249+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
243250
throw new Error((e as any)?.message);
244251
}
245252
}
@@ -643,7 +650,7 @@ class Manager {
643650
}
644651

645652
private getContext() {
646-
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/ban-ts-comment
653+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
647654
//@ts-ignore
648655
return this.context?.current || {};
649656
}

queue-manager/rango-preset/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
"dependencies": {
3737
"@rango-dev/logging-core": "^0.9.1-next.0",
3838
"rango-types": "^0.1.88",
39-
"uuid": "^9.0.0"
39+
"uuid": "^9.0.0",
40+
"ts-results": "^3.3.0"
4041
},
4142
"publishConfig": {
4243
"access": "public"
Lines changed: 82 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
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';
310

411
import { PendingSwapNetworkStatus } from 'rango-types';
12+
import { Err, Ok, type Result } from 'ts-results';
513

614
import {
715
ERROR_MESSAGE_DEPENDS_ON_OTHER_QUEUES,
816
ERROR_MESSAGE_WAIT_FOR_CHANGE_NETWORK,
917
ERROR_MESSAGE_WAIT_FOR_WALLET_DESCRIPTION,
1018
ERROR_MESSAGE_WAIT_FOR_WALLET_DESCRIPTION_WRONG_WALLET,
11-
} from '../constants';
19+
} from '../../constants';
1220
import {
1321
claimQueue,
1422
getCurrentStep,
@@ -18,44 +26,72 @@ import {
1826
isRequiredWalletConnected,
1927
isWalletNull,
2028
resetNetworkStatus,
21-
signTransaction,
2229
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';
2635

2736
/**
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.
3538
*/
36-
export async function executeTransaction(
39+
export async function checkEnvironmentBeforeExecuteTransaction(
3740
actions: ExecuterActions<SwapStorage, SwapActionTypes, SwapQueueContext>
38-
): Promise<void> {
41+
): Promise<Result<true, BlockedReason>> {
3942
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-
};
5043

5144
const swap = getStorage().swapDetails;
52-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
45+
5346
const currentStep = getCurrentStep(swap)!;
5447

5548
// Resetting network status, so we will set it again during the running of this task.
5649
resetNetworkStatus(actions);
5750

5851
/* 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+
5995
const isWrongAddress = !isRequiredWalletConnected(swap, context.state).ok;
6096
if (isWrongAddress) {
6197
const { type, address } = getRequiredWallet(swap);
@@ -71,29 +107,40 @@ export async function executeTransaction(
71107
reason: BlockReason.WAIT_FOR_CONNECT_WALLET,
72108
description,
73109
};
74-
requestBlock(blockedFor);
75-
return;
110+
return new Err(blockedFor);
76111
}
77112

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+
79124
const networkMatched = await isNetworkMatchedForTransaction(
80125
swap,
81126
currentStep,
82127
wallets,
83128
meta,
84129
providers
85130
);
131+
132+
const { claimedBy } = claimQueue();
86133
const claimerId = claimedBy();
87-
const isClaimedByAnyQueue = !!claimerId && !isClaimed;
134+
const isClaimedByAnyQueue = !!claimerId && !isClaimedByCurrentQueue(context);
135+
88136
if (isClaimedByAnyQueue && !networkMatched) {
89137
const details = ERROR_MESSAGE_DEPENDS_ON_OTHER_QUEUES;
90138

91139
const blockedFor = {
92140
reason: BlockReason.DEPENDS_ON_OTHER_QUEUES,
93141
details: details,
94142
};
95-
requestBlock(blockedFor);
96-
return;
143+
return new Err(blockedFor);
97144
} else if (!networkMatched) {
98145
const fromNamespace = getCurrentNamespaceOf(swap, currentStep);
99146
const details = ERROR_MESSAGE_WAIT_FOR_CHANGE_NETWORK(
@@ -104,32 +151,8 @@ export async function executeTransaction(
104151
reason: BlockReason.WAIT_FOR_NETWORK_CHANGE,
105152
details: details,
106153
};
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);
131155
}
132156

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);
135158
}

0 commit comments

Comments
 (0)