Solidity

如何通過數組參數創建不同的結構

  • August 26, 2020

我試圖通過參數中的數組傳遞結構的必要組件來創建不同的結構。到目前為止,編譯器沒有抱怨,但是當我執行事務時,它沒有顯示結構已經創建。這就是程式碼的外觀。通過在參數中傳遞結構的必要組件,我可以通過一次呼叫創建不同的結構嗎?

struct Schedule {
       uint releaseTime;
       bool signed;
   }
   
   struct Beneficiary {
       bool isAuthorized;
       uint percentage;
       uint received;
   }
   
   mapping(address => Beneficiary) private _beneficiaries;
   
   mapping(address => uint) internal rewards;
   
   address[] internal _beneficiariesList;
   
   mapping(uint => Schedule) private _schedules;
   Schedule[] internal _schedulesList;

constructor(address[] memory beneficiaries, uint[] memory _percentages, uint[] memory schedules) public {
       require(beneficiaries.length == _percentages.length, 'Beneficiary and percentage arrays must have the same length');
       for(uint i = 0; i < _beneficiariesList.length; i++) {
           _beneficiariesList.push(beneficiaries[i]);
           _beneficiaries[_beneficiariesList[i]].isAuthorized = true;
           _beneficiaries[_beneficiariesList[i]].percentage = _percentages[i];
           uint _totalPercentage = 0;
           _totalPercentage = _totalPercentage.add(_beneficiaries[_beneficiariesList[i]].percentage);
           require(_totalPercentage == 100, 'Percentages must sum up to 100');
       }
       for(uint s = 0; s < _schedulesList.length; s++) {
           _schedulesList[s].releaseTime = now + schedules[s];
           _schedulesList.push(_schedulesList[s]);
       }
   }

在第二個 for 循環的第一行,您的意思是填充_schedules映射而不是_schedulesList數組嗎?如果沒有,那麼改變

           _schedulesList[s].releaseTime = now + schedules[s];

           _schedules[s].releaseTime = now + schedules[s];

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