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

问题描述 投票:0回答:2

我阅读了许多关于如何使用映射、结构中的映射的文章,并基于一些线程得出了对我来说应该正确的内容。 我知道自 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++;
    }
}

一切编译正常,

createAuction
交易成功。 在 Ganache 中检查合同时,auctionCount 会增加,但我在
draws
映射中没有添加任何项目。 我还使用 truffle 调试了事务,它通过函数,通过执行
createAuction
来赋值,但更改不是持久的。 我什至尝试删除一个字符串属性,因为我读到当有 3 个字符串属性时可能会出现问题(好吧,我最多只有 2 个;))。

我肯定错过了什么,但我现在别无选择。

预先感谢您的帮助!

struct mapping storage solidity
2个回答
1
投票

如果您正在谈论

auctions
映射,请确保在访问映射项时使用正确的索引。在您的情况下,添加到映射的第一个
Auction
项目将具有
0
索引。我在Remix中尝试了你的合同,一切都很顺利。


0
投票

当我使用 truffle 控制台向映射添加元素时,我遇到了同样的问题。但是如果你调用 get 方法,即使它没有显示在 ganache 中,它也会返回正确的值。

© www.soinside.com 2019 - 2024. All rights reserved.