Skip to content

Commit d037886

Browse files
committed
Merge branch 'audit-fix/ceffu' into 'audit-fix/ceffuWithChargeGas'
Audit fix/ceffu See merge request orderlynetwork/orderly-v2/strategy-vault!20
2 parents deb902d + 2f3a8b2 commit d037886

1 file changed

Lines changed: 35 additions & 17 deletions

File tree

contracts/ProtocolVaultLedger.sol

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ contract ProtocolVaultLedger is Ownable2StepUpgradeable, UUPSUpgradeable, IProto
188188
for (uint256 i = 0; i < strategyFundAssets.length; i++) {
189189
bytes32 spId = strategyFundAssets[i].strategyProviderId;
190190
//gas optimization
191-
StrategyFundToken storage strategyFundToken = strategyFundTokenInfo[spId][USDC_HASH];
191+
StrategyFundToken storage strategyFundToken = _getStrategyFundToken(spId);
192192
PendingState storage pendingState = strategyFundToken.pendingState;
193193

194194
//reset performance fee
@@ -264,14 +264,14 @@ contract ProtocolVaultLedger is Ownable2StepUpgradeable, UUPSUpgradeable, IProto
264264

265265
if (operationType == OperationType.LP_WITHDRAW) {
266266
// Handle LP frozen shares removal
267-
AccountToken storage accountToken = accountTokenInfo[id][USDC_HASH];
267+
AccountToken storage accountToken = _getAccountToken(id);
268268
if (operationAmount > accountToken.frozenShares) {
269269
revert NotEnoughFrozenShare(operationAmount);
270270
}
271271
accountToken.frozenShares -= operationAmount;
272272
} else if (operationType == OperationType.SP_WITHDRAW) {
273273
// Handle SP frozen shares removal
274-
StrategyFundToken storage strategyFundToken = strategyFundTokenInfo[id][USDC_HASH];
274+
StrategyFundToken storage strategyFundToken = _getStrategyFundToken(id);
275275
if (operationAmount > strategyFundToken.frozenShares) {
276276
revert NotEnoughFrozenShare(operationAmount);
277277
}
@@ -373,7 +373,7 @@ contract ProtocolVaultLedger is Ownable2StepUpgradeable, UUPSUpgradeable, IProto
373373
uint256 withdrawAssets = pendingLpWithdrawAssets;
374374

375375
if (strategyProviderIds.length == 1) {
376-
strategyFundToken = strategyFundTokenInfo[strategyProviderIds[0]][USDC_HASH];
376+
strategyFundToken = _getStrategyFundToken(strategyProviderIds[0]);
377377

378378
//deposit
379379
uint256 distributeDepositShares = _convertToShares(
@@ -401,7 +401,7 @@ contract ProtocolVaultLedger is Ownable2StepUpgradeable, UUPSUpgradeable, IProto
401401
});
402402
} else if (strategyProviderIds.length > 1) {
403403
for (uint256 i = 0; i < strategyProviderIds.length; i++) {
404-
strategyFundToken = strategyFundTokenInfo[strategyProviderIds[i]][USDC_HASH];
404+
strategyFundToken = _getStrategyFundToken(strategyProviderIds[i]);
405405
allocateFundRes[i].strategyProviderId = strategyProviderIds[i];
406406
totalMainAssetsInFund += _convertToAssets(
407407
strategyFundToken.mainShares,
@@ -413,7 +413,7 @@ contract ProtocolVaultLedger is Ownable2StepUpgradeable, UUPSUpgradeable, IProto
413413
//allocate deposit
414414
if (depositAssets > 0) {
415415
for (uint256 i = 0; i < strategyProviderIds.length; i++) {
416-
strategyFundToken = strategyFundTokenInfo[strategyProviderIds[i]][USDC_HASH];
416+
strategyFundToken = _getStrategyFundToken(strategyProviderIds[i]);
417417

418418
uint256 mainAssetsInFund = _convertToAssets(
419419
strategyFundToken.mainShares,
@@ -442,7 +442,7 @@ contract ProtocolVaultLedger is Ownable2StepUpgradeable, UUPSUpgradeable, IProto
442442
//allocate withdraw
443443
if (withdrawAssets > 0) {
444444
for (uint256 i = 0; i < strategyProviderIds.length; i++) {
445-
strategyFundToken = strategyFundTokenInfo[strategyProviderIds[i]][USDC_HASH];
445+
strategyFundToken = _getStrategyFundToken(strategyProviderIds[i]);
446446

447447
uint256 mainAssetsInFund = _convertToAssets(
448448
strategyFundToken.mainShares,
@@ -491,7 +491,7 @@ contract ProtocolVaultLedger is Ownable2StepUpgradeable, UUPSUpgradeable, IProto
491491

492492
//settle strategy fund
493493
for (uint256 i = 0; i < strategyProviderIds.length; i++) {
494-
StrategyFundToken storage strategyFundToken = strategyFundTokenInfo[strategyProviderIds[i]][USDC_HASH];
494+
StrategyFundToken storage strategyFundToken = _getStrategyFundToken(strategyProviderIds[i]);
495495

496496
//hwm must be updated firstly
497497
strategyFundToken.hwm = _calculateHWM(strategyProviderIds[i]);
@@ -529,7 +529,7 @@ contract ProtocolVaultLedger is Ownable2StepUpgradeable, UUPSUpgradeable, IProto
529529

530530
AccountState[] memory accountStates = new AccountState[](accountIds.length);
531531
for (uint256 i = 0; i < accountIds.length; i++) {
532-
AccountToken storage accountToken = accountTokenInfo[accountIds[i]][USDC_HASH];
532+
AccountToken storage accountToken = _getAccountToken(accountIds[i]);
533533
accountToken.shares = accountToken.pendingShares;
534534

535535
//for event
@@ -703,7 +703,7 @@ contract ProtocolVaultLedger is Ownable2StepUpgradeable, UUPSUpgradeable, IProto
703703
StrategyFundState[] memory pendingStrategyFundStates = new StrategyFundState[](strategyProviderIds.length);
704704

705705
for (uint256 i = 0; i < strategyProviderIds.length; i++) {
706-
StrategyFundToken storage strategyFundToken = strategyFundTokenInfo[strategyProviderIds[i]][USDC_HASH];
706+
StrategyFundToken storage strategyFundToken = _getStrategyFundToken(strategyProviderIds[i]);
707707

708708
uint256 hwm = _calculateHWM(strategyProviderIds[i]);
709709
pendingStrategyFundStates[i] = StrategyFundState({
@@ -727,7 +727,7 @@ contract ProtocolVaultLedger is Ownable2StepUpgradeable, UUPSUpgradeable, IProto
727727
_check(periodId);
728728
AccountState[] memory pendingAccountStates = new AccountState[](accountIds.length);
729729
for (uint256 i = 0; i < accountIds.length; i++) {
730-
AccountToken storage accountToken = accountTokenInfo[accountIds[i]][USDC_HASH];
730+
AccountToken storage accountToken = _getAccountToken(accountIds[i]);
731731
pendingAccountStates[i] = AccountState({accountId: accountIds[i], shares: accountToken.pendingShares});
732732
}
733733
return pendingAccountStates;
@@ -750,7 +750,7 @@ contract ProtocolVaultLedger is Ownable2StepUpgradeable, UUPSUpgradeable, IProto
750750
}
751751

752752
function getStrategyFund(bytes32 spId) public view returns (StrategyFundToken memory) {
753-
return strategyFundTokenInfo[spId][USDC_HASH];
753+
return _getStrategyFundToken(spId);
754754
}
755755

756756
/// @notice Estimate cross-chain native fee for claim operations
@@ -804,7 +804,7 @@ contract ProtocolVaultLedger is Ownable2StepUpgradeable, UUPSUpgradeable, IProto
804804
}
805805

806806
function _handleLpDeposit(bytes32 accountId, uint256 amount) internal returns (uint256) {
807-
AccountToken storage accountToken = accountTokenInfo[accountId][USDC_HASH];
807+
AccountToken storage accountToken = _getAccountToken(accountId);
808808

809809
if (amount > accountToken.unAllocatedAssets) {
810810
revert NotEnoughLPDeposit(amount);
@@ -822,7 +822,7 @@ contract ProtocolVaultLedger is Ownable2StepUpgradeable, UUPSUpgradeable, IProto
822822
}
823823

824824
function _handleLpWithdraw(bytes32 requestId, bytes32 accountId, uint256 amount) internal returns (uint256) {
825-
AccountToken storage accountToken = accountTokenInfo[accountId][USDC_HASH];
825+
AccountToken storage accountToken = _getAccountToken(accountId);
826826

827827
if (amount > accountToken.frozenShares) {
828828
revert NotEnoughFrozenShare(amount);
@@ -845,7 +845,7 @@ contract ProtocolVaultLedger is Ownable2StepUpgradeable, UUPSUpgradeable, IProto
845845

846846
function _handleSPDeposit(bytes32 strategyProviderId, uint256 amount) internal returns (uint256) {
847847
//gas optimization
848-
StrategyFundToken storage strategyFundToken = strategyFundTokenInfo[strategyProviderId][USDC_HASH];
848+
StrategyFundToken storage strategyFundToken = _getStrategyFundToken(strategyProviderId);
849849
PendingState storage pendingState = strategyFundToken.pendingState;
850850
if (amount > strategyFundToken.unAllocatedAssets) {
851851
revert NotEnoughSPDeposit();
@@ -867,7 +867,7 @@ contract ProtocolVaultLedger is Ownable2StepUpgradeable, UUPSUpgradeable, IProto
867867
returns (uint256)
868868
{
869869
//gas optimization
870-
StrategyFundToken storage strategyFundToken = strategyFundTokenInfo[strategyProviderId][USDC_HASH];
870+
StrategyFundToken storage strategyFundToken = _getStrategyFundToken(strategyProviderId);
871871
PendingState storage pendingState = strategyFundToken.pendingState;
872872

873873
if (amount > strategyFundToken.frozenShares) {
@@ -891,7 +891,7 @@ contract ProtocolVaultLedger is Ownable2StepUpgradeable, UUPSUpgradeable, IProto
891891
}
892892

893893
function _calculateHWM(bytes32 strategyProviderId) internal view returns (uint256) {
894-
StrategyFundToken storage strategyFundToken = strategyFundTokenInfo[strategyProviderId][USDC_HASH];
894+
StrategyFundToken storage strategyFundToken = _getStrategyFundToken(strategyProviderId);
895895

896896
uint256 hwm = strategyFundToken.hwm;
897897
uint256 totalShares = strategyFundToken.totalShares;
@@ -968,4 +968,22 @@ contract ProtocolVaultLedger is Ownable2StepUpgradeable, UUPSUpgradeable, IProto
968968
? shares.mulDiv(10 ** decimal, 10 ** decimal, rounding)
969969
: shares.mulDiv(_totalAssets, _totalShares, rounding);
970970
}
971+
972+
/*=========================================================================================
973+
* STORAGE ACCESS HELPERS
974+
*=========================================================================================*/
975+
976+
/// @notice Get strategy fund token storage reference (reduces storage access repetition)
977+
/// @param spId strategy provider ID
978+
/// @return strategyFundToken storage reference to strategy fund token
979+
function _getStrategyFundToken(bytes32 spId) internal view returns (StrategyFundToken storage strategyFundToken) {
980+
return strategyFundTokenInfo[spId][USDC_HASH];
981+
}
982+
983+
/// @notice Get account token storage reference (reduces storage access repetition)
984+
/// @param accountId account ID
985+
/// @return accountToken storage reference to account token
986+
function _getAccountToken(bytes32 accountId) internal view returns (AccountToken storage accountToken) {
987+
return accountTokenInfo[accountId][USDC_HASH];
988+
}
971989
}

0 commit comments

Comments
 (0)