Skip to content

Commit

Permalink
add limits
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Korokhov committed May 11, 2024
1 parent 41181e1 commit 8f559ac
Show file tree
Hide file tree
Showing 7 changed files with 259 additions and 35 deletions.
48 changes: 47 additions & 1 deletion src/contracts/defaultCollateral/DefaultCollateral.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,28 @@ contract DefaultCollateral is ERC20Upgradeable, ReentrancyGuardUpgradeable, IDef
*/
mapping(address issuer => mapping(address recipient => uint256 amount)) public debt;

/**
* @inheritdoc IDefaultCollateral
*/
uint256 public limit;

/**
* @inheritdoc IDefaultCollateral
*/
address public limitIncreaser;

modifier onlyLimitIncreaser() {
if (msg.sender != limitIncreaser) {
revert NotLimitIncreaser();
}
_;
}

constructor() {
_disableInitializers();
}

function initialize(address asset_) external initializer {
function initialize(address asset_, uint256 initialLimit, address limitIncreaser_) external initializer {
__ERC20_init(
string.concat("DefaultCollateral_", IERC20Metadata(asset_).name()),
string.concat("DB_", IERC20Metadata(asset_).symbol())
Expand All @@ -75,6 +92,9 @@ contract DefaultCollateral is ERC20Upgradeable, ReentrancyGuardUpgradeable, IDef

asset = asset_;

limit = initialLimit;
limitIncreaser = limitIncreaser_;

DECIMALS = IERC20Metadata(asset).decimals();
}

Expand All @@ -99,6 +119,12 @@ contract DefaultCollateral is ERC20Upgradeable, ReentrancyGuardUpgradeable, IDef

_mint(recipient, amount);

if (limit < totalSupply()) {
revert ExceedsLimit();
}

emit Deposit(msg.sender, recipient, amount);

return amount;
}

Expand Down Expand Up @@ -129,6 +155,8 @@ contract DefaultCollateral is ERC20Upgradeable, ReentrancyGuardUpgradeable, IDef
_burn(msg.sender, amount);

IERC20(asset).safeTransfer(recipient, amount);

emit Withdraw(msg.sender, recipient, amount);
}

/**
Expand All @@ -152,4 +180,22 @@ contract DefaultCollateral is ERC20Upgradeable, ReentrancyGuardUpgradeable, IDef

emit RepayDebt(msg.sender, recipient, amount);
}

/**
* @inheritdoc IDefaultCollateral
*/
function increaseLimit(uint256 amount) external onlyLimitIncreaser {
limit += amount;

emit IncreaseLimit(amount);
}

/**
* @inheritdoc IDefaultCollateral
*/
function setLimitIncreaser(address limitIncreaser_) external onlyLimitIncreaser {
limitIncreaser = limitIncreaser_;

emit SetLimitIncreaser(limitIncreaser_);
}
}
4 changes: 2 additions & 2 deletions src/contracts/defaultCollateral/DefaultCollateralFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ contract DefaultCollateralFactory is Factory, IDefaultCollateralFactory {
/**
* @inheritdoc IDefaultCollateralFactory
*/
function create(address asset) external returns (address) {
function create(address asset, uint256 initialLimit, address limitIncreaser) external returns (address) {
address collateral = COLLATERAL_IMPLEMENTATION.clone();
DefaultCollateral(collateral).initialize(asset);
DefaultCollateral(collateral).initialize(asset, initialLimit, limitIncreaser);

_addEntity(collateral);

Expand Down
56 changes: 56 additions & 0 deletions src/interfaces/defaultCollateral/IDefaultCollateral.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,52 @@ pragma solidity 0.8.25;
import {ICollateral} from "src/interfaces/ICollateral.sol";

interface IDefaultCollateral is ICollateral {
error NotLimitIncreaser();
error InsufficientDeposit();
error ExceedsLimit();
error InsufficientWithdraw();
error InsufficientIssueDebt();

/**
* @notice Emmited when deposit happens.
* @param depositor address of the depositor
* @param recipient address of the collateral's recipient
* @param amount amount of the collateral minted
*/
event Deposit(address indexed depositor, address indexed recipient, uint256 amount);

/**
* @notice Emmited when withdrawal happens.
* @param withdrawer address of the withdrawer
* @param recipient address of the underlying asset's recipient
* @param amount amount of the collateral burned
*/
event Withdraw(address indexed withdrawer, address indexed recipient, uint256 amount);

/**
* @notice Emmited when limit is increased.
* @param amount amount to increase the limit by
*/
event IncreaseLimit(uint256 amount);

/**
* @notice Emmited when new limit increaser is set.
* @param limitIncreaser address of the new limit increaser
*/
event SetLimitIncreaser(address indexed limitIncreaser);

/**
* @notice Get a maximum possible collateral total supply.
* @return maximum collateral total supply
*/
function limit() external view returns (uint256);

/**
* @notice Get an address of the limit increaser.
* @return address of the limit increaser
*/
function limitIncreaser() external view returns (address);

/**
* @notice Deposit a given amount of the underlying asset, and mint the collateral to a particular recipient.
* @param recipient address of the collateral's recipient
Expand Down Expand Up @@ -41,4 +83,18 @@ interface IDefaultCollateral is ICollateral {
* @param amount amount of the underlying asset
*/
function withdraw(address recipient, uint256 amount) external;

/**
* @notice Increase a limit of maximum collateral total supply.
* @param amount amount to increase the limit by
* @dev Called only by limitIncreaser.
*/
function increaseLimit(uint256 amount) external;

/**
* @notice Set a new limit increaser.
* @param limitIncreaser address of the new limit increaser
* @dev Called only by limitIncreaser.
*/
function setLimitIncreaser(address limitIncreaser) external;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ interface IDefaultCollateralFactory is IFactory {
/**
* @notice Create a default collateral with a given asset.
* @param asset address of the underlying asset
* @param initialLimit initial limit for maximum collateral total supply
* @param limitIncreaser address of the limit increaser (optional)
* @return address of the created collateral
*/
function create(address asset) external returns (address);
function create(address asset, uint256 initialLimit, address limitIncreaser) external returns (address);
}
Loading

0 comments on commit 8f559ac

Please sign in to comment.