-
Notifications
You must be signed in to change notification settings - Fork 1
/
dvpn_price.py
190 lines (150 loc) · 6.37 KB
/
dvpn_price.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
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
# ONLY FOR dVPN Nodes v0.3.2
# sudo apt install python3-pip
# sudo pip install toml pycoingecko
# sudo python3 dvpn_coin_conf.py
import toml
from os import path
import argparse
import time
from statistics import mean
import requests
from datetime import datetime,timedelta, timezone
import random
import scrtxxs
VERSION = "v0.4.3"
# If not specified, node provider will charge this rate.
# $0.008/GB,$0.005/hr
GBPRICE = 8.00
HRPRICE = 5.00
GB = 1000
HR = 1000
# if not specified with --user username this is the default. i.e.,
# /home/sentinel/.sentinelnode/config.toml
USERBASEDIR = "/home/sentinel"
IBCSCRT = 'ibc/31FEE1A2A9F9C01113F90BD0BBCCE8FD6BBB8585FAF109A2101827DD1D5B95B8'
IBCATOM = 'ibc/A8C2D23A1E6F95DA4E48BA349667E322BD7A6C996D8A4AAE8BA72E190F3D1477'
IBCDEC = 'ibc/B1C0DDB14F25279A2026BC8794E12B259F8BDA546A3C5132CCAEE4431CE36783'
IBCOSMO = 'ibc/ED07A3391A112B175915CD8FAF43A2DA8E4790EDE12566649D0C2F97716B8518'
COINS = {'sentinel' : 'udvpn', 'osmosis' : IBCOSMO, 'decentr' : IBCDEC, 'cosmos' : IBCATOM, 'secret' : IBCSCRT}
SATOSHI = 1000000
COINSTATS_API = "https://openapiv1.coinstats.app/coins/price/avg?coinId=%s×tamp=%s"
def CoinGeckoPrices(days):
today = datetime.now(timezone.utc)
CoinPrices = {}
price_data = {coin: [] for coin in list(COINS.keys())}
for k in range(1,days+1):
N = random.randint(0,len(scrtxxs.COINSTATS_API_KEYS)-1)
API_KEY = scrtxxs.COINSTATS_API_KEYS[N]
headers = {
"accept": "application/json",
"X-API-KEY": f"{API_KEY}"
}
for coin in list(COINS.keys()):
day_delta = today - timedelta(days=k)
ts = int(day_delta.timestamp())
try:
response = requests.get(COINSTATS_API % (coin, ts), headers=headers)
r = response.json()
except Exception as e:
print(str(e))
time.sleep(5)
continue
price_data[coin].append(r['USD'])
#print(price_data[coin])
time.sleep(3)
for coin in list(COINS.keys()):
CoinPrices[coin] = mean(price_data[coin])
#print(CoinPrices)
return CoinPrices
def CalculateGBRate(coin_price,coin,minp):
price = (float(GBPRICE/float((GB*coin_price))))*SATOSHI
for key,value in COINS.items():
if key == coin:
for ibc,mu_price in minp.items():
if value == ibc:
if price < float(mu_price):
price = float(mu_price)
return price
def CalculateHrRate(coin_price,coin,minp):
price = (float(HRPRICE/float((HR*coin_price))))*SATOSHI
for key,value in COINS.items():
if key == coin:
for ibc,mu_price in minp.items():
if value == ibc:
if price < float(mu_price):
price = float(mu_price)
return price
def ParseNodePrices(gb,hr):
NodeGBPrices = []
NodeHrPrices = []
for k,v in gb.items():
NodeGBPrices.append(''.join([str(v),str(k)]))
node_price_string = ''
for np in NodeGBPrices:
node_price_string = ','.join([node_price_string, np])
node_gb_price_string = node_price_string.replace(',','',1)
for k,v in hr.items():
NodeHrPrices.append(''.join([str(v),str(k)]))
node_price_string = ''
for np in NodeHrPrices:
node_price_string = ','.join([node_price_string, np])
node_hr_price_string = node_price_string.replace(',','',1)
return node_gb_price_string,node_hr_price_string
if __name__ == "__main__":
print(f"dVPN Price Oracle for dVPN Node operators {VERSION} - freQniK\n\n")
IBCGBPRICES = {}
IBCHRPRICES = {}
parser = argparse.ArgumentParser(description=f"dVPN Price Oracle for dVPN Node operators {VERSION} - freQniK")
parser.add_argument('-t', '--twap', help="Time Weighted Average Price.", metavar='days')
parser.add_argument('-p', '--price-gb', help="Set the price per GB you would like to charge in USD. i.e., --price 0.005", metavar='price')
parser.add_argument('-q', '--price-hr', help="Set the price per hour you would like to charge in USD. i.e., --price 0.005", metavar='hprice')
parser.add_argument('-u', '--user', help="Set the base directory where .sentinelnode/ exists i.e., --user dvpn - implies (/home/dvpn/.sentinelnode)", metavar='user')
args = parser.parse_args()
r = requests.get('https://aimokoivunen.mathnodes.com:5000/api/minprices')
MINPRICES = r.json()
MIN_GB_PRICES = MINPRICES['MinGB']
MIN_HR_PRICES = MINPRICES['MinHr']
print("MIN PRICES: ")
print(MIN_GB_PRICES)
print(MIN_HR_PRICES)
#a = input("Press Enter to continue....")
if args.twap:
days = int(args.twap)
else:
days = 1
if args.price_gb:
GBPRICE = GB*float(args.price_gb)
if args.price_hr:
HRPRICE = HR*float(args.price_hr)
if args.user:
USERBASEDIR = '/home/' + args.user
BASEDIR = path.join(USERBASEDIR, '.sentinelnode')
print("Getting TWAP prices for all coins from Coingecko....")
CoinPrices = CoinGeckoPrices(days)
for coin in CoinPrices.keys():
if 'sentinel' in coin:
IBCGBPRICES['udvpn'] = int(CalculateGBRate(CoinPrices[coin],coin,MIN_GB_PRICES))
else:
IBCGBPRICES[COINS[coin]] = int(CalculateGBRate(CoinPrices[coin],coin,MIN_GB_PRICES))
for coin in CoinPrices.keys():
if 'sentinel' in coin:
IBCHRPRICES['udvpn'] = int(CalculateHrRate(CoinPrices[coin],coin,MIN_HR_PRICES))
else:
IBCHRPRICES[COINS[coin]] = int(CalculateHrRate(CoinPrices[coin],coin,MIN_HR_PRICES))
print("GB Prices:")
print(IBCGBPRICES)
print("\n")
print("Hourly Prices:")
print(IBCHRPRICES)
with open(path.join(BASEDIR,'config.toml')) as CONF:
toml_string = CONF.read()
DVPNCONFIG = toml.loads(toml_string)
#print(DVPNCONFIG['node']['gigabit_prices'])
#print(DVPNCONFIG['node']['hourly_prices'])
node_gb_price_string,node_hr_price_string = ParseNodePrices(IBCGBPRICES, IBCHRPRICES)
print(node_gb_price_string)
print(node_hr_price_string)
DVPNCONFIG['node']['gigabyte_prices'] = node_gb_price_string
DVPNCONFIG['node']['hourly_prices'] = node_hr_price_string
CONF = open(path.join(BASEDIR,'config.toml'), 'w')
toml.dump(DVPNCONFIG, CONF)