1

I am currently trying to build a prototype for a simple Ethereum based Ebay - I know there are already solutions for it, it is just for learning purposes.

Currently I am lacking the understanding on how to pass in changing ether values into functions. For example when I want to post a new auction:

struct Auction{
    address owner;
    uint price;
    string describtion;
  }

  Auction[] public auctions;

  function createAuction(address _owner, uint256 _price, string _describtion) public{
    Auction memory _auction = Auction({
      owner: _owner,
      price: _price,
      describtion: _describtion
    });

    auctions.push(_auction);
  }

This is the output of an example auction in remix after calling the getter of the auctions array:

Remix screenshot of generic auctions array output.

When I am now adding a new auction, I can only hand it uint256 values, which makes the prices non-ether values. My question is, how can I let the function know that the price variable is of type ether?


PS. I tried to create a static global variable, which is of type ether and it gives me back the ether value in wei. But I still can't adapt this on how to give the function different ether values with each call.

 uint256 public auctionStartPrice = 0.015 ether;

      struct Auction{
    address owner;
    uint256 price;
    string describtion;
  }

  Auction[] public auctions;

  function createAuction(address _owner, string _describtion) public{
    Auction memory _auction = Auction({
      owner: _owner,
      price: auctionStartPrice,
      describtion: _describtion
    });

    auctions.push(_auction);
  }

  function getVAlueOfAuction(uint256 x) public returns (uint256 value){
      value = auctions[x].price;
      return value;
  }

Here is the output with the global variable as starting price: enter image description here

3 Answers 3

0

Turned out, that the answer was quite obvious: Multiply the given value with a static variable of type ether, which is equal to 1 ETH:

 uint256 public myEtherValue = 1 ether;

 Auction[] public auctions;

  function createAuction(address _owner, uint256 _price, string _describtion) public{

    uint256 newPrice = SafeMath.mul(_price, myEtherValue);

    Auction memory _auction = Auction({
      owner: _owner,
      price: newPrice,
      describtion: _describtion
    });

    auctions.push(_auction);
  }

  function getVAlueOfAuction(uint256 x) public returns (uint256 value){
      value = auctions[x].price;
      return value;
  }
0

I strongly recommend against inputting in ETH. You lose the value of having decimals and run the risk of a user inputting wei and having their price 10^18 times higher than they should. You should accept input as wei and your frontend should perform the conversion from ETH to wei

0

It is important to note that 'Ether' is not a separate variable type. The suffix 'Ether' is just a convenience utility that coverts values in wei to the desired denomination. In your case, prices should be denominated in wei and stored as uint256 variables. On your front-end you can then convert wei price into ether price (1 ether == 1e18 wei). Thus, in your first example, a price of 20 is actually 20 wei which is very tiny amount. See more here

Note that this is also how EVM will interpret your input. If you try to send a transaction with a value of 20, you will be actually sending 20 wei so to send 20 ether you actually need to send value = 20,000,000,000,000,000,000

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.