Skip to content

Commit

Permalink
fix: gas optimized totalValueMinted tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
scolear committed Nov 18, 2024
1 parent b0c0d98 commit ca48fda
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions contracts/DLCManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ contract DLCManager is
bool public porEnabled;
AggregatorV3Interface public dlcBTCPoRFeed;
mapping(address => mapping(bytes32 => bool)) private _seenSigners;
uint256[39] __gap;
uint256 private _totalValueMinted;
uint256[38] __gap;

////////////////////////////////////////////////////////////////
// ERRORS //
Expand Down Expand Up @@ -154,6 +155,20 @@ contract DLCManager is
btcRedeemFeeRate = 15; // 0.15% BTC fee for now
btcFeeRecipient = btcFeeRecipientToSet;
porEnabled = false;
_totalValueMinted = 0;
}

/**
* @notice Initialize total minted value tracking
*/
function initializeV2() public reinitializer(2) {
// Calculate initial total by iterating through existing vaults
uint256 total = 0;
for (uint256 i = 0; i < _index; i++) {
total += dlcs[i].valueMinted;
}

_totalValueMinted = total;
}

/// @custom:oz-upgrades-unsafe-allow constructor
Expand Down Expand Up @@ -400,17 +415,15 @@ contract DLCManager is
revert DepositTooSmall(amountToLockDiff, minimumDeposit);
}

// We fetch the current total minted value in all vaults before we update this Vault
uint256 currentTotalMinted = getTotalValueMintedInVaults();

dlc.fundingTxId = btcTxId;
dlc.wdTxId = "";
dlc.status = DLCLink.DLCStatus.FUNDED;

dlc.valueLocked = newValueLocked;
dlc.valueMinted = newValueLocked;

if (_checkMint(amountToMint, currentTotalMinted)) {
if (_checkMint(amountToMint, _totalValueMinted)) {
_totalValueMinted = _totalValueMinted + amountToMint;
_mintTokens(dlc.creator, amountToMint);
}

Expand Down Expand Up @@ -489,6 +502,7 @@ contract DLCManager is
}

dlc.valueMinted -= amount;
_totalValueMinted -= amount;
_burnTokens(dlc.creator, amount);
emit Withdraw(uuid, amount, msg.sender);
}
Expand Down Expand Up @@ -556,11 +570,7 @@ contract DLCManager is
}

function getTotalValueMintedInVaults() public view returns (uint256) {
uint256 totalValueMinted = 0;
for (uint256 i = 0; i < _index; i++) {
totalValueMinted += dlcs[i].valueMinted;
}
return totalValueMinted;
return _totalValueMinted;
}

function isWhitelisted(address account) external view returns (bool) {
Expand Down Expand Up @@ -725,4 +735,13 @@ contract DLCManager is
dlcBTCPoRFeed = feed;
emit SetDlcBTCPoRFeed(feed);
}

function verifyTotalValueMinted() external view onlyAdmin returns (bool) {
uint256 calculatedTotal = 0;
for (uint256 i = 0; i < _index; i++) {
calculatedTotal += dlcs[i].valueMinted;
}

return calculatedTotal == _totalValueMinted;
}
}

0 comments on commit ca48fda

Please sign in to comment.