forked from ethereum-alarm-clock/ethereum-alarm-clock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataHelpers.js
192 lines (167 loc) · 5.96 KB
/
dataHelpers.js
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
const wasAborted = (executeTx) => {
const Aborted = executeTx.logs.find(e => e.event === 'Aborted')
return Aborted ? true : false
}
const parseAbortData = (executeTx) => {
const reason = [
'WasCancelled', //0
'AlreadyCalled', //1
'BeforeCallWindow', //2
'AfterCallWindow', //3
'ReservedForClaimer', //4
'InsufficientGas', //5
'MismatchGasPrice' //6
]
const abortedLogs = executeTx.logs.filter(e => e.event === 'Aborted')
let abortedNums = []
abortedLogs.forEach((log) => {
abortedNums.push(log.args.reason.toNumber())
})
const reasons = abortedNums.map((num) => {
return reason[num]
})
return reasons
}
const computeEndowment = (
payment,
donation,
callGas,
callValue,
gasPrice) => {
return payment + (donation * 2) + (callGas * gasPrice) + 180000 * gasPrice + callValue
}
const parseRequestData = async (transactionRequest) => {
const data = await transactionRequest.requestData()
return {
"claimData": {
"claimedBy": data[0][0],
"claimDeposit": data[2][0].toNumber(),
"paymentModifier": data[3][0].toNumber(),
"requiredDeposit": data[2][14].toNumber(),
},
"meta": {
"createdBy": data[0][1],
"owner": data[0][2],
"isCancelled": data[1][0],
"wasCalled": data[1][1],
"wasSuccessful": data[1][2],
},
"paymentData": {
"donationBenefactor": data[0][3],
"paymentBenefactor": data[0][4],
"donation": data[2][1].toNumber(),
"donationOwed": data[2][2].toNumber(),
"payment": data[2][3].toNumber(),
"paymentOwed": data[2][4].toNumber(),
},
"schedule": {
"claimWindowSize": data[2][5].toNumber(),
"freezePeriod": data[2][6].toNumber(),
"reservedWindowSize": data[2][7].toNumber(),
"temporalUnit": data[2][8].toNumber(),
"windowSize": data[2][9].toNumber(),
"windowStart": data[2][10].toNumber(),
},
"txData": {
"callGas": data[2][11].toNumber(),
"callValue": data[2][12].toNumber(),
"gasPrice": data[2][13].toNumber(),
"toAddress": data[0][5],
}
}
}
class RequestData {
constructor(data, txRequest) {
if (typeof data === undefined || typeof txRequest === undefined) {
throw new Error('Can not call the constructor!')
}
this.txRequest = txRequest
this.claimData = {
"claimedBy": data[0][0],
"claimDeposit": data[2][0].toNumber(),
"paymentModifier": data[3][0].toNumber(),
"requiredDeposit": data[2][14].toNumber(),
}
this.meta = {
"createdBy": data[0][1],
"owner": data[0][2],
"isCancelled": data[1][0],
"wasCalled": data[1][1],
"wasSuccessful": data[1][2],
}
this.paymentData = {
"donationBenefactor": data[0][3],
"paymentBenefactor": data[0][4],
"donation": data[2][1].toNumber(),
"donationOwed": data[2][2].toNumber(),
"payment": data[2][3].toNumber(),
"paymentOwed": data[2][4].toNumber(),
}
this.schedule = {
"claimWindowSize": data[2][5].toNumber(),
"freezePeriod": data[2][6].toNumber(),
"reservedWindowSize": data[2][7].toNumber(),
"temporalUnit": data[2][8].toNumber(),
"windowSize": data[2][9].toNumber(),
"windowStart": data[2][10].toNumber(),
}
this.txData = {
"callGas": data[2][11].toNumber(),
"callValue": data[2][12].toNumber(),
"gasPrice": data[2][13].toNumber(),
"toAddress": data[0][5],
}
}
static async from(txRequest) {
const data = await txRequest.requestData()
return new RequestData(data, txRequest)
}
async refresh() {
if (typeof this.txRequest === 'undefined') {
throw new Error('Must instantiate the RequestData first!')
}
const data = await this.txRequest.requestData()
this.claimData = {
"claimedBy": data[0][0],
"claimDeposit": data[2][0].toNumber(),
"paymentModifier": data[3][0].toNumber(),
}
this.meta = {
"createdBy": data[0][1],
"owner": data[0][2],
"isCancelled": data[1][0],
"wasCalled": data[1][1],
"wasSuccessful": data[1][2],
}
this.paymentData = {
"donationBenefactor": data[0][3],
"paymentBenefactor": data[0][4],
"donation": data[2][1].toNumber(),
"donationOwed": data[2][2].toNumber(),
"payment": data[2][3].toNumber(),
"paymentOwed": data[2][4].toNumber(),
}
this.schedule = {
"claimWindowSize": data[2][5].toNumber(),
"freezePeriod": data[2][6].toNumber(),
"reservedWindowSize": data[2][7].toNumber(),
"temporalUnit": data[2][8].toNumber(),
"windowSize": data[2][9].toNumber(),
"windowStart": data[2][10].toNumber(),
}
this.txData = {
"callGas": data[2][11].toNumber(),
"callValue": data[2][12].toNumber(),
"gasPrice": data[2][13].toNumber(),
"toAddress": data[0][5],
}
}
calcEndowment () {
return this.paymentData.payment + this.paymentData.donation * 2 + this.txData.callGas * this.txData.gasPrice + 180000 * this.txData.gasPrice + this.txData.callValue
}
}
module.exports.computeEndowment = computeEndowment
module.exports.RequestData = RequestData
module.exports.parseRequestData = parseRequestData
module.exports.parseAbortData = parseAbortData
module.exports.wasAborted = wasAborted