nosofa / uniswap-v2-py

An unofficial Python wrapper for Uniswap V2.
MIT License
86 stars 34 forks source link

Error - swap_exact_tokens_for_tokens #7

Open chrispsharering opened 3 years ago

chrispsharering commented 3 years ago

Is swap_exact_tokens_for_tokens still working as intended? The page it links off to no longer exists - swap_exact_tokens_for_tokens

Further, I've been trying to get it working to no avail, here is my code:

from uniswap.uniswap import UniswapV2Client import time

address = "my eth address" private_key = "my private key" to_address = address

my_provider = "https://mainnet.infura.io/v3/my secret" client = UniswapV2Client(address, private_key, provider=my_provider) amount_in = 0.01 10 18 min_amount_out = 1 10 15 path = [ address, "0xc778417e063141139fce010982780140aa0cd5ab" ] deadline = int(time.time()) + 10 * 60 tx = client.swap_exact_tokens_for_tokens(amount_in, min_amount_out, path, to_address, deadline)

print(tx)

The error I'm getting is:

web3._utils.datatypes.Contract object at 0x10b92f640 my eth address Traceback (most recent call last): File "/usr/local/lib/python3.9/site-packages/web3/contract.py", line 1508, in call_contract_function output_data = web3.codec.decode_abi(output_types, return_data) File "/usr/local/lib/python3.9/site-packages/eth_abi/codec.py", line 181, in decode_abi return decoder(stream) File "/usr/local/lib/python3.9/site-packages/eth_abi/decoding.py", line 127, in call return self.decode(stream) File "/usr/local/lib/python3.9/site-packages/eth_utils/functional.py", line 45, in inner return callback(fn(*args, **kwargs)) File "/usr/local/lib/python3.9/site-packages/eth_abi/decoding.py", line 173, in decode yield decoder(stream) File "/usr/local/lib/python3.9/site-packages/eth_abi/decoding.py", line 127, in call return self.decode(stream) File "/usr/local/lib/python3.9/site-packages/eth_abi/decoding.py", line 198, in decode raw_data = self.read_data_from_stream(stream) File "/usr/local/lib/python3.9/site-packages/eth_abi/decoding.py", line 305, in read_data_from_stream raise InsufficientDataBytes( eth_abi.exceptions.InsufficientDataBytes: Tried to read 32 bytes. Only got 0 bytes

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "/Users/chris/Uniswap/uniswap-v2-py/sniper.py", line 24, in module tx = client.swap_exact_tokens_for_tokens(amount_in, min_amount_out, path, to_address, deadline) File "/Users/chris/Uniswap/uniswap-v2-py/uniswap/uniswap.py", line 411, in swap_exact_tokens_for_tokens self.approve(path[0], amount) File "/Users/chris/Uniswap/uniswap-v2-py/uniswap/uniswap.py", line 170, in approve if self._is_approved(token, max_approval): File "/Users/chris/Uniswap/uniswap-v2-py/uniswap/uniswap.py", line 163, in _is_approved approved_amount = erc20_contract.functions.allowance(self.address, self.router.address).call() File "/usr/local/lib/python3.9/site-packages/web3/contract.py", line 955, in call return call_contract_function( File "/usr/local/lib/python3.9/site-packages/web3/contract.py", line 1529, in call_contract_function raise BadFunctionCallOutput(msg) from e web3.exceptions.BadFunctionCallOutput: Could not transact with/call contract function, is contract deployed correctly and chain synced?

nb my eth address has 0.09 ETH in it, so it shouldn't be a gas issue...

Any ideas would be really appreciated!

nhanth87 commented 3 years ago

path = [ address, "0xc778417e063141139fce010982780140aa0cd5ab" ]

path should be 2 tokens contract, not your address