0

Scenario:

Imagine I create a crypto currency ZZZ (based on Ethereum blockchain) for buying/selling fruits.

There are two shops wanting to sell something:

  • Apple shop, offering 5 kg of apples for sale, at price 10 ZZZ per kg
  • Orange shop, offering 3 kg of oranges for sale, at price 15 ZZZ per kg

There are three customers:

  • Alice wants to buy 2 kg of apples from Apple shop
  • Bob wants to buy 1.5 kg of apples from Apple shop
  • Bob wants to buy 1 kg of oranges from Orange shop
  • Cesar wants to buy 1.5 kg of oranges from Orange shop

After all four transactions complete there should be:

  • 1.5 kg apples left in Apple shop
  • 0.5 kg oranges left in Orange shop

ZZZ balance should be accordingly changed for each customer.

Questions:

  1. How can a shop record the available quantities in the blockchain?
  2. How can a shop transfer ownership of quantities of apples/oranges to the customers who bought them, in exchange for the ZZZ paid?
  3. I'm aware that the underlying blockchain tech will make sure no double spending of ZZZ can occur, but how can I ensure no more apples/oranges are sold than there is available on stock?
  4. Are there any sample projects which I could refer to to see how would solidity code look like for implementing such contracts where some value is partially transferred in exchange for the amount of the currency paid?
0

I've realised that I had wrong understanding of how smart contracts (token contracts, to be more specific) work. Basically ERC20 covers all I need.

ZZZ would be defined as ERC20 contract, and fruit shop contract would exchange the value with ZZZ using approve and transferFrom functions.

  1. Buyer would invoke approve function of ZZZ contract to enable shop contract to transfer the funds to seller.
  2. Seller would invoke shop contract, which would internally call ZZZ contract to check if allowance <= balanceOf and execute transferFrom function to transfer funds to seller, after which it would update it's fruit balances by moving the quantity of fruit from seller's to the buyer's address.

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.