5

From my understanding, Ether is converted to an ERC-20 token (like BAT), via a smart contract. Ether is assigned to that contract by sending Ether to the smart contract's address. Please correct me if I'm wrong in any of that.

My question though is, how does one convert an ERC-20 token (like BAT) back to Ether? If the smart contract holds the relationship between Ether that I sent to it, how do I convert the ERC-20 token back to Ether if I no longer want it? Instead, I'd just like Ether to use for other purposes.

Thank you for helping me understand this.

5 Answers 5

6

I think you have got it a bit wrong.

You are not converting ERC20 Tokens into ether or vice versa. You are mere exchanging it for ether.

Smart Contract works like this:

The amount of ether you sent in, returns the pre-specified amount of ERC20 tokens. This is based on the rate that is coded while contract creation itself.

For example, consider this:

uint256 token_per_wei = 1;

This will send you back 10^18 custom tokens (since 1 ether = 10^18 wei).

What happens to your ether?

Every contract has a funding recipient set. Your sent ether gets transferred to their account and now belongs to them.

Think of it this way:

You are buying their custom tokens using the ether.

How to get the ether back?

Every token has a rate once their ICO gets over. You need to sell at exchanges they are listed on.

Goodluck!

2

The short answer is:

You should go and find one exchange lists your tokens and trade them to get back your ethers.

The long answer is below:

Ether is converted to an ERC-20 token (like BAT), via a smart contract.

In fact, i's not a conversion.

According to https://ethereum.org/ether, ether is a fuel for operating the distributed application platform Ethereum (or we can say that ether is the native currency on Ethereum), and it doesn't conform to ERC20 standard. (That's why we need something like weth).

ERC20 tokens are just smart contracts (or dumb programs can get and set states on Ethereum) that confrom to ERC20 standard.

If you read the source code of BAT token contract, you will find a method called createTokens:

/// @dev Accepts ether and creates new BAT tokens.
function createTokens() payable external {
  if (isFinalized) throw;
  if (block.number < fundingStartBlock) throw;
  if (block.number > fundingEndBlock) throw;
  if (msg.value == 0) throw;

  uint256 tokens = safeMult(msg.value, tokenExchangeRate); // check that we're not over totals
  uint256 checkedSupply = safeAdd(totalSupply, tokens);

  // return money if something goes wrong
  if (tokenCreationCap < checkedSupply) throw;  // odd fractions won't be found

  totalSupply = checkedSupply;
  balances[msg.sender] += tokens;  // safeAdd not needed; bad semantics to use here
  CreateBAT(msg.sender, tokens);  // logs token creation
}

So the process is basically like this:

  1. You send or transfer the ethers to the contract. (Note: These ethers are no longer owned by you!)
  2. the contract mutilply the ethers with tokenExchangeRate, which is predefined with 6400, as your token balance.
  3. the contract records the token balance for the address (where you sent the ether).
  4. If finalize called, all the ethers owned by the contact will be sent to ethFundDeposit, which is 0x88e2efac3d2ef957fcd82ec201a506871ad06204, a Multi-sig Wallet owned by Brave International (Source here: https://etherscan.io/token/BAT#readContract)

In this case, you can't convert BAT tokens back to ethers since the crowd sale was finalized and the refund method won't be executed:

/// @dev Allows contributors to recover their ether in the case of a failed funding campaign.
function refund() external {
  if(isFinalized) throw;                       // prevents refund if operational
  if (block.number <= fundingEndBlock) throw; // prevents refund until sale period is over
  if(totalSupply >= tokenCreationMin) throw;  // no refunds if we sold enough
  if(msg.sender == batFundDeposit) throw;    // Brave Intl not entitled to a refund
  uint256 batVal = balances[msg.sender];
  if (batVal == 0) throw;
  balances[msg.sender] = 0;
  totalSupply = safeSubtract(totalSupply, batVal); // extra safe
  uint256 ethVal = batVal / tokenExchangeRate;     // should be safe; previous throws covers edges
  LogRefund(msg.sender, ethVal);               // log it 
  if (!msg.sender.send(ethVal)) throw;       // if you're using a contract; make sure it works with .send gas limits
}

So if you don't want these tokens, you have to trade them to get back your ethers on some exchanges (lists your tokens).

1

I see a lot of posters respond to this question by suggesting the currency exchanges. However, the more interesting solution is to create a smart contract that can accept token for ether at an agreed upon exchange rate. This is clearly not as liquid as using an exchange to determine market prices and trade quickly.

However, for composing custom tokens that are to be used in small and/or temporary applications, there is no reason a smart contract can't handle exchanging tokens for ether. As long a contract supports a mechanism to exchange token for ether with a given or proposed exchange rate, it can be done without much effort. The contract itself can contain ether or can provide the functionality to transact money on behalf of a token buyer. I can put up sample code later, if you're still interested.

0

Generally, there is no conversion going on between ethereum and an ERC-20 token (at least in the sense that you are describing it). However, it is exactly the same "conversion" that goes on when we talk about fiat currency conversions.

Lets say that I'm in the United States and I'm in a restaurant that doesn't take credit cards and I'm out of US dollars. However, I have a $100 Canadian in cash. I can't push a magic button and turn my Canadian dollars into US dollars to pay for my meal. However, let's suppose you're also at the restaurant, you have some US dollars, and you're planning to come to Canada. You might be willing to sell your US dollars to me. You might say "your bill is $50 (US) -- I'll give you $50 USD if you give me $65 Canadian". And I'd say "fair deal" and give you $65 CAD and you give me $50 USD. Note how this was an exchange, not a transmutation (like turning water into ice): I gave you something and you gave me something different in return. This happens at much larger scales using computerized systems on a day-to-day basis. These are the foreign exchange (forex) markets. Cryptocurrencies and tokens can be exchanged using the exact same mechanism. Instead of listing Canadian dollars and US dollars (though there is nothing technically preventing that from happening), you might see US dollars, ETH, and BAT.

You can think of ERC-20 token contracts as code for operating a list of balances of the relevant token. For example, BAT keeps track of which accounts "owns" how many BAT. Your ethereum account's private keys authorize transactions on your behalf within the BAT contract.

I said "generally" above; there is the possibility of constructing something like TheDAO. With TheDAO, one could "convert" DAO tokens into ether. This was possible because TheDAO's contract actually had ether stored in it (which is usually not the case with a token contract). (Simplifying things a bit) one could call a function in TheDAO and destroy TheDAO tokens and have the contract send you a portion of the ether contained within the contract.

0

So... I had a similar Q earlier today but I found this thread WAY too complicated, (not stupid.. just not overly technical either).

So I asked in separate thread: Any quick, easy ways to convert ERC-20 tokens?

Long story short... Ethersweep does it all in one go. http://ethersweep.com

Save yourself a week.

2
  • 3
    Just as a note of warning to anyone else looking to use this method: This service requires a user to enter their private key into the browser. Even if the service itself is secure, of which there's no guarantee, you're still entering your private key into a browser, which may be insecure... Commented Nov 10, 2018 at 20:03
  • Fair shout, I can't comment on browser security risks but it was pretty painless... here's the output URL if useful: ethersweep.com/transaction/status/… Commented Nov 11, 2018 at 22:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.