1

I created a token "TestMe" with 2 million tokens (18dec), minting the deployer address the whole stack. I used the "addLiquidityETH" function provided by uniswap router to add 500k tokens and 0.5 ETH. When I am using the deployer wallet to place a trade via uniswap V2 app on sepolia (Buy/sell) with a small amount of token , I always receive the error message "Insufficient Liquidity".

Here is the link to the contract where you can see the AddLiquidity tx: https://sepolia.etherscan.io/address/0x6161B4E0AE26f2aB2fAC2660B73F7aaFD44FfE72

Here my code if necessary: Deploy script:

// SPDX-License-Identifier: MIT
const fs = require("fs");

async function main() {
    
    async function deploy() {
        console.log("Starting deployment script...");
        const [deployer] = await ethers.getSigners();
        console.log("Signer fetched:", deployer.address);
      
        const TestMe = await ethers.getContractFactory("TestMe", deployer);

        const testMe = await TestMe.deploy();
        console.log("testMe contract address:", testMe.target);

        //save address from contract
        fs.writeFileSync("deployedYeetAddress.json", JSON.stringify({ address: testMe.target }));
        console.log("testMe contract address saved to deployedtestMeAddress.json");

        await testMe.approve(testMe.target, ethers.parseUnits("500000", 18));
        await testMe.addLiquidity(ethers.parseUnits("500000", 18), {
            value: ethers.parseEther("0.1")
        });
        
    }
    await deploy();

}

contract:

// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

import "lib/openzeppelin-contracts/contracts/token/ERC20/ERC20.sol";
import "lib/openzeppelin-contracts/contracts/access/Ownable.sol";
import "lib/openzeppelin-contracts/contracts/security/ReentrancyGuard.sol";
import "interfaces/uniswap-v2-periphery/contracts/interfaces/IUniswapV2Router02.sol";
import "uniswap-v2-core/contracts/interfaces/IUniswapV2Pair.sol";
import "uniswap-v2-core/contracts/interfaces/IUniswapV2Factory.sol";

contract TestMe is ERC20, Ownable{
    IUniswapV2Pair public immutable uniswapV2Pair;
    IUniswapV2Router02 public immutable uniswapV2Router;
    constructor() ERC20('TestMe','TestMe COIN'){
        _mint(msg.sender, 2000000 * 10**18);
        uniswapV2Router = IUniswapV2Router02(0xC532a74256D3Db42D0Bf7a0400fEFDbad7694008); // must be exchanged for mainnet!!!
        uniswapV2Pair = IUniswapV2Pair(
            IUniswapV2Factory(uniswapV2Router.factory()).createPair(address(this), uniswapV2Router.WETH())
        );
    }

    function addLiquidity(uint256 tokenAmount) public payable onlyOwner {
        _transfer(owner(), address(this), tokenAmount);
        this.approve(address(uniswapV2Router), tokenAmount);
        uniswapV2Router.addLiquidityETH{value: msg.value}(address(this), tokenAmount, 0, 0, owner(), block.timestamp);
    }
}
11
  • Where is the code of the attempt to trade? Is this only on the front end UI that you get that error, or is it returned from an eth call?
    – Maka
    Commented Apr 18 at 20:16
  • If I simulate I can swap eth for your token. If you only have an issue through the UI on testnets, which are rarely supported by front ends, then there may not be anything wrong with your contracts or scripts.
    – Maka
    Commented Apr 18 at 20:39
  • I only tried using uniswap front end, maybe you are right because I think 0xC532a74256D3Db42D0Bf7a0400fEFDbad7694008 is not the official uniswap v2 router adress, i got it from stackexchange because the original for sepolia was not working - thank you i will try to perform a swap without the front end Commented Apr 18 at 20:50
  • I didn't think the front end was connected to the smart contracts on the test net. You can still use the router to test, just swap via the explorer will do a simple example for you.
    – Maka
    Commented Apr 18 at 20:53
  • 1
    Added a script, much love.
    – Maka
    Commented Apr 18 at 21:33

