pragma solidity ^0.6.10;
import "@openzeppelin/contracts/access/Ownable.sol";
import "./exchanges/uniswap/interfaces/IUniswapV2Router02.sol";
import"./interfaces/IERC20.sol" ;
contract swap is Ownable {
     using SafeMath for uint256;
     uint deadline = block.timestamp + 300;  
      IUniswapV2Router02 public immutable uniswapV2Router;
  IUniswapV2Router02 public immutable sushiSwapRouter;
  constructor(address _uniswapRouterProvider,  address _sushiSwapProvider) public {
        uniswapV2Router = IUniswapV2Router02(_uniswapRouterProvider);
    sushiSwapRouter = IUniswapV2Router02(_sushiSwapProvider);

function uniSwap(uint amountIn,  address  _assetIn , address _assetOut) public onlyOwner returns (bool) {
    address[] memory path = new  address[](2);
      path[0] = _assetIn;
      path[1] = _assetOut;
     uint[] memory amounts = uniswapV2Router.getAmountsOut(amountIn, path);
        uint amountOut = amounts[amounts.length - 1];
    IERC20 token = IERC20(path[0]);
    require(token.approve(address(uniswapV2Router), amountIn), "FlashSwap: Failed to approve");
    uniswapV2Router.swapExactTokensForTokens(amountIn, amountOut, path,  address(this), deadline);
    return true; }

Javascript to interact with token ;

const { ethers } = require("ethers");
const Web3 = require("web3")
const fs = require('fs');
const {BigNumber,  FixedFormat,   FixedNumber,formatFixed    parseFixed , BigNumberish } = require("@ethersproject/bignumber");
const network = "kovan";
const provider = ethers.getDefaultProvider(network, {
    infura: {
     projectId: process.env.INFURA_API_KEY2,
//const mnemonic = fs.readFileSync("secret").toString().trim();
const mnemonic = process.env.mnemonic ;
walletMnemonic = new ethers.Wallet.fromMnemonic(mnemonic);
wallet = walletMnemonic.connect(provider);
Usdt_address = '0x07de306FF27a2B630B1141956844eB1552B956B5';
ierc20_abi =  require('./build/contracts/IERC20.json').abi 
Usdt_Contract = new ethers.Contract(Usdt_address,ierc20_abi,wallet);
Dai = "0x4F96Fe3b7A6Cf9725f59d353F723c1bDb64CA6Aa"
const fiveGwei = BigNumber.from("59000000000");
options = {gasPrice: fiveGwei, gasLimit: 2700000}
const amountIn = Web3.utils.toWei('9000000000', 'ether');
  Usdt_Contract.transfer(swap_address, numberOfTokens,options).then(function(tx) {

const swap = async() => {
  // const swap2=  swap_contract.uniSwap(
  //   amountIn  ,
  //   Usdt_address ,
  //   Dai,
  //   options)


You are setting the deadline in the contract body

uint deadline = block.timestamp + 300;

That's equivalent to setting in the constructor.

It's better to initialize deadline in the same function that calls to uniswap.

function uniSwap(uint amountIn,  address  _assetIn , address _assetOut) public onlyOwner returns (bool) {
    uint deadline = block.timestamp + 300;

    // .. other operations here

    uniswapV2Router.swapExactTokensForTokens(amountIn, amountOut, path,  address(this), deadline);


Remove any reference to deadline in the contract body.

  • So i done what you told me to do but code still fails when i try to swap tokens on uniswap . This is the tx has on kovan ether scan Txhash : 0xed0547d8932dca98b46aa721e275330f6476da5391bdfc6225908f226b648f0b
    – deficoder
    Jul 3 at 3:02
  • i'm really confused , i've tried to do everything but i cant seem to swap tokens
    – deficoder
    Jul 3 at 3:02
  • @deficoder From the transaction trace it fails when your contract tries to access USDT funds from the router. Why are you trying to transferFrom from the router address? That isn't going to work.
    – Ismael
    Jul 3 at 4:28
  • i'm confused can you please look at my code above and tell me what i need to fix . the code above that is .
    – deficoder
    Jul 3 at 14:39
  • @deficoder From the trace the call that fails has signature 23b872dd which is transferFrom. The trace doesn't match the source of question so either the wrong bytecode was deployed or it is calling the wrong contract address. Check the correct bytecode is deployed and the correct address is used.
    – Ismael
    Jul 3 at 18:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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