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

Gas optimization #896

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions packages/contracts/src/dollar/core/CreditNft.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,15 @@ contract CreditNft is ERC1155Ubiquity, ICreditNft {

/// @notice Modifier checks that the method is called by a user with the "CreditNft manager" role
modifier onlyCreditNftManager() {
_onlyCreditNftManager();
_;
}

function _onlyCreditNftManager() internal view {
require(
accessControl.hasRole(CREDIT_NFT_MANAGER_ROLE, _msgSender()),
"Caller is not a CreditNft manager"
"CreditNft: not CreditNft manager"
);
_;
}

/// @notice Ensures initialize cannot be called on the implementation contract
Expand Down Expand Up @@ -120,7 +124,7 @@ contract CreditNft is ERC1155Ubiquity, ICreditNft {
* @dev Should be called prior to any state changing functions
*/
function updateTotalDebt() public {
bool reachedEndOfExpiredKeys = false;
bool reachedEndOfExpiredKeys=false;
uint256 currentBlockNumber = _sortedBlockNumbers.popFront();
uint256 outstandingDebt = _totalOutstandingDebt;
//if list is empty, currentBlockNumber will be 0
Expand Down
43 changes: 33 additions & 10 deletions packages/contracts/src/dollar/core/ERC1155Ubiquity.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,38 +39,54 @@ abstract contract ERC1155Ubiquity is

/// @notice Modifier checks that the method is called by a user with the "Governance minter" role
modifier onlyMinter() virtual {
_onlyMinter();
_;
}

function _onlyMinter() virtual internal view {
require(
accessControl.hasRole(GOVERNANCE_TOKEN_MINTER_ROLE, _msgSender()),
"ERC1155Ubiquity: not minter"
);
_;
}
}

/// @notice Modifier checks that the method is called by a user with the "Governance burner" role
modifier onlyBurner() virtual {
_onlyBurner();
_;
}

function _onlyBurner() virtual internal view {
require(
accessControl.hasRole(GOVERNANCE_TOKEN_BURNER_ROLE, _msgSender()),
"ERC1155Ubiquity: not burner"
);
_;
}

/// @notice Modifier checks that the method is called by a user with the "Pauser" role
modifier onlyPauser() virtual {
_onlyPauser();
_;
}

function _onlyPauser() virtual internal view {
require(
accessControl.hasRole(PAUSER_ROLE, _msgSender()),
"ERC1155Ubiquity: not pauser"
);
_;
}

/// @notice Modifier checks that the method is called by a user with the "Admin" role
modifier onlyAdmin() {
_onlyAdmin();
_;
}

function _onlyAdmin() internal view {
require(
accessControl.hasRole(DEFAULT_ADMIN_ROLE, _msgSender()),
"ERC20Ubiquity: not admin"
);
_;
}

/// @notice Ensures __ERC1155Ubiquity_init cannot be called on the implementation contract
Expand Down Expand Up @@ -140,7 +156,7 @@ abstract contract ERC1155Ubiquity is
bytes memory data
) public virtual onlyMinter {
_mint(to, id, amount, data);
totalSupply += amount;
totalSupply = totalSupply + amount;
holderBalances[to].add(id);
}

Expand All @@ -159,8 +175,12 @@ abstract contract ERC1155Ubiquity is
) public virtual onlyMinter whenNotPaused {
_mintBatch(to, ids, amounts, data);
uint256 localTotalSupply = totalSupply;
for (uint256 i = 0; i < ids.length; ++i) {
uint256 idsLength = ids.length;
for (uint256 i; i < idsLength;) {
localTotalSupply += amounts[i];
unchecked{
++i;
}
}
totalSupply = localTotalSupply;
holderBalances[to].add(ids);
Expand Down Expand Up @@ -249,7 +269,7 @@ abstract contract ERC1155Ubiquity is
uint256 amount
) internal virtual override whenNotPaused {
super._burn(account, id, amount);
totalSupply -= amount;
totalSupply = totalSupply - amount;
}

/**
Expand All @@ -267,8 +287,11 @@ abstract contract ERC1155Ubiquity is
uint256[] memory amounts
) internal virtual override whenNotPaused {
super._burnBatch(account, ids, amounts);
for (uint256 i = 0; i < ids.length; ++i) {
totalSupply -= amounts[i];
for (uint256 i; i < ids.length;) {
totalSupply = totalSupply - amounts[i];
unchecked {
++i;
}
}
}

Expand Down
12 changes: 10 additions & 2 deletions packages/contracts/src/dollar/core/ERC20Ubiquity.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,28 @@ abstract contract ERC20Ubiquity is

/// @notice Modifier checks that the method is called by a user with the "pauser" role
modifier onlyPauser() {
_onlyPauser();
_;
}

function _onlyPauser() internal view {
require(
accessControl.hasRole(PAUSER_ROLE, msg.sender),
"ERC20Ubiquity: not pauser"
);
_;
}

/// @notice Modifier checks that the method is called by a user with the "admin" role
modifier onlyAdmin() {
_onlyAdmin();
_;
}

function _onlyAdmin() internal view {
require(
accessControl.hasRole(DEFAULT_ADMIN_ROLE, msg.sender),
"ERC20Ubiquity: not admin"
);
_;
}

/// @notice Ensures __ERC20Ubiquity_init cannot be called on the implementation contract
Expand Down
22 changes: 17 additions & 5 deletions packages/contracts/src/dollar/core/StakingShare.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,41 @@ contract StakingShare is ERC1155Ubiquity, ERC1155URIStorageUpgradeable {

/// @notice Modifier checks that the method is called by a user with the "Staking share minter" role
modifier onlyMinter() override {
_onlyMinter();
_;
}

function _onlyMinter() internal override view {
require(
accessControl.hasRole(STAKING_SHARE_MINTER_ROLE, msg.sender),
"Staking Share: not minter"
);
_;
}

/// @notice Modifier checks that the method is called by a user with the "Staking share burner" role
modifier onlyBurner() override {
_onlyBurner();
_;
}

function _onlyBurner() internal override view {
require(
accessControl.hasRole(STAKING_SHARE_BURNER_ROLE, msg.sender),
"Staking Share: not burner"
);
_;
}

/// @notice Modifier checks that the method is called by a user with the "Pauser" role
modifier onlyPauser() override {
_onlyPauser();
_;
}

function _onlyPauser() internal override view {
require(
accessControl.hasRole(PAUSER_ROLE, msg.sender),
"Staking Share: not pauser"
);
_;
}

/// @notice Ensures initialize cannot be called on the implementation contract
Expand Down Expand Up @@ -124,7 +136,7 @@ contract StakingShare is ERC1155Ubiquity, ERC1155URIStorageUpgradeable {
) public virtual onlyMinter whenNotPaused returns (uint256 id) {
id = totalSupply + 1;
_mint(to, id, 1, bytes(""));
totalSupply += 1;
totalSupply = totalSupply + 1;
holderBalances[to].add(id);
Stake storage _stake = _stakes[id];
_stake.minter = to;
Expand Down Expand Up @@ -250,7 +262,7 @@ contract StakingShare is ERC1155Ubiquity, ERC1155URIStorageUpgradeable {
super._burn(account, id, 1);
Stake storage _stake = _stakes[id];
require(_stake.lpAmount == 0, "LP <> 0");
totalSupply -= 1;
totalSupply = totalSupply - 1;
}

/**
Expand Down
12 changes: 10 additions & 2 deletions packages/contracts/src/dollar/core/UbiquityCreditToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,28 @@ contract UbiquityCreditToken is ERC20Ubiquity {

/// @notice Modifier checks that the method is called by a user with the "Credit minter" role
modifier onlyCreditMinter() {
_onlyCreditMinter();
_;
}

function _onlyCreditMinter() internal view {
require(
accessControl.hasRole(CREDIT_TOKEN_MINTER_ROLE, _msgSender()),
"Credit token: not minter"
);
_;
}

/// @notice Modifier checks that the method is called by a user with the "Credit burner" role
modifier onlyCreditBurner() {
_onlyCreditBurner();
_;
}

function _onlyCreditBurner() internal view {
require(
accessControl.hasRole(CREDIT_TOKEN_BURNER_ROLE, _msgSender()),
"Credit token: not burner"
);
_;
}

/**
Expand Down
12 changes: 10 additions & 2 deletions packages/contracts/src/dollar/core/UbiquityDollarToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,28 @@ contract UbiquityDollarToken is ERC20Ubiquity {

/// @notice Modifier checks that the method is called by a user with the "Dollar minter" role
modifier onlyDollarMinter() {
_onlyDollarMinter();
_;
}

function _onlyDollarMinter() internal view {
require(
accessControl.hasRole(DOLLAR_TOKEN_MINTER_ROLE, _msgSender()),
"Dollar token: not minter"
);
_;
}

/// @notice Modifier checks that the method is called by a user with the "Dollar burner" role
modifier onlyDollarBurner() {
_onlyDollarBurner();
_;
}

function _onlyDollarBurner() internal view {
require(
accessControl.hasRole(DOLLAR_TOKEN_BURNER_ROLE, _msgSender()),
"Dollar token: not burner"
);
_;
}

/**
Expand Down
12 changes: 10 additions & 2 deletions packages/contracts/src/dollar/core/UbiquityGovernanceToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,28 @@ contract UbiquityGovernanceToken is ERC20Ubiquity {

/// @notice Modifier checks that the method is called by a user with the "Governance minter" role
modifier onlyGovernanceMinter() {
_onlyGovernanceMinter();
_;
}

function _onlyGovernanceMinter() internal view {
require(
accessControl.hasRole(GOVERNANCE_TOKEN_MINTER_ROLE, _msgSender()),
"Governance token: not minter"
);
_;
}

/// @notice Modifier checks that the method is called by a user with the "Governance burner" role
modifier onlyGovernanceBurner() {
_onlyGovernanceBurner();
_;
}

function _onlyGovernanceBurner() internal view {
require(
accessControl.hasRole(GOVERNANCE_TOKEN_BURNER_ROLE, _msgSender()),
"Governance token: not burner"
);
_;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/contracts/src/dollar/facets/DiamondLoupeFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ contract DiamondLoupeFacet is IDiamondLoupe, IERC165 {
LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage();
uint256 numFacets = ds.facetAddresses.length;
facets_ = new Facet[](numFacets);
for (uint256 i = 0; i < numFacets; ) {
for (uint256 i; i < numFacets; ) {
address facetAddress_ = ds.facetAddresses[i];
facets_[i].facetAddress = facetAddress_;
facets_[i].functionSelectors = ds
.facetFunctionSelectors[facetAddress_]
.functionSelectors;
unchecked {
i++;
++i;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/contracts/src/dollar/facets/OwnershipFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ contract OwnershipFacet is IERC173 {
function transferOwnership(address _newOwner) external override {
require(
(_newOwner != address(0)),
"OwnershipFacet: New owner cannot be the zero address"
"OwnershipFacet: Can't address(0)"
);
LibDiamond.enforceIsContractOwner();
LibDiamond.setContractOwner(_newOwner);
Expand Down
2 changes: 1 addition & 1 deletion packages/contracts/src/dollar/libraries/Constants.sol
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ bytes32 constant CREDIT_NFT_MANAGER_ROLE = keccak256("CREDIT_NFT_MANAGER_ROLE");
bytes32 constant STAKING_MANAGER_ROLE = keccak256("STAKING_MANAGER_ROLE");

/// @dev Role name for inventive manager
bytes32 constant INCENTIVE_MANAGER_ROLE = keccak256("INCENTIVE_MANAGER");
bytes32 constant INCENTIVE_MANAGER_ROLE = keccak256("INCENTIVE_MANAGER_ROLE");

/// @dev Role name for Governance token manager
bytes32 constant GOVERNANCE_TOKEN_MANAGER_ROLE = keccak256(
Expand Down
Loading
Loading