This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcoingecko_ticker.py
140 lines (114 loc) · 3.74 KB
/
coingecko_ticker.py
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
from pycoingecko import CoinGeckoAPI
import time
from decimal import Decimal
from constants import currencylist
def get_btcrates():
currencies = list(currencylist.keys())
cg = CoinGeckoAPI()
price = cg.get_price(ids='bitcoin', vs_currencies=currencies, include_last_updated_at='true')
last_update = price['bitcoin']['last_updated_at']
readable_time = time.ctime(last_update)
rateinfo = f'<b>Coingecko</b>, Last Updated:\n{readable_time}\n\n'
for symbol in currencies:
fiat = "{:,}".format(price['bitcoin'][symbol])
rateinfo += f'<b>BTC/{symbol.upper()}</b>\t\t\t{currencylist[symbol].strip()}{fiat}\n'
return rateinfo
def sats2btcTable():
header = 'Unit Scale: Satoshi to BTC\n'
content = ['1 sat = 0.00000001 BTC',
'10 sats = 0.0000001 BTC',
'100 sats = 0.000001 BTC',
'1,000 sats = 0.00001 BTC',
'10,000 sats = 0.0001 BTC',
'100,000 sats = 0.001 BTC',
'1,000,000 sats = 0.01 BTC',
'10,000,000 sats = 0.1 BTC',
'100,000,000 sats = 1.0 BTC']
table = '\n'.join(content)
msg = header+"\n"+table
return msg
def get_supported_currencies():
cg = CoinGeckoAPI()
currencies = cg.get_supported_vs_currencies()
#print(currencies)
return currencies
# /sats <amount> <fiat>
# e.g. /sats 100 USD
# 100,000,000 sats = 1 btc
def sats(amount, symbol, command):
satsperbtc = 100000000
sym = symbol.lower()
msg = ''
if '@' in command:
command = command.split('@')[0]
print(command)
currencies = get_supported_currencies()
if sym not in currencies:
msg = 'Fiat currency not found. Try another?'
return msg
cg = CoinGeckoAPI()
price = cg.get_price(ids='bitcoin', vs_currencies=symbol)
rate = price['bitcoin'][sym]
if command == '/btc':
fiat = rate* (amount)
fiat_amt = "%.2f" % fiat
msg = f'{fiat_amt} {symbol.upper()}'
if command == '/sats':
fiat = rate* (amount/satsperbtc)
fiat_amt = "%.2f" % fiat
msg = f'{fiat_amt} {symbol.upper()}'
elif command == '/fiat':
btc = amount/rate
btc_amt = "%.8f" % btc
sats = int(satsperbtc * btc)
msg = f'{btc_amt} BTC or {sats} sats'
return msg
def sats_convert(rawtext):
try:
if rawtext is None:
return 'Please give an amount and a symbol, e.g. 1000 usd'
content = rawtext.split(' ')
print(content)
amt = float(content[1].strip())
fiatsym = content[2].strip()
command = content[0]
msg = sats(amt, fiatsym, command)
return msg
except Exception as e:
print(e)
content = 'Cannot parse parameters, Error\n\n'
content += 'Usage:\n/btc 0.04 USD \t[btc to fiat]\n'
content += '/sats 1000 HKD \t[sats to fiat]\n'
content += '/fiat 100 HKD \t[fiat to btc/sats]\n'
return content
if __name__ == '__main__':
# real simple 'unit' test
'''
msg = sats2btcTable()
print(msg)
rateinfo = get_btcrates()
print(rateinfo)
currencies = get_supported_currencies()
print(currencies)
amount = 100000
#amount = 'asdlkfj'
symbol = 'usd'
msg = sats(amount, symbol)
print(msg)
'''
command = '/sats 100 USD'
print(command)
msg = sats_convert(command)
print(msg + "\n")
command = '/btc 0.1337 USD'
print(command)
msg = sats_convert(command)
print(msg + "\n")
command = '/fiat 1 USD'
print(command)
msg = sats_convert(command)
print(msg + "\n")
command = '/fiat asdfa USD'
print(command)
msg = sats_convert(command)
print(msg + "\n")