Skip to content

Commit

Permalink
add documentation comments
Browse files Browse the repository at this point in the history
  • Loading branch information
traceurl committed Jan 9, 2024
1 parent 86bb2be commit 8fdebac
Show file tree
Hide file tree
Showing 14 changed files with 411 additions and 56 deletions.
24 changes: 21 additions & 3 deletions contracts/DODOV3MM/D3Pool/D3Funding.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ contract D3Funding is D3Storage {
using SafeERC20 for IERC20;

/// @notice borrow tokens from vault
/// @param token The address of the token to borrow
/// @param amount The amount of tokens to borrow
function borrow(address token, uint256 amount) external onlyOwner nonReentrant poolOngoing {
// call vault's poolBorrow function
ID3Vault(state._D3_VAULT_).poolBorrow(token, amount);
Expand All @@ -28,6 +30,8 @@ contract D3Funding is D3Storage {
}

/// @notice repay vault with certain amount of borrowed assets
/// @param token The address of the token to repay
/// @param amount The amount of tokens to repay
function repay(address token, uint256 amount) external onlyOwner nonReentrant poolOngoing {
// call vault's poolRepay
ID3Vault(state._D3_VAULT_).poolRepay(token, amount);
Expand All @@ -37,6 +41,7 @@ contract D3Funding is D3Storage {
}

/// @notice repay vault all debt of this token
/// @param token The address of the token to repay all debt
function repayAll(address token) external onlyOwner nonReentrant poolOngoing {
ID3Vault(state._D3_VAULT_).poolRepayAll(token);

Expand All @@ -46,6 +51,7 @@ contract D3Funding is D3Storage {
}

/// @notice used through liquidation
/// @param token The address of the token to update reserve
function updateReserveByVault(address token) external onlyVault {
uint256 allowance = IERC20(token).allowance(address(this), state._D3_VAULT_);
if(allowance < type(uint256).max) {
Expand All @@ -55,6 +61,7 @@ contract D3Funding is D3Storage {
}

/// @notice maker deposit, anyone could deposit but only maker could withdraw
/// @param token The address of the token to deposit
function makerDeposit(address token) external nonReentrant poolOngoing {
require(ID3Oracle(state._ORACLE_).isFeasible(token), Errors.TOKEN_NOT_FEASIBLE);

Check warning on line 66 in contracts/DODOV3MM/D3Pool/D3Funding.sol

View workflow job for this annotation

GitHub Actions / lint

Use Custom Errors instead of require statements

Check warning on line 66 in contracts/DODOV3MM/D3Pool/D3Funding.sol

View workflow job for this annotation

GitHub Actions / lint

Use Custom Errors instead of require statements
if (!state.hasDepositedToken[token]) {
Expand All @@ -74,6 +81,10 @@ contract D3Funding is D3Storage {
emit MakerDeposit(token, tokenInAmount);
}

/// @notice maker withdraw, only maker could withdraw
/// @param to The address to receive the withdrawn tokens
/// @param token The address of the token to withdraw
/// @param amount The amount of tokens to withdraw
function makerWithdraw(address to, address token, uint256 amount) external onlyOwner nonReentrant poolOngoing {
IERC20(token).safeTransfer(to, amount);

Expand All @@ -84,33 +95,40 @@ contract D3Funding is D3Storage {
emit MakerWithdraw(to, token, amount);
}

// below IM: not safe!
/// @notice check if the pool is safe
function checkSafe() public view returns (bool) {
return ID3Vault(state._D3_VAULT_).checkSafe(address(this));
}

// check when borrowing asset
/// @notice check if the pool is safe when borrowing asset
function checkBorrowSafe() public view returns (bool) {
return ID3Vault(state._D3_VAULT_).checkBorrowSafe(address(this));
}

// blow MM: dangerous!
/// @notice check if the pool can be liquidated
function checkCanBeLiquidated() public view returns (bool) {
return ID3Vault(state._D3_VAULT_).checkCanBeLiquidated(address(this));
}

/// @notice start the liquidation process
function startLiquidation() external onlyVault {
isInLiquidation = true;
}

/// @notice finish the liquidation process
function finishLiquidation() external onlyVault {
isInLiquidation = false;
}

/// @notice update the reserve of a token
/// @param token The address of the token to update reserve
function _updateReserve(address token) internal {
state.balances[token] = IERC20(token).balanceOf(address(this));
}

/// @notice check if a token is in the token list
/// @param token The address of the token to check
/// @return true if the token is in the token list, false otherwise
function _checkTokenInTokenlist(address token) internal view returns(bool){
return ID3Vault(state._D3_VAULT_).tokens(token);
}
Expand Down
26 changes: 20 additions & 6 deletions contracts/DODOV3MM/D3Pool/D3MM.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@ import {D3Trading} from "./D3Trading.sol";
import {IFeeRateModel} from "../../intf/IFeeRateModel.sol";
import {ID3Maker} from "../intf/ID3Maker.sol";

/// @title D3MM - DODO V3 Market Making Pool
/// @notice This contract inherits from D3Trading, providing more view functions.
contract D3MM is D3Trading {
/// @notice init D3MM pool
/// @notice Initializes the D3MM pool with the provided parameters
/// @param creator The address of the pool creator
/// @param maker The address of the D3Maker contract
/// @param vault The address of the vault contract
/// @param oracle The address of the oracle contract
/// @param feeRateModel The address of the fee rate model contract
/// @param maintainer The address of the maintainer contract
function init(
address creator,
address maker,
Expand All @@ -24,30 +32,35 @@ contract D3MM is D3Trading {
state._MAINTAINER_ = maintainer;
}

// ============= Set ====================
/// @notice Sets a new D3Maker contract for the pool
/// @param newMaker The address of the new D3Maker contract
function setNewMaker(address newMaker) external onlyOwner {
state._MAKER_ = newMaker;
allFlag = 0;
}

// ============= View =================
/// @notice Returns the address of the pool creator
function _CREATOR_() external view returns(address) {
return state._CREATOR_;
}

/// @notice Returns the fee rate for a given token
/// @param token The address of the token
function getFeeRate(address token) external view returns(uint256 feeRate) {
return IFeeRateModel(state._FEE_RATE_MODEL_).getFeeRate(token);
}

/// @notice Returns the list of tokens in the pool
function getPoolTokenlist() external view returns(address[] memory) {
return ID3Maker(state._MAKER_).getPoolTokenListFromMaker();
}

/// @notice Returns the list of tokens deposited in the pool
function getDepositedTokenList() external view returns (address[] memory) {
return state.depositedTokenList;
}

/// @notice get basic pool info
/// @notice Returns the basic information of the pool
function getD3MMInfo() external view returns (address vault, address oracle, address maker, address feeRateModel, address maintainer) {
vault = state._D3_VAULT_;
oracle = state._ORACLE_;
Expand All @@ -56,12 +69,13 @@ contract D3MM is D3Trading {
maintainer = state._MAINTAINER_;
}

/// @notice get a token's reserve in pool
/// @notice Returns the reserve of a given token in the pool
/// @param token The address of the token
function getTokenReserve(address token) external view returns (uint256) {
return state.balances[token];
}

/// @notice get D3MM contract version
/// @notice Returns the version of the D3MM contract
function version() external pure virtual returns (string memory) {
return "D3MM 1.0.0";
}
Expand Down
17 changes: 16 additions & 1 deletion contracts/DODOV3MM/D3Pool/D3Maker.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,21 @@ contract D3Maker is InitializableOwnable {
event SetNewToken(address indexed token);

// ============== init =============
/// @notice Initializes the contract with the owner, pool, and maxInterval
/// @param owner The address of the owner
/// @param pool The address of the pool
/// @param maxInterval The maximum interval
function init(address owner, address pool, uint256 maxInterval) external {
initOwner(owner);
_POOL_ = pool;
state.heartBeat.maxInterval = maxInterval;
}

// ============= Read for tokenMMInfo =================
/// @notice Returns the TokenMMInfo for a given token
/// @param token The address of the token
/// @return tokenMMInfo The TokenMMInfo of the token
/// @return tokenIndex The index of the token
function getTokenMMInfoForPool(address token)
external
view
Expand Down Expand Up @@ -65,7 +73,9 @@ contract D3Maker is InitializableOwnable {

// ================== Read parameters ==============

/// @notice give one token's address, give back token's priceInfo
/// @notice This function returns the price set for a given token
/// @param token The address of the token
/// @return priceSet The price set of the token
function getOneTokenPriceSet(address token) public view returns (uint80 priceSet) {
require(state.priceListInfo.tokenIndexMap[token] > 0, Errors.INVALID_TOKEN);

Check warning on line 80 in contracts/DODOV3MM/D3Pool/D3Maker.sol

View workflow job for this annotation

GitHub Actions / lint

Use Custom Errors instead of require statements

Check warning on line 80 in contracts/DODOV3MM/D3Pool/D3Maker.sol

View workflow job for this annotation

GitHub Actions / lint

Use Custom Errors instead of require statements
uint256 tokenOriIndex = state.priceListInfo.tokenIndexMap[token] - 1;
Expand All @@ -80,6 +90,7 @@ contract D3Maker is InitializableOwnable {
}

/// @notice get one token index. odd for none-stable, even for stable, true index = (tokenIndex[address] - 1) / 2
/// @param token The address of the token
function getOneTokenOriginIndex(address token) public view returns (int256) {
//require(state.priceListInfo.tokenIndexMap[token] > 0, Errors.INVALID_TOKEN);
return int256(state.priceListInfo.tokenIndexMap[token]) - 1;
Expand Down Expand Up @@ -122,6 +133,7 @@ contract D3Maker is InitializableOwnable {
+ (priceSet << (slotInnerIndex * MakerTypes.ONE_PRICE_BIT)) + rightPriceSet;
}

/// @notice check heartbeat
function checkHeartbeat() public view returns (bool) {
if (block.timestamp - state.heartBeat.lastHeartBeat <= state.heartBeat.maxInterval) {
return true;
Expand All @@ -130,13 +142,15 @@ contract D3Maker is InitializableOwnable {
}
}

/// @notice get pool's token list
function getPoolTokenListFromMaker() external view returns(address[] memory tokenlist) {
return poolTokenlist;
}

// ============= Set params ===========

/// @notice maker could use multicall to set different params in one tx.
/// @param data A list of calldata to call
function multicall(bytes[] calldata data) external returns (bytes[] memory results) {
results = new bytes[](data.length);
for (uint256 i = 0; i < data.length; i++) {
Expand Down Expand Up @@ -387,6 +401,7 @@ contract D3Maker is InitializableOwnable {
}

/// @notice set acceptable setting interval, if setting gap > maxInterval, swap will revert.
/// @param newMaxInterval The new max interval for heartbeat
function setHeartbeat(uint256 newMaxInterval) public onlyOwner {
state.heartBeat.maxInterval = newMaxInterval;

Expand Down
Loading

0 comments on commit 8fdebac

Please sign in to comment.