0

I want to swap tokenA to tokenB. Thequestion heppened:

  File "E:\Code\pythonProject\infura_API_test\3_swap_by_web3_py.py", line 76, in <module>
    tx_swap = router.functions.exactInputSingle(params).buildTransaction(\
AttributeError: 'exactInputSingle' object has no attribute 'buildTransaction'

This is source code:

import os
import json
import time

from dotenv import load_dotenv
from web3 import Web3

# ===============================================
# load .env
tmp = load_dotenv("./.env")
print("load env: ", tmp)

# 
PRIVATE_KEY = os.getenv("PRIVATE_KEY")
MY_ADDRESS = os.getenv("MY_ADDRESS")

# 
GETH = os.getenv("GETH")     # g_eth
UNI = os.getenv("UNI")      # uniswap
WETH = os.getenv("WETH")     # WETH

#
main = os.getenv("main")
goerli = os.getenv("goerli")
sepolia = os.getenv("sepolia")

#
FACTORY_ADDRESS = "0x1F98431c8aD98523631AE4a59f267346ea31F984"
QUOTER_ADDRESS = "0xb27308f9F90D607463bb33eA1BeBb41C27CE5AB6"
SWAP_ROUTER_ADDRESS = "0xE592427A0AEce92De3Edee1F18E0157C05861564"

#
with open("ABIs/SwapRouter.json", "r") as f:
    swap_router_abi = json.loads(f.read())

with open("ABIs/Quoter.json", "r") as f:
    quoter_abi = json.loads(f.read())

# ===============================================
w3 = Web3(Web3.HTTPProvider(goerli))
print("connect to w3: ", w3.is_connected())

router = w3.eth.contract(address=SWAP_ROUTER_ADDRESS, abi=swap_router_abi)
quoter = w3.eth.contract(address=QUOTER_ADDRESS, abi=quoter_abi)

# pool fee is one of  500 (0.05%), 3000 (0.30%), 100000 (1%).
pool_fee = 500
# amount you want to swap
amount = w3.to_wei("0.0001", "ether")

# estimate output amount
sqrtPriceLimitX96 = 0   # this value is ignored if it is set to 0.
estimated_amount_out = quoter.functions.quoteExactInputSingle(WETH, UNI, pool_fee, amount, sqrtPriceLimitX96).call()

input("You will get {} UNI TOKENS. Press any key to coutinue.".format(w3.from_wei(estimated_amount_out, "ether")))

# swap dai to weth
deadline = int(time.time() + 60*10)     # Set deadline as 10 min.
amountOutMinimum = int(0*0.99)          # Set slippage as 1%.
sqrtPriceLimitX96 = 0                   # this value is ignored if it is set to 0.
nonce = w3.eth.get_transaction_count(MY_ADDRESS)
params = [WETH, UNI, pool_fee, MY_ADDRESS, deadline, amount, amountOutMinimum, sqrtPriceLimitX96]

tx_swap = router.functions.exactInputSingle(params).buildTransaction({
    'chainId': 5,
    'type': 2,
    'gas': 10**6,
    'maxFeePerGas': w3.to_wei('100', 'gwei'),
    'nonce': nonce + 1,
})

#
signed_tx_swap = w3.eth.account.sign_transaction(tx_swap, private_key=PRIVATE_KEY)
#
result = w3.eth.send_raw_transaction(signed_tx_swap.rawTransaction)
print(result.hex())

1 Answer 1

0

I get the answer form github:

'in v6, it's snaked cased as build_transaction()'

from:https://github.com/ethereum/web3.py/issues/1340

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.