How to set the ERC20 token price in respect to ether in solidity

0 votes

Hi, all

I have a input field in my DAPP where user can input ETH and click the submit button to get token worth ETH..How to calculate the token price in ether and get the token and show it in DAPP..

Token Price is 0.10 cents

function getPrice() public pure returns(uint256) {
     uint256 totalEther = uint256(225800608429140) / uint256(10**18);
     uint256 result = uint256(231481480000000) * 10**18;
     uint256 totalTokens = result / 1 ether;
    //  _totalEther =  totalEther;
     return totalTokens;
 }

but this isn't working.

Nov 7, 2020 in Blockchain by jas
• 120 points
5,402 views

1 answer to this question.

0 votes
Hi, @Jas,
In Solidity, you don't directly set a token's price in respect to Ether (ETH) within the ERC-20 contract itself. Instead, you typically create a mechanism (like a function) that defines the exchange rate between ETH and the token.
answered Nov 9, 2020 by anonymous
• 10,440 points

Related Questions In Blockchain

0 votes
1 answer

How to set token price in solidity?

We know that 1 Ether = 1018 wei, ...READ MORE

answered Sep 25, 2018 in Blockchain by slayer
• 29,370 points
9,291 views
0 votes
1 answer

How to save complex object in the contract in solidity

You can't pass objects when initiating a ...READ MORE

answered Oct 15, 2018 in Blockchain by Omkar
• 69,180 points
3,818 views
0 votes
1 answer

How to set the hex-encoded data field in a Web3j Ethereum transaction?

You can use the "data" field of ...READ MORE

answered Oct 15, 2018 in Blockchain by Omkar
• 69,180 points
3,242 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,750 points
2,286 views
0 votes
1 answer

How do I send back ethers to the sender of the tokens in a smart contract?

Whenever a smart contract receives ether via ...READ MORE

answered May 30, 2018 in Blockchain by Perry
• 17,100 points
3,925 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,808 views
0 votes
2 answers

Getting error while Initialising the Blockchain

Hello, @Edureka, You have two options: Execute with sudo. This ...READ MORE

answered Sep 17, 2020 in Blockchain by Roshni
• 10,440 points
3,895 views
0 votes
2 answers

Code to change owner

Hi, @Wilson, U have to write the require ...READ MORE

answered Oct 5, 2020 in Blockchain by Rajiv
• 8,870 points
2,205 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP