0

I'm working on creating a simple contract which will receive UNI tokens from an external account and swap it for ether from UniSwap.

I'm having a very hard time understanding the documents. Can anyone please help me with how to setup the contract so that it can accept the UNI token, and I can interact with it directly in remix. I went through the interfaces written too at github.com/Uniswap/uniswap-v2-core/blob/master/contracts/interfaces/IUniswapV2Callee.sol

But I'm not able to set it up. what is it end-to-end that I need to setup for it to receive token and make it available in the smart contract?

0

the interface you need to use is IUniswapV2Router01 (im not familiar at all with v3 ngl, so if you were looking to use uniswap v3 im afraid i cant help you without doing an amount of research im definitely too lazy to do lol), not IUniswapV2Callee. Look at the swapExactTokensForETH function, and from there it should be pretty self explanatory. If you need more hints (or some example code) ill be glad to help :)

EDIT : this should work (i didnt test it and wrote it 100% out of memory, it might contain syntax errors)

// Needs IUniswapV2Router01 and IERC20 to work
IERC20 UniToken = IERC20(UniTokenAddress)
IUniswapV2Router01 router = IUniswapV2Router01(UniswapRouterAddress) // Actually both UniswapRouter01 and 02 will work, but including only UniswapRouter01 will be cheaper to deploy


function TransferAndSwap(uint256 amount) public {
// gets the tokens from msg.sender's wallet 
UniToken.transferFrom(msg.sender, address(this), amount) // requires allowance from msg.sender to this contract
//generates the path to make the swap
address[] memory path = new address[](2);
path[0] = address(UniToken);
path[1] = //WETH address;
//lets the contract trade 'amount' of UNI using the UniswapV2Router01
UniToken.approve(address(router), amount)
// Makes the swap 
router.swapExactTokensForETH(
    amount,
    0, // accepts any slippage, might be an issue if you're planning to make big transactions (allowing malicious actors to frontrun you), there is a workaround for that including UniswapV2Pair's getReserves functions, you should be able to figure it out ;) 
    path,
    address(this), // or any address you want, if you want to send the output ETH to another address. 
    block.timestamp
);
}
// The fallback function, to recieve  ether from the uniswapV2Router
fallback() external payable {}
15
  • You are absolutely right, and it only takes a few lines of code to implement.
    – zmy
    Jul 30 at 3:36
  • Thanks a lot for your response. I looked at the swap functions, but wouldn't that be the next step after receiving UNI tokens? I would like that my contract initially can receive the tokens, and assign whatever the parameters I get to public variables, do some other tasks, so I can interact with them directly in remix. Then I can move forward with swapping. Some sample codes would really help. Jul 30 at 5:10
  • Hey, indeed that would be the 2nd step after recieving the tokens (that's the "hard" part so i assumed it was the part you were requesting help for). To have your contract interact with UNI (or any ERC20 token), you have to integrate the IERC20 library in your code. from there you just have to tell your contract where the UNI contract lives with IERC20 UNIToken = IERC20(//paste the uni contract address here), then you can call functions using UNIToken.balanceOf(address(this)) (this is an example that lets you get the contract's UNI balance)
    – Foxxxey
    Jul 30 at 7:58
  • maybe take a look at my answer here ethereum.stackexchange.com/questions/106311/…, theres some more info that can help you :)
    – Foxxxey
    Jul 30 at 7:59
  • I need to interact with the transaction, eg. when we transfer ether to the contract, the fallback function is called and we can get the msg.sender property. The same way I want to implement such a function which will be called when I transfer UNI token from external account to contract. And then emit an event from that function with sender address, tokens received, etc. And store all that info. in a mapping. I read that ERC20 doesn't allow developers to handle incoming token transactions. I tried to implement IERC777Recipient, but I can't get it work Jul 30 at 12:34

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.