我阅读了许多关于如何使用映射、结构中的映射的文章,并基于一些线程得出了对我来说应该正确的内容。 我知道自 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 个;))。
我肯定错过了什么,但我现在别无选择。
预先感谢您的帮助!
当我使用 truffle 控制台向映射添加元素时,我遇到了同样的问题。但是如果你调用 get 方法,即使它没有显示在 ganache 中,它也会返回正确的值。