1 Answer 1

1

I didn't think the front end was connected to the smart contracts on the test net. You can still use the router to test, just swap via the explorer:

  • https://sepolia.etherscan.io/address/0xc532a74256d3db42d0bf7a0400fefdbad7694008#writeContract

  • Click connect to web3

  • Scroll to 9. swapETHForExactTokens (or 10. swapExactETHForTokens)

  • Enter a value to pay in eth, with the format of using a decimal point, for example 1.0 for 1

  • Input an amountOut (or amountOutMin), but this time no decimal, instead * 10 ** decimals the token has, for example 1000000000000000000, for 1

    • Try anything greater than 0 for the test.
  • path should be weth and your token

['0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9','0x6161B4E0AE26f2aB2fAC2660B73F7aaFD44FfE72']
  • to should be your address or your contract, wherever the destination is.

  • deadline is some unix timestamp in the future.

    • 2*10**10 is distant.
  • Click write

  • Confirm in wallet.

example swap: - https://sepolia.etherscan.io/tx/0x37c0026e736dbe4e4572f0507a5cb4a7485ca00707f70a12d515b98a95258aff

enter image description here

edit to add a requested example:


RPC = 'https://rpc2.sepolia.org'
CHAIN_ID = 0xaa36a7

from web3 import Web3 as w, HTTPProvider as hp
w3 = w(hp(RPC))

from os import getenv
from dotenv import load_dotenv
load_dotenv()
KEY = getenv('KEY')
EOA = getenv('ADD')
#----------------------

ROUTER = '0xC532a74256D3Db42D0Bf7a0400fEFDbad7694008'
NATIVE = '0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9'
TOKEN = '0x6161B4E0AE26f2aB2fAC2660B73F7aaFD44FfE72'

