1

im pretty new to solidity, and i was wondering how you can 1 ERC token by another across different liquidity pools.

Example:

I have 1 DAI and I change it for eth in the ETH-DAI Uniswap pool then I swap ETH by SUSHI in the SUHSI-ETH Sushiswap pool

so the tx looks like this swap 1 dai for x eth swap x eth for y sushi

I guess you cant do it directly and you need to deploy a smart contract that interacts with both contracts (Uniswap ETH-DAI and Sushiswap SUSHI-ETH) and then you interact with your deployed smart contract. Does this have a particular name? I saw that it exist something called atomic swaps but I guess its not the same

8
  • I don't think the question makes any reference to different chains/side-chains/layers. It just concerns different liquidity pools in different AMMs on the same chain. May 28, 2021 at 14:06
  • As @RichardHorrocks pointed, i was talking about the same chain. here is a sample transaction of what I want to achieve: bscscan.com/tx/…
    – user71155
    May 30, 2021 at 16:41
  • @user71155 tx was constructed in the smart contract. Jun 2, 2021 at 2:12
  • @VladimirKovalchuk exactly! Thats what i want to achieve, from a smart contract create a function that takes an input amount, a path and talks to all the different liquidity pools in that path. For example, I input 1 BNB and I give two addresses, the BNB-CAKE LP address and the CAKE - BUSD LP address (it can be from another DEX, so I cant use their native router SC), the contract then needs to swap in a single transaction (really important) BNB for CAKE and then CAKE for BUSD.
    – user71155
    Jun 3, 2021 at 7:08
  • @user71155, so, do that, what kind of issue do you have:) Jun 3, 2021 at 9:00

2 Answers 2

1

Here you can find an example of such a smart contract. https://blog.infura.io/build-a-flash-loan-arbitrage-bot-on-infura-part-ii/

Nothing unusual here. Just do a call to different smart contracts in a single transaction.

0

By using the routers of both of the DEX, you'll use their swapping functions in a single contract called in one function.

NOTE: The router addresses should already be approved on the tokens you want to swap

   function multiSwap(uint256 deadline, uint256 amountOutMinUniswap) 
   external 
   payable {
   uint256 amountOutMinBancor = 1;
   uint256 amountOutMinSushiSwap = 1;

_tradeOnBancor(msg.value, amountOutMinBancor);
_tradeOnSushi(IERC20(BNT).balanceOf(address(this)), amountOutMinSushiSwap, deadline);
_tradeOnUniswap(IERC20(INJ).balanceOf(address(this)), amountOutMinUniswap, deadline);

}

This is how the transaction will be executed for three swaps, every swap will have its own function which will handle the swap on that DEX for you

This is how the trade function will look like

    function _tradeOnUniswap(uint256 amountIn, uint256 amountOutMin, 
    uint256 deadline) private {
    address tokenIn = INJ;
    address tokenOut = DAI;
    uint24 fee = 3000;
   address recipient = msg.sender;
   uint160 sqrtPriceLimitX96 = 0;

ISwapRouter.ExactInputSingleParams memory params = ISwapRouter.ExactInputSingleParams(
    tokenIn,
    tokenOut,
    fee,
    recipient,
    deadline,
    amountIn,
    amountOutMin,
    sqrtPriceLimitX96
);

uniswapRouter.exactInputSingle(params);
uniswapRouter.refundETH();

// refund leftover ETH to user
(bool success,) = msg.sender.call{ value: address(this).balance }("");
require(success, "refund failed");

}

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.