I have read documentation on this function. I'm using Hardhat
to visualize Blockchain at a particular moment.
I deposit()
ETH
to WETH
, then I approve()
WETH
to router. Everything looks fine. But when i try to call swapExactTokensForTokens()
, it do not work. This is my code:
from web3 import Web3
from interface import getInterface
import threading
import time
from Token import getERC20Abi
time1 = time.time()
w3 = Web3(Web3.HTTPProvider("http://127.0.0.1:8545/"))
uniswap = w3.eth.contract(address="0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D", abi=getInterface())#UniswapV2Router02
walletAddress = Web3.toChecksumAddress('0x3c44cdddb6a900fa2b585dd299e03d12fa4293bc')
recipient = Web3.toChecksumAddress('0x14dc79964da2c08b23698b3d3cc7ca32193d9955')
ETHToken = w3.eth.contract(address='0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2', abi=getERC20Abi())
private_key = '0x5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a'
Chuyen ETH sang WETH
transaction = ETHToken.functions.deposit().buildTransaction({
'chainId':31337,
'from': walletAddress,
'gas': 70000,
'value':1000000000000000000,
'maxFeePerGas': 57562177587,
'maxPriorityFeePerGas': w3.toWei('1', 'gwei'),
'nonce': 21
})
signed_txn = w3.eth.account.sign_transaction(transaction, private_key=private_key)
w3.eth.send_raw_transaction(signed_txn.rawTransaction)
print(ETHToken.functions.balanceOf(walletAddress).call())
Approve WETH cho Router
transaction_1 = ETHToken.functions.approve('0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D', 1000000000000000000).buildTransaction({
'chainId':31337,
'from': walletAddress,
'gas': 70000,
'maxFeePerGas': 57562177587,
'maxPriorityFeePerGas': w3.toWei('1', 'gwei'),
'nonce': 22
})
signed_txn_1 = w3.eth.account.sign_transaction(transaction_1, private_key=private_key)
w3.eth.send_raw_transaction(signed_txn_1.rawTransaction)
print("Luong WETH da improve cho router:")
print(ETHToken.functions.allowance(Web3.toChecksumAddress('0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266'), '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D').call())
# Swap WETH vs ENJ
timestamp = int((time.time() + 100000000000)//1)
transaction_2 = uniswap.functions.swapExactTokensForTokens(100000000000000000,0,
["0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2","0xF629cBd94d3791C9250152BD8dfBDF380E2a3B9c"],
recipient,timestamp).buildTransaction({
'chainId':31337,
'gas': 70000,
'from': walletAddress,
'maxFeePerGas': 57562177587,
'maxPriorityFeePerGas': w3.toWei('1', 'gwei'),
'nonce': 23
})
signed_txn_2 = w3.eth.account.sign_transaction(transaction_2, private_key=private_key)
w3.eth.send_raw_transaction(signed_txn_2.rawTransaction)
# print(ETHToken.functions.allowance(walletAddress, '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D').call())
pls explain it for me
If you look at your hardhat console output, you should see something like this (this is example output found somewhere else online):
Error: Transaction reverted without a reason string
at <UnrecognizedContract>.<unknown> (0x5aa53f03197e08c4851cad8c92c7922da5857e5d)
at <UnrecognizedContract>.<unknown> (0x5aa53f03197e08c4851cad8c92c7922da5857e5d)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at runNextTicks (node:internal/process/task_queues:65:3)
at listOnTimeout (node:internal/timers:526:9)
at processTimers (node:internal/timers:500:7)
at async EthModule._estimateGasAction (node_modules/hardhat/src/internal/hardhat-network/provider/modules/eth.ts:425:7)
at async HardhatNetworkProvider.request (node_modules/hardhat/src/internal/hardhat-network/provider/provider.ts:108:18)
at async EthersProviderWrapper.send (node_modules/@nomiclabs/hardhat-ethers/src/internal/ethers-provider-wrapper.ts:13:20)
In this case, you can at least see the contract address (0x5aa53f03197e08c4851cad8c92c7922da5857e5d
) that was causing the revert. I think with uniswap, even with WETH, there's a separate function you need to use to swap it.