I am reading ERC20 protocol and learning how that is implemented from OpenZeppelin: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol

The code demoed how fungible tokens created using the Ethereum blockchain. But what I am confusing is that how that relates to the Ether balance on the accounts in Ethereum network. When transferring ethers, we use payable(...).transfer method in Solidity but I can't find anywhere it is used in the ERC20 implementation.

If ERC20 is only about token exchange, how should we implement purchase tokens by ether?

2 Answers 2


To clear things up there are multiple things to keep in mind:

  • Ethers are just numerical values "baked in" Ethereum, which means that ETH is the fundamental 'money' unit in the protocol. That's why we have specific solidity functions (send, transfer, call) for sending this native token (ETH).
  • ERC-20 is just a contract standard, outlining what methods the contract has to implement. All user balances are just variables in the contract and nothing more. All internal logic is up to the developer to determine.

To answer your questions:

  • Where do ERC-20 tokens come from? They have to be minted, either by the admin at contract deployment or via other logic, implemented in the _mint() function. This function might be restricted in different ways (admin only, only with ethers sent as value in the transaction, only for whitelisted addresses, might be internal etc.)
  • Where do i get ERC-20 token from? Either you mint them or pay someone to send you a specific amount.
  • How does buying ERC-20 tokens with ether work? Fundamentally buying something is swapping money for the thing you want to buy. In this case you can use a DEX (decentralized exchange) for swapping currencies. The way most such exchanges work is by swapping ERC-20 tokens for ERC-20 tokens. If you have ETH some, exchanges like Uniswap will "wrap" your ethers to WETH (ERC-20 token) and swap the WETH for something else. WETH is a simple ERC-20 that mints 1 WETH for every ETH you send it, thus making ETH=WETH in value.

TL;DR Most DEX'es will "wrap" your ETH (native currency) to WETH (ERC-20 token, equal in value to ETH) and then swap WETH to for other ERC-20. This action known as wrapping is equivalent to sending ETH and minting the same amount of WETH. Wrapping under the hood is just calling the deposit() function with some eth value in the transaction. The deposit function will then call _mint() with the correct value and set this value as owned by you in its internal variables. SENDING ETH TO CONTRACTS IS DANGEROUS, READ THIS


An ERC20 token contract is just a measure of value, (in some cases) shares or power, and is not transformable to native Ether per se.

There're no built-in purchase methods in the ERC20 token standard.

Usually, a pattern like this is used:

  • Minting is limited to the authorized callers only (the minter or admin, or owner roles)
  • A protocol might offer an option to buy tokens with native Ether, or in exchange for transferring the user's tokens, and will mint the corresponding tokens for the user in exchange
  • Alternatively, you can buy most normal tokens using other contracts like SwapPair, or use an external exchange protocol (like Uniswap or PancakeSwap), with the help of the swap pairs that implement specific logic and calculations for the swaps, as well as hold tokens (to create a permissionless swap experience)

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.