Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/fa #98

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 47 additions & 4 deletions modules/calculation/price_fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@
from modules.calculation.abi import ERC20ABI, LPABI
from modules.balancer.balancer_main import Balancer
import decimal
from loguru import logger

balancer = Balancer()

_net = 1
_net_short = 2
_net_extra = 3
_pool_main_contract = 4
_pool_address = 5
_time_created = 6
_token1_address = 7
_token1_symbol = 8
_token2_address = 9
_token2_symbol = 10
_dex = 11
_creation_block = 12

#web3 = Web3(Web3.HTTPProvider("https://eth.llamarpc.com"))

Expand All @@ -13,10 +29,12 @@

# def __init__(self):

def uni_v2_price(block_num, token0, token1, pool, net, dex):
def uni_v2_price(web3, block_num, token0, token1, pool, net, dex):
#token0, token1 = lpContract.functions.token0().call(), lpContract.functions.token1().call()
#reserves = lpContract.functions.getReserves().call(block_identifier=block_num)
print(reserves)
lp_contract = web3.eth.contract(address = pool, abi=LPABI)
reserves = lp_contract.functions.getReserves().call(block_identifier=block_num)
print(f"{reserves} RESERVADOS")
print(token0, token1)
tkc0 = web3.eth.contract(address=token0, abi=ERC20ABI)
tkc1 = web3.eth.contract(address=token1, abi=ERC20ABI)
decimal0 = tkc0.functions.decimals().call()
Expand All @@ -37,5 +55,30 @@ def price_fetch(web3, creation_block, token0, token1, pool, net, dex):
latest_block = web3.eth.block_number
print(creation_block, latest_block)
for block in range(creation_block, latest_block + 1):
uni_v2_price(block, token0, token1, pool, net, dex)
uni_v2_price(web3, block, token0, token1, pool, net, dex)

def price_fetch_v2(lp, block_num):
w3 = balancer.w3(lp[_net])
logger.debug(f"w3.is_connected() {w3.is_connected()}")
lp_contract = w3.eth.contract(address=lp[_pool_address], abi=LPABI)
logger.debug(f"lp_contract:{lp_contract}")
reserves = lp_contract.functions.getReserves().call(block_identifier=block_num)
logger.debug(f"reserves: {reserves}")
w3 = balancer.w3(lp[_net])
token0_contract = w3.eth.contract(address=lp[_token1_address], abi=ERC20ABI)
decimal0 = token0_contract.functions.decimals().call()
logger.debug(f"decimal0: {decimal0}")
token1_contract = w3.eth.contract(address=lp[_token2_address], abi=ERC20ABI)
decimal1 = token1_contract.functions.decimals().call()
logger.debug(f"decimal1: {decimal1}")

reserve0 = float(reserves[0] / 10 **decimal0)
reserve1 = float(reserves[1] / 10 **decimal1)

#":.8f" fixes the print out to not use scientific notation
logger.debug(f"reserve0: {reserve0:.8f}")
logger.debug(f"reserve1: {reserve1:.8f}")

price = reserve0 / reserve1
form_price = "{:.10f}".format(price)
logger.debug(f"Price is: {form_price}")
34 changes: 17 additions & 17 deletions modules/fa/fa.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

goplus_url_base = 'https://api.gopluslabs.io/api/v1/token_security/'
dexscreener_url_base = 'https://cfw.dexscreener.com/sc/dex:'
dextools_url_base = 'https://www.dextools.io/shared/data/pair?address='
#dextools_url_base = 'https://www.dextools.io/shared/data/pair?address='

def goplus_fa(network, token):
if network == "pls":
Expand Down Expand Up @@ -104,27 +104,27 @@ def dexscreener_fa(network, pool):
is_passed = 0
return is_passed

def dextools_fa(network, pool):
custom_url = (dextools_url_base + pool.lower() + "&chain=" +
network + "&audit=true&locks=true")
print(custom_url)
res = requests.get(custom_url, headers=headers)
res_json = res.json()
dext_score = int(res_json["data"][0]["dextScore"]["total"])
if dext_score >= 50:
is_passed = 1
print("Dextools is PASSED")
elif dext_score <= 50:
is_passed = 0
print("dext_score is :", dext_score)
return is_passed
#def dextools_fa(network, pool):
# custom_url = (dextools_url_base + pool.lower() + "&chain=" +
# network + "&audit=true&locks=true")
# print(custom_url)
# res = requests.get(custom_url, headers=headers)
# res_json = res.json()
# dext_score = int(res_json["data"][0]["dextScore"]["total"])
# if dext_score >= 50:
# is_passed = 1
# print("Dextools is PASSED")
# elif dext_score <= 50:
# is_passed = 0
# print("dext_score is :", dext_score)
# return is_passed

def full_fa(name, token, lp, chain_short, chain_extra, chain):
print(name, token, lp, chain_short, chain_extra, chain)
chain_name_arr = token_parser(chain)
goplus_analysis = goplus_fa(chain_name_arr[0], token)
dexscreener_analysis = dexscreener_fa(chain, lp)
dextools_analysis = dextools_fa(chain_extra, lp)
full_result = (goplus_analysis + dexscreener_analysis + dextools_analysis)
#dextools_analysis = dextools_fa(chain_extra, lp)
full_result = (goplus_analysis + dexscreener_analysis)
print("Score is: ", full_result)
return full_result
2 changes: 1 addition & 1 deletion modules/ta/ta_iterate.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ def iterate():
#price_fetch()
print(pool)
result = price_fetch(
w3, pool[12], pool[10], pool[9], pool[5], pool[1], pool[1])
w3, pool[12], pool[9], pool[7], pool[5], pool[1], pool[1])

17 changes: 17 additions & 0 deletions test_price_fetch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from modules.calculation.price_fetch import *
from loguru import logger
from modules.balancer.balancer_main import Balancer
import db.main as db
logger.debug("Imports done")

balancer = Balancer()

pool_list = db.get_all_pools('pools_found')
logger.debug(pool_list[8])
w3 = balancer.w3(pool_list[8][1])
block_number = w3.eth.block_number

price_fetch_v2(pool_list[8], block_number)

#price in usd for 1 Polygon
#0.000000000006549692684170795