ROUTER_ABI = '''
[{"inputs":[{"internalType":"address","name":"_factory","type":"address"},{"internalType":"address","name":"_WETH","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"WETH","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"tokenA","type":"address"},{"internalType":"address","name":"tokenB","type":"address"},{"internalType":"uint256","name":"amountADesired","type":"uint256"},{"internalType":"uint256","name":"amountBDesired","type":"uint256"},{"internalType":"uint256","name":"amountAMin","type":"uint256"},{"internalType":"uint256","name":"amountBMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"addLiquidity","outputs":[{"internalType":"uint256","name":"amountA","type":"uint256"},{"internalType":"uint256","name":"amountB","type":"uint256"},{"internalType":"uint256","name":"liquidity","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"amountTokenDesired","type":"uint256"},{"internalType":"uint256","name":"amountTokenMin","type":"uint256"},{"internalType":"uint256","name":"amountETHMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"addLiquidityETH","outputs":[{"internalType":"uint256","name":"amountToken","type":"uint256"},{"internalType":"uint256","name":"amountETH","type":"uint256"},{"internalType":"uint256","name":"liquidity","type":"uint256"}],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"factory","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"uint256","name":"reserveIn","type":"uint256"},{"internalType":"uint256","name":"reserveOut","type":"uint256"}],"name":"getAmountIn","outputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"reserveIn","type":"uint256"},{"internalType":"uint256","name":"reserveOut","type":"uint256"}],"name":"getAmountOut","outputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"}],"name":"getAmountsIn","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"}],"name":"getAmountsOut","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountA","type":"uint256"},{"internalType":"uint256","name":"reserveA","type":"uint256"},{"internalType":"uint256","name":"reserveB","type":"uint256"}],"name":"quote","outputs":[{"internalType":"uint256","name":"amountB","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"address","name":"tokenA","type":"address"},{"internalType":"address","name":"tokenB","type":"address"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"internalType":"uint256","name":"amountAMin","type":"uint256"},{"internalType":"uint256","name":"amountBMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"removeLiquidity","outputs":[{"internalType":"uint256","name":"amountA","type":"uint256"},{"internalType":"uint256","name":"amountB","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"internalType":"uint256","name":"amountTokenMin","type":"uint256"},{"internalType":"uint256","name":"amountETHMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"removeLiquidityETH","outputs":[{"internalType":"uint256","name":"amountToken","type":"uint256"},{"internalType":"uint256","name":"amountETH","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"internalType":"uint256","name":"amountTokenMin","type":"uint256"},{"internalType":"uint256","name":"amountETHMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"removeLiquidityETHSupportingFeeOnTransferTokens","outputs":[{"internalType":"uint256","name":"amountETH","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"internalType":"uint256","name":"amountTokenMin","type":"uint256"},{"internalType":"uint256","name":"amountETHMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"bool","name":"approveMax","type":"bool"},{"internalType":"uint8","name":"v","type":"uint8"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"name":"removeLiquidityETHWithPermit","outputs":[{"internalType":"uint256","name":"amountToken","type":"uint256"},{"internalType":"uint256","name":"amountETH","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"internalType":"uint256","name":"amountTokenMin","type":"uint256"},{"internalType":"uint256","name":"amountETHMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"bool","name":"approveMax","type":"bool"},{"internalType":"uint8","name":"v","type":"uint8"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"name":"removeLiquidityETHWithPermitSupportingFeeOnTransferTokens","outputs":[{"internalType":"uint256","name":"amountETH","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"tokenA","type":"address"},{"internalType":"address","name":"tokenB","type":"address"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"internalType":"uint256","name":"amountAMin","type":"uint256"},{"internalType":"uint256","name":"amountBMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"bool","name":"approveMax","type":"bool"},{"internalType":"uint8","name":"v","type":"uint8"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"name":"removeLiquidityWithPermit","outputs":[{"internalType":"uint256","name":"amountA","type":"uint256"},{"internalType":"uint256","name":"amountB","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapETHForExactTokens","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapExactETHForTokens","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapExactETHForTokensSupportingFeeOnTransferTokens","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapExactTokensForETH","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapExactTokensForETHSupportingFeeOnTransferTokens","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapExactTokensForTokens","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapExactTokensForTokensSupportingFeeOnTransferTokens","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"uint256","name":"amountInMax","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapTokensForExactETH","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"uint256","name":"amountInMax","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapTokensForExactTokens","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}]
'''

dex = w3.eth.contract(address=ROUTER, abi=ROUTER_ABI)

swap = dex.functions.swapExactETHForTokens(
  # don't put value here this time that is only for explorers, put it in the transaction parameters
  0,
  ['0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9','0x6161B4E0AE26f2aB2fAC2660B73F7aaFD44FfE72'],
  EOA,
  2*10**10
).build_transaction({
  'value': 1000000000000000,
  'from': EOA,
  'chainId': CHAIN_ID,
  'gas': 500000,
  'maxFeePerGas': w3.eth.gas_price * 2,
  'maxPriorityFeePerGas': w3.eth.max_priority_fee * 2,
  'nonce': w3.eth.get_transaction_count(EOA)
})

w3.eth.call(swap)

# helpers
def sign_tx(tx, key):
  sig = w3.eth.account.sign_transaction
  signed_tx = sig(tx, private_key=key)
  return signed_tx

def send_tx(signed_tx):
  w3.eth.send_raw_transaction(signed_tx.rawTransaction)
  tx_hash = w3.to_hex(w3.keccak(signed_tx.rawTransaction))
  return tx_hash

def main():
  ('[-] Attempting swap...')
  tx_hash = send_tx(sign_tx(swap, KEY))
  receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
  print (f'[>] Hash of attempt: {tx_hash}\n[>] {receipt}')

if __name__ == '__main__':
  main()
2
  • The scrollscan link is not my token / contract tho, token tracker says: Cog Pool LP Token, does this make sense? Also there is no swapTokensforETH function in the writeContract Tab for me.. sorry im new to this :D Commented Apr 19 at 6:20
  • 1
    Oh my, corrected. Much love.
    – Maka
    Commented Apr 19 at 8:44

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.