-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNano.lua
138 lines (118 loc) · 4.06 KB
/
Nano.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
-- Inofficial Nano Extension for MoneyMoney
-- Fetches balances from mynano.ninja and returns them as securities
--
-- Copyright (c) 2023 aaronk6, marothe
--
-- 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.03,
description = "Fetches balances from mynano.ninja and returns them as securities",
services = { "Nano" },
}
local currencyName = "Nano"
local currency = "EUR" -- fixme: Don't hardcode
local currencyField = "eur"
local currencyId = "nano"
local marketName = "CoinGecko"
local priceUrl = "https://api.coingecko.com/api/v3/simple/price?ids=" .. currencyId .. "&vs_currencies=" .. currencyField
local balanceUrl = "https://mynano.ninja/api/node"
local apiErrorString = "Failed to get balance from API"
local apiUnknownError = "Unknown Error"
local postContentType = "application/json"
local jsonObject = {}
jsonObject["action"] = "account_balance"
local addresses
local balances
function SupportsBank (protocol, bankCode)
return protocol == ProtocolWebBanking and bankCode == "Nano"
end
function InitializeSession (protocol, bankCode, username, username2, password, username3)
addresses = strsplit(",%s*", username)
end
function ListAccounts (knownAccounts)
local account = {
name = currencyName,
accountNumber = currencyName,
currency = currency,
portfolio = true,
type = "AccountTypePortfolio"
}
return {account}
end
function RefreshAccount (account, since)
local s = {}
local prices = queryPrices()
local balances = queryBalances(addresses)
for i,v in ipairs(addresses) do
s[i] = {
name = v,
currency = nil,
market = marketName,
quantity = balances[i] / 1000000000000000000000000000000,
price = prices[currencyField],
}
end
return {securities = s}
end
function EndSession ()
end
function queryPrices()
local connection = Connection()
local res = JSON(connection:request("GET", priceUrl))
return res:dictionary()[currencyId]
end
function queryBalances(addresses)
local connection = Connection()
local balances = {}
local res
for key, address in pairs(addresses) do
jsonObject["account"]=address
local jsonString = JSON():set(jsonObject):json()
res = JSON(connection:request("POST", balanceUrl, jsonString, postContentType))
if res:dictionary()["error"] then
apiError(res:dictionary()["error"])
elseif res:dictionary()["balance"] == nil then
apiError(apiUnknownError)
end
table.insert(balances, res:dictionary()["balance"])
end
return balances
end
function apiError(msg)
error(apiErrorString .. " (" .. msg .. ")")
end
-- from http://lua-users.org/wiki/SplitJoin
function strsplit(delimiter, text)
local list = {}
local pos = 1
if string.find("", delimiter, 1) then -- this would result in endless loops
error("delimiter matches empty string!")
end
while 1 do
local first, last = string.find(text, delimiter, pos)
if first then -- found?
table.insert(list, string.sub(text, pos, first-1))
pos = last+1
else
table.insert(list, string.sub(text, pos))
break
end
end
return list
end