1

Hi I am pretty new to solidity. I am busy with a project that records all kinds of information about cattle.

In my code below my cattle struct has an array of CattleHealth and CattleGrowth which are suppose to hold records of inspections performed. Now in my RecordHealth and RecordGrowth functions I intended to record the information with a time stamp and add it as a single record to the corresponding array.

However, I realized I might have made a big mistake using an address for the record as this will overwrite the information each time an entry is made (Correct me if I am wrong). How then can I add separate records and keep it linked to the correct cow/bull?

contract WagyuRecordContract
{
    address farmer;

    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;
    }

    struct CattleHealth
    {
        uint DateRecorded;
        string BodyCondition;
        string HealthStatus;
        string Medication; 
    }

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

    struct CattleMovements
    {
        string From;
        string To;
    }

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

    modifier Farmer() 
    {
        require(msg.sender == farmer);
        _;
    }

    function addNewCattle(address rfid, string _name, uint _weight, string _gender, string _colour,
    string _breed, uint _age, uint _dob) Farmer public
    {
        cattle[rfid].Name = _name;
        cattle[rfid].Weight = _weight;
        cattle[rfid].Gender = _gender;
        cattle[rfid].Colour = _colour;
        cattle[rfid].Breed = _breed;
        cattle[rfid].Age = _age;
        cattle[rfid].DOB = _dob;
    }

    function NewCattleDetails(address rfid, bool _parent, string _location, string _sireName, string _damName, bool _active, bool _forSale) public Farmer
    {
        cattle[rfid].Parent = _parent;
        cattle[rfid].Location =_location;
        cattle[rfid].SireName = _sireName;
        cattle[rfid].DamName =_damName;
        cattle[rfid].Active = _active;
        cattle[rfid].ForSale = _forSale;
    }

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

    function RecordGrowth(address rfid, uint256 _foodIntake, uint256 _growth) Farmer public
    {
        growth[rfid].DateRecorded = now;
        growth[rfid].FoodIntake = _foodIntake;
        growth[rfid].Growth = _growth;
        cattle[rfid].GrowthRecord.push(growth[rfid]);
    }
   }
1

Yes, you are right you will keep only last growth and health record. I think you can remove those two lines:

mapping (address=> CattleHealth) public health;
mapping (address=> CattleGrowth) public growth;

Instead create a records in:

CattleHealth[] HealthRecord;
CattleGrowth[] GrowthRecord;

using push() method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.