Skip to content

Commit

Permalink
fix: Optimize calls to TroveManager on trove open
Browse files Browse the repository at this point in the history
  • Loading branch information
bingen committed Apr 25, 2024
1 parent c267c34 commit 46a4a17
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
3 changes: 1 addition & 2 deletions contracts/src/BorrowerOperations.sol
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,11 @@ contract BorrowerOperations is LiquityBase, Ownable, CheckContract, IBorrowerOpe
);

// Set the stored Trove properties and mint the NFT
vars.stake = contractsCache.troveManager.setTrovePropertiesOnOpen(
(vars.stake, vars.arrayIndex) = contractsCache.troveManager.setTrovePropertiesOnOpen(
_owner, troveId, _ETHAmount, vars.compositeDebt, _annualInterestRate
);

sortedTroves.insert(troveId, _annualInterestRate, _upperHint, _lowerHint);
vars.arrayIndex = contractsCache.troveManager.addTroveIdToArray(troveId);
emit TroveCreated(_owner, troveId, vars.arrayIndex);

// Pull ETH tokens from sender and move them to the Active Pool
Expand Down
4 changes: 1 addition & 3 deletions contracts/src/Interfaces/ITroveManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ interface ITroveManager is IERC721, ILiquityBase {

function updateStakeAndTotalStakes(uint256 _troveId) external returns (uint256);

function addTroveIdToArray(uint256 _troveId) external returns (uint256 index);

function getPendingETHReward(uint256 _troveId) external view returns (uint256);

function getPendingBoldDebtReward(uint256 _troveId) external view returns (uint256);
Expand Down Expand Up @@ -105,7 +103,7 @@ interface ITroveManager is IERC721, ILiquityBase {
uint256 _coll,
uint256 _debt,
uint256 _annualInterestRate
) external returns (uint256);
) external returns (uint256, uint256);

function troveIsStale(uint256 _troveId) external view returns (bool);

Expand Down
8 changes: 5 additions & 3 deletions contracts/src/TroveManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1312,7 +1312,7 @@ contract TroveManager is ERC721, LiquityBase, Ownable, CheckContract, ITroveMana
}

// Push the trove's id to the Trove list, and record the corresponding array index on the Trove struct
function addTroveIdToArray(uint256 _troveId) external override returns (uint256) {
function _addTroveIdToArray(uint256 _troveId) internal returns (uint256) {
_requireCallerIsBorrowerOperations();

/* Max array size is 2**128 - 1, i.e. ~3e30 troves. No risk of overflow, since troves have minimum Bold
Expand Down Expand Up @@ -1571,7 +1571,7 @@ contract TroveManager is ERC721, LiquityBase, Ownable, CheckContract, ITroveMana
uint256 _coll,
uint256 _debt,
uint256 _annualInterestRate
) external returns (uint256) {
) external returns (uint256, uint256) {
_requireCallerIsBorrowerOperations();
// TODO: optimize gas for writing to this struct
Troves[_troveId].status = Status.active;
Expand All @@ -1584,8 +1584,10 @@ contract TroveManager is ERC721, LiquityBase, Ownable, CheckContract, ITroveMana
// mint ERC721
_mint(_owner, _troveId);

uint256 index = _addTroveIdToArray(_troveId);

// Record the Trove's stake (for redistributions) and update the total stakes
return _updateStakeAndTotalStakes(_troveId);
return (_updateStakeAndTotalStakes(_troveId), index);
}

function updateTroveDebtAndInterest(uint256 _troveId, uint256 _entireTroveDebt, uint256 _newAnnualInterestRate)
Expand Down

0 comments on commit 46a4a17

Please sign in to comment.