Skip to main content

A flexible python library to interact with evm-like DEX.

Project description

WEB3Dex python lib

A flexible python library to interact with evm-like DEX. WEB3Dex library provide a unified interface for SC based on uniswap-fork. Based on web3.

Supported Dex in chains

  • Avalance
  • Bsc
  • Cronos
  • Ethereum
  • Fantom
  • Moonbeam
  • Moonriver
  • Polygon

Get it ready

pip install git+https://github.com/0xfffangel/web3dex-python.git

How to start

Python script:

from web3dex.ethereum import Uniswap

uniswap = Uniswap()
USDC = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"

print("reserves: ", uniswap.reserves(USDC))
print("liquidity_in: ", uniswap.liquidity_in(USDC))
print("liquidity_out: ", uniswap.liquidity_out(USDC))
print("reserve_ratio: {:.18f}".format(uniswap.reserve_ratio(USDC)))
print("price: {:.18f}".format(uniswap.price(USDC)))

Result:

reserves:  [64985095.457761, 32622.06165275629, 1660409488]
liquidity_in:  64985095.457761
liquidity_out:  32622.565503187332
reserve_ratio:  0.0005019929788971377
price:  0.000500486992281985

How to swap them

import web3dex

# setup env
uniswap = web3dex.ethereum.Uniswap()
USDC = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
wallet_address = ""
private_key = ""
amount = 0.001

# approve token for wallet_address if now allowance
if not uniswap.check_approval(wallet_address, USDC):
    tx = uniswap.approve(token=USDC, wallet_address=wallet_address)
    signed_tx = uniswap.signTransaction(transaction = tx, private_key = private_key)
    tx_hash = uniswap.sendTransaction(signed_transaction = signed_tx)
    if not uniswap.waitTransaction(tx_hash):
        raise Exception("TransactionExpection: " + tx_hash.hex())

# swap from base to token
tx = uniswap.swapFromBaseToTokens(amount, USDC, wallet_address)
signed_tx = uniswap.signTransaction(transaction = tx, private_key = private_key)
tx_hash = uniswap.sendTransaction(signed_transaction = signed_tx)
if not uniswap.waitTransaction(tx_hash):
    raise Exception("TransactionExpection: " + tx_hash.hex())
print(tx_hash)

# get updated balances
print("base balance {:.18f}".format(uniswap.balance(wallet_address)))
print("USDC balance {:.18f}".format(uniswap.balance(wallet_address, USDC)))

# swap from token to base
tx = uniswap.swapFromTokensToBase(amount, USDC, wallet_address)
signed_tx = uniswap.signTransaction(transaction = tx, private_key = private_key)
tx_hash = uniswap.sendTransaction(signed_transaction = signed_tx)
if not uniswap.waitTransaction(tx_hash):
    raise Exception("TransactionExpection: " + tx_hash.hex())
print(tx_hash)

Open PR for new Dex

  1. Define a new Dex config json in the chain folder (ex for uniswap: web3dex/configs/ethereum/uniswap.json):
{
    "PROVIDER": "https://mainnet.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161",
    "FACTORY_ADDR": "0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f",
    "ROUTER_ADDR": "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D",
    "FACTORY_ABI_FILE": "./abi/uniswapv2_factory_abi.json",
    "ROUTER_ABI_FILE": "./abi/uniswapv2_router_abi.json",
    "LIQUIDITY_ABI_FILE": "./abi/uniswapv2_liquidity_abi.json",
    "BASE_SYMBOL": "ETH",
    "BASE_CONTRACT": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
}
  1. Add new Dex object in the chain script (ex: web3dex/configs/ethereum.py):
class Uniswap(Dex):
    def __init__(self):
        super().__init__(configs + "/uniswap.json"))
  1. Add the class name into the all groups to be listed:
all = [
    ...,
    Uniswap()
]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

web3dex-0.0.1.tar.gz (20.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

web3dex-0.0.1-py3-none-any.whl (40.9 kB view details)

Uploaded Python 3

File details

Details for the file web3dex-0.0.1.tar.gz.

File metadata

  • Download URL: web3dex-0.0.1.tar.gz
  • Upload date:
  • Size: 20.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for web3dex-0.0.1.tar.gz
Algorithm Hash digest
SHA256 2f634c215eb561eccc021f9cb30bc6e48089b489f08efbe9d801bdc85db0d98b
MD5 4412ebf40ef74754ceab27a3c821cc31
BLAKE2b-256 eaf3df611d9457823e34f86f3cff3031d2f54a38a28cef4ab6b9dfbc19460ac4

See more details on using hashes here.

File details

Details for the file web3dex-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: web3dex-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 40.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for web3dex-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0c58b504dc9a7f72a9334d51ccb2b176c011b9ffff67be9222acc47ad81c45a6
MD5 15417151d92e3a6134d26a601ecf20cc
BLAKE2b-256 c4355631f515293e0c8f0de988cdec8851aec3fb3826e0b48810f7c80a639ba3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page