You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In Community.sol L393-394, save _projectInstance.lenderFee() in a variable instead of calling it two times.
In the contract Community in the function lendToProject, defining two storage variables as CommunityStruct storage community = _communities[_communityID] and ProjectDetails storage project = community.projectDetails[_project] would save gas (limit the scope of some variables to avoid stack too deep). Ex:
function lendToProject(
uint256 _communityID,
address _project,
uint256 _lendingAmount,
bytes calldata _hash
)
external
virtual
override
nonReentrant
whenNotPaused
isPublishedToCommunity(_communityID, _project)
{
// Local instance of variable. For saving gas.
address _sender = _msgSender();
CommunityStruct storage community = _communities[_communityID];
// Revert if sender is not community owner.
// Only community owner can lend.
require(
_sender == community.owner,
"Community::!owner"
);
// Local instance of variable. For saving gas.
IProject _projectInstance = IProject(_project);
// Calculate lenderFee
uint256 _lenderFee = (_lendingAmount * _projectInstance.lenderFee()) /
(_projectInstance.lenderFee() + 1000);
// Calculate amount going to project. Lending amount - lending fee.
uint256 _amountToProject = _lendingAmount - _lenderFee;
ProjectDetails storage project = community.projectDetails[_project];
// Revert if _amountToProject is not within further investment needed.
require(
_amountToProject <= project.lendingNeeded - project.totalLent,
"Community::lending>needed"
);
{
// Local instance of variable. For saving gas.
IDebtToken _currency = community.currency;
IDebtToken _wrappedToken = IDebtToken(
homeFi.wrappedToken(address(_currency))
);
// Update investment in Project
_projectInstance.lendToProject(_amountToProject);
// Update total lent by lender
project.totalLent += _amountToProject;
// First claim interest if principal lent > 0
if (
project.lentAmount > 0
) {
claimInterest(_communityID, _project, _wrappedToken);
}
// Increment lent principal
project.lentAmount += _lendingAmount;
// Update lastTimestamp
project.lastTimestamp = block.timestamp;
// Transfer _lenderFee to HomeFi treasury from lender account
_currency.safeTransferFrom(_msgSender(), homeFi.treasury(), _lenderFee);
// Transfer _amountToProject to _project from lender account
_currency.safeTransferFrom(_msgSender(), _project, _amountToProject);
// Mint new _lendingAmount amount wrapped token to lender
_wrappedToken.mint(_sender, _lendingAmount);
}
emit LenderLent(_communityID, _project, _sender, _lendingAmount, _hash);
}
for loops can be optimized, the most optimized loop is:
for (uint256 i; i < length;) {
// contentunchecked {
++i;
}
}
and if the iteration is over an array list, store its length in a variable before the loop instead of computing it at each iteration (same for all other storage variables).
Unoptimized loops appear in Community.sol L624, in HomeFiProxy.sol L87, L136, in Project.sol L248, L311, L322, L368, L603, L650, L710, in Tasks.sol L181.
The text was updated successfully, but these errors were encountered:
Community.sol
L393-394, save_projectInstance.lenderFee()
in a variable instead of calling it two times.Community
in the functionlendToProject
, defining twostorage
variables asCommunityStruct storage community = _communities[_communityID]
andProjectDetails storage project = community.projectDetails[_project]
would save gas (limit the scope of some variables to avoid stack too deep). Ex:for
loops can be optimized, the most optimized loop is:and if the iteration is over an array list, store its length in a variable before the loop instead of computing it at each iteration (same for all other storage variables).
Unoptimized loops appear in
Community.sol
L624, inHomeFiProxy.sol
L87, L136, inProject.sol
L248, L311, L322, L368, L603, L650, L710, inTasks.sol
L181.The text was updated successfully, but these errors were encountered: