A python library to work with multiple DEX based on web3
Last updated May 8, 2026
35
Stars
14
Forks
0
Issues
0
Stars/day
Attention Score
23
Topics
Language breakdown
Python 100.0%
▸ Files
click to expand
README
WEB3Dex ===
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 web3dex
How to start
Python script:from web3dex.ethereum import Uniswap
uniswap = Uniswap() USDC = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
print("reserves: ", uniswap.reserves(USDC)) print("liquidityin: ", uniswap.liquidityin(USDC)) print("liquidityout: ", uniswap.liquidityout(USDC)) print("reserveratio: {:.18f}".format(uniswap.reserveratio(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.checkapproval(walletaddress, USDC):
tx = uniswap.approve(token=USDC, walletaddress=walletaddress)
signedtx = uniswap.signTransaction(transaction = tx, privatekey = private_key)
txhash = uniswap.sendTransaction(signedtransaction = 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)
signedtx = uniswap.signTransaction(transaction = tx, privatekey = private_key)
txhash = uniswap.sendTransaction(signedtransaction = 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)
signedtx = uniswap.signTransaction(transaction = tx, privatekey = private_key)
txhash = uniswap.sendTransaction(signedtransaction = signed_tx)
if not uniswap.waitTransaction(tx_hash):
raise Exception("TransactionExpection: " + tx_hash.hex())
print(tx_hash)
Open PR for new Dex
- 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",
"FACTORYABIFILE": "./abi/uniswapv2factoryabi.json",
"ROUTERABIFILE": "./abi/uniswapv2routerabi.json",
"LIQUIDITYABIFILE": "./abi/uniswapv2liquidityabi.json",
"BASE_SYMBOL": "ETH",
"BASE_CONTRACT": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
}
- Add new Dex object in the chain script (ex:
web3dex/configs/ethereum.py):
class Uniswap(Dex):
def init(self):
super().init(configs + "/uniswap.json"))
- Add the class name into the
allgroups to be listed:
all = [
...,
Uniswap()
]🔗 More in this category