Skip to content

Commit

Permalink
update code
Browse files Browse the repository at this point in the history
  • Loading branch information
traceurl committed Nov 8, 2023
1 parent 1f881fb commit 365ff73
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion contracts/DODOV3MM/D3PoolNoBorrow/D3MMNoBorrow.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ contract D3MMNoBorrow is D3MM {

/// @notice get D3MM contract version
function version() external pure virtual override returns (string memory) {
return "D3MM No Borrow";
return "D3MMNoBorrow 1.0.0";
}
}
26 changes: 11 additions & 15 deletions contracts/DODOV3MM/D3PoolNoBorrow/D3MakerFreeSlot.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {D3Maker} from "../D3Pool/D3Maker.sol";
/// @notice D3MakerFreeSlot is a special type of D3Maker, in which the market maker can set a new token info in an exsiting token slot, which can save gas
contract D3MakerFreeSlot is D3Maker {

event ReplaceToken(address indexed oldToken, address indexed newToken);

/// @notice maker set a new token info into an occupied slot, replacing old token info
/// @param token token's address
/// @param priceSet packed price, [mid price(16) | mid price decimal(8) | fee rate(16) | ask up rate (16) | bid down rate(16)]
Expand All @@ -26,6 +28,7 @@ contract D3MakerFreeSlot is D3Maker {
address oldToken
) external onlyOwner {
require(state.priceListInfo.tokenIndexMap[token] == 0, Errors.HAVE_SET_TOKEN_INFO);
require(state.priceListInfo.tokenIndexMap[oldToken] != 0, Errors.OLD_TOKEN_NOT_FOUND);
// check amount
require(kAsk >= 0 && kAsk <= 10000, Errors.K_LIMIT);
require(kBid >= 0 && kBid <= 10000, Errors.K_LIMIT);
Expand All @@ -39,7 +42,7 @@ contract D3MakerFreeSlot is D3Maker {

uint256 tokenIndex = uint256(getOneTokenOriginIndex(oldToken));
bool isStable = (tokenIndex % 2 == 0);
require(isStable == stableOrNot, "stable type not match");
require(isStable == stableOrNot, Errors.STABLE_TYPE_NOT_MATCH);

// remove old token info
state.tokenMMInfoMap[oldToken].priceInfo = 0;
Expand All @@ -61,29 +64,22 @@ contract D3MakerFreeSlot is D3Maker {
uint256 indexInStable = tokenIndex / 2;
uint256 innerSlotIndex = indexInStable % MakerTypes.PRICE_QUANTITY_IN_ONE_SLOT;
uint256 slotIndex = indexInStable / MakerTypes.PRICE_QUANTITY_IN_ONE_SLOT;
if (innerSlotIndex == 0) {
state.priceListInfo.tokenPriceStable.push(priceSet);
} else {
state.priceListInfo.tokenPriceStable[slotIndex] = (
uint256(priceSet) << (MakerTypes.ONE_PRICE_BIT * innerSlotIndex)
) + state.priceListInfo.tokenPriceStable[slotIndex];
}
uint256 oldPriceSlot = state.priceListInfo.tokenPriceStable[slotIndex];
uint256 newPriceSlot = stickPrice(oldPriceSlot, innerSlotIndex, priceSet);
state.priceListInfo.tokenPriceStable[slotIndex] = newPriceSlot;
} else {
uint256 indexInNStable = (tokenIndex - 1) / 2;
uint256 innerSlotIndex = indexInNStable % MakerTypes.PRICE_QUANTITY_IN_ONE_SLOT;
uint256 slotIndex = indexInNStable / MakerTypes.PRICE_QUANTITY_IN_ONE_SLOT;
if (innerSlotIndex == 0) {
state.priceListInfo.tokenPriceNS.push(priceSet);
} else {
state.priceListInfo.tokenPriceNS[slotIndex] = (
uint256(priceSet) << (MakerTypes.ONE_PRICE_BIT * innerSlotIndex)
) + state.priceListInfo.tokenPriceNS[slotIndex];
}
uint256 oldPriceSlot = state.priceListInfo.tokenPriceNS[slotIndex];
uint256 newPriceSlot = stickPrice(oldPriceSlot, innerSlotIndex, priceSet);
state.priceListInfo.tokenPriceNS[slotIndex] = newPriceSlot;
}
// to avoid reset the same token, tokenIndexMap record index from 1, but actualIndex = tokenIndex[address] - 1
state.priceListInfo.tokenIndexMap[token] = tokenIndex + 1;
state.tokenMMInfoMap[token].tokenIndex = uint16(tokenIndex);

emit SetNewToken(token);
emit ReplaceToken(oldToken, token);
}
}
2 changes: 2 additions & 0 deletions contracts/DODOV3MM/lib/Errors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ library Errors {
string public constant K_LIMIT = "D3MAKER_K_LIMIT_ERROR";
string public constant PRICE_UP_BELOW_PRICE_DOWN = "D3MAKER_PRICE_UP_BELOW_PRICE_DOWN";
string public constant HAVE_SET_TOKEN_INFO = "D3MAKER_HAVE_SET_TOKEN_INFO";
string public constant OLD_TOKEN_NOT_FOUND = "D3MAKER_OLD_TOKEN_NOT_FOUND";
string public constant STABLE_TYPE_NOT_MATCH = "D3MAKER_STABLE_TYPE_NOT_MATCH";
string public constant K_LENGTH_NOT_MATCH = "D3MAKER_K_LENGTH_NOT_MATCH";
string public constant AMOUNTS_LENGTH_NOT_MATCH = "D3MAKER_AMOUNTS_LENGTH_NOT_MATCH";
string public constant PRICES_LENGTH_NOT_MATCH = "D3MAKER_PRICES_LENGTH_NOT_MATCH";
Expand Down
8 changes: 7 additions & 1 deletion contracts/DODOV3MM/periphery/D3Oracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ struct PriceSource {
contract D3Oracle is ID3Oracle, InitializableOwnable {
// originToken => priceSource
mapping(address => PriceSource) public priceSources;
mapping(bytes32 => bool) public whitelistVersion;
address public sequencerFeed;

uint256 private constant GRACE_PERIOD_TIME = 3600;
Expand Down Expand Up @@ -55,6 +56,10 @@ contract D3Oracle is ID3Oracle, InitializableOwnable {
priceSources[token].isWhitelisted = isAvailable;
}

function setWhitelistVersion(bytes32 version, bool isAllowed) external onlyOwner {
whitelistVersion[version] = isAllowed;
}

/// @notice Get the price for a token
/// @dev The price definition is: how much virtual USD the token values if token amount is 1e18.
/// @dev Example 1: if the token decimals is 18, and worth 2 USD, then price is 2e18.
Expand Down Expand Up @@ -94,7 +99,8 @@ contract D3Oracle is ID3Oracle, InitializableOwnable {
/// @dev PMMRangeOrder will parse token amount if the decimals is not 18
/// @dev Do not use this function in other place. If use, make sure both tokens' decimals are 18
function getMaxReceive(address fromToken, address toToken, uint256 fromAmount) external view returns (uint256) {
if (keccak256(abi.encodePacked(ID3MM(msg.sender).version())) == keccak256(abi.encodePacked("D3MM No Borrow"))) {
bytes32 version = keccak256(abi.encodePacked(ID3MM(msg.sender).version()));
if (whitelistVersion[version]) {
if (!priceSources[fromToken].isWhitelisted || !priceSources[toToken].isWhitelisted) {
return type(uint256).max;
}
Expand Down

0 comments on commit 365ff73

Please sign in to comment.