-
Notifications
You must be signed in to change notification settings - Fork 1
/
bitpanda-light.lua
389 lines (356 loc) · 11.9 KB
/
bitpanda-light.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
-- Inofficial Bitpanda-Light Extension (www.bitpanda.com) for MoneyMoney
-- Fetches available data from Bitpanda API
--
-- Username: API-Key
--
-- MIT License
--
-- Copyright (c) 2021 GimliGloinsSon
--
-- 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.2,
url = "https://api.bitpanda.com/v1/",
services = {"bitpanda-light"},
description = "Loads current Balances for FIATs, Krypto, Indizes, Stocks, ETFs, ETCs (Ressources) and Commodities from bitpanda"}
local connection = Connection()
local apiKey
local walletCurrency = "EUR"
local coinDict = {
-- Krypto
[1] = "Bitcoin",
[3] = "Litecoin",
[5] = "Etherum",
[6] = "Lisk",
[7] = "Dash",
[8] = "Ripple",
[9] = "Bitcoin Cash",
[11] = "Pantos",
[12] = "Komodo",
[13] = "IOTA",
[14] = "EOS",
[15] = "OmiseGo",
[16] = "Augur",
[17] = "0x",
[18] = "ZCash",
[19] = "NEM",
[20] = "Stellar",
[21] = "Tezos",
[22] = "Cardano",
[23] = "NEO",
[24] = "Etherum Classic",
[25] = "Chainlink",
[26] = "Waves",
[27] = "Tether",
[30] = "USD Coin",
[31] = "Tron",
[32] = "Cosmos",
[33] = "Bitpanda Ecosystem Token",
[34] = "Basic Attention Token",
[37] = "Chiliz",
[38] = "Tron",
[39] = "Doge",
[43] = "Qtum",
[44] = "Vechain",
[51] = "Polkadot",
[52] = "Yearn.Finance",
[53] = "Maker",
[54] = "Compound",
[55] = "Synthetix Network Token",
[56] = "Uniswap",
[57] = "Filecoin",
[58] = "Aave",
[59] = "Kyber Network",
[60] = "Band Protocol",
[61] = "REN",
[63] = "UMA",
[66] = "Ocean Protocol",
[69] = "Aragon",
[129] = "1inch",
[131] = "The Graph",
[133] = "Terra",
[134] = "Polygon",
[138] = "Dedentraland",
[139] = "PancakeSwap",
[141] = "SushiSwap",
[143] = "Symbol",
[151] = "Axie Infinity Shard",
[193] = "SHIBA INU",
-- Metals
[28] = "Gold",
[29] = "Silver",
[35] = "Palladium",
[36] = "Platinum",
-- Indizes
[40] = "Bitpanda Crypto Index 5",
[41] = "Bitpanda Crypto Index 10",
[42] = "Bitpanda Crypto Index 25",
-- Stocks
[75] = "Apple",
[78] = "Microsoft",
[89] = "Allianz",
[106] = "Boeing",
}
local priceTable = {}
local allAssetWallets = {}
local allFiatWallets = {}
function SupportsBank (protocol, bankCode)
return protocol == ProtocolWebBanking and bankCode == "bitpanda-light"
end
function InitializeSession (protocol, bankCode, username, username2, password, username3)
-- Login.
apiKey = username
prices = connection:request("GET", "https://api.bitpanda.com/v1/ticker", nil, nil, nil)
priceTable = JSON(prices):dictionary()
allAssetWallets = queryPrivate("asset-wallets")
allFiatWallets = queryPrivate("fiatwallets")
urlStock = "https://api.bitpanda.com/v2/masterdata"
stocks = connection:request("GET", urlStock, nil, nil, nil)
stockPriceTable = JSON(stocks):dictionary()
stockPrices = stockPriceTable.data.attributes.stocks
etfPrices = stockPriceTable.data.attributes.etfs
etcPrices = stockPriceTable.data.attributes.etcs
end
function ListAccounts (knownAccounts)
-- Return array of accounts.
local accounts = {}
-- FIAT Wallets
table.insert(accounts,
{
name = "FIAT Wallets",
owner = user,
accountNumber = "FIAT Accounts",
currency = walletCurrency,
portfolio = true,
type = AccountTypePortfolio,
subAccount = "fiat"
})
-- Crypto Wallets
table.insert(accounts,
{
name = "Krypto Wallets",
owner = user,
accountNumber = "Krypto Accounts",
currency = walletCurrency,
portfolio = true,
type = AccountTypePortfolio,
subAccount = "cryptocoin"
})
-- Indizes Wallets
table.insert(accounts,
{
name = "Index Wallets",
owner = user,
accountNumber = "Index Accounts",
currency = walletCurrency,
portfolio = true,
type = AccountTypePortfolio,
subAccount = "index.index"
})
-- Commodity Wallets
table.insert(accounts,
{
name = "Commoditie Wallets",
owner = user,
accountNumber = "Comm Accounts",
currency = walletCurrency,
portfolio = true,
type = AccountTypePortfolio,
subAccount = "commodity.metal"
})
-- Stock Wallets
table.insert(accounts,
{
name = "Stock Wallets",
owner = user,
accountNumber = "Stock Accounts",
currency = walletCurrency,
portfolio = true,
type = AccountTypePortfolio,
subAccount = "security.stock"
})
-- ETF Wallets
table.insert(accounts,
{
name = "ETF Wallets",
owner = user,
accountNumber = "ETF Accounts",
currency = walletCurrency,
portfolio = true,
type = AccountTypePortfolio,
subAccount = "security.etf"
})
-- ETC Wallets (Ressources)
table.insert(accounts,
{
name = "Ressource Wallets",
owner = user,
accountNumber = "Ressource Accounts",
currency = walletCurrency,
portfolio = true,
type = AccountTypePortfolio,
subAccount = "security.etc"
})
return accounts
end
function RefreshAccount (account, since)
MM.printStatus("Refreshing account " .. account.name)
local sum = 0
local getTrans = {}
local getBal = {}
local t = {} -- List of transactions to return
-- transactions for Depot
if account.portfolio then
if account.subAccount == "cryptocoin" then
getTrans = allAssetWallets.data.attributes.cryptocoin.attributes.wallets
elseif account.subAccount == "index.index" then
getTrans = allAssetWallets.data.attributes.index.index.attributes.wallets
elseif account.subAccount == "commodity.metal" then
getTrans = allAssetWallets.data.attributes.commodity.metal.attributes.wallets
elseif account.subAccount == "security.stock" then
getTrans = allAssetWallets.data.attributes.security.stock.attributes.wallets
elseif account.subAccount == "security.etf" then
getTrans = allAssetWallets.data.attributes.security.etf.attributes.wallets
elseif account.subAccount == "security.etc" then
getTrans = allAssetWallets.data.attributes.security.etc.attributes.wallets
elseif account.subAccount == "fiat" then
getTrans = allFiatWallets.data
else
return
end
for index, cryptTransaction in pairs(getTrans) do
if tonumber(cryptTransaction.attributes.balance) >= 0 then
local transaction = transactionForCryptTransaction(cryptTransaction, account.currency, account.subAccount)
t[#t + 1] = transaction
end
end
return {securities = t}
else
return
end
end
function transactionForCryptTransaction(transaction, currency, type)
local symbol = nil
local currPrice = 0
local currQuant = tonumber(transaction.attributes.balance)
local currAmount = 0
local isinString = ""
local wpName = transaction.attributes.name
local calcCurrency = currency
if type == "fiat" then
calcCurrency = transaction.attributes.fiat_symbol
symbol = transaction.attributes.fiat_symbol
currAmount = currQuant
currQuant = nil
elseif type == "index.index" then
symbol = transaction.attributes.cryptocoin_symbol
currAmount = currQuant
currQuant = nil
elseif type == "security.stock" then
symbol = transaction.attributes.cryptocoin_symbol
currPrice = tonumber(queryStockMasterdata(symbol, "avg_price", stockPrices))
isinString = queryStockMasterdata(symbol, "isin", stockPrices)
wpName = wpName .. " - " .. queryStockMasterdata(symbol, "name", stockPrices)
currAmount = currPrice * currQuant
calcCurrency = nil
elseif type == "security.etf" then
symbol = transaction.attributes.cryptocoin_symbol
currPrice = tonumber(queryStockMasterdata(symbol, "avg_price", etfPrices))
isinString = queryStockMasterdata(symbol, "isin", etfPrices)
wpName = wpName .. " - " .. queryStockMasterdata(symbol, "name", etfPrices)
currAmount = currPrice * currQuant
calcCurrency = nil
elseif type == "security.etc" then
symbol = transaction.attributes.cryptocoin_symbol
currPrice = tonumber(queryStockMasterdata(symbol, "avg_price", etcPrices))
isinString = queryStockMasterdata(symbol, "isin", etcPrices)
wpName = wpName .. " - " .. queryStockMasterdata(symbol, "name", etcPrices)
currAmount = currPrice * currQuant
calcCurrency = nil
else
symbol = transaction.attributes.cryptocoin_symbol
currPrice = tonumber(queryPrice(symbol, currency))
currAmount = currPrice * currQuant
calcCurrency = nil
end
t = {
--String name: Bezeichnung des Wertpapiers
name = wpName,
--String isin: ISIN
isin = isinString,
--String securityNumber: WKN
securityNumber = symbol,
--String market: Börse
market = "bitpanda",
--String currency: Währung bei Nominalbetrag oder nil bei Stückzahl
currency = calcCurrency,
--Number quantity: Nominalbetrag oder Stückzahl
quantity = currQuant,
--Number amount: Wert der Depotposition in Kontowährung
amount = currAmount,
--Number originalCurrencyAmount: Wert der Depotposition in Originalwährung
originalCurrencyAmount = currAmount,
--String currencyOfOriginalAmount: Originalwährung
currencyOfOriginalAmount = calcCurrency,
--Number exchangeRate: Wechselkurs zum Kaufzeitpunkt
--Number tradeTimestamp: Notierungszeitpunkt; Die Angabe erfolgt in Form eines POSIX-Zeitstempels.
tradeTimestamp = os.time(),
--Number price: Aktueller Preis oder Kurs
price = currPrice,
--String currencyOfPrice: Von der Kontowährung abweichende Währung des Preises
currencyOfPrice = calcCurrency,
--Number purchasePrice: Kaufpreis oder Kaufkurs
--String currencyOfPurchasePrice: Von der Kontowährung abweichende Währung des Kaufpreises
}
return t
end
function EndSession ()
-- Logout.
end
function queryPrivate(method, params)
local path = method
if not (params == nil) then
local queryParams = httpBuildQuery(params)
if string.len(queryParams) > 0 then
path = path .. "?" .. queryParams
end
end
local headers = {}
headers["X-API-KEY"] = apiKey
content = connection:request("GET", url .. path, nil, nil, headers)
return JSON(content):dictionary()
end
function queryPrice(symbol, currency)
return priceTable[symbol][currency]
end
function queryStockMasterdata(symbol, field, table)
for key, value in pairs(table) do
if value.attributes.symbol == symbol then
return value.attributes[field]
end
end
return 0
end
function httpBuildQuery(params)
local str = ''
for key, value in pairs(params) do
str = str .. key .. "=" .. value .. "&"
end
str = str.sub(str, 1, -2)
return str
end