Solidity
映射返回 false 但函式返回 true
我有一個映射 uint256=>bool。我給 projectId 看它是否註冊。我註冊了一個項目。我使用鏈環自動化來做某事。但是當我呼叫 checkuppkeep 函式進行測試時,映射返回 false 但函式在註冊後返回 true。基本上is_funding公共視圖函式返回true,但在契約_isFunding中
$$ projectId $$映射返回假。到底是怎麼回事?
_isFunding[projectId] = true;
function checkUpkeep( bytes memory /* checkData */ ) public view override returns ( bool upkeepNeeded, bytes memory /* performData */ ) { bool isFunded = _isFunding[projectId]; bool timePassed = (block.timestamp - (projectToTime[projectId][time[projectId]])) > time[projectId]; upkeepNeeded = (isFunded); } function getTimeleft(uint256 _projectID) public view returns (uint256 a, uint256 b) { a = block.timestamp - projectToTime[_projectID][time[_projectID]]; b = time[_projectID]; } function is_funding(uint256 _projectID) public view returns (bool) { return _isFunding[_projectID]; }
安全帽測試文件:
it("return false if project hasnt submit", async () => { blockNumber = await ethers.provider.getBlockNumber(); console.log(`Before ${blockNumber}`); await moveTime(s_fundingTime + 1); await moveBlocks(s_fundingTime + 1); blockNumber = await ethers.provider.getBlockNumber(); console.log(`Before ${blockNumber}`); const { upkeepNeeded } = await governor.checkUpkeep([]); const { a, b } = await governor.getTimeleft(projectId); console.log(await governor.is_funding(projectId)); console.log(a.toString(), b.toString()); console.log(upkeepNeeded); // assert(!upkeepNeeded);
測試結果控制台:
Before 226 Moving time... Moved forward 61 seconds.. Moving blocks... Before 287 true 121 60 false
很好,如果一個項目有一個時間戳,那麼為什麼不使用結構,而不是映射的映射。Struct ProjectsInfo {單位時間戳;book idfunded} Mapping (unit=>ProjectsInfo) projectToTime
此外,使用此解決方案,如果您想要優化(打包),最好減少時間戳的大小,如 uint248。
根據您發布的程式碼,您呼叫checkUpkeep 時不帶參數projectId。