-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgasprice.scriptable
More file actions
76 lines (62 loc) · 2.24 KB
/
gasprice.scriptable
File metadata and controls
76 lines (62 loc) · 2.24 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
const FG_COLOR = new Color("#62688f")
const BG_COLOR = new Color("#151c2f")
const CRYPTONATOR_TICKER_URL = "https://api.cryptonator.com/api/ticker/eth-"
const ETHGASSTATION_GAS_URL = "https://www.ethgasstation.info/json/ethgasAPI.json"
const GAS_PER_TRANSACTION = 21000
const GWEI_PER_ETH = 1000000000
const HEADERS = { "User-Agent": "GasPrice" }
async function averageGasPrice() {
let req = new Request(ETHGASSTATION_GAS_URL)
req.headers = HEADERS // not necessary but be a nice netizen
const data = await req.loadJSON()
if (req.response.statusCode === 200) {
return(data.average / 10) // API gwei value 10x too large
} else {
return(0)
}
}
async function gweiToFiat(price, currency) {
let req = new Request(CRYPTONATOR_TICKER_URL + currency)
req.headers = HEADERS
const data = await req.loadJSON()
if (req.response.statusCode === 200 && data.success === true) {
return(price / GWEI_PER_ETH * data.ticker.price)
} else {
return(0)
}
}
function gweiPerTransaction(price) {
return(price * GAS_PER_TRANSACTION)
}
function createWidget(gasPrice, fiatPrice, currency) {
let widget = new ListWidget()
widget.backgroundColor = BG_COLOR
const gasRow = widget.addStack()
gasRow.bottomAlignContent()
let gasValue = gasRow.addText(gasPrice.toString())
gasValue.textColor = FG_COLOR
gasValue.font = Font.boldSystemFont(32)
const gasUnitCell = gasRow.addStack()
gasUnitCell.setPadding(0,4,3.3,0)
let gasLabel = gasUnitCell.addText("GWEI")
gasLabel.textColor = FG_COLOR
gasLabel.font = Font.systemFont(16)
let fiatValue = widget.addText(`≈ ${fiatPrice} ${currency}`)
fiatValue.textColor = FG_COLOR
fiatValue.font = Font.systemFont(16)
return widget
}
const currency = (args.widgetParameter !== null) ? args.widgetParameter : "EUR"
const gasPrice = await averageGasPrice()
let fiatPrice = await gweiToFiat(gweiPerTransaction(gasPrice), currency)
fiatPrice = Math.round(fiatPrice * 100) / 100
const widget = createWidget(gasPrice, fiatPrice, currency)
if (config.runsInWidget) {
Script.setWidget(widget)
} else if (config.runsWithSiri){
const message = `Average transaction gas price is ${gasPrice} gwei (or ${fiatPrice} ${currency})`
Speech.speak(message)
} else {
widget.presentSmall()
}
Script.complete()