14

I am trying to create a framework for users to offer generic goods for sale. For example, a single generic item (a Gizmo) might have 14 people selling it for different prices and from different parts of the world. To enable this, I need to manage a list of prices, kept in storage, that can be queried by a web3.js-driven frontend.

Something like a mapping would be best:

uint productId;  // Set automatically for each product
uint memberId;  // Set automatically at join
uint price;  // Prices are in US cents

/* (productId => (memberId => price)) */
mapping (uint => mapping (uint => uint)) priceLedgers;

/* Pseudo-JavaScript below */
for (var i=0, i < priceLedgers[productId].length, i++) {
    // List of members with their prices here.
}

Maybe aonther way to explain what I am trying to do is with Python.

priceOffers = {}
priceOffers[1] = 234
priceOffers[45] = 99392
priceOffers[23] = 111

>>> priceOffers
{1: 234, 45: 99392, 23: 111}

>>> for key, value in priceOffers.iteritems():
    print(key, value)
(1, 234)
(45,99392)
(23, 111)

productId = 34
priceLedgers = {}
priceLedgers[34] = priceOffers

Is this possible in Solidity?

1
  • Do you actually need a mapping here? It seems like a list of structs would serve you just as well. Apr 13, 2016 at 15:17

3 Answers 3

14

You could just store the index of the mapping in an array.

uint[] indexes;
mapping (uint => uint) example;

function add(uint x){
  example[indexes.length] = x;
 indexes.push(indexes.length);
}

Then just iterate over the array index as key.

if you want custom unordered keys, it's the same. You would just need to pass the key in the add function

function add(uint data,uint index){
  example[index] = data;
  indexes.push(index);
}
0
6

Here is implementation of such structure: https://github.com/chriseth/solidity-examples/blob/master/iterable_mapping.sol

3

I have found a solution that will probably work.

/* Basic arrays of objects */
Product[] products;
Member[] members;  // `Member` defined elsewhere

/* Custom data types for my objects */
struct Product {
    string name;  // Product’s display name
    Offer[] priceList;  // An array of prices – see struct below.
    uint dateAdded;  // Unix timestamp for when product was added
}

// This is a type that sets a seller’s price for a product
stuct Offer {
    uint memberId;  // Seller’s memberId
    uint priceTag;  // Price seller will take for 1 quantity in US cents
}

Pseudo-JavaScript:

/* Get all sellers and prices */
for (var i=0, i < products[productId].priceList.length, i++) {
    memberId = products[productId].priceList[i].memberId

    var member = members[memberId]
    var price = products[productId].priceList[i].priceTag
}

/* Get one seller's price */
var myProduct;
var memberToFind = 4;
var membersPrice;
for (var i=0, i < products[productId].priceList.length, i++) {
    if (products[productId].priceList[i].memberId == memberToFind) {
        membersPrice = products[productId].priceList[i].price;
        // Do something.
    }
}

Can anyone find a better way?

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.