I'm looking for a simple solidity contract that I can send tx swap data to, wether in hex or an array.

send funds to contract
Swap a > b,
Swap b > c,
Swap c > a,
return funds to address

I can take care of the javascript side of things sending the needed info to the contract but not terribly good at solidity. Just trying to keep things simple.


welcome to ethereum stackexchange :) This should work (its messy and probably not really gas efficient, but it will work ^^)

// Import IERC20, IUniswapV2Factory, IUniswapV2Pair, IUniswapV2Router01 and IUniswapV2Router02
// Im using the V2 contracts because they're more common (only uniswap on mainnet uses V3, all the other DEXes use V2). Ofc this has nothing to do with the fact that i have no idea how uniswapV3 contracts work ^^'. 
contract MultiSwap {
    IUniswapV2Router02 router;
    IUniswapV2Factory factory;
    constructor(IUniswapV2Router02 _router, IUniswapV2Factory _factory) {
        router = _router;
        factory = _factory;
    function multiswap(address[] path, uint256 inputAmount, uint256 slippagePercentForEach) external {
        IERC20(path[0]).transferFrom(msg.sender, address(this), inputAmount); //Takes the initial amount from the sender
        for(uint256 i, i < path.length - 1, i++) {
            address[] memory tempPath = new address[](2);
            tempPath[0] = path[i];
            tempPath[1] = path[i+1]; // Well this is kinda stupid, there must be a more elegant way of doing that but hey that works lol
            uint256 amountIn = IERC20(path[i]).balanceOf(address(this)); // This assumes the balance is 0 before a call is made, if its not the case the tokens that were here will be swapped as well (that can be fixed pretty easily) 
            IUniswapV2Pair pair = factory.getPair(path[i], path[i+1]); // gets the pair address for the current swap
            (uint256 reserve1, uint256 reserve2) = pair.getReserves(); // gets the amount of tokens in said pair
            (uint256 reserveIn, uint256 reserveOut) = pair.token0() = path[i] ? (reserve1, reserve2) : (reserve2, reserve1); // sorts the amounts if needed
            uint256 expectedOutput = amountIn * reserveOut / reserveIn; // compute the expected price 
            uint256 minimumOutput = expectedOutput - (expectedOutput * slippagePercentForEach / 100); // Defines the minimum acceptable amount using slippagePercentForEach
            if (i == path.length - 2) { // If this is the last swap, send the output to msg.sender instead of sending it back to the contract 
                router.swapExactTokensForTokensSupportingFeeOnTransferTokens( //Actually do the swap. Im using the supportingFeeOnTransferTokens version just in case, but you dont necessarly need to. 
            else {
  • What compiler version should I use for this?
    – Vince M
    Sep 17 at 12:16
  • I coded this for 0.8 since it has built in overflow checks for operations but iirc uniswap contracts use 0.6.12, so if you want to go with 0.6.12 you should use the safeMath operators for all the math
    – Foxxxey
    Sep 17 at 14:27

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.