-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcoingecko.lua
More file actions
416 lines (353 loc) · 12.6 KB
/
coingecko.lua
File metadata and controls
416 lines (353 loc) · 12.6 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
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
-- CoinGecko Extension for MoneyMoney
-- Fetches balance from your crypto wallets using CoinGecko API
--
-- Specify your crypto wallet addresses as username
-- Username: BTC(addr1, addr2), ETH(addr3, addr4), SOL(addr5), ...
-- SOL automatically discovers all SPL tokens in the wallet!
--
-- Copyright (c) 2024-2025 Robert Gering
--
-- 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 = 2.1,
country = "de",
url = "https://api.coingecko.com",
description = string.format(MM.localizeText("Track Bitcoin, Ethereum, Solana + auto-discover all SPL tokens. Powered by CoinGecko prices.")),
services = { "CoinGecko" },
}
-- State
local wallets
local allCoins
local allCoinsById -- Mapping of CoinGecko ID -> coin data
local prices
local solanaTokenMap -- Mapping of Solana mint address -> CoinGecko ID
-- Constants
local currency = "EUR"
local coins = {
btc = { id = "bitcoin", name = "Bitcoin" },
eth = { id = "ethereum", name = "Ethereum" },
ltc = { id = "litecoin", name = "Litecoin" },
doge = { id = "dogecoin", name = "Dogecoin" },
bch = { id = "bitcoin-cash", name = "Bitcoin Cash" },
bsv = { id = "bitcoin-cash-sv", name = "Bitcoin Cash SV" },
iot = { id = "iota", name = "IOTA" },
usdt = { id = "tether", name = "Tether" },
sol = { id = "solana", name = "Solana" },
}
local contractERC20Addresses = {
usdt = "0xdac17f958d2ee523a2206206994597c13d831ec7"
}
-- Configuration for native coins and their fetch methods
local nativeCoinConfig = {
btc = {
fetchMethod = "blockcypher",
decimals = 100000000 -- Satoshi to BTC
},
eth = {
fetchMethod = "blockcypher",
decimals = 1000000000000000000 -- Wei to ETH
},
sol = {
fetchMethod = "solana",
decimals = 1000000000 -- Lamports to SOL
}
}
function SupportsBank(protocol, bankCode)
return protocol == ProtocolWebBanking and bankCode == "CoinGecko"
end
function InitializeSession(protocol, bankCode, username, username2, password, username3)
wallets = parseAddresses(username)
if wallets == nil or next(wallets) == nil then
MM.printStatus("Wallet Adressen konnten nicht ermittelt werden")
return LoginFailed
end
-- Prices will be fetched in RefreshAccount after token discovery
end
function ListAccounts(knownAccounts)
local account = {
name = "CoinGecko",
accountNumber = "Wallet",
currency = currency,
portfolio = true,
type = AccountTypePortfolio
}
return {account}
end
function RefreshAccount(account, since)
local s = {}
local balances = fetchBalances(wallets)
-- Now fetch prices for all discovered coins (including dynamic SPL tokens)
prices = fetchPrices(balances)
if prices == nil then
MM.printStatus("Preise konnten nicht geladen werden")
return {}
end
for symbol, balance in pairs(balances) do
local coin = lookupCoin(symbol)
if not coin then
MM.printStatus("Coin nicht gefunden: " .. symbol:upper())
goto continue
end
local name = coin.name .. " (" .. symbol:upper() .. ")"
MM.printStatus("Verarbeite: " .. name .. ") ")
local priceData = prices[coin.id]
if not priceData or not priceData.eur then
MM.printStatus("Keine Preisdaten für: " .. coin.id)
goto continue
end
local price = priceData.eur
local amount = balance * price
table.insert(s, {
name = name,
market = "CoinGecko",
quantity = balance,
price = price,
currency = nil,
amount = amount
})
::continue::
end
return {securities = s}
end
function EndSession()
-- NOP
end
-- API Functions
function fetchAllCoins()
-- Return cached data if already loaded
if allCoins and allCoinsById and solanaTokenMap then
return allCoins
end
MM.printStatus("Lade Coin-Liste von CoinGecko")
local connection = Connection()
local content = connection:request("GET", "https://api.coingecko.com/api/v3/coins/list?include_platform=true")
local coinList = JSON(content):dictionary()
local coins = {}
local coinsById = {}
local solanaMap = {}
local solanaTokenCount = 0
for _, coin in ipairs(coinList) do
coins[coin.symbol] = coin
coinsById[coin.id] = coin -- Index by ID for lookup
-- Build Solana mint address -> CoinGecko ID mapping
if coin.platforms and coin.platforms.solana and coin.platforms.solana ~= "" then
solanaMap[coin.platforms.solana] = coin.id
solanaTokenCount = solanaTokenCount + 1
end
end
-- Cache for the session
allCoins = coins
allCoinsById = coinsById
solanaTokenMap = solanaMap
return coins
end
function fetchPrices(balances)
MM.printStatus("Lade Preise von CoinGecko")
local ids = {}
for symbol in pairs(balances) do
local coin = lookupCoin(symbol)
if coin and coin.id then
table.insert(ids, coin.id)
end
end
if #ids == 0 then
MM.printStatus("Keine gültigen Coin-IDs gefunden")
return {}
end
local connection = Connection()
local idsParam = table.concat(ids, ",")
local currencyParam = currency:lower()
local url = string.format("https://api.coingecko.com/api/v3/simple/price?ids=%s&vs_currencies=%s", idsParam, currencyParam)
local content = connection:request("GET", url)
local json = JSON(content)
return json:dictionary()
end
function fetchBalances(wallets)
local balances = {}
for symbol, address_list in pairs(wallets) do
local total_balance = 0
if symbol == "sol" then
-- Special handling for SOL: get SOL balance + all SPL tokens
for _, address in ipairs(address_list) do
-- Add SOL balance
local solBalance = fetchBalance(symbol, address)
total_balance = total_balance + solBalance
-- Find all SPL tokens for this wallet
local tokens = fetchAllSolanaTokens(address)
for mintAddress, tokenBalance in pairs(tokens) do
-- Get token info from CoinGecko
local tokenInfo = fetchTokenInfoFromCoinGecko(mintAddress)
if tokenInfo then
-- Store token balance with symbol as key
local tokenSymbol = tokenInfo.symbol:lower()
balances[tokenSymbol] = (balances[tokenSymbol] or 0) + tokenBalance
-- Add token to coins registry for price lookup
if not coins[tokenSymbol] then
coins[tokenSymbol] = tokenInfo
end
end
end
end
balances[symbol] = total_balance
else
-- Regular token handling
for _, address in ipairs(address_list) do
local balance = fetchBalance(symbol, address)
total_balance = total_balance + balance
end
balances[symbol] = total_balance
end
end
return balances
end
function fetchBalance(symbol, address)
-- Check if it's a native coin
local nativeConfig = nativeCoinConfig[symbol]
if nativeConfig then
if nativeConfig.fetchMethod == "blockcypher" then
local rawBalance = fetchBlockcypherBalance(symbol, address)
return rawBalance / nativeConfig.decimals
elseif nativeConfig.fetchMethod == "solana" then
return fetchSolanaBalance(symbol, address)
end
end
-- Check if it's an ERC20 token
if contractERC20Addresses[symbol] then
return fetchERC20Balance(symbol, address)
end
MM.printStatus("Nicht unterstützter Coin: " .. symbol:upper())
return 0
end
function fetchBlockcypherBalance(symbol, address)
MM.printStatus("Lade " .. symbol:upper() .. " Bestand von Blockcypher API")
local connection = Connection()
local url = string.format("https://api.blockcypher.com/v1/%s/main/addrs/%s/balance", symbol, address)
local content = connection:request("GET", url)
return JSON(content):dictionary()["final_balance"]
end
function fetchSolanaBalance(symbol, address)
MM.printStatus("Lade " .. symbol:upper() .. " Bestand von Solana API")
local connection = Connection()
local url = "https://api.mainnet-beta.solana.com"
local payload = {
jsonrpc = "2.0", id = 1,
method = "getBalance",
params = {address}
}
local content = connection:request("POST", url, JSON():set(payload):json(), "application/json")
local lamports = JSON(content):dictionary()["result"]["value"]
local decimals = nativeCoinConfig[symbol] and nativeCoinConfig[symbol].decimals or 1000000000
return lamports / decimals
end
function fetchAllSolanaTokens(address)
MM.printStatus("Lade alle Token für SOL Wallet")
local connection = Connection()
local url = "https://api.mainnet-beta.solana.com"
-- Get all token accounts for this wallet
local payload = {
jsonrpc = "2.0", id = 1,
method = "getTokenAccountsByOwner",
params = {
address,
{programId = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"},
{encoding = "jsonParsed"}
}
}
local content = connection:request("POST", url, JSON():set(payload):json(), "application/json")
local result = JSON(content):dictionary()
local tokens = {}
if result["result"] and result["result"]["value"] then
for _, tokenAccount in ipairs(result["result"]["value"]) do
local mintAddress = tokenAccount["account"]["data"]["parsed"]["info"]["mint"]
local balance = tonumber(tokenAccount["account"]["data"]["parsed"]["info"]["tokenAmount"]["uiAmount"]) or 0
if balance > 0 then -- Only include tokens with balance
tokens[mintAddress] = balance
end
end
end
return tokens
end
function fetchTokenInfoFromCoinGecko(mintAddress)
-- Ensure CoinGecko token map is loaded (will use cache if already loaded)
fetchAllCoins()
-- Lookup token by mint address in CoinGecko's Solana platform mapping
local coingeckoId = solanaTokenMap[mintAddress]
if not coingeckoId then
return nil
end
-- Lookup token by ID in allCoinsById
local tokenInfo = allCoinsById[coingeckoId]
if tokenInfo then
return {
symbol = tokenInfo.symbol:upper(),
name = tokenInfo.name,
id = tokenInfo.id
}
end
return nil
end
function fetchERC20Balance(symbol, address)
MM.printStatus("Lade " .. symbol:upper() .. " Bestand von CoinGecko API")
local connection = Connection()
local contractAddress = contractERC20Addresses[symbol]
if contractAddress then
local url = string.format("https://api.coingecko.com/api/v3/simple/token_price/ethereum?contract_addresses=%s&vs_currencies=%s", contractAddress, currency:lower())
local content = connection:request("GET", url)
local json = JSON(content)
local balance = json:dictionary()[contractAddress] and json:dictionary()[contractAddress][currency:lower()]
return tonumber(balance) or 0
end
return 0
end
-- Helper
-- Parse the username string and extract wallet addresses
-- Expected format: BTC(addr1, addr2), ETH(addr3, addr4), SOL(addr5), ...
function parseAddresses(username)
local addresses = {}
for symbol, address_str in string.gmatch(username, "([A-Z]+)%(([^)]+)%)") do
local symbol_lower = symbol:lower()
addresses[symbol_lower] = {}
for address in string.gmatch(address_str, "%s*([^,]+)%s*") do
table.insert(addresses[symbol_lower], address)
MM.printStatus("Gefunden: " .. symbol:upper() .. " (" .. address .. ")")
end
end
return addresses
end
function lookupCoin(symbol)
-- first, try hardcoded local coins
local coin = coins[symbol]
if coin == nil then
-- if not found, try to fetch all coins from CoinGecko
if allCoins == nil then
allCoins = fetchAllCoins()
if allCoins == nil then
MM.printStatus("Crypto Coins konnten nicht geladen werden")
return nil
end
end
coin = allCoins[symbol]
end
if coin == nil then
MM.printStatus("Nicht gefunden: " .. symbol:upper())
return nil
end
return coin
end
-- SIGNATURE: MC4CFQCiAIuMzYtxAfKikuSJlXC2wk2oMgIVAI8fKN0LWcHRFxO449ODkf/M6ulZ