Copy from web3 import Web3
from eth_account import Account
# Configuration
MONBRIDGEDEX_ADDRESS = '0x7dd7fc9380e3107028a158f49bd25a8a8d48b225'
MONAD_RPC = 'https://rpc.monad.xyz'
CHAIN_ID = 143
# Connect to Monad
w3 = Web3(Web3.HTTPProvider(MONAD_RPC))
# Minimal ABI for the functions we need
MONBRIDGEDEX_ABI = [
{
"inputs": [
{"name": "amountIn", "type": "uint256"},
{"name": "path", "type": "address[]"},
{"name": "supportFeeOnTransfer", "type": "bool"},
{"name": "userSlippageBPS", "type": "uint16"}
],
"name": "getBestSwapData",
"outputs": [
{
"components": [
{"name": "swapType", "type": "uint8"},
{"name": "routerType", "type": "uint8"},
{"name": "router", "type": "address"},
{"name": "path", "type": "address[]"},
{"name": "v3Fees", "type": "uint24[]"},
{"name": "amountIn", "type": "uint256"},
{"name": "amountOutMin", "type": "uint256"},
{"name": "deadline", "type": "uint256"},
{"name": "supportFeeOnTransfer", "type": "bool"}
],
"type": "tuple"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"components": [
{"name": "swapType", "type": "uint8"},
{"name": "routerType", "type": "uint8"},
{"name": "router", "type": "address"},
{"name": "path", "type": "address[]"},
{"name": "v3Fees", "type": "uint24[]"},
{"name": "amountIn", "type": "uint256"},
{"name": "amountOutMin", "type": "uint256"},
{"name": "deadline", "type": "uint256"},
{"name": "supportFeeOnTransfer", "type": "bool"}
],
"name": "swapData",
"type": "tuple"
}
],
"name": "execute",
"outputs": [{"name": "amountOut", "type": "uint256"}],
"stateMutability": "payable",
"type": "function"
},
{
"inputs": [],
"name": "WETH",
"outputs": [{"name": "", "type": "address"}],
"stateMutability": "view",
"type": "function"
}
]
ERC20_ABI = [
{
"inputs": [
{"name": "spender", "type": "address"},
{"name": "amount", "type": "uint256"}
],
"name": "approve",
"outputs": [{"name": "", "type": "bool"}],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{"name": "owner", "type": "address"},
{"name": "spender", "type": "address"}
],
"name": "allowance",
"outputs": [{"name": "", "type": "uint256"}],
"stateMutability": "view",
"type": "function"
}
]
# Initialize contract
contract = w3.eth.contract(address=MONBRIDGEDEX_ADDRESS, abi=MONBRIDGEDEX_ABI)
def get_swap_quote(token_in, token_out, amount_in):
"""Get swap quote from MonBridgeDex"""
path = [token_in, token_out]
swap_data = contract.functions.getBestSwapData(
amount_in,
path,
False, # supportFeeOnTransfer
0 # auto slippage
).call()
return swap_data
def execute_swap(private_key, token_in, token_out, amount_in, slippage_bps=50):
"""Execute a swap on MonBridgeDex"""
account = Account.from_key(private_key)
# Get swap data
path = [token_in, token_out]
swap_data = contract.functions.getBestSwapData(
amount_in,
path,
False,
slippage_bps
).call()
# Check if ETH swap
weth_address = contract.functions.WETH().call()
is_eth_swap = token_in.lower() == weth_address.lower()
# Approve token if needed
if not is_eth_swap:
token_contract = w3.eth.contract(address=token_in, abi=ERC20_ABI)
allowance = token_contract.functions.allowance(
account.address,
MONBRIDGEDEX_ADDRESS
).call()
if allowance < amount_in:
approve_tx = token_contract.functions.approve(
MONBRIDGEDEX_ADDRESS,
2**256 - 1 # Max approval
).build_transaction({
'from': account.address,
'gas': 100000,
'gasPrice': w3.eth.gas_price,
'nonce': w3.eth.get_transaction_count(account.address),
'chainId': CHAIN_ID
})
signed_approve = account.sign_transaction(approve_tx)
approve_hash = w3.eth.send_raw_transaction(signed_approve.rawTransaction)
w3.eth.wait_for_transaction_receipt(approve_hash)
# Build swap transaction
swap_tx = contract.functions.execute(swap_data).build_transaction({
'from': account.address,
'value': amount_in if is_eth_swap else 0,
'gas': 500000,
'gasPrice': w3.eth.gas_price,
'nonce': w3.eth.get_transaction_count(account.address),
'chainId': CHAIN_ID
})
# Sign and send
signed_swap = account.sign_transaction(swap_tx)
swap_hash = w3.eth.send_raw_transaction(signed_swap.rawTransaction)
receipt = w3.eth.wait_for_transaction_receipt(swap_hash)
return receipt
# Usage example
if __name__ == "__main__":
USDC = "0x..." # USDC address
DAI = "0x..." # DAI address
PRIVATE_KEY = "0x..." # Your private key
amount = 100 * 10**6 # 100 USDC (6 decimals)
# Get quote
quote = get_swap_quote(USDC, DAI, amount)
print(f"Expected output: {quote[6] / 10**18} tokens")
# Execute swap
receipt = execute_swap(PRIVATE_KEY, USDC, DAI, amount, 50)
print(f"Swap successful: {receipt['transactionHash'].hex()}")