Structs

Let's look at an even more complex data structure:

struct Offer {    bool isForSale;    uint punkIndex;    address seller;    uint minValue;    address onlySellTo;}mapping (uint => Offer) public punksOfferedForSale;

Here, instead of using a single simple type like a string or an address, we define our own custom data structure, an Offer. The offer is a collection of simple types, we pack into one bundle for easy access:

  • isForSale: A boolean true|false stating whether the offer is valid or not.
  • punkIndex: The identifier for the offered punk
  • seller: The address of the account making the sell-offer
  • minValue: The asked price of the punk
  • onlySellTo: An optional address to make the offer exclusive to one buyer.

The mapping itself looks something like this:

{    "0": {        "isForSale": true,        "punkIndex": 0,        "seller": "0xe08c32737c021c7d05d116b00a68a02f2d144ac0",        "minValue": 1001000000000000000000,        "onlySellTo": "0x0000000000000000000000000000000000000000"    },    "1": {        "isForSale": false,        "punkIndex": 1,        "seller": "0xb88f61e6fbda83fbfffabe364112137480398018",        "minValue": 0,        "onlySellTo": "0x0000000000000000000000000000000000000000"    },    "...": {},    "9999": {        "isForSale": true,        "punkIndex": 9999,        "seller": "0x2ee43f9a329623f4e97c29d813dff92ee968daee",        "minValue": 9000000000000000000000,        "onlySellTo": "0x0123456789012345678901234567890123456789"    },}

The above is a commonly used data structure called JSON (short for "JavaScript Object Notation") a simple key-value syntax which makes it easy to visualize hierarchical data. It reads as follows:

  • Punk #0
    • is offered for sale "isForSale": true
    • by 0xe08c327...44ac0 "seller": "0xe08...44ac0"
    • for 1001 Eth (1001 billion billion Wei) "minValue": 1001000000000000000000
    • to anyone, openly on the market "onlySellTo": "0x000...000
  • Punk #1 is not for sale "isForSale": false
  • Punk #9999
    • is for sale
    • by 0x2ee43f9...8daee
    • for 9000 Eth
    • to 0x01234...6789 "onlySellTo": "0x01234...56789"

Alternatively we can map it out in a more traditional table like so:

Punk uintFor Sale Order.isForSaleSeller Order.sellerPrice Order.minValueRecipient Order.onlySellTo
0true0xe08c327...144ac01001 Eth
1false0xb88f61e...3980180 Eth
...............
9999true0x2ee43f9...68daee9000 Eth0x0123456...456789

With the above, we can truly appreciate this incredible characteristic of Ethereum smart contracts: That they can hold arbitrary state. But unlike a traditional database, the blockchain ensures the data is always available and never lost or tampered with.