-
Notifications
You must be signed in to change notification settings - Fork 16
/
Router.ts
195 lines (159 loc) · 6 KB
/
Router.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
import Actions from '../Actions';
import Config from '../Config';
import { TxStatus, ClaimStatus, ExecuteStatus } from '../Enum';
import { shouldClaimTx, shouldExecuteTx } from '../EconomicStrategy';
import W3Util from '../Util';
import { ITxRequest } from '../Types';
export default class Router {
public actions: Actions;
public config: Config;
public util: W3Util;
public txRequestStates: object = {};
public transitions: object = {};
constructor(config: Config, actions: any) {
this.actions = actions;
this.config = config;
this.util = config.util;
this.transitions[TxStatus.BeforeClaimWindow] = this.beforeClaimWindow.bind(this);
this.transitions[TxStatus.ClaimWindow] = this.claimWindow.bind(this);
this.transitions[TxStatus.FreezePeriod] = this.freezePeriod.bind(this);
this.transitions[TxStatus.ExecutionWindow] = this.executionWindow.bind(this);
this.transitions[TxStatus.Executed] = this.executed.bind(this);
this.transitions[TxStatus.Missed] = this.missed.bind(this);
this.transitions[TxStatus.Done] = (txRequest: any) => {
this.config.logger.info(`[${txRequest.address}] Finished. Deleting from cache...`);
this.config.cache.del(txRequest.address);
return TxStatus.Done;
};
}
public async beforeClaimWindow(txRequest: any): Promise<TxStatus> {
if (txRequest.isCancelled) {
// TODO Status.CleanUp?
return TxStatus.Executed;
}
if (await txRequest.beforeClaimWindow()) {
return TxStatus.BeforeClaimWindow;
}
return TxStatus.ClaimWindow;
}
public async claimWindow(txRequest: any): Promise<TxStatus> {
if (!(await txRequest.inClaimWindow())) {
return TxStatus.FreezePeriod;
}
if (txRequest.isClaimed) {
return TxStatus.FreezePeriod;
}
if (this.config.claiming) {
const shouldClaim = await shouldClaimTx(txRequest, this.config);
if (shouldClaim) {
try {
const claimingStatus: ClaimStatus = await this.actions.claim(txRequest);
if (claimingStatus === ClaimStatus.SUCCESS) {
this.config.logger.info(`[${txRequest.address}] claimed`);
} else {
this.config.logger.debug(`[${txRequest.address}] error: ${claimingStatus}`);
}
} catch (e) {
this.config.logger.error(`[${txRequest.address}] claiming failed`);
// TODO handle gracefully?
throw new Error(e);
}
} else {
this.config.logger.info(`[${txRequest.address}] not profitable to claim.`);
this.config.logger.debug(
`ECONOMIC STRATEGY: ${JSON.stringify(this.config.economicStrategy)}`
);
}
}
return TxStatus.ClaimWindow;
}
public async freezePeriod(txRequest: any): Promise<TxStatus> {
if (await txRequest.inFreezePeriod()) {
return TxStatus.FreezePeriod;
}
if (await txRequest.inExecutionWindow()) {
return TxStatus.ExecutionWindow;
}
return TxStatus.FreezePeriod;
}
public async inReservedWindowAndNotClaimedLocally(txRequest: any): Promise<boolean> {
const inReserved = await txRequest.inReservedWindow();
return inReserved && txRequest.isClaimed && !this.isLocalClaim(txRequest);
}
public async executionWindow(txRequest: any): Promise<TxStatus> {
if (txRequest.wasCalled) {
return TxStatus.Executed;
}
if (await this.isTransactionMissed(txRequest)) {
return TxStatus.Missed;
}
if (await this.inReservedWindowAndNotClaimedLocally(txRequest)) {
return TxStatus.ExecutionWindow;
}
const shouldExecute = await shouldExecuteTx(txRequest, this.config);
if (shouldExecute) {
try {
const executionStatus: ExecuteStatus = await this.actions.execute(txRequest);
if (executionStatus === ExecuteStatus.SUCCESS) {
this.config.logger.info(`[${txRequest.address}] executed`);
return TxStatus.Executed;
} else {
this.config.logger.debug(`[${txRequest.address}] error: ${executionStatus}`);
}
} catch (e) {
this.config.logger.error(`[${txRequest.address}] execution failed`);
//TODO handle gracefully?
throw new Error(e);
}
} else {
this.config.logger.info(
`[${txRequest.address}] not profitable to execute. Gas price too high`
);
}
return TxStatus.ExecutionWindow;
}
public async executed(txRequest: any): Promise<TxStatus> {
/**
* We don't cleanup because cleanup needs refactor according to latest logic in EAC
* https://github.com/ethereum-alarm-clock/ethereum-alarm-clock/blob/master/contracts/Library/RequestLib.sol#L433
*
* await this.actions.cleanup(txRequest);
*/
//
return TxStatus.Done;
}
public async missed(txRequest: any): Promise<TxStatus> {
// TODO cleanup
return TxStatus.Done;
}
public async isTransactionMissed(txRequest: any): Promise<boolean> {
const afterExecutionWindow =
parseInt(txRequest.executionWindowEnd, 10) <= parseInt(await txRequest.now(), 10);
return Boolean(afterExecutionWindow && !txRequest.wasCalled);
}
public isLocalClaim(txRequest: any): boolean {
const localClaim = this.config.wallet.isKnownAddress(txRequest.claimedBy);
if (!localClaim) {
this.config.logger.debug(
`[${txRequest.address}] In reserve window and not claimed by this TimeNode.`
);
}
return localClaim;
}
public async route(txRequest: ITxRequest): Promise<any> {
let status: TxStatus = this.txRequestStates[txRequest.address] || TxStatus.BeforeClaimWindow;
const statusFunction = this.transitions[status];
let nextStatus: TxStatus = await statusFunction(txRequest);
while (nextStatus !== status) {
this.config.logger.debug(
`[${txRequest.address}] Transitioning from ${TxStatus[status]} to ${
TxStatus[nextStatus]
} (${nextStatus})`
);
status = nextStatus;
nextStatus = await this.transitions[status](txRequest);
}
this.txRequestStates[txRequest.address] = nextStatus;
return nextStatus;
}
}