Skip to content
Open
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
61 changes: 43 additions & 18 deletions src/Djed.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,53 @@ contract Djed is ReentrancyGuard {
event SoldReserveCoins(address indexed seller, address indexed receiver, uint256 amountRC, uint256 amountBC);
event SoldBothCoins(address indexed seller, address indexed receiver, uint256 amountSC, uint256 amountRC, uint256 amountBC);

struct TreasuryParams {
address treasury;
uint256 initialTreasuryFee;
uint256 treasuryRevenueTarget;
}

struct DjedParams {
uint256 reserveRatioMin;
uint256 reserveRatioMax;
uint256 fee;
uint256 thresholdSupplySC;
uint256 rcMinPrice;
uint256 rcInitialPrice;
uint256 txLimit;
}

struct CoinParams {
string stableName;
string stableSymbol;
string reserveName;
string reserveSymbol;
}

constructor(
address oracleAddress, uint256 _scalingFactor,
address _treasury, uint256 _initialTreasuryFee, uint256 _treasuryRevenueTarget,
uint256 _reserveRatioMin, uint256 _reserveRatioMax,
uint256 _fee, uint256 _thresholdSupplySC, uint256 _rcMinPrice, uint256 _rcInitialPrice, uint256 _txLimit
address oracleAddress,
uint256 _scalingFactor,
TreasuryParams memory treasuryParams,
DjedParams memory djedParams,
CoinParams memory coinParams
) payable {
stableCoin = new Coin("StableCoin", "SC");
reserveCoin = new Coin("ReserveCoin", "RC");
stableCoin = new Coin(coinParams.stableName, coinParams.stableSymbol);
reserveCoin = new Coin(coinParams.reserveName, coinParams.reserveSymbol);
scDecimalScalingFactor = 10**stableCoin.decimals();
rcDecimalScalingFactor = 10**reserveCoin.decimals();
scalingFactor = _scalingFactor;

treasury = _treasury;
initialTreasuryFee = _initialTreasuryFee;
treasuryRevenueTarget = _treasuryRevenueTarget;
treasury = treasuryParams.treasury;
initialTreasuryFee = treasuryParams.initialTreasuryFee;
treasuryRevenueTarget = treasuryParams.treasuryRevenueTarget;

reserveRatioMin = _reserveRatioMin;
reserveRatioMax = _reserveRatioMax;
fee = _fee;
thresholdSupplySC = _thresholdSupplySC;
rcMinPrice = _rcMinPrice;
rcInitialPrice = _rcInitialPrice;
txLimit = _txLimit;
reserveRatioMin = djedParams.reserveRatioMin;
reserveRatioMax = djedParams.reserveRatioMax;
fee = djedParams.fee;
thresholdSupplySC = djedParams.thresholdSupplySC;
rcMinPrice = djedParams.rcMinPrice;
rcInitialPrice = djedParams.rcInitialPrice;
txLimit = djedParams.txLimit;
Comment on lines +70 to +86
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Add comprehensive parameter validation before assignments.

The constructor correctly assigns parameters from the structs (fixing issue #18), but lacks validation of critical values before assigning them to immutable variables. Since these cannot be changed post-deployment, invalid parameters would permanently break the protocol.

Required validations:

  • treasuryParams.treasury != address(0) - prevents failed fee transfers
  • treasuryParams.treasuryRevenueTarget > 0 - prevents division by zero in treasuryFee() (line 210)
  • _scalingFactor > 0 - prevents division by zero in multiple functions
  • djedParams.reserveRatioMin > 0 && djedParams.reserveRatioMin <= djedParams.reserveRatioMax - ensures valid ratio bounds
  • djedParams.reserveRatioMax > 0 - ensures valid maximum ratio
  • djedParams.fee <= _scalingFactor - prevents fees exceeding 100%
  • djedParams.rcInitialPrice > 0 && djedParams.rcMinPrice > 0 - ensures valid reserve coin pricing
  • djedParams.txLimit > 0 - ensures valid transaction limit

Apply this diff to add validation:

     ) payable {
+        require(treasuryParams.treasury != address(0), "Invalid treasury address");
+        require(treasuryParams.treasuryRevenueTarget > 0, "Treasury revenue target must be positive");
+        require(_scalingFactor > 0, "Scaling factor must be positive");
+        require(djedParams.reserveRatioMin > 0, "Min reserve ratio must be positive");
+        require(djedParams.reserveRatioMax > 0, "Max reserve ratio must be positive");
+        require(djedParams.reserveRatioMin <= djedParams.reserveRatioMax, "Min ratio must be <= max ratio");
+        require(djedParams.fee <= _scalingFactor, "Fee cannot exceed 100%");
+        require(djedParams.rcInitialPrice > 0, "RC initial price must be positive");
+        require(djedParams.rcMinPrice > 0, "RC min price must be positive");
+        require(djedParams.txLimit > 0, "Transaction limit must be positive");
+
         stableCoin = new Coin(coinParams.stableName, coinParams.stableSymbol);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
stableCoin = new Coin(coinParams.stableName, coinParams.stableSymbol);
reserveCoin = new Coin(coinParams.reserveName, coinParams.reserveSymbol);
scDecimalScalingFactor = 10**stableCoin.decimals();
rcDecimalScalingFactor = 10**reserveCoin.decimals();
scalingFactor = _scalingFactor;
treasury = _treasury;
initialTreasuryFee = _initialTreasuryFee;
treasuryRevenueTarget = _treasuryRevenueTarget;
treasury = treasuryParams.treasury;
initialTreasuryFee = treasuryParams.initialTreasuryFee;
treasuryRevenueTarget = treasuryParams.treasuryRevenueTarget;
reserveRatioMin = _reserveRatioMin;
reserveRatioMax = _reserveRatioMax;
fee = _fee;
thresholdSupplySC = _thresholdSupplySC;
rcMinPrice = _rcMinPrice;
rcInitialPrice = _rcInitialPrice;
txLimit = _txLimit;
reserveRatioMin = djedParams.reserveRatioMin;
reserveRatioMax = djedParams.reserveRatioMax;
fee = djedParams.fee;
thresholdSupplySC = djedParams.thresholdSupplySC;
rcMinPrice = djedParams.rcMinPrice;
rcInitialPrice = djedParams.rcInitialPrice;
txLimit = djedParams.txLimit;
require(treasuryParams.treasury != address(0), "Invalid treasury address");
require(treasuryParams.treasuryRevenueTarget > 0, "Treasury revenue target must be positive");
require(_scalingFactor > 0, "Scaling factor must be positive");
require(djedParams.reserveRatioMin > 0, "Min reserve ratio must be positive");
require(djedParams.reserveRatioMax > 0, "Max reserve ratio must be positive");
require(djedParams.reserveRatioMin <= djedParams.reserveRatioMax, "Min ratio must be <= max ratio");
require(djedParams.fee <= _scalingFactor, "Fee cannot exceed 100%");
require(djedParams.rcInitialPrice > 0, "RC initial price must be positive");
require(djedParams.rcMinPrice > 0, "RC min price must be positive");
require(djedParams.txLimit > 0, "Transaction limit must be positive");
stableCoin = new Coin(coinParams.stableName, coinParams.stableSymbol);
reserveCoin = new Coin(coinParams.reserveName, coinParams.reserveSymbol);
scDecimalScalingFactor = 10**stableCoin.decimals();
rcDecimalScalingFactor = 10**reserveCoin.decimals();
scalingFactor = _scalingFactor;
treasury = treasuryParams.treasury;
initialTreasuryFee = treasuryParams.initialTreasuryFee;
treasuryRevenueTarget = treasuryParams.treasuryRevenueTarget;
reserveRatioMin = djedParams.reserveRatioMin;
reserveRatioMax = djedParams.reserveRatioMax;
fee = djedParams.fee;
thresholdSupplySC = djedParams.thresholdSupplySC;
rcMinPrice = djedParams.rcMinPrice;
rcInitialPrice = djedParams.rcInitialPrice;
txLimit = djedParams.txLimit;
🤖 Prompt for AI Agents
In src/Djed.sol around lines 70 to 86, the constructor assigns many
immutable/config values without validating them; add require checks before the
assignments to prevent invalid permanent state. Specifically:
require(treasuryParams.treasury != address(0));
require(treasuryParams.treasuryRevenueTarget > 0); require(_scalingFactor > 0);
require(djedParams.reserveRatioMin > 0 && djedParams.reserveRatioMin <=
djedParams.reserveRatioMax); require(djedParams.reserveRatioMax > 0);
require(djedParams.fee <= _scalingFactor); require(djedParams.rcInitialPrice > 0
&& djedParams.rcMinPrice > 0); require(djedParams.txLimit > 0); place these
checks at the top of the constructor (before assigning immutables) with clear
revert messages so invalid inputs are rejected at deployment.


oracle = IOracle(oracleAddress);
oracle.acceptTermsOfService();
Expand Down Expand Up @@ -87,9 +111,10 @@ contract Djed is ReentrancyGuard {
}

function ratio() external view returns (uint256) {
return scalingFactor * R(0) / L(scPrice(0));
uint256 liabilities = L(scPrice(0));
require(liabilities > 0, "ratio: liabilities zero");
return (scalingFactor * R(0)) / liabilities;
}

// # Public Trading Functions:

function buyStableCoins(address receiver, uint256 feeUI, address ui) external payable nonReentrant {
Expand Down