Skip to content

Commit

Permalink
Added --price, --user argument. Editing script no longer necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
freQniK committed Jul 30, 2022
1 parent a817665 commit 67b0258
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 6 deletions.
File renamed without changes.
44 changes: 38 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,48 @@ sudo apt install python3-pip
sudo pip install toml pycoingecko
```

Edit the lines in the python script that say **EDIT**
to your specifications.
Lines 16,17,22
## Note
It is no longer necessary to edit the file as we have provided command line arguments for the configuration. Please see below.

# Run
```shell
sudo python3 dvpn_coin_conf.py --twap days
$ sudo python3 dvpn_price.py --help
usage: dvpn_price.py [-h] [-t twap] [-p price] [-u user]

dVPN Price Oracle for dVPN Node operators v0.3.2

optional arguments:
-h, --help show this help message and exit
-t twap, --twap twap Time Weighted Average Price. --twap days
-p price, --price price
Set the price per GB you would like to charge in USD.
i.e., --price 0.005
-u user, --user user Set the base directory where .sentinelnode/ exists
i.e., --user dvpn - implies (/home/dvpn/.sentinelnode)

```

Where **days** in `--twap` is the number of days to average a price over based on market price of the coin for each previous day.

## Example
```shell
sudo python3 dvpn_price.py --twap 10 --price 0.001 --user sentinel
```

Where **days** is the number of days to average a price over based on market price of the coin for each previous day.
This will average the price of *OSMO, SCRT, ATOM, DEC, DVPN* over the last 10 days. It will set a price of *$0.001/GB* and change the **config.toml** in the directory `/home/sentinel/.sentinelnode/config.toml`

## Cronjob
You can also create a cronjob (as root) to have this run every week, every month, every day, every hour, every minute. Just be sure to restart your node eventually for the changes to take place.

### Example
```shell
sudo crontab -e
```

Place the following line at the bottom of the file:
```
59 12 * * * python3 /home/sentinel/Scripts/dvpn_price.py --twap 14 --price 0.003 --user sentinel
```

You can also create a cronjob (as root) to have this run every week, every month, every day, every hour, every minute. Just be sure to restart your node eventually for the changes to take place.
This will update your sentinel node **config.toml** every day at 12:59 p.m.

105 changes: 105 additions & 0 deletions dvpn_price.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# 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 pycoingecko import CoinGeckoAPI
from os import path
import argparse
import datetime
import time
from statistics import mean


# If not specified, node provider will charge this rate.
# $0.005/GB
PRICE = 5.00
GB = 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' : 'dvpn', 'osmosis' : IBCOSMO, 'decentr' : IBCDEC, 'cosmos' : IBCATOM, 'secret' : IBCSCRT}

SATOSHI = 1000000

def CoinGeckoPrices(days):
cg = CoinGeckoAPI()
today = datetime.datetime.now()
#CoinPrices = cg.get_price(list(COINS.keys()), 'usd')
CoinPrices = {}
price_data = {coin: [] for coin in list(COINS.keys())}
for k in range(1,days+1):
for coin in list(COINS.keys()):
delta = datetime.timedelta(days=k)
yesterday = today - delta
data = cg.get_coin_history_by_id(id=coin, date=yesterday.strftime("%d-%m-%Y"),vs_currencies='usd')
price_data[coin].append(data["market_data"]["current_price"]["usd"])
#print(price_data[coin])
time.sleep(2)

for coin in list(COINS.keys()):
CoinPrices[coin] = mean(price_data[coin])
#print(CoinPrices)
return CoinPrices

def CalculateRate(coin_price):
return (float(PRICE/float((GB*coin_price))))*SATOSHI

if __name__ == "__main__":
IBCPRICES = {}
parser = argparse.ArgumentParser(description="dVPN Price Oracle for dVPN Node operators v0.3.2")
parser.add_argument('-t', '--twap', help="Time Weighted Average Price. --twap days", metavar='twap')
parser.add_argument('-p', '--price', help="Set the price per GB you would like to charge in USD. i.e., --price 0.005", metavar='price')
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()



if args.twap:
days = int(args.twap)
else:
days = 1
CoinPrices = CoinGeckoPrices(days)

if args.price:
PRICE = GB*float(args.price)

if args.user:
USERBASEDIR = '/home/' + args.user

BASEDIR = path.join(USERBASEDIR, '.sentinelnode')

for coin in CoinPrices.keys():
if 'sentinel' in coin:
IBCPRICES['udvpn'] = int(CalculateRate(CoinPrices[coin]))
else:
IBCPRICES[COINS[coin]] = int(CalculateRate(CoinPrices[coin]))

print(IBCPRICES)


with open(path.join(BASEDIR,'config.toml')) as CONF:
toml_string = CONF.read()
DVPNCONFIG = toml.loads(toml_string)
print(DVPNCONFIG['node']['price'])
NodePrices = []
for k,v in IBCPRICES.items():
NodePrices.append(''.join([str(v),str(k)]))

node_price_string = ''
for np in NodePrices:
node_price_string = ','.join([node_price_string, np])
node_price_string = node_price_string.replace(',','',1)
print(node_price_string)
DVPNCONFIG['node']['price'] = node_price_string
CONF = open(path.join(BASEDIR,'config.toml'), 'w')
toml.dump(DVPNCONFIG, CONF)

0 comments on commit 67b0258

Please sign in to comment.