-
Notifications
You must be signed in to change notification settings - Fork 5k
/
Copy pathAbstractObservedTransactionMethod.js
177 lines (148 loc) · 5.85 KB
/
AbstractObservedTransactionMethod.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
/*
This file is part of web3.js.
web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file AbstractObservedTransactionMethod.js
* @author Samuel Furter <samuel@ethereum.org>
* @date 2018
*/
import PromiEvent from '../../PromiEvent';
import AbstractMethod from '../AbstractMethod';
export default class AbstractObservedTransactionMethod extends AbstractMethod {
/**
* @param {String} rpcMethod
* @param {Number} parametersAmount
* @param {Utils} utils
* @param {Object} formatters
* @param {AbstractWeb3Module} moduleInstance
* @param {TransactionObserver} transactionObserver
*
* @constructor
*/
constructor(rpcMethod, parametersAmount, utils, formatters, moduleInstance, transactionObserver) {
super(rpcMethod, parametersAmount, utils, formatters, moduleInstance);
this.transactionObserver = transactionObserver;
this.promiEvent = new PromiEvent();
}
/**
* This type will be used in the AbstractMethodFactory.
*
* @returns {String}
*/
static get Type() {
return 'observed-transaction-method';
}
/**
* Sends the request and returns a PromiEvent Object
*
* @method execute
*
*
* @callback callback callback(error, result)
* @returns {PromiEvent}
*/
execute() {
this.beforeExecution(this.moduleInstance);
this.moduleInstance.currentProvider
.send(this.rpcMethod, this.parameters)
.then((transactionHash) => {
let confirmations, receipt;
if (this.callback) {
this.callback(false, transactionHash);
return;
}
this.promiEvent.emit('transactionHash', transactionHash);
const transactionConfirmationSubscription = this.transactionObserver.observe(transactionHash).subscribe(
(transactionConfirmation) => {
confirmations = transactionConfirmation.confirmations;
receipt = transactionConfirmation.receipt;
if (this.hasRevertReceiptStatus(receipt)) {
if (this.parameters[0].gas === receipt.gasUsed) {
this.handleError(
new Error(
`Transaction ran out of gas. Please provide more gas:\n${JSON.stringify(
receipt,
null,
2
)}`
),
receipt,
confirmations
);
transactionConfirmationSubscription.unsubscribe();
return;
}
this.handleError(
new Error(
`Transaction has been reverted by the EVM:\n${JSON.stringify(receipt, null, 2)}`
),
receipt,
confirmations
);
transactionConfirmationSubscription.unsubscribe();
return;
}
this.promiEvent.emit('confirmation', confirmations, this.afterExecution(receipt));
},
(error) => {
this.handleError(error, receipt, confirmations);
},
() => {
if (this.promiEvent.listenerCount('receipt') > 0) {
this.promiEvent.emit('receipt', this.afterExecution(receipt));
this.promiEvent.removeAllListeners();
return;
}
this.promiEvent.resolve(this.afterExecution(receipt));
}
);
})
.catch((error) => {
if (this.callback) {
this.callback(error, null);
return;
}
this.handleError(error, false, 0);
});
return this.promiEvent;
}
/**
* This methods calls the correct error methods of the PromiEvent object.
*
* @method handleError
*
* @param {Error} error
* @param {Object} receipt
* @param {Number} confirmations
*/
handleError(error, receipt, confirmations) {
if (this.promiEvent.listenerCount('error') > 0) {
this.promiEvent.emit('error', error, receipt, confirmations);
this.promiEvent.removeAllListeners();
return;
}
this.promiEvent.reject(error);
}
/**
* Checks if the status property has a revert state
*
* @method hasRevertReceiptStatus
*
* @param {Object} receipt
*
* @returns {Boolean}
*/
hasRevertReceiptStatus(receipt) {
return Boolean(parseInt(receipt.status)) === false && receipt.status !== undefined && receipt.status !== null;
}
}