Skip to content

Commit

Permalink
Merge pull request #50 from mraniki/dev
Browse files Browse the repository at this point in the history
fix: cleanup the logger, updated env var name, updated the example.
  • Loading branch information
mraniki authored Apr 13, 2023
2 parents 191d782 + 65041e0 commit 886f524
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 26 deletions.
52 changes: 30 additions & 22 deletions dxsp/main.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import os
import logging
from dotenv import load_dotenv
import json
import requests
import asyncio

import logging

from dotenv import load_dotenv

from web3 import Web3
from pycoingecko import CoinGeckoAPI

from dxsp.assets.blockchains import blockchains




class DexSwap:


Expand Down Expand Up @@ -99,26 +101,26 @@ def _get(url, params=None, headers=None):
return response.json()

async def get_quote(self, symbol):
self.logger.debug(f"get_quote {symbol}")
asset_in_address = await self.search_contract(symbol)
self.logger.debug(f"asset_in_address {asset_in_address}")
asset_out_address = await self.search_contract(self.base_trading_symbol)
self.logger.debug(f"asset_out_address {asset_out_address}")
try:
if self.protocol_type == "1inch":
asset_out_amount=1000000000000
quote_url = f"{self.dex_url}/quote?fromTokenAddress={asset_in_address}&toTokenAddress={asset_out_address}&amount={asset_out_amount}"
quote = self._get(quote_url)
return quote['toTokenAmount']
if self.protocol_type in ["uniswap_v2","uniswap_v3"]:
return
except Exception as e:
self.logger.debug(f"error {e}")
self.logger.debug(f"get_quote {symbol}")
asset_in_address = await self.search_contract(symbol)
self.logger.debug(f"asset_in_address {asset_in_address}")
asset_out_address = await self.search_contract(self.base_trading_symbol)
self.logger.debug(f"asset_out_address {asset_out_address}")
try:
if self.protocol_type == "1inch":
asset_out_amount=1000000000000
quote_url = f"{self.dex_url}/quote?fromTokenAddress={asset_in_address}&toTokenAddress={asset_out_address}&amount={asset_out_amount}"
quote = self._get(quote_url)
return quote['toTokenAmount']
if self.protocol_type in ["uniswap_v2","uniswap_v3"]:
return
except Exception as e:
self.logger.debug(f"error {e}")
return

async def get_abi(self,addr):
self.logger.debug(f"get_abi {addr}")
try:
self.logger.debug(f"get_abi {addr}")
url = self.block_explorer_url
params = {
"module": "contract",
Expand Down Expand Up @@ -236,6 +238,7 @@ async def get_swap(self,
asset_out_balance = await self.get_token_balance(asset_out_symbol)
self.logger.debug(f"asset_out_balance {asset_out_balance} {asset_out_symbol}")
if asset_out_balance == 0:
self.logger.debug(f"No Money on {asset_out_balance} balance: {asset_out_balance}")
return
#ASSETS IN
asset_in_address = await self.search_contract(asset_in_symbol)
Expand Down Expand Up @@ -290,6 +293,7 @@ async def get_swap(self,
if self.protocol_type ['uniswap_v3']:
return
if swap_TX:
self.logger.debug(f"swap_TX {swap_TX}")
signed_TX = await self.get_sign(swap_TX)
txHash = str(self.w3.to_hex(signed_TX))
txResult = await self.get_block_explorer_status(txHash)
Expand All @@ -307,15 +311,16 @@ async def get_block_explorer_status (self,txHash):
return checkTransactionRequest['status']

async def search_gecko_contract(self,token):
self.logger.debug(f"get_block_explorer_status {txHash}")
try:
self.logger.debug(f"get_block_explorer_status {txHash}")
coin_info = await self.search_gecko(token)
return coin_info['platforms'][f'{coin_platform}']
except Exception as e:
self.logger.debug(f"error {e}")
return

async def search_gecko(self,token):
self.logger.debug(f"search_gecko {token}")
try:
search_results = self.gecko_api.search(query=token)
search_dict = search_results['coins']
Expand All @@ -334,6 +339,7 @@ async def search_gecko(self,token):
return

async def search_gecko_platform(self):
self.logger.debug(f"search_gecko_platform")
try:
assetplatform = self.gecko_api.get_asset_platforms()
output_dict = [x for x in assetplatform if x['chain_identifier'] == int(self.chain_id)]
Expand All @@ -358,8 +364,8 @@ async def search_contract(self, token):
self.logger.debug(f"search_contract {token}")
#📝tokenlist
main_list = 'https://raw.githubusercontent.com/viaprotocol_type/tokenlists/main/all_tokens/all.json'
personal_list = os.getenv("TOKEN_LIST", "https://raw.githubusercontent.com/mraniki/tokenlist/main/TT.json")
test_token_list=os.getenv("TEST_TOKEN_LIST", "https://raw.githubusercontent.com/mraniki/tokenlist/main/testnet.json")
personal_list = os.getenv("DXSP_TOKEN_LIST", "https://raw.githubusercontent.com/mraniki/tokenlist/main/TT.json")
test_token_list=os.getenv("DXSP_TEST_TOKEN_LIST", "https://raw.githubusercontent.com/mraniki/tokenlist/main/testnet.json")

try:
token_contract = await self.get_contract_address(main_list,token)
Expand All @@ -377,6 +383,7 @@ async def search_contract(self, token):
return

async def get_token_contract(self, token):
self.logger.debug(f"get_token_contract {token}")
try:
token_address= await self.search_contract(token)
token_abi= await self.get_abi(token_address)
Expand All @@ -386,6 +393,7 @@ async def get_token_contract(self, token):
return

async def get_token_balance(self, token):
self.logger.debug(f"get_token_balance {token}")
try:
token_contract = await self.get_token_contract(token)
token_balance = token_contract.functions.balanceOf(self.wallet_address).call()
Expand Down
10 changes: 6 additions & 4 deletions examples/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@

#OPTIONAL PARAMETERS
#network_provider_url = os.getenv("NETWORK_PROVIDER_URL")

#DEX CONNECTIVITY
#w3 = Web3(Web3.HTTPProvider(network_provider_url))
#protocol_type = os.getenv("protocol_type", "uniswap_v2")
# dex_exchange = os.getenv("DEX_EXCHANGE", 'uniswap_v2')

# protocol_type = os.getenv("protocol_type", "uniswap_v2")
# dex_exchange = os.getenv("DEX_EXCHANGE", '0x1F98431c8aD98523631AE4a59f267346ea31F984')
# base_trading_symbol = os.getenv("BASE_TRADE_SYMBOL", 'USDT')
# amount_trading_option = os.getenv("AMOUNT_TRADING_OPTION", 1)

Expand All @@ -42,8 +44,8 @@ async def main():
dex = DexSwap(chain_id=chain,wallet_address=wallet_address,private_key=private_key,block_explorer_api=block_explorer_api)

#BUY 10 USDC to SWAP with BITCOIN
demo_tx = await dex.get_swap('USDT','wBTC',10)
print("demo_tx ", demo_tx)
#demo_tx = await dex.get_swap('USDT','wBTC',10)
#print("demo_tx ", demo_tx)

#SWAP with your OWN defined exchange like Sushiswap on ARBITRUM
# sushi_router = '0xd9e1cE17f2641f24aE83637ab66a2cca9C378B9F'
Expand Down

0 comments on commit 886f524

Please sign in to comment.