Uniswap – How Does Uniswap Interact with Token Contract When Users Swap?

contract-developmentswapstransactionsuniswap

I've been getting the hang of smart contract development and token creation in general, but the one thing I've been struggling with is understanding how Uniswap interacts with my token contract when users swap Ethereum for my token or vice versa.

When transfers take place with my token there are a couple transaction fees that are specific to the transfer function I've defined, and I can't figure out if those fees will be incurred when users swap for my token on Uniswap. In other words, is the transfer function of my token contract called when swapping occurs? If not, how could I include those transaction fees specific to my contract when users swap for my token?

I've seen a lot of other token contracts out there actually define a Uniswap factory/router reference in the code and then create a pair contract there. That seems to give them more configurability from what I can tell. Would I need to do something like that to customize the behavior of swapping with my token on Uniswap? Or does it make sense just to set up the token pair on the Uniswap website?

Best Answer

When you swap an ERC20 token for ETH on Uniswap, the token's transfer function is called - see IERC20.transfer. IIRC when you "buy" your wallet is the recipient and the Token-WETH pair contract (created by the Uniswap Factory) is the sender. On the other hand, when you "sell" your wallet is the sender and the pair is the recipient.

If you'd like to get the address of the pair (your token-WETH) you can use the factory to get it:

IUniswapV2Router router = IUniswapV2Router(uniswapV2RouterAddress);
IUniswapV2Pair pair = IUniswapV2Factory(router.factory())
    .getPair(address(this), router.WETH());

See Uniswap interface code and this answer (for example) to find the Uniswap router address (or google it)

Related Topic