Created
February 16, 2023 17:52
-
-
Save UncleBarney/d19f2144b21af739ee873d459dcf8e5b to your computer and use it in GitHub Desktop.
carbon_deploy_fund
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import pymysql | |
import config | |
import sys | |
import urllib3 | |
import requests | |
from decimal import * | |
from datetime import date, datetime, timedelta | |
from binance.client import Client | |
logging.basicConfig(level=logging.INFO) | |
logger = logging.getLogger() | |
getcontext().prec = 10 | |
dbconnection = None | |
http = urllib3.PoolManager() | |
dbconnection = None | |
binanceClient = None | |
try: | |
dbconnection = pymysql.connect(host=config.db_address, | |
user=config.db_username, | |
db=config.db_name, | |
passwd=config.db_password, | |
connect_timeout=5, | |
autocommit=True) | |
except pymysql.MySQLError as e: | |
logger.error("Unable to connect to MySQL instance") | |
logger.error(e) | |
sys.exit() | |
binanceClient = Client(api_key=config.binance_api_key, | |
api_secret=config.binance_api_secret) | |
def get_account_net(binanceClient): | |
url = "https://api.binance.com/api/v3/ticker/price?symbol=BTCUSDC" | |
data = requests.get(url) | |
data = data.json() | |
price = Decimal(data['price']) | |
count = Decimal(binanceClient.get_margin_account()['totalNetAssetOfBtc']) | |
print('Current market price is BTC is: {} and we have {} BTC in margin account. Net value is {}'.format(price, count, price * count)) | |
return str(price * count) | |
def deploy_balance(dbConnection, balance): | |
try: | |
update_query = 'Update `properties_config` set config_value=%s, update_time=%s where id="1"' | |
with dbConnection.cursor() as cur: | |
cur.execute(update_query, (balance, datetime.today().strftime('%Y-%m-%d'))) | |
dbconnection.commit() | |
print('balance updated in DB') | |
except Exception as e: | |
print('Unable to update the db balance: {}'.format(e)) | |
def lambda_handler(event, context): | |
print('Deploying fund for {}'.format(datetime.now())) | |
balance = get_account_net(binanceClient) | |
deploy_balance(dbconnection, balance) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment