-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectGasPrice.js
More file actions
109 lines (87 loc) · 3.02 KB
/
selectGasPrice.js
File metadata and controls
109 lines (87 loc) · 3.02 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
/**
Template Controllers
@module Templates
*/
/**
The select gas price template
@class [template] dapp_selectGasPrice
@constructor
*/
/**
The the factor by which the gas price should be changeable.
@property toPowerFactor
*/
var toPowerFactor = 1.4;
/**
Calculates the gas * gas price.
@method calculategasInTa
@return {Number}
*/
var calculategasInTa = function(template, gas, gasPrice, returnGasPrice){
// Only defaults to 50 shannon if there's no default set
gasPrice = gasPrice || 50000000000;
if(!_.isObject(gasPrice))
gasPrice = new BigNumber(String(gasPrice), 10);
// We multiply it by factor^2 to offset the default factor multiplicator that set at -2
var suggestedGasPrice = gasPrice.times(new BigNumber(toPowerFactor).toPower(2));
if(_.isUndefined(gas)) {
console.warn('No gas provided for {{> dapp_selectGasPrice}}');
return new BigNumber(0);
}
// divide and multiply to round it to the nearest billion wei (1 shannon)
var billion = new BigNumber(1000000000);
suggestedGasPrice = suggestedGasPrice.times(new BigNumber(toPowerFactor).toPower(TemplateVar.get(template, 'feeMultiplicator'))).dividedBy(billion).round().times(billion);
return (returnGasPrice)
? suggestedGasPrice
: suggestedGasPrice.times(gas);
}
Template['dapp_selectGasPrice'].onCreated(function(){
TemplateVar.set('gasInTa', '0');
TemplateVar.set('gasPrice', '0');
TemplateVar.set('feeMultiplicator', 1);
});
Template['dapp_selectGasPrice'].helpers({
/**
Return the currently selected fee value calculate with gas price
@method (fee)
*/
'fee': function(){
if(_.isFinite(TemplateVar.get('feeMultiplicator')) && _.isFinite(this.gas)) {
var template = Template.instance();
// set the value
TemplateVar.set('gasInTa', calculategasInTa(template, this.gas, this.gasPrice).floor().toString(10));
TemplateVar.set('gasPrice', (new BigNumber(1000000000).times(TemplateVar.get('feeMultiplicator'))));
// return the fee
return SeroTools.formatBalance((new BigNumber(1000000000).times(TemplateVar.get('feeMultiplicator'))).toString(10), '0,0.[000000000000000000]', this.unit);
}
},
/**
Return the current unit
@method (unit)
*/
'unit': function(){
var unit = this.unit || SeroTools.getUnit();
if(unit)
return unit.toUpperCase();
},
/**
Get the correct text, if TAPi18n is available.
@method i18nText
*/
'i18nText': function(key){
if(typeof TAPi18n === 'undefined') {
return (key === 'high') ? '+' : '-';
} else {
return TAPi18n.__('elements.selectGasPrice.'+ key);
}
}
});
Template['dapp_selectGasPrice'].events({
/**
Change the selected fee
@event change input[name="fee"], input input[name="fee"]
*/
'change input[name="fee"], input input[name="fee"]': function(e){
TemplateVar.set('feeMultiplicator', Number(e.currentTarget.value));
},
});