Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract constants #124

Closed
Closed
Show file tree
Hide file tree
Changes from 2 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
49 changes: 17 additions & 32 deletions src/Blue.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,9 @@ import {IERC20} from "src/interfaces/IERC20.sol";
import {IOracle} from "src/interfaces/IOracle.sol";

import {MathLib} from "src/libraries/MathLib.sol";
import {Constants} from "src/libraries/Constants.sol";
import {SafeTransferLib} from "src/libraries/SafeTransferLib.sol";

uint256 constant WAD = 1e18;
uint256 constant ALPHA = 0.5e18;

/// @dev The prefix used for EIP-712 signature.
string constant EIP712_MSG_PREFIX = "\x19\x01";

/// @dev The name used for EIP-712 signature.
string constant EIP712_NAME = "Blue";

/// @dev The domain typehash used for the EIP-712 signature.
bytes32 constant EIP712_DOMAIN_TYPEHASH =
keccak256("EIP712Domain(string name,uint256 chainId,address verifyingContract)");

/// @dev The typehash for approveManagerWithSig Authorization used for the EIP-712 signature.
bytes32 constant EIP712_AUTHORIZATION_TYPEHASH =
keccak256("Authorization(address delegator,address manager,bool isAllowed,uint256 nonce,uint256 deadline)");

/// @dev The highest valid value for s in an ECDSA signature pair (0 < s < secp256k1n ÷ 2 + 1).
uint256 constant MAX_VALID_ECDSA_S = 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0;

// Market id.
type Id is bytes32;

