-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbitfinex.lua
243 lines (208 loc) · 6.02 KB
/
bitfinex.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
-- Inofficial Bitfinex Extension (www.bitfinex.com) for MoneyMoneyApp
-- Fetches balances from Bitfinex API and returns them as securities
--
-- Username: Bitfinex API Key
-- Password: Bitfinex API Secret
--
-- Copyright (c) 2017 beanieboi
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in all
-- copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
WebBanking{
version = 1.1,
url = "https://api.bitfinex.com",
description = "Fetch balances from Bitfinex API and list them as securities",
services= { "Bitfinex Account" },
}
local apiKey
local apiSecret
local apiVersion = "v2"
local currency = "EUR" -- fixme: Don't hardcode
local currencyName = "EUR" -- fixme: Don't hardcode
local market = "Bitfinex"
local accountName = "Balances"
local accountNumber = "Main"
local balances
local usdToEurRate
local prices
local currencySymbols = {
AGI = "tAGIUSD",
AID = "tAIDUSD",
AIO = "tAIOUSD",
ANT = "tANTUSD",
AVT = "tAVTUSD",
BAT = "tBATUSD",
BCH = "tBCHUSD",
BCI = "tBCIUSD",
BFT = "tBFTUSD",
BTC = "tBTCUSD",
BTG = "tBTGUSD",
CFI = "tCFIUSD",
DAI = "tDAIUSD",
DAT = "tDATUSD",
DSH = "tDSHUSD",
DTH = "tDTHUSD",
EDO = "tEDOUSD",
ELF = "tELFUSD",
EOS = "tEOSUSD",
ETC = "tETCUSD",
ETH = "tETHUSD",
ETP = "tETPUSD",
FUN = "tFUNUSD",
GNT = "tGNTUSD",
IOS = "tIOSUSD",
IOT = "tIOTUSD",
LRC = "tLRCUSD",
LTC = "tLTCUSD",
MIT = "tMITUSD",
MNA = "tMNAUSD",
MTN = "tMTNUSD",
NEO = "tNEOUSD",
ODE = "tODEUSD",
OMG = "tOMGUSD",
QSH = "tQSHUSD",
QTM = "tQTMUSD",
RCN = "tRCNUSD",
RDN = "tRDNUSD",
REP = "tREPUSD",
REQ = "tREQUSD",
RLC = "tRLCUSD",
RRT = "tRRTUSD",
SAN = "tSANUSD",
SNG = "tSNGUSD",
SNT = "tSNTUSD",
SPK = "tSPKUSD",
STJ = "tSTJUSD",
TNB = "tTNBUSD",
TRX = "tTRXUSD",
WAX = "tWAXUSD",
XLM = "tXLMUSD",
XMR = "tXMRUSD",
XRP = "tXRPUSD",
XVG = "tXVGUSD",
YYW = "tYYWUSD",
ZEC = "tZECUSD",
ZRX = "tZRXUSD"
}
function SupportsBank(protocol, bankCode)
return protocol == ProtocolWebBanking and bankCode == "Bitfinex Account"
end
function InitializeSession(protocol, bankCode, username, username2, password, username3)
apiKey = username
apiSecret = password
usdToEurRate = getUsdToEurRate()
balances = queryPrivate("auth/r/wallets")
prices = getRates()
end
function ListAccounts(knownAccounts)
local account = {
name = accountName,
accountNumber = accountNumber,
currency = currency,
portfolio = true,
type = "AccountTypePortfolio"
}
return {account}
end
function RefreshAccount(account, since)
local name
local currencyName
local s = {}
for index, values in pairs(balances) do
currencyName = values[2]
shares = tonumber(values[3])
if prices[currencyName] ~= nil and shares > 0 then
s[#s+1] = {
name = currencyName,
market = market,
currency = nil,
quantity = shares,
price = prices[currencyName] ~= nil and prices[currencyName] or 1
}
end
end
return {securities=s}
end
function EndSession ()
end
function queryPrivate(method, request)
if request == nil then
request = {}
end
local path = string.format("/%s/%s", apiVersion, method)
local nonce = string.format("%d", math.floor(MM.time() * 1000000))
local postData = httpBuildQuery(request)
local message = string.format("/api%s%s%s", path, nonce, postData)
local signature = bin2hex(MM.hmac384(apiSecret, message))
local headers = {}
headers['bfx-nonce'] = nonce
headers["bfx-apikey"] = apiKey
headers["bfx-signature"] = signature
connection = Connection()
content = connection:request("POST", url .. path, postData, "application/x-www-form-urlencoded; charset=UTF-8", headers)
json = JSON(content)
return json:dictionary()
end
function bin2hex(s)
return (s:gsub(".", function (byte)
return string.format("%02x", string.byte(byte))
end))
end
function httpBuildQuery(params)
local str = ''
for key, value in pairs(params) do
str = str .. key .. "=" .. value .. "&"
end
return str.sub(str, 1, -2)
end
function getRates()
local rates = {USD = 1 * usdToEurRate}
local path = string.format("/%s/tickers", apiVersion)
local query = httpBuildQuery({symbols=currencySymbolsString()})
connection = Connection()
content = connection:request("GET", url .. path .. "?" .. query)
json = JSON(content):dictionary()
for index, values in pairs(json) do
local symbol = values[1]
local price = tonumber(values[8]) * usdToEurRate
local currency = getKeyFromSymbol(symbol)
rates[currency] = price
end
return rates
end
function getKeyFromSymbol(symbol)
for key, value in pairs(currencySymbols) do
if value == symbol then
return key
end
end
end
function getUsdToEurRate()
connection = Connection()
content = connection:request("GET", "https://conversion-api.abwesend.com/conversion/usd/eur")
json = JSON(content):dictionary()
return json["value"]
end
function currencySymbolsString()
local s = ""
for key, name in pairs(currencySymbols) do
s = s .. string.format("%s,", name)
end
return s
end
-- SIGNATURE: MCwCFDRLhorBH/Y2kqk36qEmjAJdjEhkAhQyjkZUeiy46K2L+oC/9dyPbpaKBA==