0

For my class project I am trying to replicate ebay on a DApp. When a buyer buys something from a seller, the wallet shows ether transacted, I also want to show quantity transacted. E.g.

Jan 1 2018:

Seller A has 0 ether and 10 carrots costing 2 ethers each Buyer B has 10 ether and 0 carrots costing 0 ethers each

Jan 2 2018:

Buyer buys 2 carrots costing 1 ether each, costing .2 ether for transaction Seller A has 2 ether and 8 carrots costing 2 ethers each Buyer B has 7.8 ether and 2 carrots costing 2 ethers each

How do I include the quantity of carrots in solidity and show increment for buyer and decrement for seller?

1 Answer 1

0

You could store the carrots in a mapping keyed by the accounts.

mapping (address => uint) carrots;

carrots[0x0SELLER....] would hold the number of carrots the seller has.

When someone calls the buyCarrots() function you would do something like this:

function buyCarrotsFromSeller(address _seller) payable {
  uint carrotsToBuy = carrotPrice / msg.value; // You would need to extend this to account for change and rounding errors.

  require(carrots[_seller] >= carrotsToBuy);

  carrots[_seller] -= carrotsToBuy;
  carrots[msg.sender] += carrotsToBuy;

  // Finally, transfer the ether paid for the carrots to the seller
  _seller.transfer(msg.value);
}
1
  • thanks for the comment, will try this out right away and let you know if this works..
    – param
    Nov 29, 2017 at 19:10

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.