I am trying to create a smartcontract but I am unable to understand which method will be used when someone tries to buy/sell from any swap platform.

I've checked transfer and transferFrom but from the definition of these methods there is only the amount of token (linked to smartcontract) that is passed during the call method. How does the exchange with ETH occur ?

  • are you asking about uniswap v2 or uniswap v3 ? because they have different functions to buy or sell
    – Nulik
    Jul 25, 2021 at 22:45
  • 1
    How does the exchange with ETH occur this goes in the Router contract. Did you download both? The "core" and the "periphery" ?
    – Nulik
    Jul 25, 2021 at 22:47
  • I'm talking about any exchange in general. I want to understand how it works : Does the router call transferFrom from both tokens ? therefore there won't be any need for me to code this action ?
    – Hich
    Jul 26, 2021 at 7:06
  • @Hitch no, the Pair contract does the ERC20 transfer. The router only manages the process at higher level
    – Nulik
    Jul 26, 2021 at 14:05

1 Answer 1


Uniswap V2 in this case uses existing tokens to automatically create pairs via a factory contract. so the pair contract and the router contract methods are set in stone and you can't change them.

the only thing you can modify is the ERC20 token you are going to use in the pair you want to create. Since Uniswap V2 uses ERC20 what you need to implement is the minimum of the interface methods described by the IERC20 interface. you can find them in the openZepplin implementation which is the most used one.

if you intend to use the router to do swaps, there are swap methods that you can use namely : swapTokensForExactTokens, swapExactTokensForTokens and both their fee supporting versions (some tokens use extra fees and other thing so they don't transfer exactly the amounts that you pass in). there is also methods for swapping ETH for Tokens and vice versa as well as fee supporting variation of those too.

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.