Better oracle check commit

This commit is contained in:
Samyak Jain 2024-06-13 04:36:05 +04:00
parent 7e9a4c7066
commit 9ff76a2779

View File

@ -1,9 +1,9 @@
pragma solidity >=0.7.0; pragma solidity >=0.7.0;
pragma experimental ABIEncoderV2; pragma experimental ABIEncoderV2;
import { BigMathMinified } from "./libraries/bigMathMinified.sol"; import {BigMathMinified} from "./libraries/bigMathMinified.sol";
import { LiquiditySlotsLink } from "./libraries/liquiditySlotsLink.sol"; import {LiquiditySlotsLink} from "./libraries/liquiditySlotsLink.sol";
import { LiquidityCalcs } from "./libraries/liquidityCalcs.sol"; import {LiquidityCalcs} from "./libraries/liquidityCalcs.sol";
interface IGovernorBravo { interface IGovernorBravo {
function _acceptAdmin() external; function _acceptAdmin() external;
@ -253,7 +253,9 @@ interface IFluidLiquidityAdmin {
uint256[] memory borrowExchangePrices_ uint256[] memory borrowExchangePrices_
); );
function readFromStorage(bytes32 slot_) external view returns (uint256 result_); function readFromStorage(
bytes32 slot_
) external view returns (uint256 result_);
} }
interface IFluidVaultT1Factory { interface IFluidVaultT1Factory {
@ -276,16 +278,18 @@ interface IFluidVaultT1Factory {
bool allowed_ bool allowed_
) external; ) external;
/// @notice Computes the address of a vault based on its given ID (`vaultId_`). /// @notice Computes the address of a vault based on its given ID (`vaultId_`).
/// @param vaultId_ The ID of the vault. /// @param vaultId_ The ID of the vault.
/// @return vault_ Returns the computed address of the vault. /// @return vault_ Returns the computed address of the vault.
function getVaultAddress(uint256 vaultId_) external view returns (address vault_); function getVaultAddress(
uint256 vaultId_
) external view returns (address vault_);
function readFromStorage(bytes32 slot_) external view returns (uint256 result_); function readFromStorage(
bytes32 slot_
) external view returns (uint256 result_);
} }
interface IFluidVaultT1 { interface IFluidVaultT1 {
/// @notice updates the Vault oracle to `newOracle_`. Must implement the FluidOracle interface. /// @notice updates the Vault oracle to `newOracle_`. Must implement the FluidOracle interface.
function updateOracle(address newOracle_) external; function updateOracle(address newOracle_) external;
@ -329,9 +333,14 @@ interface IFluidVaultT1 {
} }
/// @notice returns all Vault constants /// @notice returns all Vault constants
function constantsView() external view returns (ConstantViews memory constantsView_); function constantsView()
external
view
returns (ConstantViews memory constantsView_);
function readFromStorage(bytes32 slot_) external view returns (uint256 result_); function readFromStorage(
bytes32 slot_
) external view returns (uint256 result_);
struct Configs { struct Configs {
uint16 supplyRateMagnifier; uint16 supplyRateMagnifier;
@ -355,10 +364,16 @@ interface IFluidOracle {
function getExchangeRate() external view returns (uint256 exchangeRate_); function getExchangeRate() external view returns (uint256 exchangeRate_);
/// @notice Get the `exchangeRate_` between the underlying asset and the peg asset in 1e27 for operates /// @notice Get the `exchangeRate_` between the underlying asset and the peg asset in 1e27 for operates
function getExchangeRateOperate() external view returns (uint256 exchangeRate_); function getExchangeRateOperate()
external
view
returns (uint256 exchangeRate_);
/// @notice Get the `exchangeRate_` between the underlying asset and the peg asset in 1e27 for liquidations /// @notice Get the `exchangeRate_` between the underlying asset and the peg asset in 1e27 for liquidations
function getExchangeRateLiquidate() external view returns (uint256 exchangeRate_); function getExchangeRateLiquidate()
external
view
returns (uint256 exchangeRate_);
} }
interface IFluidReserveContract { interface IFluidReserveContract {
@ -518,7 +533,11 @@ contract PayloadIGP26 {
address oldVault_, address oldVault_,
address newVault_, address newVault_,
uint256 withdrawalLimit uint256 withdrawalLimit
) internal view returns(AdminModuleStructs.UserSupplyConfig memory config_) { )
internal
view
returns (AdminModuleStructs.UserSupplyConfig memory config_)
{
uint256 userSupplyData_ = LIQUIDITY.readFromStorage( uint256 userSupplyData_ = LIQUIDITY.readFromStorage(
LiquiditySlotsLink.calculateDoubleMappingStorageSlot( LiquiditySlotsLink.calculateDoubleMappingStorageSlot(
LiquiditySlotsLink.LIQUIDITY_USER_SUPPLY_DOUBLE_MAPPING_SLOT, LiquiditySlotsLink.LIQUIDITY_USER_SUPPLY_DOUBLE_MAPPING_SLOT,
@ -540,9 +559,11 @@ contract PayloadIGP26 {
user: newVault_, user: newVault_,
token: token_, token: token_,
mode: uint8(userSupplyData_ & 1), mode: uint8(userSupplyData_ & 1),
expandPercent: (userSupplyData_ >> LiquiditySlotsLink.BITS_USER_SUPPLY_EXPAND_PERCENT) & X14, expandPercent: (userSupplyData_ >>
expandDuration: (userSupplyData_ >> LiquiditySlotsLink.BITS_USER_SUPPLY_EXPAND_DURATION) & X24, LiquiditySlotsLink.BITS_USER_SUPPLY_EXPAND_PERCENT) & X14,
baseWithdrawalLimit: withdrawalLimit * 1e12 / supplyExchangePrice expandDuration: (userSupplyData_ >>
LiquiditySlotsLink.BITS_USER_SUPPLY_EXPAND_DURATION) & X24,
baseWithdrawalLimit: (withdrawalLimit * 1e12) / supplyExchangePrice
}); });
} }
@ -552,7 +573,11 @@ contract PayloadIGP26 {
address newVault_, address newVault_,
uint256 baseLimit, uint256 baseLimit,
uint256 maxLimit uint256 maxLimit
) internal view returns(AdminModuleStructs.UserBorrowConfig memory config_) { )
internal
view
returns (AdminModuleStructs.UserBorrowConfig memory config_)
{
uint256 userBorrowData_ = LIQUIDITY.readFromStorage( uint256 userBorrowData_ = LIQUIDITY.readFromStorage(
LiquiditySlotsLink.calculateDoubleMappingStorageSlot( LiquiditySlotsLink.calculateDoubleMappingStorageSlot(
LiquiditySlotsLink.LIQUIDITY_USER_BORROW_DOUBLE_MAPPING_SLOT, LiquiditySlotsLink.LIQUIDITY_USER_BORROW_DOUBLE_MAPPING_SLOT,
@ -574,14 +599,18 @@ contract PayloadIGP26 {
user: newVault_, user: newVault_,
token: token_, token: token_,
mode: uint8(userBorrowData_ & 1), mode: uint8(userBorrowData_ & 1),
expandPercent: (userBorrowData_ >> LiquiditySlotsLink.BITS_USER_BORROW_EXPAND_PERCENT) & X14, expandPercent: (userBorrowData_ >>
expandDuration: (userBorrowData_ >> LiquiditySlotsLink.BITS_USER_BORROW_EXPAND_DURATION) & X24, LiquiditySlotsLink.BITS_USER_BORROW_EXPAND_PERCENT) & X14,
baseDebtCeiling: baseLimit * 1e12 / borrowExchangePrice, expandDuration: (userBorrowData_ >>
maxDebtCeiling: maxLimit * 1e12 / borrowExchangePrice LiquiditySlotsLink.BITS_USER_BORROW_EXPAND_DURATION) & X24,
baseDebtCeiling: (baseLimit * 1e12) / borrowExchangePrice,
maxDebtCeiling: (maxLimit * 1e12) / borrowExchangePrice
}); });
} }
function getAllowance(address token) internal pure returns (uint256, uint256, uint256) { function getAllowance(
address token
) internal pure returns (uint256, uint256, uint256) {
if (token == ETH_ADDRESS) { if (token == ETH_ADDRESS) {
return (3 * 1e18, 4 * 1e18, 0); return (3 * 1e18, 4 * 1e18, 0);
} else if (token == wstETH_ADDRESS) { } else if (token == wstETH_ADDRESS) {
@ -593,11 +622,11 @@ contract PayloadIGP26 {
} else if (token == sUSDe_ADDRESS) { } else if (token == sUSDe_ADDRESS) {
return (9_200 * 1e18, 13_900 * 1e18, 100 * 1e18); return (9_200 * 1e18, 13_900 * 1e18, 100 * 1e18);
} else { } else {
revert ("no allowance found"); revert("no allowance found");
} }
} }
function getOracleAddress(uint256 vaultId) internal pure returns(address) { function getOracleAddress(uint256 vaultId) internal pure returns (address) {
if (vaultId == 11) { if (vaultId == 11) {
return 0x5b2860C6D6F888319C752aaCDaf8165C21095E3a; // VAULT_ETH_USDC return 0x5b2860C6D6F888319C752aaCDaf8165C21095E3a; // VAULT_ETH_USDC
} else if (vaultId == 12) { } else if (vaultId == 12) {
@ -619,17 +648,24 @@ contract PayloadIGP26 {
} else if (vaultId == 20) { } else if (vaultId == 20) {
return 0x32eE0cB3587C6e9f8Ad2a0CF83B6Cf326848b7c6; // VAULT_WEETH_USDT return 0x32eE0cB3587C6e9f8Ad2a0CF83B6Cf326848b7c6; // VAULT_WEETH_USDT
} else { } else {
revert ("no oracle address"); revert("no oracle address");
} }
} }
function getVaultConfig(address vault) internal view returns (IFluidVaultT1.Configs memory configs) { function getVaultConfig(
uint vaultVariables2 = IFluidVaultT1(vault).readFromStorage(bytes32(uint256(1))); address vault
) internal view returns (IFluidVaultT1.Configs memory configs) {
uint vaultVariables2 = IFluidVaultT1(vault).readFromStorage(
bytes32(uint256(1))
);
configs.supplyRateMagnifier = uint16(vaultVariables2 & X16); configs.supplyRateMagnifier = uint16(vaultVariables2 & X16);
configs.borrowRateMagnifier = uint16((vaultVariables2 >> 16) & X16); configs.borrowRateMagnifier = uint16((vaultVariables2 >> 16) & X16);
configs.collateralFactor = (uint16((vaultVariables2 >> 32) & X10)) * 10; configs.collateralFactor = (uint16((vaultVariables2 >> 32) & X10)) * 10;
configs.liquidationThreshold = (uint16((vaultVariables2 >> 42) & X10)) * 10; configs.liquidationThreshold =
configs.liquidationMaxLimit = (uint16((vaultVariables2 >> 52) & X10) * 10); (uint16((vaultVariables2 >> 42) & X10)) *
10;
configs.liquidationMaxLimit = (uint16((vaultVariables2 >> 52) & X10) *
10);
configs.withdrawalGap = uint16((vaultVariables2 >> 62) & X10) * 10; configs.withdrawalGap = uint16((vaultVariables2 >> 62) & X10) * 10;
configs.liquidationPenalty = uint16((vaultVariables2 >> 72) & X10); configs.liquidationPenalty = uint16((vaultVariables2 >> 72) & X10);
configs.borrowFee = uint16((vaultVariables2 >> 82) & X10); configs.borrowFee = uint16((vaultVariables2 >> 82) & X10);
@ -654,19 +690,39 @@ contract PayloadIGP26 {
CloneVaultStruct memory data; CloneVaultStruct memory data;
data.oldVaultAddress = VAULT_T1_FACTORY.getVaultAddress(oldVaultId); data.oldVaultAddress = VAULT_T1_FACTORY.getVaultAddress(oldVaultId);
data.newVaultAddress = VAULT_T1_FACTORY.getVaultAddress(oldVaultId + 10); data.newVaultAddress = VAULT_T1_FACTORY.getVaultAddress(
oldVaultId + 10
);
IFluidVaultT1.ConstantViews memory oldConstants = IFluidVaultT1(data.oldVaultAddress).constantsView(); IFluidVaultT1.ConstantViews memory oldConstants = IFluidVaultT1(
IFluidVaultT1.ConstantViews memory newConstants = IFluidVaultT1(data.newVaultAddress).constantsView(); data.oldVaultAddress
).constantsView();
IFluidVaultT1.ConstantViews memory newConstants = IFluidVaultT1(
data.newVaultAddress
).constantsView();
data.newOracleAddress = getOracleAddress(oldVaultId + 10); data.newOracleAddress = getOracleAddress(oldVaultId + 10);
(data.supplyBaseAllowance, , data.supplyReserveAllowance) = getAllowance(newConstants.supplyToken); (
(data.borrowBaseAllowance, data.borrowMaxAllowance, data.borrowReserveAllowance) = getAllowance(newConstants.borrowToken); data.supplyBaseAllowance,
,
data.supplyReserveAllowance
) = getAllowance(newConstants.supplyToken);
(
data.borrowBaseAllowance,
data.borrowMaxAllowance,
data.borrowReserveAllowance
) = getAllowance(newConstants.borrowToken);
{ {
require(oldConstants.supplyToken == newConstants.supplyToken, "not-same-supply-token"); require(
require(oldConstants.borrowToken == newConstants.borrowToken, "not-same-borrow-token"); oldConstants.supplyToken == newConstants.supplyToken,
"not-same-supply-token"
);
require(
oldConstants.borrowToken == newConstants.borrowToken,
"not-same-borrow-token"
);
} }
// Set user supply config for the vault on Liquidity Layer. // Set user supply config for the vault on Liquidity Layer.
@ -674,7 +730,6 @@ contract PayloadIGP26 {
AdminModuleStructs.UserSupplyConfig[] AdminModuleStructs.UserSupplyConfig[]
memory configs_ = new AdminModuleStructs.UserSupplyConfig[](1); memory configs_ = new AdminModuleStructs.UserSupplyConfig[](1);
configs_[0] = getUserSupplyDataAndSetLimits( configs_[0] = getUserSupplyDataAndSetLimits(
newConstants.supplyToken, newConstants.supplyToken,
data.oldVaultAddress, data.oldVaultAddress,
@ -688,7 +743,7 @@ contract PayloadIGP26 {
// Set user borrow config for the vault on Liquidity Layer. // Set user borrow config for the vault on Liquidity Layer.
{ {
AdminModuleStructs.UserBorrowConfig[] AdminModuleStructs.UserBorrowConfig[]
memory configs_ = new AdminModuleStructs.UserBorrowConfig[](1); memory configs_ = new AdminModuleStructs.UserBorrowConfig[](1);
configs_[0] = getUserBorrowDataAndSetLimits( configs_[0] = getUserBorrowDataAndSetLimits(
newConstants.borrowToken, newConstants.borrowToken,
@ -703,11 +758,19 @@ contract PayloadIGP26 {
// Clone core settings from old vault to new vault. // Clone core settings from old vault to new vault.
{ {
IFluidVaultT1.Configs memory configs = getVaultConfig(data.oldVaultAddress); IFluidVaultT1.Configs memory configs = getVaultConfig(
data.oldVaultAddress
);
{ {
require( require(
IFluidOracle(configs.oracle).getExchangeRate() == IFluidOracle(data.newOracleAddress).getExchangeRate(), (IFluidOracle(configs.oracle).getExchangeRate() ==
IFluidOracle(data.newOracleAddress)
.getExchangeRateOperate()) &&
(IFluidOracle(data.newOracleAddress)
.getExchangeRateOperate() ==
IFluidOracle(data.newOracleAddress)
.getExchangeRateLiquidate()),
"oracle exchangePrice is not same" "oracle exchangePrice is not same"
); );
} }
@ -726,17 +789,24 @@ contract PayloadIGP26 {
// Update oracle on new vault. // Update oracle on new vault.
{ {
IFluidVaultT1(data.newVaultAddress).updateOracle(data.newOracleAddress); IFluidVaultT1(data.newVaultAddress).updateOracle(
data.newOracleAddress
);
} }
// Update rebalancer on new vault. // Update rebalancer on new vault.
{ {
IFluidVaultT1(data.newVaultAddress).updateRebalancer(0x264786EF916af64a1DB19F513F24a3681734ce92); IFluidVaultT1(data.newVaultAddress).updateRebalancer(
0x264786EF916af64a1DB19F513F24a3681734ce92
);
} }
// Approve new vault to spend the reserves dust tokens // Approve new vault to spend the reserves dust tokens
{ {
uint256 len = data.supplyReserveAllowance == 0 || data.borrowReserveAllowance == 0 ? 1 : 2; uint256 len = data.supplyReserveAllowance == 0 ||
data.borrowReserveAllowance == 0
? 1
: 2;
uint256 i = 0; uint256 i = 0;
data.protocols = new address[](len); data.protocols = new address[](len);
@ -757,7 +827,11 @@ contract PayloadIGP26 {
data.amounts[i] = data.borrowReserveAllowance; data.amounts[i] = data.borrowReserveAllowance;
} }
FLUID_RESERVE.approve(data.protocols, data.tokens, data.amounts); FLUID_RESERVE.approve(
data.protocols,
data.tokens,
data.amounts
);
} }
} }
} }