此合同允许用户在上次发生时签入并存储:
pragma solidity ^0.4.18;
contract DSquare {
mapping (address => uint) public checkins;
function checkin() public {
require(now - checkins[msg.sender] > 1 minutes);
checkins[msg.sender] = now;
}
}
在打算存储所有用户签入的第二个版本中,我将ValueType从uint更改为uint[],从而产生了以下契约:
pragma solidity ^0.4.18;
contract DSquare {
mapping (address => uint[]) public checkins;
function checkin() public {
uint[] storage userCheckins = checkins[msg.sender];
if (userCheckins.length > 0) {
require(now - userCheckins[userCheckins.length - 1] > 1 minutes);
}
userCheckins.push(now);
}
}
之后我就不能再打电话给get函数了。使用web3,它显示:“未明错误:”签入“的参数数无效。得到1预期的2”。
在工作组生成的JSON文件中,我可以看到它实际上有第二个参数:
"abi": [
{
"constant": true,
"inputs": [
{
"name": "",
"type": "address"
},
{
"name": "",
"type": "uint256"
}
],
"name": "checkins",
"outputs": [
{
"name": "",
"type": "uint256"
}
]
(完整的JSON在这里:https://gist.github.com/saulobrito/ebdd40dd610da23ece1c97c4c912b492)
在Remix上也是一样。这就是第一个合同的表现方式:
在这里,第二个版本:
这是个虫子吗?get函数不应该只依赖于键吗?有什么想法吗?
谢谢!
发布于 2018-03-19 14:30:17
您的mapping
指向动态数组(带有s)。其中每一个都有一个索引。这就是为什么它要求一个加法和一个uint。
您可以手动构建它,它看起来如下所示:
pragma solidity ^0.4.18;
contract DSquare {
mapping (address => uint[]) public checkins;
function checkin() public {
uint[] storage userCheckins = checkins[msg.sender];
if (userCheckins.length > 0) {
require(now - userCheckins[userCheckins.length - 1] > 1 minutes);
}
userCheckins.push(now);
}
function returnCheckinValue(address mapKey, uint arrayRow) public view returns(uint value) {
return checkins[mapKey][arrayRow];
}
}
希望能帮上忙。
https://ethereum.stackexchange.com/questions/43250
复制相似问题