forked from ethereum/mist
/
sendTransactionConfirmation.js
228 lines (188 loc) · 7.39 KB
/
sendTransactionConfirmation.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
Template Controllers
@module Templates
*/
var setWindowSize = function(template){
Tracker.afterFlush(function(){
ipc.send('backendAction_setWindowSize', 580, template.$('.popup-windows').height() + 60);
});
}
var defaultEstimateGas = 50000000;
/**
The sendTransaction confirmation popup window template
@class [template] popupWindows_sendTransactionConfirmation
@constructor
*/
Template['popupWindows_sendTransactionConfirmation'].onCreated(function(){
var template = this;
this.autorun(function(){
var data = Session.get('data');
if(data) {
// set window size
setWindowSize(template);
// set provided gas to templateVar
TemplateVar.set('providedGas', data.gas || 0);
TemplateVar.set('initialProvidedGas', data.gas || 0);
// add gasPrice if not set
if(!data.gasPrice) {
web3.eth.getGasPrice(function(e, res){
if(!e) {
data.gasPrice = '0x'+ res.toString(16);
Session.set('data', data);
}
});
}
// check if to is a contract
if(data.to) {
web3.eth.getCode(data.to, function(e, res){
if(!e && res && res.length > 2) {
TemplateVar.set(template, 'toIsContract', true);
setWindowSize(template);
}
});
}
if(data.from) {
web3.eth.getCode(data.from, function(e, res){
if(!e && res && res.length > 2) {
TemplateVar.set(template, 'fromIsContract', true);
}
});
}
// esitmate gas usage
var estimateData = _.clone(data);
estimateData.gas = defaultEstimateGas;
web3.eth.estimateGas(estimateData, function(e, res){
console.log('Estimated gas: ', res, e);
if(!e && res) {
Tracker.nonreactive(function(){
if(defaultEstimateGas === res)
return TemplateVar.set(template, 'estimatedGas', 'invalid');
else
TemplateVar.set(template, 'estimatedGas', res);
// set the gas to the estimation, if not provided or lower
var gas = TemplateVar.get(template, 'providedGas');
if(gas == 0) {
TemplateVar.set(template, 'providedGas', res + 100000);
TemplateVar.set(template, 'initialProvidedGas', res + 100000);
}
});
}
});
}
});
});
Template['popupWindows_sendTransactionConfirmation'].onRendered(function(){
this.$('input[type="password"]').focus();
});
Template['popupWindows_sendTransactionConfirmation'].helpers({
/**
Returns the total amount
@method (totalAmount)
*/
'totalAmount': function(){
var amount = ExpTools.formatBalance(this.value, '0,0.00[0000000000000000]', 'expanse');
var dotPos = (~amount.indexOf('.')) ? amount.indexOf('.') + 3 : amount.indexOf(',') + 3;
return amount ? amount.substr(0, dotPos) + '<small style="font-size: 0.5em;">'+ amount.substr(dotPos) +'</small>' : '0';
},
/**
Calculates the fee used for this transaction in exp
@method (estimatedFee)
*/
'estimatedFee': function() {
var gas = TemplateVar.get('estimatedGas');
if(gas && this.gasPrice)
return ExpTools.formatBalance(new BigNumber(gas, 10).times(new BigNumber(this.gasPrice, 10)), '0,0.0[0000000] unit', 'expanse');
},
/**
Calculates the provided gas amount in expanse
@method (providedGas)
*/
'providedGas': function() {
var gas = TemplateVar.get('providedGas');
if(gas && this.gasPrice)
return ExpTools.formatBalance(new BigNumber(gas, 10).times(new BigNumber(this.gasPrice, 10)), '0,0.0[0000000]', 'expanse');
},
/**
Shortens the address to 0xffff...ffff
@method (shortenAddress)
*/
'shortenAddress': function(address){
if(_.isString(address)) {
return address.substr(0,6) +'...'+ address.substr(-4);
}
},
/**
Checks if its a contract execution and returns the execution function
@method (executionFunction)
*/
'executionFunction': function(){
if(TemplateVar.get('toIsContract') && this.data && this.data.length > 8) {
return (this.data.substr(0,2) === '0x')
? this.data.substr(0, 10)
: '0x'+ this.data.substr(0, 8);
}
},
/**
Formats the data so that all zeros are wrapped in span.zero
@method (formattedData)
*/
'formattedData': function(){
return (TemplateVar.get('toIsContract'))
? this.data.replace(/([0]{2,})/g,'<span class="zero">$1</span>').replace(/(0x[a-f0-9]{8})/i,'<span class="function">$1</span>')
: this.data.replace(/([0]{2,})/g,'<span class="zero">$1</span>');
}
});
Template['popupWindows_sendTransactionConfirmation'].events({
/**
Gets the new provided gas in expanse amount and calculates the resulting providedGas
@event change .provided-gas, input .provided-gas
*/
'change .provided-gas, input .provided-gas': function(e, template){
var gas = template.$('.provided-gas').text().replace(/[, ]+/g,'');//template.$('.provided-gas').text();
TemplateVar.set('providedGas', gas);
},
/**
Cancel the transaction confirmation and close the popup
@event click .cancel
*/
'click .cancel': function(){
ipc.send('backendAction_unlockedAccount', 'Transaction not confirmed');
ipc.send('backendAction_closePopupWindow');
},
/**
Confirm the transaction
@event submit form
*/
'submit form': function(e, template){
e.preventDefault();
var pw = template.find('input[type="password"]').value,
gas = web3.fromDecimal(TemplateVar.get('providedGas'));
console.log('Choosen Gas: ', gas, TemplateVar.get('providedGas'));
if(!gas || !_.isFinite(gas))
return;
TemplateVar.set('unlocking', true);
web3.personal.unlockAccount(Session.get('data').from, pw || '', 2, function(e, res){
pw = null;
TemplateVar.set(template, 'unlocking', false);
if(!e && res) {
ipc.send('backendAction_unlockedAccount', null, gas);
} else {
Tracker.afterFlush(function(){
template.find('input[type="password"]').value = '';
template.$('input[type="password"]').focus();
});
if(e.message.indexOf('CONNECTION ERROR') !== -1) {
GlobalNotification.warning({
content: TAPi18n.__('mist.popupWindows.sendTransactionConfirmation.errors.connectionTimeout'),
duration: 3
});
} else {
GlobalNotification.warning({
content: TAPi18n.__('mist.popupWindows.sendTransactionConfirmation.errors.wrongPassword'),
duration: 3
});
}
}
});
}
});