Search code examples
structmappingstoragesolidity

Solidity struct mapping not stored in contract


I read many articles on how to use mappings, mappings in struct and came out with something that should be correct to me, based on a few threads. I know that since solidity 0.7.0 things have changed with nested mappings in struct and did the following :

contract Test {
    constructor() {
    }   

    struct Bid {
        uint auction_id;
        address addr;
        uint amount;
    }   

    struct Auction {
        uint id; 
        string dtype;
        uint start_date;
        uint end_date;
        string label;
        uint price;
        uint amount;
        bool closed;
        mapping(uint => Bid) bids;
        uint bidCount;
    }   

    uint public auctionCount = 0;
    mapping(uint => Auction) public auctions;

    function createAuction( string memory plabel, string memory ptype, uint nbhours, uint pprice) external {
        Auction storage nd = auctions[auctionCount];
        nd.id = auctionCount;
        nd.dtype = ptype;
        nd.start_date = block.timestamp;
        nd.end_date = block.timestamp+nbhours*60*60;
        nd.label = plabel;
        nd.price = pprice;
        nd.amount = 0;
        nd.closed = false;
        nd.bidCount = 0;
        auctionCount++;
    }
}

Everything compiles fine, the createAuction transaction is succesful. When checking on the contract in Ganache, auctionCount is incremented but I have no items added in the drawsmapping. I also debugged the transaction with truffle and it goes through the function, assigning values through the execution of createAuction, but the changes are not persistent. I even tried removing one string attribute since I read that when there are 3 it could have been a problem (ok, I have only 2 max ;)).

I must have missed something, but I'm out of options right now.

Thanks in advance for your help !


Solution

  • If you are talking about auctions mapping, ensure you use the correct index when accessing mapping items. In your case, the first Auction item you add to the mapping will have a 0 index. I tried your contract in Remix, and everything worked well.