2

I am starting to learn solidity so this is probably a very basic question, but I couldn't find enough clarification only.

My goal is to purchase a token on PancakeSwap/UniSwap using the ETH balance of my smart contract. The contract has enough ETH balance for the swap.

If possible I want to swap this ETH for some TokenA, instead of having to use WETH, as I think this would be easier since I can selfdestruct and recover all my funds with easy if I don't have any tokens.

I am attempting to call swapExactETHForTokens of the router as follows:

    function swap() public {
        uint deadline = block.timestamp + 100;
        address[] memory path = new address[](2);
        path[0] = address(WBNB);
        path[1] = address(target);
        pancakeRouter.swapExactETHForTokens(0, path, owner, deadline);
    }

The problem is that I do not have enough funds, which makes sense since I have not specified the amount of ETH I want to trade. I think I could solve this by making this function payable and sending the amount of ETH I want to trade. (https://uniswap.org/docs/v2/smart-contracts/router02/)

However, this is not what I want to achieve. I would like to use the balance of the contract to perform this transaction. What would be the easiest / fastest / less gas intensive way to achieve this?

Thanks.

Edit:

I think I did not stress enough the need to use the contract balance as funds. I know the intended usage would be to send the ETH with the tx and not send any ETH to the contract directly.

Why do I want to use the funds in the SC instead? because this way I can, for instance, send 100 tx to this contract that execute this swap call, and only one of them will run successfully, since the funs are limited. So I guess I should swap for WBNB then?

3 Answers 3

5

You should be able to achieve that if you send the ether along the function call. In solc v0.8 you achieve that using the function call options {value: amount}.

The resulting function should look like this:

function swap() public {
    uint deadline = block.timestamp + 100;
    address[] memory path = new address[](2);
    path[0] = address(WBNB);
    path[1] = address(target);

    // Send all balance
    uint256 amount = address(this).balance;

    // TODO: Check the result
    pancakeRouter.swapExactETHForTokens{value: amount}(0, path, owner, deadline);
}
0
  1. You do not need to send funds to your smart contract. Leave them in your wallet. The contract should be able to be used with any account. You send it parameters and it executes them for you.

  2. Look at the Pancake swap or Uniswap UI and input the tokens you want to swap (see url below). Do they show the route as:

**BNB to Desired token?
or **WBNB to Desired token?

Most of the time the most common pairs are ERC20 vs ERC20. Look at this contract as it will route through WETH if there is better liquidity. https://cryptomarketpool.com/how-to-swap-tokens-on-uniswap-using-a-smart-contract/

function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline) external payable returns (uint[] memory amounts); Copy Swaps an exact amount of ETH for as many output tokens as possible, along the route determined by the path. The first element of path must be WETH, the last is the output token, and any intermediate elements represent intermediate pairs to trade through (if, for example, a direct pair does not exist). https://uniswap.org/docs/v2/smart-contracts/router02/#swapexactethfortokens

4
  • 1. Why not? For my use case it would be required (or so I think) to have the funds in the contract. 2. According to the documentation on swapExactETHForTokens: "Swaps an exact amount of ETH for as many output tokens as possible, along the route determined by the path. The first element of path must be WETH, the last is the output token, and any intermediate elements represent intermediate pairs to trade through (if, for example, a direct pair does not exist)." So I think I should use WBNB as the first argument.
    – lobis
    Commented May 31, 2021 at 4:22
  • This does not solve the question.
    – lobis
    Commented May 31, 2021 at 4:45
  • 1
    I added some more information.
    – carry7
    Commented May 31, 2021 at 4:47
  • Thanks, I have added additional information myself.
    – lobis
    Commented May 31, 2021 at 5:01
0

So far I don't think what I want is possible using swapExactETHForTokens. I think I will have to settle with using WBNB but would love to be corrected! I guess including a swap from WBNB to BNB in the selfdestruct would achieve the same purpose.

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.