There's buyprice on function buy and sellprice on function sell. I would like to set up

1 ETH =  10000 (SYMBOL)
0.1 ETH = 1000 (SYMBOL)
0.01 ETH = 100 (SYMBOL)
0.001 ETH = 10 (SYMBOL)

How can I set a number on buyprice and sellprice? Even if i calculated it, it doesn't work at all

function buy() payable returns (uint amount) {
  amount = msg.value / buyPrice;      // calculates the amount
  require(balanceOf[this] >= amount); // checks if it has enough to sell
  balanceOf[msg.sender] += amount;    // adds the amount to buyer's balance
  balanceOf[this] -= amount;          // subtracts amount from seller's balance
  Transfer(this, msg.sender, amount); // execute an event reflecting the change
  return amount;                      // ends function and returns

function sell(uint amount) returns (uint revenue) {
  require(balanceOf[msg.sender] >= amount); // checks if the sender has enough to sell
  balanceOf[this] += amount;                // adds the amount to owner's balance
  balanceOf[msg.sender] -= amount;          // subtracts the amount from seller's balance
  revenue = amount * sellPrice;
  msg.sender.transfer(revenue);             // sends ether to the seller: it's important to do this last to prevent recursion attacks
  Transfer(msg.sender, this, amount);       // executes an event reflecting on the change
  return revenue;                           // ends function and returns

2 Answers 2


This seems to be part of the ethereum website example, the whole code is posted there. The price can be set in the constructor

contract Mycontract{
    uint public sellprice;
    uint public buyprice;
    address public admin;

    constructor(uint _sellprice, uint _buyprice) {
        sellprice = _sellprice;
        buyprice = _buyprice;
        admin = msg.sender;

    function sell()...

    function buy()..


If you need the price to be changed over time, you add this function:

function updateprice(uint _sellprice, uint buyprice) public {
    sellprice = _sellprice;
    buyprice = _buyprice;

Hope this helps.

  • Tnx for your answer, however it is unclear with my main question. I will put my words in constructor but where do i have to put it in my source? my source is down here. please check this ., etherscan.io/address/… Commented May 6, 2018 at 14:22

OK, first of all you need to convert all amounts to basic units. For ether, basic unit is Wei and 1 ETH = 1e18 Wei (1,000,000,000,000,000,000 Wei). So,

1 ETH = 1e18 Wei (1,000,000,000,000,000,000 Wei)
0.1 ETH = 1e17 Wei (100,000,000,000,000,000 Wei)
0.01 ETH = 1e16 Wei (10,000,000,000,000,000 Wei)
0.001 ETH = 1e15 Wei  (1,000,000,000,000,000 Wei)

You token seems to have 18 decimals as well, so one whole token is 1e18 basic units (BU), and

10000 (SYMBOL) = 1e22 BU (10,000,000,000,000,000,000,000 BU)
1000 (SYMBOL) = 1e21 BU (1,000,000,000,000,000,000,000 BU)
100 (SYMBOL) = 1e20 BU (100,000,000,000,000,000,000 BU)
10 (SYMBOL) = 1e19 BU (10,000,000,000,000,000,000 BU)

Now you may calculate your buy and sell prices in basic units:

tokenAmount = ethAmount / buyPrice
10000 (SYMBOL) = 1 ETH / buyPrice
1e22 BU = 1e18 Wei / buyPrice
buyPrice = 1e-4
buyPrice = 0.0001

The problem is that Solidity dose not support fractional numbers, so you cannot divide by 0.0001 directly. Fortunately, you may just multiple by 10000 like this:

uint invertedBuyPrice = 10000;
amount = msg.value * invertedBuyPrice;

Now sell price:

ethAmount = tokenAmount * sellPrice
1 ETH = 10000 (SYMBOL) * sellPrice
1e18 Wei = 1e22 BU * sellPrice
sellPrice = 1e-4
sellPrice = 0.0001

Now we have the same problem, seel price is fractional and Solidity cannot deal with fractions, though the same solution works here as well:

invertedSellPrice = 10000;
revenue = amount / invertedSellPrice;

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.