This repository has been archived by the owner on Aug 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
publish_feed.lua
executable file
·267 lines (243 loc) · 6.83 KB
/
publish_feed.lua
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/usr/bin/env lua5.1
local seawolf = require 'seawolf'.__build([[text]], [[variable]])
local trim, tonumber = seawolf.text.trim, tonumber
local explode, empty = seawolf.text.explode, seawolf.variable.empty
local common = require 'common'
local api_call = common.api_call
local cli_wallet_call = common.cli_wallet_call
local parse_args = common.parse_args
-- Parse params and options
local options, params = parse_args(arg)
local witness = trim(params[1])
local server = trim(params[2])
if '' == witness or '' == server then
print "Usage: publish_feed [OPTION]... [WITNESS NAME] [CLI_WALLET SERVER]"
print [[]]
print [[ Available options:]]
print [[ --bias[=NUMBER] Set the bias percentage]]
print [[]]
print [[ Parameters:]]
print " WITNESS NAME The witness username"
print " CLI_WALLET SERVER Format: protocol://server:port"
print " protocol Whether http or https"
print " server Example: localhost"
print " port Example: 8093"
print [[]]
print [[]]
os.exit(1)
end
-- Detailed settings for each pair
local apis = {}
apis = {
coinmarketcap = {
cache = {url = nil, data = nil},
pairs = {
steem = {usd = true},
sbd = {usd = true},
},
alias = {
usd = 'USD',
sbd = 'steem-dollars',
},
get_url = function (from, to)
return ('https://api.coinmarketcap.com/v1/ticker/%s/?convert=%s'):format(from, to)
end,
get_price = function(from, to, data)
if data and data[1] then
return tonumber(data[1].price_usd)
end
end
},
poloniex = {
cache = {url = nil, data = nil},
pairs = {
btc = {steem = true, sbd = true, usd = true},
},
alias = {
usd = 'USDT',
btc = 'BTC',
steem = 'STEEM',
sbd = 'SBD',
},
get_url = function (from, to)
return 'https://poloniex.com/public?command=returnTicker'
end,
get_price = function(from, to, data)
if to == 'USDT' then
to = from
from = 'USDT'
end
local pair = ('%s_%s'):format(from, to)
if data[pair] then
return tonumber(data[pair].last)
end
end
},
bittrex = {
cache = {url = nil, data = nil},
pairs = {
btc = {steem = true, sbd = true},
},
alias = {},
get_url = function (from, to)
return ('https://bittrex.com/api/v1.1/public/getticker?market=%s-%s'):format(from, to)
end,
get_price = function (from, to, data)
if data and data.success and data.result then
return tonumber(data.result.Last)
end
end,
},
blockchain = {
cache = {url = nil, data = nil},
pairs = {
btc = {usd = true},
},
alias = {
usd = 'USD',
},
get_url = function (from, to)
return 'https://blockchain.info/ticker'
end,
get_price = function (from, to, data)
if data and data[to] then
return data[to].last
end
end,
},
coindesk = {
cache = {url = nil, data = nil},
pairs = {
btc = {usd = true},
},
alias = {
usd = 'USD',
},
get_url = function (from, to)
return 'https://api.coindesk.com/v1/bpi/currentprice.json'
end,
get_price = function (from, to, data)
if data and data.bpi and data.bpi[to] then
return data.bpi[to].rate_float
end
end,
},
hitbtc = {
cache = {url = nil, data = nil},
pairs = {
btc = {usd = true, steem = true, sbd = true},
},
alias = {
usd = [[USD]],
btc = [[BTC]],
steem = [[STEEM]],
sbd = [[SBD]],
},
get_url = function (from, to)
if [[STEEM]] == to or [[SBD]] == to then
return ([[https://api.hitbtc.com/api/2/public/ticker/%s%s]]):format(to, from)
else
return ([[https://api.hitbtc.com/api/2/public/ticker/%s%s]]):format(from, to)
end
end,
get_price = function (from, to, data)
if data then
return data.last
end
end,
},
}
-- Fetch current prices from ALL active pairs
local function fetch_market_prices()
local result = {}
for provider, api in pairs(apis) do
for from, tos in pairs(api.pairs) do
for to, status in pairs(tos) do
if status then
-- Build URL to fetch data
local url = api.get_url(api.alias[from] or from, api.alias[to] or to)
-- Test cache
if api.cache.url ~= url then
api.cache.url = url
-- Fetch data from URL
api.cache.data = api_call(url)
end
if api.cache.data then
local price = api.get_price(api.alias[from] or from, api.alias[to] or to, api.cache.data)
if price then
local pair = ('%s_%s'):format(from, to)
if nil == result[pair] then
result[pair] = {}
end
result[pair][provider] = price
end
end
end
end
end
end
return result
end
local function calc_pairs_averages(prices)
local result = {}
for pair, markets in pairs(prices) do
local total, count = 0, 0
for _, price in pairs(markets) do
total = total + price
count = count + 1
end
result[pair] = total / count
end
return result
end
local function calc_price(coin, data)
local proxied_price
if data['btc_usd'] and data['btc_' .. coin] then
proxied_price = tonumber(('%f'):format(data['btc_usd'] * data['btc_' .. coin]))
end
if data[coin .. '_usd'] and proxied_price then
-- avg(market_price, proxied)
return (data[coin .. '_usd'] + proxied_price)/2
elseif nil ~= data[coin .. '_usd'] then
-- market_price
return data[coin .. '_usd']
elseif nil ~= proxied_price then
-- proxied
return proxied_price
end
end
local function calc_sbd_price(data)
local proxied_sbd_price
if data['btc_usd'] and data['btc_sbd'] then
proxied_sbd_price = tonumber(('%f'):format(data['btc_usd'] * data['btc_sbd']))
end
if data.sbd_usd and proxied_sbd_price then
print 'avg(market_price, proxied)'
return (data.sbd_usd + proxied_sbd_price)/2
elseif nil ~= data.sbd_usd then
print 'market_price'
return data.sbd_usd
elseif nil ~= proxied_sbd_price then
print 'proxied'
return proxied_sbd_price
end
end
local function publish_feed(witness, base, quote)
local result = cli_wallet_call(
server,
'publish_feed',
{witness, {base = ("%.3f SBD"):format(base), quote = ("%.3f STEEM"):format(quote)}, true}
)
end
local market_prices = fetch_market_prices()
local pairs_averages = calc_pairs_averages(market_prices)
-- Calculate STEEM quote
local steem_quote = 1.000
if not empty(options.bias) then
steem_quote = 1 - ((options.bias*1/100)/(1 + options.bias*1/100))
end
-- Calculate SBD price
local sbd_price = calc_price('sbd', pairs_averages)
-- Calculate SBD base
local sbd_base = sbd_price * steem_quote
publish_feed(witness, sbd_base, steem_quote)