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.


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.

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

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