0

I'm trying to make a delegatecall to the PancakeSwap Routerv2 from my contract, to perform a swap, but for some reason it's not working. Here's my code:

// pancakeswap router address
address public CAKE_ROUTER = 0x10ED43C718714eb63d5aA57B78B54704E256024E;

function swapExactTokensForTokens(
    uint256 amountIn,
    uint256 amountOutMin,
    address[] calldata path,
    address to,
    uint256 deadline
) external override returns (bool success) 
{
    (success, ) = CAKE_ROUTER.delegatecall(
        abi.encodeWithSignature("swapExactTokensForTokens(uint256,uint256,address[],address,uint256)", amountIn, amountOutMin, path, to, deadline)
    );
    require(success, "swapExactTokensForTokens: failed");
}

The code is reverting on the require(success) statement. Any ideas of what I'm doing wrong? This is my first time trying to implement delegatecalls, so I have a feeling I'm not understanding things 100% correctly. The router address and selector are correct (I think).

1

What you are trying to do cannot work.

When you use delegatecall on the router, it will execute the swapExactTokensForTokens function in the context of your contract.

It is a bit like if you were copy/pasting the swapExactTokensForTokens into your contract. The function will try to use the storage variables of your contract. It most likely reverts when it cannot find the proper storage variables to execute the function, such as the address of the pairs and what not.

1
  • This cleared up my understanding a lot. Thank you very much!
    – MoonGusto
    Aug 10 at 18:59

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.