简体   繁体   English

Solidity 结构映射未存储在合约中

[英]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 :我知道自从solidity 0.7.0以来,结构中的嵌套映射发生了变化,并做了以下事情:

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.一切编译正常, createAuction交易成功。 When checking on the contract in Ganache, auctionCount is incremented but I have no items added in the draws mapping.检查 Ganache 中的合同时,auctionCount 会增加,但我没有在draws映射中添加任何项目。 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.我还用 truffle 调试了事务,它通过函数,通过执行createAuction分配值,但更改不是持久的。 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 ;)).我什至尝试删除一个字符串属性,因为我读到当有 3 个时它可能是一个问题(好吧,我只有 2 个最大值;))。

I must have missed something, but I'm out of options right now.我一定错过了什么,但我现在别无选择。

Thanks in advance for your help !在此先感谢您的帮助 !

If you are talking about auctions mapping, ensure you use the correct index when accessing mapping items.如果您正在谈论auctions映射,请确保在访问映射项目时使用正确的索引。 In your case, the first Auction item you add to the mapping will have a 0 index.在您的情况下,您添加到映射的第一个Auction项目将具有0索引。 I tried your contract in Remix , and everything worked well.我在Remix中尝试了你的合同,一切都很顺利。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM