0

I'm learning Solidity. Trying to implement this contract: Flashloaner.sol

pragma solidity =0.6.6;

import './UniswapV2Library.sol';
import './interfaces/IUniswapV2Router02.sol';
import './interfaces/IUniswapV2Pair.sol';
import './interfaces/IERC20.sol';
//import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.5.1/contracts/math/SafeMath.sol";


contract FlashLoaner {
  IUniswapV2Router02 immutable sushiRouter;
  address immutable factory;
  uint constant deadline = 10 days;

  constructor(address _factory, address _uniRouter, address _sushiRouter) public {
    factory = _factory;  
    sushiRouter = IUniswapV2Router02(_sushiRouter);
  }

  function uniswapV2Call(address _sender, uint _amount0, uint _amount1, bytes calldata _data) external {
      address[] memory path = new address[](2);
      uint amountToken = _amount0 == 0 ? _amount1 : _amount0;
      
      address token0 = IUniswapV2Pair(msg.sender).token0();
      address token1 = IUniswapV2Pair(msg.sender).token1();

      require(msg.sender == UniswapV2Library.pairFor(factory, token0, token1), "Unauthorized"); 
      require(_amount0 == 0 || _amount1 == 0);

      path[0] = _amount0 == 0 ? token1 : token0;
      path[1] = _amount0 == 0 ? token0 : token1;

      IERC20 token = IERC20(_amount0 == 0 ? token1 : token0);
      
      token.approve(address(sushiRouter), amountToken);

      // no need for require() check, if amount required is not sent sushiRouter will revert
      uint amountRequired = UniswapV2Library.getAmountsIn(factory, amountToken, path)[0];
      uint amountReceived = sushiRouter.swapExactTokensForTokens(amountToken, amountRequired, path, msg.sender, deadline)[1];

      // YEAHH PROFIT
      token.transfer(_sender, amountReceived - amountRequired);
    
  }
}

In Remix where I have installed the folders and files from this project. After compiling the contract I have Remix set on: Injected Web3 with my MetaMask connected. The error on trying to deploy is:

creation of FlashLoaner errored: Error encoding arguments: Error: invalid address (argument="address", value="", code=INVALID_ARGUMENT, version=address/5.1.0) (argument=null, value="", code=INVALID_ARGUMENT, version=abi/5.1.2)

Do I need to hardcode the 'address' elements someplace?

Further, on compiling there are warnings about this code from the contract:

constructor(address _factory, address _uniRouter, address _sushiRouter) public {
    factory = _factory;  
    sushiRouter = IUniswapV2Router02(_sushiRouter);

The warning is:

flash-loans/contracts/FlashLoaner.sol:15:33: Warning: Unused function parameter. Remove or comment out the variable name to silence this warning. constructor(address _factory, address _uniRouter, address _sushiRouter) public { ^----------------^

There's something fundamental I am missing in trying to deploy this contract. Any pointers much appreciated!

1
  • when deploying you need to pass correct arguments in order to the contract constructor. you are not doing this correctly with an address type argument and you get that error for that. check your deployment arguments. Jul 5 at 20:57
0

So - I saw that there's too much going on here and set up everything locally using Go-ethereum.

Using Truffle.

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.