Solidity

我可以這樣創建一個新地址嗎?

  • September 16, 2018

我正忙於一個項目。所以我在另一個結構中有一個結構數組。在這種情況下,例如 Cattle 中的 CattleHealth 數組。在我的 RecordHealth 函式中,我獲取 Cattle 結構的地址並添加其目前列表的目前長度以創建一個唯一地址。這是正確的方法嗎?

 contract WagyuRecordContract
   {
       address owner;

       struct Cattle
       {
           address RFID;
           string Name;
           uint256 Weight;
           string Gender;
           string Colour;
           string Breed;
           uint Age; 
           uint DOB;
           string Location;
           bool Parent;
           string SireName;
           string DamName;
           bool Active;
           bool ForSale;
           CattleHealth[] HealthRecord;
           CattleGrowth[] GrowthRecord;
           CattleMovements[] MovementsRecord;
           Facility SlaughterDetails;
           Meat[] DistributionDetails
       }


       struct CattleGrowth
       {
           uint DateRecorded;
           uint256 FoodIntake;
           uint256 Growth;
       }


       mapping (address => Cattle) public cattle;
       mapping (address=> CattleGrowth) public growth;           

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


       function RecordHealth(address rfid, string _bodyCond, string _healthStat, uint256, string _med) Owner public
       {
           health[rfid+cattle[rfid].HealthRecord.length].DateRecorded = now;
           health[rfid+cattle[rfid].HealthRecord.length].BodyCondition = _bodyCond;
           health[rfid+cattle[rfid].HealthRecord.length].HealthStatus = _healthStat;
           health[rfid+cattle[rfid].HealthRecord.length].Medication = _med;
           cattle[rfid].HealthRecord.push(health[rfid+cattle[rfid].HealthRecord.length]);
       }
}

編輯:我的好友在下面使用了這種方法,沒有使用映射。我不明白這是如何工作的。-1 有什麼用?它對牛有何獨特之處?

  function RecordHealth(address rfid, string _bodyCond, string _healthStat, string _med) Owner public
   {
      cattle[rfid].HealthRecord.push(now, _bodyCond, _healthStat, _med)-1;
   }

消極的。雖然在 C 或類似語言中,指針算術是正正常則,但在 Solidity 中,不能保證最終打包和最終循環錯位都有效。所以強烈建議不要這樣做。

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