Is it possible to call Uniswap from a smart contrat and make a swap with smart contract ether, and not from the message sender ether ? (and send back the swapped token to the owner)
I tried like this on Kovan testnet but it failed :
pragma solidity ^0.8.2;
import "https://github.com/Uniswap/uniswap-v2-periphery/blob/master/contracts/interfaces/IUniswapV2Router02.sol";
contract Coffre{
address owner =msg.sender;
IUniswapV2Router02 uni = IUniswapV2Router02(0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D);
address private token_address = 0x514910771AF9Ca656af840dff83E8264EcF986CA; //chainlink
function getPathForETHtoToken() private view returns (address[] memory) {
address[] memory path = new address[](2);
path[0] = uni.WETH();
path[1] = token_address;
return path;
}
function swapContractEthToLink() public {
uni.swapExactETHForTokens(0,getPathForETHtoToken(), owner, block.timestamp + 15);
}
Also with swapExactETHForTokens I can't specify how many Eth from the contract I want to swap so it's probably not the right function
Best Answer
If you want to swap ETH for Tokens, you need to send ETH to your contract, so you are missing the
payable
feature in your function.This is a code example that worked for me:
As per your last remark:
You specify the amount of ETH by sending it when you call the payable function. Note that the function now is
payable
and transfers the ETH amount through{value: msg.value}