Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Check to CeloDistributionSchedule activate function #11109

Merged
arthurgousset marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,14 @@ contract CeloDistributionSchedule is UsingRegistry, ReentrancyGuard, Initializab
require(address(this).balance > 0, "Contract does not have CELO balance.");
require(!areDependenciesSet, "Contract has already been activated.");
require(block.timestamp > _l2StartTime, "L2 start time cannot be set to a future date.");
ICeloToken celoToken = ICeloToken(address(getCeloToken()));
require(
registry.getAddressForOrDie(CELO_DISTRIBUTION_SCHEDULE_ID) == address(this),
"CeloDistributionSchedule address is incorrectly set in Registry."
);
soloseng marked this conversation as resolved.
Show resolved Hide resolved
areDependenciesSet = true;
l2StartTime = _l2StartTime;
communityRewardFund = address(getGovernance());
ICeloToken celoToken = ICeloToken(address(getCeloToken()));
totalAllocatedAtL2Start = celoToken.allocatedSupply();
setCommunityRewardFraction(_communityRewardFraction);
setCarbonOffsettingFund(_carbonOffsettingPartner, _carbonOffsettingFraction);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,6 @@ interface ICeloToken is IERC20 {
*/
function setRegistry(address registryAddress) external;

/**
* @notice Used set the address of the CeloDistributionSchedule contract.
* @param celoTokenDistributionScheduleAddress The address of the CeloDistributionSchedule contract.
*/
function setCeloTokenDistributionScheduleAddress(
address celoTokenDistributionScheduleAddress
) external;

/**
* @dev Mints a new token.
* @param to The address that will own the minted token.
Expand Down
31 changes: 5 additions & 26 deletions packages/protocol/contracts/common/GoldToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,12 @@ contract GoldToken is
// Burn address is 0xdEaD because truffle is having buggy behaviour with the zero address
address constant BURN_ADDRESS = address(0x000000000000000000000000000000000000dEaD);

ICeloDistributionSchedule public celoTokenDistributionSchedule;

event Transfer(address indexed from, address indexed to, uint256 value);

event TransferComment(string comment);

event Approval(address indexed owner, address indexed spender, uint256 value);

event SetCeloTokenDistributionScheduleAddress(address indexed newScheduleAddress);

/**
* @notice Sets initialized == true on implementation contracts
* @param test Set to true to skip implementation initialization
Expand All @@ -66,23 +62,6 @@ contract GoldToken is
setRegistry(registryAddress);
}

/**
* @notice Used set the address of the CeloDistributionSchedule contract.
* @param celoTokenDistributionScheduleAddress The address of the CeloDistributionSchedule contract.
*/
function setCeloTokenDistributionScheduleAddress(
address celoTokenDistributionScheduleAddress
) external onlyOwner {
require(
celoTokenDistributionScheduleAddress != address(0) ||
celoTokenDistributionScheduleAddress != address(celoTokenDistributionSchedule),
"Invalid address."
);
celoTokenDistributionSchedule = ICeloDistributionSchedule(celoTokenDistributionScheduleAddress);

emit SetCeloTokenDistributionScheduleAddress(celoTokenDistributionScheduleAddress);
}

/**
* @notice Transfers CELO from one address to another.
* @param to The address to transfer CELO to.
Expand Down Expand Up @@ -174,8 +153,8 @@ contract GoldToken is
function transferFrom(address from, address to, uint256 value) external returns (bool) {
require(to != address(0), "transfer attempted to reserved address 0x0");
require(
to != address(celoTokenDistributionSchedule),
"transfer attempted to reserved celoTokenDistributionSchedule address"
to != registry.getAddressForOrDie(CELO_DISTRIBUTION_SCHEDULE_ID),
soloseng marked this conversation as resolved.
Show resolved Hide resolved
"transfer attempted to reserved CeloDistributionSchedule address"
);
require(value <= balanceOf(from), "transfer value exceeded balance of sender");
require(
Expand Down Expand Up @@ -249,7 +228,7 @@ contract GoldToken is
* @return The total amount of allocated CELO.
*/
function allocatedSupply() external view onlyL2 returns (uint256) {
return CELO_SUPPLY_CAP - address(celoTokenDistributionSchedule).balance;
return CELO_SUPPLY_CAP - registry.getAddressForOrDie(CELO_DISTRIBUTION_SCHEDULE_ID).balance;
}

/**
Expand Down Expand Up @@ -316,8 +295,8 @@ contract GoldToken is
*/
function _transfer(address to, uint256 value) internal returns (bool) {
require(
to != address(celoTokenDistributionSchedule),
"transfer attempted to reserved celoTokenDistributionSchedule address"
to != registry.getAddressForOrDie(CELO_DISTRIBUTION_SCHEDULE_ID),
"transfer attempted to reserved CeloDistributionSchedule address"
);
require(value <= balanceOf(msg.sender), "transfer value exceeded balance of sender");

Expand Down
7 changes: 7 additions & 0 deletions packages/protocol/contracts/common/UsingRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import "./interfaces/IAccounts.sol";
import "./interfaces/IFeeCurrencyWhitelist.sol";
import "./interfaces/IFreezer.sol";
import "./interfaces/IRegistry.sol";
import "./interfaces/ICeloDistributionSchedule.sol";

import "../governance/interfaces/IElection.sol";
import "../governance/interfaces/IGovernance.sol";
Expand Down Expand Up @@ -48,6 +49,8 @@ contract UsingRegistry is Ownable {

bytes32 constant CELO_TOKEN_REGISTRY_ID = keccak256(abi.encodePacked("CeloToken"));
bytes32 constant LOCKED_CELO_REGISTRY_ID = keccak256(abi.encodePacked("LockedCelo"));
bytes32 constant CELO_DISTRIBUTION_SCHEDULE_ID =
keccak256(abi.encodePacked("CeloDistributionSchedule"));
// solhint-enable state-visibility

IRegistry public registry;
Expand Down Expand Up @@ -135,4 +138,8 @@ contract UsingRegistry is Ownable {
function getValidators() internal view returns (IValidators) {
return IValidators(registry.getAddressForOrDie(VALIDATORS_REGISTRY_ID));
}

function getCeloDistributionSchedule() internal view returns (ICeloDistributionSchedule) {
return ICeloDistributionSchedule(registry.getAddressForOrDie(CELO_DISTRIBUTION_SCHEDULE_ID));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,8 @@ contract CeloDistributionScheduleTest is Test, Constants, IsL2Check {
setUpL1();

// Setup L2 after minting L1 supply.
registryAddress = proxyAdminAddress;
deployCodeTo("Registry.sol", abi.encode(false), registryAddress);
registry = IRegistry(registryAddress);

registry.setAddressFor("CeloToken", address(celoToken));
registry.setAddressFor("Governance", address(governance));

celoToken.setRegistry(registryAddress);
deployCodeTo("Registry.sol", abi.encode(false), proxyAdminAddress);
}

function setUpL1() public {
Expand All @@ -87,7 +81,7 @@ contract CeloDistributionScheduleTest is Test, Constants, IsL2Check {

deployCodeTo("GoldToken.sol", abi.encode(false), celoTokenAddress);
celoToken = ICeloToken(celoTokenAddress);

celoToken.setRegistry(registryAddress);
// Using a mock contract, as foundry does not allow for library linking when using deployCodeTo
governance = new MockGovernance();

Expand All @@ -107,11 +101,10 @@ contract CeloDistributionScheduleTest is Test, Constants, IsL2Check {
vm.warp(block.timestamp + l2StartTime);
vm.prank(celoDistributionOwner);
celoDistributionSchedule = new CeloDistributionSchedule(true);
registry.setAddressFor("CeloDistributionSchedule", address(celoDistributionSchedule));

vm.deal(address(celoDistributionSchedule), L2_INITIAL_STASH_BALANCE);

celoToken.setCeloTokenDistributionScheduleAddress(address(celoDistributionSchedule));

vm.prank(celoDistributionOwner);
celoDistributionSchedule.initialize(registryAddress);

Expand All @@ -133,19 +126,17 @@ contract CeloDistributionScheduleTest_initialize is CeloDistributionScheduleTest

vm.prank(celoDistributionOwner);
celoDistributionSchedule = new CeloDistributionSchedule(true);

celoToken.setCeloTokenDistributionScheduleAddress(address(celoDistributionSchedule));

registry.setAddressFor("CeloDistributionSchedule", address(celoDistributionSchedule));
vm.prank(celoDistributionOwner);
celoDistributionSchedule.initialize(registryAddress);
}

function test_ShouldSetAOwnerToCeloDistributionScheduleInstance() public {
function test_ShouldSetAnOwnerToCeloDistributionScheduleInstance() public {
assertEq(celoDistributionSchedule.owner(), celoDistributionOwner);
}

function test_ShouldSetRegistryAddressToCeloDistributionScheduleInstance() public {
assertEq(address(celoDistributionSchedule.registry()), proxyAdminAddress);
assertEq(address(celoDistributionSchedule.registry()), l1RegistryAddress);
}

function test_ShouldNotSetBeneficiariesToCeloDistributionScheduleInstance() public {
Expand All @@ -163,7 +154,7 @@ contract CeloDistributionScheduleTest_initialize is CeloDistributionScheduleTest

function test_Reverts_WhenRegistryIsTheNullAddress() public {
celoDistributionSchedule = new CeloDistributionSchedule(true);

registry.setAddressFor("CeloDistributionSchedule", address(celoDistributionSchedule));
vm.expectRevert("Cannot register the null address");
celoDistributionSchedule.initialize(address(0));
}
Expand All @@ -187,6 +178,7 @@ contract CeloDistributionScheduleTest_activate_L1 is CeloDistributionScheduleTes
super.setUpL1();

celoDistributionSchedule = new CeloDistributionSchedule(true);
registry.setAddressFor("CeloDistributionSchedule", address(celoDistributionSchedule));
celoDistributionSchedule.initialize(registryAddress);
}

Expand Down Expand Up @@ -221,8 +213,10 @@ contract CeloDistributionScheduleTest_activate is CeloDistributionScheduleTest {
function test_Reverts_WhenCommunityFractionIsZero() public {
vm.warp(block.timestamp + l2StartTime);
celoDistributionSchedule = new CeloDistributionSchedule(true);
registry.setAddressFor("CeloDistributionSchedule", address(celoDistributionSchedule));
celoDistributionSchedule.initialize(registryAddress);
vm.deal(address(celoDistributionSchedule), L2_INITIAL_STASH_BALANCE);

vm.expectRevert(
"Value must be different from existing community reward fraction and less than 1."
);
Expand All @@ -237,6 +231,7 @@ contract CeloDistributionScheduleTest_activate is CeloDistributionScheduleTest {
function test_Reverts_WhenCarbonOffsettingPartnerIsNullAddress() public {
vm.warp(block.timestamp + l2StartTime);
celoDistributionSchedule = new CeloDistributionSchedule(true);
registry.setAddressFor("CeloDistributionSchedule", address(celoDistributionSchedule));
celoDistributionSchedule.initialize(registryAddress);
vm.deal(address(celoDistributionSchedule), L2_INITIAL_STASH_BALANCE);

Expand All @@ -253,6 +248,7 @@ contract CeloDistributionScheduleTest_activate is CeloDistributionScheduleTest {
vm.warp(block.timestamp + l2StartTime);
registry.setAddressFor("Governance", address(0));
celoDistributionSchedule = new CeloDistributionSchedule(true);
registry.setAddressFor("CeloDistributionSchedule", address(celoDistributionSchedule));
vm.deal(address(celoDistributionSchedule), L2_INITIAL_STASH_BALANCE);
celoDistributionSchedule.initialize(registryAddress);

Expand Down Expand Up @@ -283,11 +279,9 @@ contract CeloDistributionScheduleTest_activate is CeloDistributionScheduleTest {
vm.warp(block.timestamp + l2StartTime);
vm.prank(celoDistributionOwner);
celoDistributionSchedule = new CeloDistributionSchedule(true);

registry.setAddressFor("CeloDistributionSchedule", address(celoDistributionSchedule));
vm.deal(address(celoDistributionSchedule), L2_INITIAL_STASH_BALANCE);

celoToken.setCeloTokenDistributionScheduleAddress(address(celoDistributionSchedule));

vm.prank(celoDistributionOwner);
celoDistributionSchedule.initialize(registryAddress);

Expand All @@ -302,6 +296,50 @@ contract CeloDistributionScheduleTest_activate is CeloDistributionScheduleTest {
carbonOffsettingFraction
);
}

function test_Reverts_WhenCeloDistributionAddressNotSetInRegistry() public {
vm.warp(block.timestamp + l2StartTime);
vm.prank(celoDistributionOwner);
celoDistributionSchedule = new CeloDistributionSchedule(true);

vm.deal(address(celoDistributionSchedule), L2_INITIAL_STASH_BALANCE);

vm.prank(celoDistributionOwner);
celoDistributionSchedule.initialize(registryAddress);

vm.expectRevert("identifier has no registry entry");

vm.prank(celoDistributionOwner);

celoDistributionSchedule.activate(
l2StartTime,
communityRewardFraction,
carbonOffsettingPartner,
carbonOffsettingFraction
);
}

function test_Reverts_WhenCeloDistributionAddressIncorrectlySetInRegistry() public {
vm.warp(block.timestamp + l2StartTime);
vm.prank(celoDistributionOwner);
celoDistributionSchedule = new CeloDistributionSchedule(true);
registry.setAddressFor("CeloDistributionSchedule", randomAddress);
vm.deal(address(celoDistributionSchedule), L2_INITIAL_STASH_BALANCE);

vm.prank(celoDistributionOwner);
celoDistributionSchedule.initialize(registryAddress);

vm.expectRevert("CeloDistributionSchedule address is incorrectly set in Registry.");

vm.prank(celoDistributionOwner);

celoDistributionSchedule.activate(
l2StartTime,
communityRewardFraction,
carbonOffsettingPartner,
carbonOffsettingFraction
);
}
}

contract CeloDistributionScheduleTest_setCommunityRewardFraction is CeloDistributionScheduleTest {
Expand Down Expand Up @@ -340,9 +378,7 @@ contract CeloDistributionScheduleTest_setCommunityRewardFraction is CeloDistribu
function test_Reverts_WhenDependenciesNotSet() public {
vm.prank(celoDistributionOwner);
celoDistributionSchedule = new CeloDistributionSchedule(true);

celoToken.setCeloTokenDistributionScheduleAddress(address(celoDistributionSchedule));

registry.setAddressFor("CeloDistributionSchedule", address(celoDistributionSchedule));
vm.prank(celoDistributionOwner);
celoDistributionSchedule.initialize(registryAddress);

Expand Down Expand Up @@ -424,9 +460,7 @@ contract CeloDistributionScheduleTest_setCarbonOffsettingFund is CeloDistributio
function test_Reverts_WhenDependenciesNotSet() public {
vm.prank(celoDistributionOwner);
celoDistributionSchedule = new CeloDistributionSchedule(true);

celoToken.setCeloTokenDistributionScheduleAddress(address(celoDistributionSchedule));

registry.setAddressFor("CeloDistributionSchedule", address(celoDistributionSchedule));
vm.prank(celoDistributionOwner);
celoDistributionSchedule.initialize(registryAddress);

Expand Down Expand Up @@ -468,6 +502,7 @@ contract CeloDistributionScheduleTest_distributeAccordingToSchedule_L1 is
super.setUpL1();

celoDistributionSchedule = new CeloDistributionSchedule(true);
registry.setAddressFor("CeloDistributionSchedule", address(celoDistributionSchedule));
celoDistributionSchedule.initialize(registryAddress);
}

Expand All @@ -491,7 +526,7 @@ contract CeloDistributionScheduleTest_distributeAccordingToSchedule is

function test_Reverts_WhenDependenciesAreNotSet() public {
celoDistributionSchedule = new CeloDistributionSchedule(true);

registry.setAddressFor("CeloDistributionSchedule", address(celoDistributionSchedule));
celoDistributionSchedule.initialize(registryAddress);

vm.expectRevert("Distribution schedule has not been activated.");
Expand Down Expand Up @@ -703,7 +738,7 @@ contract CeloDistributionScheduleTest_getDistributableAmount is CeloDistribution

function test_Reverts_WhenDependenciesNotSet() public {
celoDistributionSchedule = new CeloDistributionSchedule(true);

registry.setAddressFor("CeloDistributionSchedule", address(celoDistributionSchedule));
celoDistributionSchedule.initialize(registryAddress);

vm.expectRevert("Distribution schedule has not been activated.");
Expand Down
2 changes: 2 additions & 0 deletions packages/protocol/test-sol/unit/common/FeeHandler.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ contract FeeHandlerTest is Test, Constants {
address registryAddress = 0x000000000000000000000000000000000000ce10;
address owner = address(this);
address user = actor("user");
address celoDistributionSchedule = actor("CeloDistributionSchedule");

uint256 celoAmountForRate = 1e24;
uint256 stableAmountForRate = 2 * celoAmountForRate;
Expand Down Expand Up @@ -102,6 +103,7 @@ contract FeeHandlerTest is Test, Constants {
registry.setAddressFor("GoldToken", address(celoToken));
registry.setAddressFor("CeloToken", address(celoToken));
registry.setAddressFor("Reserve", address(mockReserve));
registry.setAddressFor("CeloDistributionSchedule", celoDistributionSchedule);

mockReserve.setGoldToken(address(celoToken));
mockReserve.addToken(address(stableToken));
Expand Down
Loading
Loading