forked from ethereum/meteor-package-elements
-
Notifications
You must be signed in to change notification settings - Fork 0
/
selectGasPrice.js
130 lines (109 loc) · 3.03 KB
/
selectGasPrice.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
/**
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 = 2;
/**
Calculates the gas * gas price.
@method calculateGasInWei
@return {Number}
*/
var calculateGasInWei = function(template, gas, gasPrice, returnGasPrice) {
// Only defaults to 20 shannon if there's no default set
gasPrice = gasPrice || 20000000000;
if (!_.isObject(gasPrice)) gasPrice = new BigNumber(String(gasPrice), 10);
if (_.isUndefined(gas)) {
console.warn("No gas provided for {{> dapp_selectGasPrice}}");
return new BigNumber(0);
}
var feeMultiplicator = Number(TemplateVar.get(template, "feeMultiplicator"));
// divide and multiply to round it to the nearest billion wei (1 shannon)
var billion = new BigNumber(1000000000);
gasPrice = gasPrice
.times(new BigNumber(toPowerFactor).toPower(feeMultiplicator))
.dividedBy(billion)
.round()
.times(billion);
return returnGasPrice ? gasPrice : gasPrice.times(gas);
};
Template["dapp_selectGasPrice"].onCreated(function() {
TemplateVar.set("gasInWei", "0");
TemplateVar.set("gasPrice", "0");
TemplateVar.set("feeMultiplicator", 0);
});
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(
"gasInWei",
calculateGasInWei(template, this.gas, this.gasPrice)
.floor()
.toString(10)
);
TemplateVar.set(
"gasPrice",
calculateGasInWei(template, this.gas, this.gasPrice, true)
.floor()
.toString(10)
);
// return the fee
return EthTools.formatBalance(
calculateGasInWei(template, this.gas, this.gasPrice).toString(10),
"0,0.[000000000000000000] UNIT",
"ether"
);
}
},
/**
Return the current unit
@method (unit)
*/
unit: function() {
var unit = this.unit || EthTools.getUnit();
if (unit) return unit.toUpperCase();
},
/**
Get the correct text, if TAPi18n is available.
@method i18nText
*/
i18nText: function(key) {
if (
typeof TAPi18n !== "undefined" &&
TAPi18n.__("elements.selectGasPrice." + key) !==
"elements.selectGasPrice." + key
) {
return TAPi18n.__("elements.selectGasPrice." + key);
} else if (typeof this[key] !== "undefined") {
return this[key];
} else {
return key === "high" ? "+" : "-";
}
}
});
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));
}
});