2

I am trying write a contract like below.

pragma solidity ^0.4.9;

/* Contract accepting ethers during 10 minutes */
contract Trading {

    address receivingAccount;
    uint deadline;

    struct Asset {
        string name;
        uint price;
        uint barcode;
    }

    struct Stock {
        Asset asset;
        uint count;
    }

    struct Vendor {
        string name;
        address account;
        Stock[] stocks;
    }

    Asset[] assets;
    Vendor[] vendors;

    /*  at initialization, setup the owner */
    function Trading(address _vendor0, address _vendor1) {
        // create 2 assets
        assets.push(Asset("Asset0", 12, 1234));
        Asset memory asset1 = Asset("Asset1", 34, 5678);
        assets.push(asset1);

        // create 2 vendors
        Vendor memory vendor0 = Vendor("Vendor0", _vendor0, new Stock[](5));
        Vendor memory vendor1 = Vendor("Vendor1", _vendor1, new Stock[](5));

        // add assets to the stock of vendor0
        vendor0.stocks[0] = Stock(assets[0], 2);
        vendor0.stocks[1] = Stock(assets[1], 2);
        vendors.push(vendor0);

        // add assets to the stock of vendor1
        vendor1.stocks[0] = Stock(assets[0], 1);
        vendor1.stocks[1] = Stock(assets[1], 1);

        // set the deadline as 10 minutes
        deadline = now + 10 * 1 minutes;
    }  

    function listAssetBarcodes() constant returns (uint[]) {
        uint[] memory barcodes = new uint[](assets.length);
        for (uint i=0; i<assets.length; i++) {
            barcodes[i] = assets[i].barcode;
        }
        return barcodes;
    }

    modifier afterDeadline() { if (now >= deadline) _; }

    /* checks if the time limit has been reached and ends the contract */
    function dispose() afterDeadline {
        suicide(receivingAccount);
    }
}

However, when I add the statement vendors.push(vendor0); to the constructor (as above) I am having the following compilation error from the online compiler:

Unimplemented feature (/src/libsolidity/codegen/ArrayUtils.cpp:215): Copying of type struct Trading.Stock memory[] memory to storage not yet supported.

Can anyone help me how can I use the arrays in Solidity properly? Or is there a list or vector like structure we can use or we are obligated to use arrays for the moment?

2 Answers 2

1

Based on error message somewhen this feature will be implemented. For now you can do something like this:

    // create 2 vendors
    // Vendor memory vendor0 = Vendor("Vendor0", _vendor0, new Stock[](5));
    // Vendor memory vendor1 = Vendor("Vendor1", _vendor1, new Stock[](5));

    // // add assets to the stock of vendor0
    // vendor0.stocks[0] = Stock(assets[0], 2);
    // vendor0.stocks[1] = Stock(assets[1], 2);
    // vendors.push(vendor0);

    // // add assets to the stock of vendor1
    // vendor1.stocks[0] = Stock(assets[0], 1);
    // vendor1.stocks[1] = Stock(assets[1], 1);

    // add assets to the stock of vendor0
    vendors.push(Vendor("Vendor0", _vendor0, new Stock[](5)));
    vendors[vendors.length-1].stocks[0] = Stock(assets[0], 2);
    vendors[vendors.length-1].stocks[1] = Stock(assets[1], 2);


    // add assets to the stock of vendor1
    vendors.push(Vendor("Vendor1", _vendor1, new Stock[](5)));
    vendors[vendors.length-1].stocks[0] = Stock(assets[0], 1);
    vendors[vendors.length-1].stocks[1] = Stock(assets[1], 1);
2
  • This solution also does not work. The compiler is giving a similar error. Unimplemented feature (/src/libsolidity/codegen/ArrayUtils.cpp:215): Copying of type struct Trading.Stock memory[] memory to storage not yet supported. Commented Mar 13, 2017 at 13:59
  • Sorry, do not know why browser-solidity did not show an error :(. You can store the stocks in global storage variable. For example in something like this mapping (address=>Stock[]) vendor_stocks Commented Mar 13, 2017 at 14:35
1

You can use mapping(uint => Stock) instead of Stock[].

So you can implement Vendor struct as follows:

struct Vendor {
    string name;
    address account;
    mapping(uint => Stock) stocks;
    uint stockCount;    
}

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.