Python
Python integration patterns using web3.py. Since the Hydrex SDK is TypeScript-only, Python workflows typically use the SDK on the Node.js side for calldata generation and Python for signing/sending.
⚠
TypeScript-first SDK
The Hydrex SDK is a TypeScript package. For Python workflows, generate calldata using the SDK in a Node.js process or API, then forward it to Python for transaction signing and submission.
Setup
python
from web3 import Web3
RPC_URL = "https://mainnet.base.org"
w3 = Web3(Web3.HTTPProvider(RPC_URL))
# Contract addresses
SWAP_ROUTER = "0x6f4bE24d7dC93b6ffcBAb3Fd0747c5817Cea3F9e"
POSITION_MANAGER = "0xC63E9672f8e93234C73cE954a1d1292e4103Ab86"
VOTER = "0xc69E3eF39E3fFBcE2A1c570f8d3ADF76909ef17b"
# Load ABIs from @hydrexfi/hydrex-sdk package (exported directly)
# import { hydrexSwapRouterABI } from '@hydrexfi/hydrex-sdk'Reading Pool State
Use the exported ABIs from the SDK package to interact directly with Hydrex contracts via web3.py.
python
import json
from web3 import Web3
w3 = Web3(Web3.HTTPProvider("https://mainnet.base.org"))
# Load pool ABI (exported from SDK package)
pool_abi = json.loads('[...]') # paste pool ABI here
pool = w3.eth.contract(address="0xPoolAddress", abi=pool_abi)
# Read pool state
global_state = pool.functions.globalState().call()
sqrt_price_x96 = global_state[0]
current_tick = global_state[1]
liquidity = pool.functions.liquidity().call()
print(f"sqrtPriceX96: {sqrt_price_x96}")
print(f"Current tick: {current_tick}")
print(f"Liquidity: {liquidity}")Signing & Sending Transactions
Pass SDK-generated calldata to web3.py for signing and submission.
python
from web3 import Web3
from eth_account import Account
w3 = Web3(Web3.HTTPProvider("https://mainnet.base.org"))
private_key = "0xYourPrivateKey"
account = Account.from_key(private_key)
# Calldata generated by the TypeScript SDK
# Run the SDK on the server side and pass calldata to Python for signing
calldata = "0x..."
value = 0
tx = {
'from': account.address,
'to': "0x6f4bE24d7dC93b6ffcBAb3Fd0747c5817Cea3F9e",
'data': calldata,
'value': value,
'gas': 300000,
'gasPrice': w3.eth.gas_price,
'nonce': w3.eth.get_transaction_count(account.address),
'chainId': 8453, # Base mainnet
}
signed = account.sign_transaction(tx)
tx_hash = w3.eth.send_raw_transaction(signed.rawTransaction)
receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
print(f"Tx hash: {tx_hash.hex()}")