Skip to content

Commit

Permalink
bump: v2.0.10
Browse files Browse the repository at this point in the history
bump: v2.0.10
  • Loading branch information
yruej301 authored Jul 16, 2024
2 parents 3775363 + 553a51e commit 02babeb
Show file tree
Hide file tree
Showing 16 changed files with 103 additions and 87 deletions.
71 changes: 59 additions & 12 deletions contracts/Clearinghouse.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ import "./interfaces/engine/IPerpEngine.sol";
import "./EndpointGated.sol";
import "./interfaces/IEndpoint.sol";
import "./ClearinghouseStorage.sol";
import "./Version.sol";

interface IProxyManager {
function getProxyManagerHelper() external view returns (address);

function getCodeHash(string memory name) external view returns (bytes32);
}

enum YieldMode {
Expand All @@ -45,12 +46,7 @@ interface IBlast {
) external;
}

contract Clearinghouse is
EndpointGated,
ClearinghouseStorage,
IClearinghouse,
Version
{
contract Clearinghouse is EndpointGated, ClearinghouseStorage, IClearinghouse {
using MathSD21x18 for int128;
using ERC20Helper for IERC20Base;

Expand Down Expand Up @@ -268,12 +264,15 @@ contract Clearinghouse is
require(_isAboveInitial(txn.sender), ERR_SUBACCT_HEALTH);
}

/// @notice control insurance balance, only callable by owner
function depositInsurance(IEndpoint.DepositInsurance calldata txn)
function depositInsurance(bytes calldata transaction)
external
virtual
onlyEndpoint
{
IEndpoint.DepositInsurance memory txn = abi.decode(
transaction[1:],
(IEndpoint.DepositInsurance)
);
require(txn.amount <= INT128_MAX, ERR_CONVERSION_OVERFLOW);
int256 multiplier = int256(
10**(MAX_DECIMALS - _decimals(QUOTE_PRODUCT_ID))
Expand Down Expand Up @@ -452,7 +451,11 @@ contract Clearinghouse is
.updateBalance(QUOTE_PRODUCT_ID, subaccount, amountSettled);
}

function settlePnl(IEndpoint.SettlePnl calldata txn) external onlyEndpoint {
function settlePnl(bytes calldata transaction) external onlyEndpoint {
IEndpoint.SettlePnl memory txn = abi.decode(
transaction[1:],
(IEndpoint.SettlePnl)
);
for (uint128 i = 0; i < txn.subaccounts.length; ++i) {
_settlePnl(txn.subaccounts[i], txn.productIds[i]);
}
Expand Down Expand Up @@ -496,14 +499,18 @@ contract Clearinghouse is
address value;
}

function upgradeClearinghouseLiq(address _clearinghouseLiq) external {
function _getProxyManager() internal view returns (address) {
AddressSlot storage proxyAdmin;
assembly {
proxyAdmin.slot := 0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103
}
return proxyAdmin.value;
}

function upgradeClearinghouseLiq(address _clearinghouseLiq) external {
require(
msg.sender ==
IProxyManager(proxyAdmin.value).getProxyManagerHelper(),
IProxyManager(_getProxyManager()).getProxyManagerHelper(),
ERR_UNAUTHORIZED
);
clearinghouseLiq = _clearinghouseLiq;
Expand Down Expand Up @@ -543,4 +550,44 @@ contract Clearinghouse is
ERR_DEPOSIT_TOO_SMALL
);
}

function assertCode(bytes calldata transaction) external view virtual {
IEndpoint.AssertCode memory txn = abi.decode(
transaction[1:],
(IEndpoint.AssertCode)
);
require(
txn.contractNames.length == txn.codeHashes.length,
ERR_CODE_NOT_MATCH
);
for (uint256 i = 0; i < txn.contractNames.length; i++) {
bytes32 expectedCodeHash = IProxyManager(_getProxyManager())
.getCodeHash(txn.contractNames[i]);
require(txn.codeHashes[i] == expectedCodeHash, ERR_CODE_NOT_MATCH);
}
}

function manualAssert(bytes calldata transaction) external view virtual {
IEndpoint.ManualAssert memory txn = abi.decode(
transaction[1:],
(IEndpoint.ManualAssert)
);
ISpotEngine spotEngine = ISpotEngine(
address(engineByType[IProductEngine.EngineType.SPOT])
);
IPerpEngine perpEngine = IPerpEngine(
address(engineByType[IProductEngine.EngineType.PERP])
);
perpEngine.manualAssert(txn.openInterests);
spotEngine.manualAssert(txn.totalDeposits, txn.totalBorrows);
}

function getWithdrawPool() external view returns (address) {
return withdrawPool;
}

function setWithdrawPool(address _withdrawPool) external onlyOwner {
require(_withdrawPool != address(0));
withdrawPool = _withdrawPool;
}
}
4 changes: 1 addition & 3 deletions contracts/ClearinghouseLiq.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ import "./interfaces/engine/IPerpEngine.sol";
import "./EndpointGated.sol";
import "./interfaces/IEndpoint.sol";
import "./ClearinghouseStorage.sol";
import "./Version.sol";

contract ClearinghouseLiq is
EndpointGated,
ClearinghouseStorage,
IClearinghouseLiq,
Version
IClearinghouseLiq
{
using MathSD21x18 for int128;

Expand Down
2 changes: 2 additions & 0 deletions contracts/ClearinghouseStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ abstract contract ClearinghouseStorage {

uint256 internal spreads;

address internal withdrawPool;

function getLiqPriceX18(uint32 productId, int128 amount)
internal
view
Expand Down
34 changes: 7 additions & 27 deletions contracts/Endpoint.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ import "./interfaces/engine/ISpotEngine.sol";
import "./interfaces/engine/IPerpEngine.sol";
import "./interfaces/IERC20Base.sol";
import "./interfaces/IVerifier.sol";
import "./Version.sol";

interface ISanctionsList {
function isSanctioned(address addr) external view returns (bool);
}

contract Endpoint is IEndpoint, EIP712Upgradeable, OwnableUpgradeable, Version {
contract Endpoint is IEndpoint, EIP712Upgradeable, OwnableUpgradeable {
using ERC20Helper for IERC20Base;

IERC20Base private quote; // deprecated
Expand Down Expand Up @@ -460,14 +459,9 @@ contract Endpoint is IEndpoint, EIP712Upgradeable, OwnableUpgradeable, Version {
address(0)
);
} else if (txType == TransactionType.SettlePnl) {
SettlePnl memory txn = abi.decode(transaction[1:], (SettlePnl));
clearinghouse.settlePnl(txn);
clearinghouse.settlePnl(transaction);
} else if (txType == TransactionType.DepositInsurance) {
DepositInsurance memory txn = abi.decode(
transaction[1:],
(DepositInsurance)
);
clearinghouse.depositInsurance(txn);
clearinghouse.depositInsurance(transaction);
} else if (txType == TransactionType.MintLp) {
MintLp memory txn = abi.decode(transaction[1:], (MintLp));
require(
Expand Down Expand Up @@ -584,8 +578,7 @@ contract Endpoint is IEndpoint, EIP712Upgradeable, OwnableUpgradeable, Version {
UpdatePrice memory txn = abi.decode(transaction[1:], (UpdatePrice));
_updatePrice(txn.productId, txn.priceX18);
} else if (txType == TransactionType.SettlePnl) {
SettlePnl memory txn = abi.decode(transaction[1:], (SettlePnl));
clearinghouse.settlePnl(txn);
clearinghouse.settlePnl(transaction);
} else if (
txType == TransactionType.MatchOrders ||
txType == TransactionType.MatchOrdersRFQ
Expand Down Expand Up @@ -671,12 +664,7 @@ contract Endpoint is IEndpoint, EIP712Upgradeable, OwnableUpgradeable, Version {
requireSubaccount(txn.subaccount);
clearinghouse.claimSequencerFees(txn, fees);
} else if (txType == TransactionType.ManualAssert) {
ManualAssert memory txn = abi.decode(
transaction[1:],
(ManualAssert)
);
perpEngine.manualAssert(txn.openInterests);
spotEngine.manualAssert(txn.totalDeposits, txn.totalBorrows);
clearinghouse.manualAssert(transaction);
} else if (txType == TransactionType.LinkSigner) {
SignedLinkSigner memory signedTx = abi.decode(
transaction[1:],
Expand Down Expand Up @@ -753,6 +741,8 @@ contract Endpoint is IEndpoint, EIP712Upgradeable, OwnableUpgradeable, Version {
txn.productId,
txn.minDepositRateX18
);
} else if (txType == TransactionType.AssertCode) {
clearinghouse.assertCode(transaction);
} else {
revert();
}
Expand Down Expand Up @@ -857,16 +847,6 @@ contract Endpoint is IEndpoint, EIP712Upgradeable, OwnableUpgradeable, Version {
return nonces[sender];
}

function registerTransferableWallet(address wallet, bool)
external
virtual
onlyOwner
{
// comment this out because contract size exceeds limit and we don't use
// this function anymore.
// transferableWallets[wallet] = true;
}

function updateSanctions(address _sanctions) external onlyOwner {
sanctions = ISanctionsList(_sanctions);
}
Expand Down
4 changes: 1 addition & 3 deletions contracts/OffchainExchange.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ import "./libraries/Logger.sol";
import "./interfaces/IOffchainExchange.sol";
import "./EndpointGated.sol";
import "./common/Errors.sol";
import "./Version.sol";
import "./interfaces/engine/ISpotEngine.sol";
import "./interfaces/engine/IPerpEngine.sol";

contract OffchainExchange is
IOffchainExchange,
EndpointGated,
EIP712Upgradeable,
Version
EIP712Upgradeable
{
using MathSD21x18 for int128;
IClearinghouse internal clearinghouse;
Expand Down
3 changes: 1 addition & 2 deletions contracts/PerpEngine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ import "./libraries/MathHelper.sol";
import "./libraries/MathSD21x18.sol";
import "./BaseEngine.sol";
import "./PerpEngineLp.sol";
import "./Version.sol";

contract PerpEngine is PerpEngineLp, Version {
contract PerpEngine is PerpEngineLp {
using MathSD21x18 for int128;

function initialize(
Expand Down
3 changes: 1 addition & 2 deletions contracts/SpotEngine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ import "./libraries/RiskHelper.sol";
import "./BaseEngine.sol";
import "./SpotEngineState.sol";
import "./SpotEngineLP.sol";
import "./Version.sol";

contract SpotEngine is SpotEngineLP, Version {
contract SpotEngine is SpotEngineLP {
using MathSD21x18 for int128;

function initialize(
Expand Down
6 changes: 5 additions & 1 deletion contracts/SpotEngineState.sol
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,11 @@ abstract contract SpotEngineState is ISpotEngine, BaseEngine {
}
}

function getMinDepositRate(uint32 productId) external returns (int128) {
function getMinDepositRate(uint32 productId)
external
view
returns (int128)
{
return minDepositRatesX18[productId];
}
}
9 changes: 4 additions & 5 deletions contracts/Verifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ pragma solidity ^0.8.0;
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/utils/cryptography/draft-EIP712Upgradeable.sol";
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import "./Version.sol";
import "./common/Errors.sol";
import "./libraries/MathHelper.sol";
import "./interfaces/IVerifier.sol";

contract Verifier is EIP712Upgradeable, OwnableUpgradeable, IVerifier, Version {
contract Verifier is EIP712Upgradeable, OwnableUpgradeable, IVerifier {
Point[8] internal pubkeys;
Point[256] internal aggregatePubkey;
bool[256] internal isAggregatePubkeyLatest;
Expand Down Expand Up @@ -230,7 +229,7 @@ contract Verifier is EIP712Upgradeable, OwnableUpgradeable, IVerifier, Version {
bytes32 digest,
bytes memory signature,
uint8 signerIndex
) public returns (bool) {
) public view returns (bool) {
address expectedAddress = getPubkeyAddress(signerIndex);
address recovered = ECDSA.recover(digest, signature);
return expectedAddress == recovered;
Expand All @@ -241,11 +240,11 @@ contract Verifier is EIP712Upgradeable, OwnableUpgradeable, IVerifier, Version {
uint64 idx,
bytes[] calldata signatures
) public {
bytes32 msg = keccak256(
bytes32 data = keccak256(
abi.encodePacked(uint256(block.chainid), uint256(idx), txn)
);
bytes32 hashedMsg = keccak256(
abi.encodePacked("\x19Ethereum Signed Message:\n32", msg)
abi.encodePacked("\x19Ethereum Signed Message:\n32", data)
);

uint256 nSignatures = 0;
Expand Down
11 changes: 0 additions & 11 deletions contracts/Version.sol

This file was deleted.

2 changes: 0 additions & 2 deletions contracts/common/Constants.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ int128 constant SLOW_MODE_FEE = 1000000; // $1
int128 constant LIQUIDATION_FEE = 1e18; // $1
int128 constant HEALTHCHECK_FEE = 1e18; // $1

uint64 constant VERSION = 27;

uint128 constant INT128_MAX = uint128(type(int128).max);

uint64 constant SECONDS_PER_DAY = 3600 * 24;
Expand Down
2 changes: 2 additions & 0 deletions contracts/common/Errors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,5 @@ string constant ERR_INVALID_RISK_GROUP = "IRG";
string constant ERR_VERIFY_SCHNORR = "VSR";

string constant ERR_DEPOSIT_TOO_SMALL = "DTS";

string constant ERR_CODE_NOT_MATCH = "CNM";
11 changes: 8 additions & 3 deletions contracts/interfaces/IEndpoint.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
pragma solidity ^0.8.0;

import "./clearinghouse/IClearinghouse.sol";
import "./IVersion.sol";

interface IEndpoint is IVersion {
interface IEndpoint {
event SubmitTransactions();

// events that we parse transactions into
Expand Down Expand Up @@ -34,7 +33,8 @@ interface IEndpoint is IVersion {
MatchOrdersRFQ,
TransferQuote,
RebalanceXWithdraw,
UpdateMinDepositRate
UpdateMinDepositRate,
AssertCode
}

struct UpdateProduct {
Expand Down Expand Up @@ -159,6 +159,11 @@ interface IEndpoint is IVersion {
int128[] totalBorrows;
}

struct AssertCode {
string[] contractNames;
bytes32[] codeHashes;
}

struct Rebate {
bytes32[] subaccounts;
int128[] amounts;
Expand Down
3 changes: 1 addition & 2 deletions contracts/interfaces/IOffchainExchange.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
pragma solidity ^0.8.0;

import "./clearinghouse/IClearinghouse.sol";
import "./IVersion.sol";

interface IOffchainExchange is IVersion {
interface IOffchainExchange {
event FillOrder(
uint32 indexed productId,
// original order information
Expand Down
6 changes: 0 additions & 6 deletions contracts/interfaces/IVersion.sol

This file was deleted.

Loading

0 comments on commit 02babeb

Please sign in to comment.