1

I'm struggling to integrate swapExactETHForTokens into remix but I fail since I don't have any kind of coding experience

It shouldn't be anything fancy just a simple code that would allow me to buy a token from the contract

I checked out the official docs https://uniswap.org/docs/v2/smart-contracts/router02/#swapexacttokensforeth

I'm trying this code but not sure if it's the right one How to use swapExactETHForTokens in smart contract?

I know what each of those function should be filled with but I don't know exactly what's the exact uint integer and where exactly I need to insert the wallet, contract, amount, and so on

Can anyone show me a full working example(like swap eth to usdt) that I can throw on remix and go from there?

1
  • You probably don't have an answer because your question is too general and broad. You should try to learn about Dapp development first (tutorials) and how to interact with Dapp in general.
    – Undead8
    Commented Apr 7, 2021 at 18:04

1 Answer 1

2

I know this question is too old. But I wrote a very basic code for people who still don't know how to do the simple swapExactETHForTokens interacting with Dex router contract.

In my example code, I'm using BSC-Testnet Pancakeswap router, but this logic will work at any other Uniswap fork at many other chains. Just change the router address and a bit of code if necessary.

Code:

pragma solidity >=0.8.7;

//interface declaration
interface pancakeSwapper{
    //pancakeswap "swapExactETHForTokens" Function (you can find this in IPancakeRouter.sol at Pancakeswap Github)
    function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline) external payable returns (uint[] memory amounts);
}

contract VeriyBasicSwapping{
    //bsc-testnet pancakeswap router address.
    //(I'm using BSC Testnet Pancakeswap Router. https://pancake.kiemtienonline360.com/)
    address internal constant router = 0x9Ac64Cc6e4415144C455BD8E4837Fea55603e5c3;

    //pancakeSwapper interface implementation with their router address 
    pancakeSwapper constant public swapper = pancakeSwapper(router);

    //creating swap function
    function veryBasicSwapExactETHForTokens(uint256 _amountOutMin, address[] calldata _path, address _to, uint256 _deadline) external payable {
        /**
            You can change any of these parameters.
                - value: msg.value (transaction sender will define input ether value)
                - amountOutMin: just use 1 (basically disabling slippage, I don't recommend this, but works.)
                - path: ["Ether address","Token address"] (exactly in this sequence and format for THIS function.)
                - to: Wallet address which will receive the output tokens.
                - deadline: define it manually (you can use https://www.unixtimestamp.com/index.php **don't forget to increase some minutes) or just use "block.timestamp + 30"
        **/

        //do swap
        swapper.swapExactETHForTokens{ value: msg.value }(_amountOutMin, _path, _to, _deadline);
    }
}

After contract deploy, we can easily do the swap.


At Remix:

Input the Ether value you want to be spent in the WEI amount. You can use https://eth-converter.com/ to do this conversion.

  • Convertion:

enter image description here

  • Input value:

enter image description here

After this, go to deployed contracts section and input swap parameters like this. In this example, I'm swapping WBNB to Safemoon Token, defining amountOutMin as 1, setting my wallet to receive the Output tokens and Deadline.

enter image description here

Finally, just click Transact button, confirm at Metamask, and booya!

  • Output Transaction:

enter image description here


You can also verify this contract and swap directly from blockchain explorer as etherscan, bscscan, etc..

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.