Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flashswap #16

Open
jurgenBE opened this issue Dec 29, 2021 · 5 comments
Open

Flashswap #16

jurgenBE opened this issue Dec 29, 2021 · 5 comments

Comments

@jurgenBE
Copy link

jurgenBE commented Dec 29, 2021

I have a question, if i use the code 'as is' it does not work, i always get 'Fail' but don't know if it's a failure in the contract or not, so i tried to add en event, but that's also not shown. Did your code work on mainnet like that, don't you have to sign the transaction or something. Do i have to hex the data first? I am getting desprerate, i've tried so many things...

this is the code, i do a reversal from apefactory to pancakerouter, and added 1 required to see if i see the message and added in the last try this emit event :

I already found out that it is in the pancakeCall function : i found that the error is showed here :

require(msg.sender == UniswapV2Library.pairFor(fromApeFactory, token0, token1), 'message fail 1');

// SPDX-License-Identifier: MIT
pragma solidity >=0.6.6 <0.8.0;

import './utils/SafeMath.sol';
import './UniswapV2Library.sol';
import './interfaces/IERC20.sol';
import './interfaces/IUniswapV2Pair.sol';
import './interfaces/IUniswapV2Factory.sol';
import './interfaces/IUniswapV2Router02.sol';
import './interfaces/IPancakeCallee.sol';
import './interfaces/IApeCallee.sol';

contract EventExample {
using SafeMath for uint;

event DataStored(string val);
uint256 val;

address private owner;
address private constant fromApeFactory = 0x0841BD0B734E4F5853f0dD8d7Ea041c241fb0Da6;
address private constant pancake_router_address = 0x10ED43C718714eb63d5aA57B78B54704E256024E; 
IUniswapV2Router02 toPancakeRouter = IUniswapV2Router02(pancake_router_address);

constructor() {
    owner = msg.sender;
}

function startArbitrage(
    address token0,
    address token1,
    uint amount0,
    uint amount1
) external{       
    
    emit DataStored('start');

    address pairAddress = IUniswapV2Factory(fromApeFactory).getPair(token0, token1);
    require(pairAddress != address(0), 'This pool does not exist');

    emit DataStored('step 1');      

    IUniswapV2Pair(pairAddress).swap(
        amount0,
        amount1,
        address(this),
        bytes('not empty')
    );
}

function pancakeCall(
    address _sender,
    uint _amount0,
    uint _amount1,
    bytes calldata _data
) external override {
    address[] memory path = new address[](2);
    
    // obtain an amout of token that you exchanged
    uint amountToken = _amount0 == 0 ? _amount1 : _amount0;

    address token0 = IUniswapV2Pair(msg.sender).token0();
    address token1 = IUniswapV2Pair(msg.sender).token1();

    require(msg.sender == UniswapV2Library.pairFor(fromApeFactory, token0, token1), 'message fail 1');
    require(_amount0 == 0 || _amount1 == 0, 'message fail 2');

    // if _amount0 is zero sell token1 for token0
    // else sell token0 for token1 as a result
    path[0] = _amount0 == 0 ? token1 : token0;
    path[1] = _amount0 == 0 ? token0 : token1;

    require(0 != 0, 'Just before approve');
    
    emit DataStored('step 2'); 
}

receive() external payable {}    

}

@yuyasugano
Copy link
Owner

@jurgenBE Sorry I don't see any error message in the post. Can you share what exactly you ran and what you got on your machine or on your console? Please note that the article and this repository were created for entertaining and educational purpose only it does not guarantee that all codes are workable all the time. Thanks.

@benedictmc
Copy link

@jurgenBE Hey so I think the issue here is that UniswapV2Library.pairFor(fromApeFactory, token0, token1) does not work for V2 pairs. IUniswapV2Factory(fromApeFactory).getPair(token0, token1) will calculate the right pair address. When the require is called the two pairs don't match and thats why the execution stops at that point

@yuyasugano
Copy link
Owner

@benedictmc If you've got a tweaked code in hands, please raise a PR in the repository. I'll look into that and merge it into main branch. Thanks.

@secp600k1
Copy link

@benedictmc thanks for that. were you able to execute the function after making this edit? I still get the same 'execution revert' error.

@yuyasugano
Copy link
Owner

@0xplask @benedictmc Please confirm the revised code and let me know if it works. I did some online research and updated the contract and the script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants