-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy pathcheckActionEffect.ts
More file actions
203 lines (183 loc) · 6.3 KB
/
Copy pathcheckActionEffect.ts
File metadata and controls
203 lines (183 loc) · 6.3 KB
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
196
197
198
199
200
201
202
203
import { gte, lte } from 'biggystring'
import { DONE_THRESHOLD } from '../../../constants/WalletAndCurrencyConstants'
import { filterNull } from '../../../util/safeFilters'
import { checkPushEvent } from '../push'
import type {
ActionEffect,
EffectCheckResult,
ExecutionContext,
SeqEffect
} from '../types'
/**
* Check whether an ActionEffect is observed as effective (completed).
*
* @param context Execution context for the action queue (same param as evaluateAction)
* @param effect The effect to check for effectiveness (completed)
* @returns `EffectCheckResult` object containing:
* 1. `isEffective`: boolean indicating whether the effect is effective.
* Partially complete effects are not effective and so the boolean must be false.
* 2. `delay`: in milliseconds to let the caller know how long to delay the
* `nextExecutionTime`.
* 3. `updatedEffect`: for if the effect is partially effective.
*
* ### Partially Completed Effects
*
* SeqEffect introduced the concept of a "partially completed effect" in
* order to support tracking the progress of precomputed effects which are
* delegated to an external execution environment (e.g. push-server).
* A SeqEffect is partially-completed when it's opIndex is less then the last
* index of it's childEffects.
*/
export async function checkActionEffect(
context: ExecutionContext,
effect: ActionEffect
): Promise<EffectCheckResult> {
const { account } = context
const UNEXPECTED_NULL_EFFECT_ERROR_MESSAGE =
`Unexpected null effect while running check. ` +
`This could be caused by a dryrun effect leaking into program state when it shouldn't.`
switch (effect.type) {
case 'seq': {
const checkedEffects = filterNull(effect.childEffects)
if (checkedEffects.length !== effect.childEffects.length)
throw new Error(UNEXPECTED_NULL_EFFECT_ERROR_MESSAGE)
// Only check the child effect at the current opIndex
const childEffect = checkedEffects[effect.opIndex]
const childEffectCheck = await context.checkActionEffect(childEffect)
// Completely effective
if (
childEffectCheck.isEffective &&
effect.opIndex >= effect.childEffects.length - 1
) {
return {
delay: 0,
isEffective: true
}
}
// Partially effective
if (childEffectCheck.isEffective) {
// Progress the partially completed effect forward
const updatedEffect: SeqEffect = {
...effect,
opIndex: effect.opIndex + 1
}
return {
delay: 0,
isEffective: false,
updatedEffect
}
}
// Ineffective
return {
delay: childEffectCheck.delay,
isEffective: false
}
}
case 'par': {
const checkedEffects = filterNull(effect.childEffects)
if (checkedEffects.length !== effect.childEffects.length)
throw new Error(UNEXPECTED_NULL_EFFECT_ERROR_MESSAGE)
// Check all child effects concurrently
const childEffectPromises = checkedEffects.map(async childEffect => {
return await context.checkActionEffect(childEffect)
})
const childEffectChecks = await Promise.all(childEffectPromises)
const isEffective = childEffectChecks.every(result => result.isEffective)
// Include an updated effect if partially completed
const updatedEffect: ActionEffect | undefined = !isEffective
? {
type: 'par',
childEffects: checkedEffects.map((effect, index) => {
return childEffectChecks[index].isEffective
? { type: 'done' }
: effect
})
}
: undefined
// Let delay be the maximum delay of the remaining ineffective effects or zero
const delay = childEffectChecks.reduce(
(max, result) =>
result.isEffective ? max : Math.max(result.delay, max),
0
)
return {
delay,
isEffective,
updatedEffect
}
}
case 'address-balance': {
// TODO: Use effect.address when we can check address balances
const { aboveAmount, belowAmount, tokenId, walletId } = effect
const wallet = await account.waitForCurrencyWallet(walletId)
// The wallet object can exist before its engine loads (wallet cache),
// so don't evaluate the effect against cached, possibly stale
// balances. Report "not yet effective" until the engine has synced:
if (wallet.syncStatus.totalRatio < DONE_THRESHOLD) {
return {
delay: 15000,
isEffective: false
}
}
const walletBalance = wallet.balanceMap.get(tokenId) ?? '0'
return {
delay: 15000,
isEffective:
(aboveAmount != null && gte(walletBalance, aboveAmount)) ||
(belowAmount != null && lte(walletBalance, belowAmount))
}
}
case 'push-event': {
const { eventId } = effect
return {
delay: 15000,
isEffective: await checkPushEvent(context, eventId)
}
}
case 'price-level': {
// TODO: Implement
throw new Error('No implementation for price effect')
}
case 'tx-confs': {
const { txId, walletId, confirmations } = effect
const wallet = await account.waitForCurrencyWallet(walletId)
// Get transaction
const txs = await wallet.getTransactions({
// TODO: Add a parameter to limit to one transaction in result
tokenId: null,
searchString: txId
})
// If not transaction is found with the effect's txId, then we can assume
// that we're waiting to synchronize with network state.
if (txs.length === 0) {
return {
delay: 6000,
isEffective: false
}
}
const tx = txs[0]
if (tx.confirmations === 'dropped')
throw new Error('Transaction was dropped')
if (typeof tx.confirmations === 'number') {
return {
delay: 6000,
isEffective: tx.confirmations >= confirmations
}
} else {
return {
delay: 6000,
isEffective:
confirmations === 0 ||
(confirmations > 0 && tx.confirmations === 'confirmed')
}
}
}
case 'done': {
if (effect.error != null) throw effect.error
return {
delay: 0,
isEffective: true
}
}
}
}