4

I'm trying to better understand how to implement smart contracts with tokens built on top of Ethereum. Before diving too deeply into the implementation details with Solidity, I'd like to understand possible roadblocks.

I'd like to understand better how data is carried in smart contracts, and how something like "colored coins" from Bitcoin lingo might work.

So, I've come up with an example of how I think that smart contracts could be used based on what I've read: Beervesting.

For my example, I came up with a new token: BeerCoins

Let's say these BeerCoins are created by mining, and breweries may want to purchase or mine them to create a beer futures market... maybe they want to sell their beer at a discount before they begin brewing it.

Brewery holding BeerCoins

Here are a few types of transactions that I think could be done with these BeerCoins:

Transaction 1 - Alice buys BeerCoins

So, let's say that Alice is a savvy beer drinker, and she wants to buy her beer in advance for a discount using Ethereum. She can redeem her beer once it's been brewed (in 2 months).

Alice trades Ethereum for BeerCoins, and the BeerCoins now change state: They're redeemable for 1 pint of beer each.

Transaction 2 - Alice sells some BeerCoins to Bob

Now, perhaps some time goes by, and Bob wants to get in on this BeerCoin action. He doesn't want to wait for the next batch; he'd like to buy some of the current batch. He's got a party coming up in the next few weeks, and good beer takes a couple of months to brew. Alice decides to give some (full) BeerCoins to Bob in exchange for some Bitcoins.

Transaction 3 - Bob redeems BeerCoins for Beer

Now, Bob's ready to have his party, so he sends some of his BeerCoins back to the Brewery, and they ship him beer. The Brewery attaches a tracking number to transaction, and the BeerCoins are transferred and emptied as soon as the beer is delivered (as reported by an oracle, the shipping company).

Beervesting!

So my question is:

Is this smart contract implementation possible? Can Ethereum tokens carry a state in this way?

Are there any good examples for this kind of implementation?

I think there are a few things to consider:

  • How to store/modify state data in a token? Are there best practices?
  • How much data can practically be stored in a token?

Thanks in advance for the help.

2 Answers 2

1

It is not really feasible to give every token a state. The storage cost just would be too big -> You would need to give every single token a owner. It might work if you don't have that many token. (1000l beer each) But still a possible implementation would be something like this:

contract beer{
   struct beerToken{
      uint deliveryDate;
      uint256 quantity;
      ...
   }
   mapping(address=>beer) beers;
}

A better implementation for the scenerio you suggested would be something like this:

contract beer{
   //the first uint could be a timestamp when the beer gets delivered
   //everyone now owns token of a delivery of beer
   //you could also add more variables to this
   mapping(uint=>mapping(address=>uint256)) balances;

   //which = timestamp
   function transfer(uint which,address _to, uint256 _value){
      if(balances[which][msg.sender]-value>0){
         balances[which][msg.sender]-=value;
         balances[which][_to]+=value;
      }
   }
}

Just ask if you need more infos. I'm glad to help.

2
  • Thank you for the help. Would the proposed implementation change if the commodity were, for example, a precious metal, rather than beer? Or, if there was an open-ended delivery date (i.e. whenever the token-holder wanted to trade it in). Aug 27, 2017 at 8:23
  • I was under the impression that tokens lived forever. If they could be created at contact initiation, and destroyed when redeemed... maybe the storage wouldn't be too bad? Sep 23, 2017 at 13:20
1

In my opinion it's totally in the ethereum context and can be done for sure.

Instead having a "state", Bob would just transfer to the let say last beer batch contract or x address on delivery! Then theses token burnt/destroy once all batch token back or x time reached.

Very nice idea!

Sorry for typos I'm on mobile and french :)

Note: from what I know oracle is consuming method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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