January 25, 2026

Capitalizations Index – B ∞/21M

ERC20 Token in solidity with price in Euro/USD/FiatCurrency

ERC20 Token in solidity with price in Euro/USD/FiatCurrency

In some cases it is necessary to set the price of token in the fiat currency such as Euro, USD and etc.

courtesy of bitcoinsdailynews.com

One possible way to achieve that is to use FiatContract. The idea is simple — call the function from FiatContract then it returns the price of one cent in ETH Wei.

Let’s consider an example from fiatcontract.com:

// returns $5.00 USD in ETH wei.
function FiveETHUSD() constant returns (uint256)

To calculate the price of one token in Wei I introduced an EuroWei value:

1 Euro = 1e18 EuroWei (like 1 ETH = 1e18 Wei)

For example, if the current price of ETH is 100 Euro, then the following expressions are true:

1 cent = 0.0001 ETH = 1e14 Wei

1 cent = 0.01 Euro = 1e16 EuroWei

General form of expressions:

1e16 EuroWei = oneCent Wei

currentPrice EuroWei = x Wei

After applying the fundamental rule of proportions we could calculate the price of one token in Wei:

x Wei = (currentPrice EuroWei / oneCent Wei) * 1e16 EuroWei

The code of our formula in Solidity:

public priceOfOneTokenInEuroWei = 1000000000000;
function getPrice() public returns(uint256 priceOfOneTokenInWei) 

We could easily write the code for buy() and sell() functions using getPrice():

public priceOfOneTokenInEuroWei = 1000000000000;
function buy() payable public returns (uint256 amount) 
//revenue — ether; amount — tokens
function sell(uint256 _amount) public payable returns(uint256 revenue)

Evidently, when user want to buy tokens, then he/she send Ether to the smart contract’s function buy() and this function calculate how many tokens he/she will receive based on price of one token in Euro/USD.

When user want to sell tokens, then he/she send tokens to the smart contract’s function sell() and this function calculate how many Ethers he/she will receive based on price of one token in Euro/USD.

The source code could be found in the repository of Social Decentralized Token (SDT)

Published at Fri, 29 Mar 2019 13:19:44 +0000

Previous Article

TechCrunch Founder: I’ve Never Been More Bullish on Crypto

Next Article

Bitcoinisierung: Warum die Kryptowährung Nr. 1 die Wirtschaft Argentiniens retten kann

You might be interested in …

What is swisscoin (scn)?

What is Swisscoin (SCN) ?

What is Swisscoin (SCN) ? Swisscoin in an ERC-20 Token 0x3Ff663f89631d3948f85CE1365da09910EAa013f check it out on Etherscan !