Expand Down Expand Up @@ -96,8 +77,11 @@ contract Blue {
constructor(address newOwner) {
owner = newOwner;

domainSeparator =
keccak256(abi.encode(EIP712_DOMAIN_TYPEHASH, keccak256(bytes(EIP712_NAME)), block.chainid, address(this)));
domainSeparator = keccak256(
abi.encode(
Constants.EIP712_DOMAIN_TYPEHASH, keccak256(bytes(Constants.EIP712_NAME)), block.chainid, address(this)
)
);
}

// Modifiers.
Expand All @@ -118,7 +102,7 @@ contract Blue {
}

function enableLltv(uint256 lltv) external onlyOwner {
require(lltv < WAD, "LLTV too high");
require(lltv < Constants.WAD, "LLTV too high");
isLltvEnabled[lltv] = true;
}

Expand All @@ -143,8 +127,8 @@ contract Blue {
accrueInterests(market, id);

if (totalSupply[id] == 0) {
supplyShare[id][msg.sender] = WAD;
totalSupplyShares[id] = WAD;
supplyShare[id][msg.sender] = Constants.WAD;
totalSupplyShares[id] = Constants.WAD;
} else {
uint256 shares = amount.wMul(totalSupplyShares[id]).wDiv(totalSupply[id]);
supplyShare[id][msg.sender] += shares;
Expand Down Expand Up @@ -186,8 +170,8 @@ contract Blue {
accrueInterests(market, id);

if (totalBorrow[id] == 0) {
borrowShare[id][onBehalf] = WAD;
totalBorrowShares[id] = WAD;
borrowShare[id][onBehalf] = Constants.WAD;
totalBorrowShares[id] = Constants.WAD;
} else {
uint256 shares = amount.wMul(totalBorrowShares[id]).wDiv(totalBorrow[id]);
borrowShare[id][onBehalf] += shares;
Expand Down Expand Up @@ -260,7 +244,7 @@ contract Blue {
require(!isHealthy(market, id, borrower), "cannot liquidate a healthy position");

// The liquidation incentive is 1 + ALPHA * (1 / LLTV - 1).
uint256 incentive = WAD + ALPHA.wMul(WAD.wDiv(market.lltv) - WAD);
uint256 incentive = Constants.WAD + Constants.ALPHA.wMul(Constants.WAD.wDiv(market.lltv) - Constants.WAD);
uint256 repaid =
seized.wMul(market.collateralOracle.price()).wDiv(incentive).wDiv(market.borrowableOracle.price());
uint256 repaidShares = repaid.wMul(totalBorrowShares[id]).wDiv(totalBorrow[id]);
Expand Down Expand Up @@ -292,13 +276,14 @@ contract Blue {
uint256 deadline,
Signature calldata signature
) external {
require(uint256(signature.s) <= MAX_VALID_ECDSA_S, "invalid s");
require(uint256(signature.s) <= Constants.MAX_VALID_ECDSA_S, "invalid s");
// v ∈ {27, 28} (source: https://ethereum.github.io/yellowpaper/paper.pdf #308)
require(signature.v == 27 || signature.v == 28, "invalid v");

bytes32 structHash =
keccak256(abi.encode(EIP712_AUTHORIZATION_TYPEHASH, delegator, manager, isAllowed, nonce, deadline));
bytes32 digest = keccak256(abi.encodePacked(EIP712_MSG_PREFIX, domainSeparator, structHash));
bytes32 structHash = keccak256(
abi.encode(Constants.EIP712_AUTHORIZATION_TYPEHASH, delegator, manager, isAllowed, nonce, deadline)
);
bytes32 digest = keccak256(abi.encodePacked(Constants.EIP712_MSG_PREFIX, domainSeparator, structHash));
address signatory = ecrecover(digest, signature.v, signature.r, signature.s);

require(signatory != address(0) && delegator == signatory, "invalid signatory");
Expand Down
28 changes: 28 additions & 0 deletions src/libraries/Constants.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

/// @notice Constants.
library Constants {
/// @dev 1 wad.
uint256 internal constant WAD = 1e18;

/// @dev Scaling factor for the liquidation incentive.
uint256 internal constant ALPHA = 0.5e18;

/// @dev The prefix used for EIP-712 signature.
string internal constant EIP712_MSG_PREFIX = "\x19\x01";

/// @dev The name used for EIP-712 signature.
string internal constant EIP712_NAME = "Blue";

/// @dev The domain typehash used for the EIP-712 signature.
bytes32 internal constant EIP712_DOMAIN_TYPEHASH =
keccak256("EIP712Domain(string name,uint256 chainId,address verifyingContract)");

/// @dev The typehash for approveManagerWithSig Authorization used for the EIP-712 signature.
bytes32 internal constant EIP712_AUTHORIZATION_TYPEHASH =
keccak256("Authorization(address delegator,address manager,bool isAllowed,uint256 nonce,uint256 deadline)");

/// @dev The highest valid value for s in an ECDSA signature pair (0 < s < secp256k1n ÷ 2 + 1).
uint256 internal constant MAX_VALID_ECDSA_S = 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0;
}
10 changes: 6 additions & 4 deletions test/forge/Blue.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ pragma solidity 0.8.20;
import {IERC20} from "src/interfaces/IERC20.sol";
import {IOracle} from "src/interfaces/IOracle.sol";

import {Constants} from "src/libraries/Constants.sol";
Rubilmax marked this conversation as resolved.
Show resolved Hide resolved

import "forge-std/Test.sol";
import "forge-std/console.sol";

Expand Down Expand Up @@ -174,7 +176,7 @@ contract BlueTest is Test {
}

function testEnableLltv(uint256 newLltv) public {
newLltv = bound(newLltv, 0, WAD - 1);
newLltv = bound(newLltv, 0, Constants.WAD - 1);

vm.prank(OWNER);
blue.enableLltv(newLltv);
Expand All @@ -183,7 +185,7 @@ contract BlueTest is Test {
}

function testEnableLltvShouldFailWhenLltvTooHigh(uint256 newLltv) public {
newLltv = bound(newLltv, WAD, type(uint256).max);
newLltv = bound(newLltv, Constants.WAD, type(uint256).max);

vm.prank(OWNER);
vm.expectRevert("LLTV too high");
Expand Down Expand Up @@ -363,7 +365,7 @@ contract BlueTest is Test {
uint256 borrowingPower = amountCollateral.wMul(LLTV);
uint256 amountBorrowed = borrowingPower.wMul(0.8e18);
uint256 toSeize = amountCollateral.wMul(LLTV);
uint256 incentive = WAD + ALPHA.wMul(WAD.wDiv(LLTV) - WAD);
uint256 incentive = Constants.WAD + Constants.ALPHA.wMul(Constants.WAD.wDiv(LLTV) - Constants.WAD);

borrowableAsset.setBalance(address(this), amountLent);
collateralAsset.setBalance(BORROWER, amountCollateral);
Expand Down Expand Up @@ -405,7 +407,7 @@ contract BlueTest is Test {
uint256 borrowingPower = amountCollateral.wMul(LLTV);
uint256 amountBorrowed = borrowingPower.wMul(0.8e18);
uint256 toSeize = amountCollateral;
uint256 incentive = WAD + ALPHA.wMul(WAD.wDiv(market.lltv) - WAD);
uint256 incentive = Constants.WAD + Constants.ALPHA.wMul(Constants.WAD.wDiv(market.lltv) - Constants.WAD);

borrowableAsset.setBalance(address(this), amountLent);
collateralAsset.setBalance(BORROWER, amountCollateral);
Expand Down