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

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;
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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