Solc

在合約中使用共享庫類型

  • April 24, 2018

我想DateRange在兩個契約中重用類型。但是,我因多個錯誤而失敗。目前我有一個錯誤:

G21FE98E9 Member "endTicks" not found or not visible after argument-dependent lookup in tuple(uint64,uint64)

我沒有找到任何可以幫助解決此問題的相關資訊。

這是完整的合約程式碼:

pragma solidity ^0.4.21;

contract SeasonFactory {    
   address public owner;
   address[] public seasons;

   event SeasonCreated(uint64 indexed beginTicks, uint64 indexed endTicks, address season);

   function SeasonFactory() public {
       owner = msg.sender;
   }

   function newSeason(uint64 beginTicks, uint64 endTicks) public restricted {
       require(beginTicks < endTicks);
       require(seasons.length == 0 || Season(seasons[seasons.length - 1]).period().endTicks() < beginTicks);

       Season season = new Season(owner, beginTicks, endTicks);
       seasons.push(season);
       emit SeasonCreated(beginTicks, endTicks, season);
   }

   function getSeasonsCount() public view returns(uint) {
       return seasons.length;
   }

   function getSeasonForDate(uint64 ticks) public view returns(address) {
       for (uint i = seasons.length - 1; i >= 0; i--) {
           Season season = Season(seasons[i]);
           if (ticks >= season.period.beginTicks() && ticks <= season.period.endTicks())
               return season;
       }
       return 0;
   }

   modifier restricted {
       require(owner == msg.sender);
       _;
   }
}

contract Season {
   address public owner;
   SharedTypes.DateRange public period;

   function Season(address owner_, uint64 beginTicks, uint64 endTicks) public {
       owner = owner_;
       period = SharedTypes.DateRange(beginTicks, endTicks);
   }
}

library SharedTypes {
   struct DateRange {    
       uint64 beginTicks;
       uint64 endTicks;
   }
}

這裡有什麼問題?

根據這篇文章,合約不能直接訪問另一個合約的儲存。

您可能需要實現一個 getter 函式,如本文所述。

我嘗試在您的季節契約中使用以下功能,並且效果很好:

function getTicks(uint ind) view public returns( uint64 ){
   if(ind == 1) return period.endTicks;
   else return period.beginTicks;
}

引用自:https://ethereum.stackexchange.com/questions/46551