Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
7.2 kB
2
Indexable
Never


// The logic is simply to borrow some amount of tokens from any V2Pair or V3Pool, make a flash swap on it, swap then again in other
// two DEXs, finalize the flash swap and keep the remaning balance in the smart contract of that borrowed token.

// The borrowed token is usually the wrapped native token of the network. WETH for Ethereum, WBNB for BSC. I focused on BSC for budget
// reasons. BOR contains the address of the borrowed token, OTH the other transition token involved




pragma solidity ^0.8.19;


interface ierc {

    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(address indexed owner, address indexed spender, uint256 value);
    function totalSupply() external view returns (uint256);
    function balanceOf(address account) external view returns (uint256);
    function transfer(address to, uint256 value) external returns (bool);
    function allowance(address owner, address spender) external view returns (uint256);
    function approve(address spender, uint256 value) external returns (bool);
    function transferFrom(address from, address to, uint256 value) external returns (bool);
}

using Math for uint256;
//using SafeCast for uint256;

contract UBahn6 {

    address Pair1;
    address Pair2;
    uint DEXA;
    uint DEXB;

    address t0;
    address t1;
    uint In;
    address PancakePool = 0x0eD7e52944161450477ee417DE9Cd3a859b14fD0;
    address BOR = 0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c;

    uint160 MIN_SQRT_RATIO = 4295128739;
    uint160 MAX_SQRT_RATIO = 1461446703485210103287273052203988822378723970342;

    address OTH;
    address owner;
    bool secondo = false;

    uint Q1;
    uint Q2;

    function uniswapV3SwapCallback(
        int256 amount0Delta,
        int256 amount1Delta,
        bytes memory data
        ) external {

        //  Pay back the Pool
        (uint ze, address to, address pai, uint Inn) = abi.decode(data,(uint,address,address,uint));
        //   IUniswapV3Pool(Pair1).swap(address(this),true,In,0,"");

        uint F3 = uint(IUniswapV3Pool(pai).fee()/10**6);

        if (ze==0) {

            ierc(to).approve(pai,uint(amount0Delta*(1+int(F3))));
            ierc(to).transfer(pai,uint(amount0Delta*(1+int(F3))));

            }

        if (ze==1) {
            ierc(to).approve(pai,uint(amount1Delta*(1+int(F3))));
            ierc(to).transfer(pai,uint(amount1Delta*(1+int(F3))));
        }

    }


    function pancakeV3SwapCallback(
        int256 amount0Delta,
        int256 amount1Delta,
        bytes memory data
        ) external {

        //  Pay back the Pool
        (uint ze, address to, address pai, uint Inn) = abi.decode(data,(uint,address,address,uint));
        //   IUniswapV3Pool(Pair1).swap(address(this),true,In,0,"");

        uint F3 = uint(IUniswapV3Pool(pai).fee()/10**6);

        if (ze==0) {

            ierc(to).approve(pai,uint(amount0Delta*(1+int(F3))));
            ierc(to).transfer(pai,uint(amount0Delta*(1+int(F3))));
            }

        if (ze==1) {
            ierc(to).approve(pai,uint(amount1Delta*(1+int(F3))));
            ierc(to).transfer(pai,uint(amount1Delta*(1+int(F3))));
        }

    }



    function Pairswap(uint amount,
                      uint dexa,
                      uint dexb,
                      address pair1,
                      address pair2,
                      uint q1,
                      uint q2) external payable
    {

        owner = msg.sender;

        bytes memory by = abi.encode(dexa,dexb,pair1,pair2,q1,q2);

        IUniswapV2Pair(PancakePool).swap(0,amount,address(this),by);

   }

   function pancakeCall(address sender, uint amount0, uint amount1, bytes calldata data) external  {

        (DEXA, DEXB, Pair1, Pair2, Q1, Q2) = abi.decode(data, (uint,uint,address,address,uint,uint));

        In = ierc(BOR).balanceOf(address(this));


        if (DEXA==2) {

            t0 = IUniswapV2Pair(Pair1).token0();
            t1 = IUniswapV2Pair(Pair1).token1();
            (uint112 r0,uint112 r1,) = IUniswapV2Pair(Pair1).getReserves();

            ierc(BOR).approve(address(this),ierc(BOR).balanceOf(address(this)));
            ierc(BOR).transferFrom(address(this),Pair1,ierc(BOR).balanceOf(address(this)));

//            uint Q = BOR==t0 ? getAmountOut(In,r0,r1) : getAmountOut(In,r1,r0);

            if (BOR==t0) {
                   IUniswapV2Pair(Pair1).swap(0,Q1,address(this),"");
                }  else {
                   IUniswapV2Pair(Pair1).swap(Q1,0,address(this),""); }

        } else {

            t0 = IUniswapV3Pool(Pair1).token0();
            t1 = IUniswapV3Pool(Pair1).token1();

            if (BOR==t0) {
                   uint z = 0;
                   IUniswapV3Pool(Pair1).swap(address(this),true,int(In),MIN_SQRT_RATIO+1,abi.encode(z,BOR,Pair1,In));

                }  else {
                   uint z = 1;
                   IUniswapV3Pool(Pair1).swap(address(this),false,int(In),MAX_SQRT_RATIO-1,abi.encode(z,BOR,Pair1,In));

            }}



        secondo = true;

        if (In==1500011) { revert("SECONDO TRUE"); }

        uint am2;

        if (BOR==t0) {
            am2 = ierc(t1).balanceOf(address(this));
            OTH = t1;
        } else {
            am2 = ierc(t0).balanceOf(address(this));
            OTH = t0;
        }


        if (DEXB==2) {

            address t2 = IUniswapV2Pair(Pair2).token0();
            address t3 = IUniswapV2Pair(Pair2).token1();
            (uint112 r2,uint112 r3,) = IUniswapV2Pair(Pair2).getReserves();

            if (t2!=OTH && t3!=OTH) {

                revert("Stesso token ma con indirizzi diversi");
            }

 //           uint Q2;

//            Q2 = BOR==t3 ? getAmountOut(am2,r2,r3) : getAmountOut(am2,r3,r2);

            ierc(OTH).approve(address(this),ierc(OTH).balanceOf(address(this)));
            ierc(OTH).transferFrom(address(this),Pair2,ierc(OTH).balanceOf(address(this)));



            if (BOR==t3) {
                   IUniswapV2Pair(Pair2).swap(0,Q2,address(this),"");
                }  else {
                   IUniswapV2Pair(Pair2).swap(Q2,0,address(this),"");
        }} else {


            uint In2 = ierc(OTH).balanceOf(address(this));

            address t2 = IUniswapV3Pool(Pair2).token0();
            address t3 = IUniswapV3Pool(Pair2).token1();


            if (BOR==t3) {
                   uint z = 0;

                   IUniswapV3Pool(Pair2).swap(address(this),true,int(In2),MIN_SQRT_RATIO+1,abi.encode(z,OTH,Pair2,In));

                }  else {
                   uint z = 1;
                   IUniswapV3Pool(Pair2).swap(address(this),false,int(In2),MAX_SQRT_RATIO-1,abi.encode(z,OTH,Pair2,In));

                }}

        uint Out = ierc(BOR).balanceOf(address(this));

        if (Out<In) {
           revert("T");
        } else {
            uint256 loanFee = Math.mulDiv(In,25,9997)+1;

            ierc(BOR).approve(PancakePool,In+loanFee);
            ierc(BOR).transferFrom(address(this),PancakePool,In+loanFee);

            ierc(BOR).approve(owner,ierc(BOR).balanceOf(address(this)));
            ierc(BOR).transferFrom(address(this),owner,ierc(BOR).balanceOf(address(this)));
    }
}}