Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
32 changes: 16 additions & 16 deletions snapshots/Hub.Operations.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"add": "86660",
"add: with transfer": "107957",
"draw": "104177",
"eliminateDeficit: full": "72422",
"eliminateDeficit: partial": "82027",
"mintFeeShares": "82721",
"payFee": "70834",
"refreshPremium": "70391",
"remove: full": "75451",
"remove: partial": "80589",
"reportDeficit": "111899",
"restore: full": "76569",
"restore: full - with transfer": "169178",
"restore: partial": "85291",
"restore: partial - with transfer": "143271",
"transferShares": "69648"
"add": "86654",
"add: with transfer": "107951",
"draw": "104159",
"eliminateDeficit: full": "72416",
"eliminateDeficit: partial": "82021",
"mintFeeShares": "82703",
"payFee": "70816",
"refreshPremium": "70373",
"remove: full": "75445",
"remove: partial": "80583",
"reportDeficit": "111893",
"restore: full": "76563",
"restore: full - with transfer": "169172",
"restore: partial": "85273",
"restore: partial - with transfer": "143253",
"transferShares": "69630"
}
12 changes: 6 additions & 6 deletions snapshots/NativeTokenGateway.Operations.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"borrowNative": "227759",
"repayNative": "166193",
"supplyAsCollateralNative": "160052",
"supplyNative": "135728",
"withdrawNative: full": "125393",
"withdrawNative: partial": "136541"
"borrowNative": "228479",
"repayNative": "166471",
"supplyAsCollateralNative": "160046",
"supplyNative": "135723",
"withdrawNative: full": "125388",
"withdrawNative: partial": "136535"
}
12 changes: 6 additions & 6 deletions snapshots/SignatureGateway.Operations.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"borrowWithSig": "212949",
"repayWithSig": "186465",
"borrowWithSig": "213712",
"repayWithSig": "186743",
"setSelfAsUserPositionManagerWithSig": "75385",
"setUsingAsCollateralWithSig": "85349",
"supplyWithSig": "151960",
"updateUserDynamicConfigWithSig": "63086",
"updateUserRiskPremiumWithSig": "62056",
"withdrawWithSig": "130648"
"supplyWithSig": "151955",
"updateUserDynamicConfigWithSig": "63111",
"updateUserRiskPremiumWithSig": "62081",
"withdrawWithSig": "130643"
}
10 changes: 5 additions & 5 deletions snapshots/Spoke.Getters.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"getUserAccountData: supplies: 0, borrows: 0": "12962",
"getUserAccountData: supplies: 1, borrows: 0": "49251",
"getUserAccountData: supplies: 2, borrows: 0": "80826",
"getUserAccountData: supplies: 2, borrows: 1": "100237",
"getUserAccountData: supplies: 2, borrows: 2": "118994"
"getUserAccountData: supplies: 0, borrows: 0": "12987",
"getUserAccountData: supplies: 1, borrows: 0": "49318",
"getUserAccountData: supplies: 2, borrows: 0": "80914",
"getUserAccountData: supplies: 2, borrows: 1": "101283",
"getUserAccountData: supplies: 2, borrows: 2": "120512"
}
52 changes: 26 additions & 26 deletions snapshots/Spoke.Operations.ZeroRiskPremium.json
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
{
"borrow: first": "189363",
"borrow: second action, same reserve": "169312",
"liquidationCall (receiveShares): full": "295052",
"liquidationCall (receiveShares): partial": "294770",
"liquidationCall: full": "304431",
"liquidationCall: partial": "304149",
"permitReserve + repay (multicall)": "164456",
"permitReserve + supply (multicall)": "146735",
"permitReserve + supply + enable collateral (multicall)": "161148",
"repay: full": "123772",
"repay: partial": "128742",
"borrow: first": "190151",
"borrow: second action, same reserve": "170100",
"liquidationCall (receiveShares): full": "302258",
"liquidationCall (receiveShares): partial": "301676",
"liquidationCall: full": "319683",
"liquidationCall: partial": "319101",
"permitReserve + repay (multicall)": "164598",
"permitReserve + supply (multicall)": "146729",
"permitReserve + supply + enable collateral (multicall)": "161142",
"repay: full": "123914",
"repay: partial": "128872",
"setUserPositionManagersWithSig: disable": "47039",
"setUserPositionManagersWithSig: enable": "68951",
"supply + enable collateral (multicall)": "141328",
"supply: 0 borrows, collateral disabled": "122803",
"supply: 0 borrows, collateral enabled": "105774",
"supply: second action, same reserve": "105703",
"updateUserDynamicConfig: 1 collateral": "74548",
"updateUserDynamicConfig: 2 collaterals": "89453",
"updateUserRiskPremium: 1 borrow": "94781",
"updateUserRiskPremium: 2 borrows": "103916",
"supply + enable collateral (multicall)": "141322",
"supply: 0 borrows, collateral disabled": "122797",
"supply: 0 borrows, collateral enabled": "105768",
"supply: second action, same reserve": "105697",
"updateUserDynamicConfig: 1 collateral": "74536",
"updateUserDynamicConfig: 2 collaterals": "89404",
"updateUserRiskPremium: 1 borrow": "95584",
"updateUserRiskPremium: 2 borrows": "105233",
"usingAsCollateral: 0 borrows, enable": "59578",
"usingAsCollateral: 1 borrow, disable": "104896",
"usingAsCollateral: 1 borrow, disable": "105702",
"usingAsCollateral: 1 borrow, enable": "42466",
"usingAsCollateral: 2 borrows, disable": "125992",
"usingAsCollateral: 2 borrows, disable": "127220",
"usingAsCollateral: 2 borrows, enable": "42478",
"withdraw: 0 borrows, full": "127753",
"withdraw: 0 borrows, partial": "132439",
"withdraw: 1 borrow, partial": "158821",
"withdraw: 2 borrows, partial": "172927",
"withdraw: non collateral": "105709"
"withdraw: 0 borrows, full": "127735",
"withdraw: 0 borrows, partial": "132550",
"withdraw: 1 borrow, partial": "159621",
"withdraw: 2 borrows, partial": "174148",
"withdraw: non collateral": "105691"
}
52 changes: 26 additions & 26 deletions snapshots/Spoke.Operations.json
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
{
"borrow: first": "258289",
"borrow: second action, same reserve": "201238",
"liquidationCall (receiveShares): full": "327083",
"liquidationCall (receiveShares): partial": "326801",
"liquidationCall: full": "336462",
"liquidationCall: partial": "336180",
"permitReserve + repay (multicall)": "161948",
"permitReserve + supply (multicall)": "146735",
"permitReserve + supply + enable collateral (multicall)": "161148",
"repay: full": "117851",
"repay: partial": "137221",
"borrow: first": "259059",
"borrow: second action, same reserve": "202008",
"liquidationCall (receiveShares): full": "334275",
"liquidationCall (receiveShares): partial": "333693",
"liquidationCall: full": "351700",
"liquidationCall: partial": "351118",
"permitReserve + repay (multicall)": "162062",
"permitReserve + supply (multicall)": "146729",
"permitReserve + supply + enable collateral (multicall)": "161142",
"repay: full": "117993",
"repay: partial": "137351",
"setUserPositionManagersWithSig: disable": "47039",
"setUserPositionManagersWithSig: enable": "68951",
"supply + enable collateral (multicall)": "141328",
"supply: 0 borrows, collateral disabled": "122803",
"supply: 0 borrows, collateral enabled": "105774",
"supply: second action, same reserve": "105703",
"updateUserDynamicConfig: 1 collateral": "74548",
"updateUserDynamicConfig: 2 collaterals": "89453",
"updateUserRiskPremium: 1 borrow": "148128",
"updateUserRiskPremium: 2 borrows": "197859",
"supply + enable collateral (multicall)": "141322",
"supply: 0 borrows, collateral disabled": "122797",
"supply: 0 borrows, collateral enabled": "105768",
"supply: second action, same reserve": "105697",
"updateUserDynamicConfig: 1 collateral": "74536",
"updateUserDynamicConfig: 2 collaterals": "89404",
"updateUserRiskPremium: 1 borrow": "148916",
"updateUserRiskPremium: 2 borrows": "199144",
"usingAsCollateral: 0 borrows, enable": "59578",
"usingAsCollateral: 1 borrow, disable": "158243",
"usingAsCollateral: 1 borrow, disable": "159031",
"usingAsCollateral: 1 borrow, enable": "42466",
"usingAsCollateral: 2 borrows, disable": "227935",
"usingAsCollateral: 2 borrows, disable": "229127",
"usingAsCollateral: 2 borrows, enable": "42478",
"withdraw: 0 borrows, full": "127753",
"withdraw: 0 borrows, partial": "132439",
"withdraw: 1 borrow, partial": "209666",
"withdraw: 2 borrows, partial": "255403",
"withdraw: non collateral": "105709"
"withdraw: 0 borrows, full": "127735",
"withdraw: 0 borrows, partial": "132550",
"withdraw: 1 borrow, partial": "210448",
"withdraw: 2 borrows, partial": "256590",
"withdraw: non collateral": "105691"
}
84 changes: 58 additions & 26 deletions src/hub/Hub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {PercentageMath} from 'src/libraries/math/PercentageMath.sol';
import {WadRayMath} from 'src/libraries/math/WadRayMath.sol';
import {AssetLogic} from 'src/hub/libraries/AssetLogic.sol';
import {SharesMath} from 'src/hub/libraries/SharesMath.sol';
import {Premium} from 'src/hub/libraries/Premium.sol';
import {IOU} from 'src/hub/libraries/IOU.sol';
import {IBasicInterestRateStrategy} from 'src/hub/interfaces/IBasicInterestRateStrategy.sol';
import {IHubBase, IHub} from 'src/hub/interfaces/IHub.sol';

Expand Down Expand Up @@ -306,15 +306,23 @@ contract Hub is IHub, AccessManaged {
spoke.drawnShares -= drawnShares;
_applyPremiumDelta(asset, spoke, premiumDelta);

uint256 premiumAmount = premiumDelta.restoredPremiumRay.fromRayUp();
uint256 liquidity = asset.liquidity + drawnAmount + premiumAmount;
uint256 restoredRay = IOU.calculateDrawnRay(drawnShares, asset.drawnIndex) +
premiumDelta.restoredPremiumRay;
uint256 liquidity = asset.liquidity + restoredRay.fromRayUp();
uint256 balance = IERC20(asset.underlying).balanceOf(address(this));
require(balance >= liquidity, InsufficientTransferred(liquidity.uncheckedSub(balance)));
asset.liquidity = liquidity.toUint120();

asset.updateDrawnRate(assetId);

emit Restore(assetId, msg.sender, drawnShares, premiumDelta, drawnAmount, premiumAmount);
emit Restore(
assetId,
msg.sender,
drawnShares,
premiumDelta,
drawnAmount,
premiumDelta.restoredPremiumRay.fromRayUp()
);

return drawnShares;
}
Expand All @@ -336,7 +344,7 @@ contract Hub is IHub, AccessManaged {
spoke.drawnShares -= drawnShares;
_applyPremiumDelta(asset, spoke, premiumDelta);

uint256 deficitAmountRay = uint256(drawnShares) * asset.drawnIndex +
uint256 deficitAmountRay = IOU.calculateDrawnRay(drawnShares, asset.drawnIndex) +
premiumDelta.restoredPremiumRay;
asset.deficitRay += deficitAmountRay.toUint200();
spoke.deficitRay += deficitAmountRay.toUint200();
Expand Down Expand Up @@ -541,20 +549,32 @@ contract Hub is IHub, AccessManaged {
function getAssetOwed(uint256 assetId) external view returns (uint256, uint256) {
Asset storage asset = _assets[assetId];
uint256 drawnIndex = asset.getDrawnIndex();
return (asset.drawn(drawnIndex), asset.premium(drawnIndex));
uint256 premium = IOU
.calculatePremiumRay(asset.premiumShares, asset.premiumOffsetRay, drawnIndex)
.fromRayUp();
return (asset.getAssetDrawn(drawnIndex), premium);
}

/// @inheritdoc IHubBase
function getAssetTotalOwed(uint256 assetId) external view returns (uint256) {
Asset storage asset = _assets[assetId];
return asset.totalOwed(asset.getDrawnIndex());
return
IOU
.calculateTotalOwedRay({
drawnShares: asset.drawnShares,
premiumShares: asset.premiumShares,
premiumOffsetRay: asset.premiumOffsetRay,
deficitRay: asset.deficitRay,
drawnIndex: asset.getDrawnIndex()
})
.fromRayUp();
}

/// @inheritdoc IHubBase
function getAssetPremiumRay(uint256 assetId) external view returns (uint256) {
Asset storage asset = _assets[assetId];
return
Premium.calculatePremiumRay({
IOU.calculatePremiumRay({
premiumShares: asset.premiumShares,
premiumOffsetRay: asset.premiumOffsetRay,
drawnIndex: asset.getDrawnIndex()
Expand Down Expand Up @@ -634,14 +654,28 @@ contract Hub is IHub, AccessManaged {
function getSpokeOwed(uint256 assetId, address spoke) external view returns (uint256, uint256) {
Asset storage asset = _assets[assetId];
SpokeData storage spokeData = _spokes[assetId][spoke];
return (_getSpokeDrawn(asset, spokeData), _getSpokePremium(asset, spokeData));
uint256 drawnIndex = asset.getDrawnIndex();
uint256 drawn = IOU.calculateDrawnRay(spokeData.drawnShares, drawnIndex).fromRayUp();
uint256 premium = IOU
.calculatePremiumRay(spokeData.premiumShares, spokeData.premiumOffsetRay, drawnIndex)
.fromRayUp();
return (drawn, premium);
}

/// @inheritdoc IHubBase
function getSpokeTotalOwed(uint256 assetId, address spoke) external view returns (uint256) {
Asset storage asset = _assets[assetId];
SpokeData storage spokeData = _spokes[assetId][spoke];
return _getSpokeDrawn(asset, spokeData) + _getSpokePremium(asset, spokeData);
return
IOU
.calculateTotalOwedRay({
drawnShares: spokeData.drawnShares,
premiumShares: spokeData.premiumShares,
premiumOffsetRay: spokeData.premiumOffsetRay,
deficitRay: spokeData.deficitRay,
drawnIndex: asset.getDrawnIndex()
})
.fromRayUp();
}

/// @inheritdoc IHubBase
Expand Down Expand Up @@ -798,20 +832,11 @@ contract Hub is IHub, AccessManaged {
return shares;
}

/// @dev Returns the spoke's drawn amount for a specified asset.
function _getSpokeDrawn(
Asset storage asset,
SpokeData storage spoke
) internal view returns (uint256) {
return asset.toDrawnAssetsUp(spoke.drawnShares);
}

/// @dev Returns the spoke's premium amount for a specified asset.
function _getSpokePremium(
Asset storage asset,
SpokeData storage spoke
) internal view returns (uint256) {
return _getSpokePremiumRay(asset, spoke).fromRayUp();
return IOU.calculateDrawnRay(spoke.drawnShares, asset.getDrawnIndex()).fromRayUp();
}

/// @dev Returns the spoke's premium amount with full precision for a specified asset.
Expand All @@ -820,7 +845,7 @@ contract Hub is IHub, AccessManaged {
SpokeData storage spoke
) internal view returns (uint256) {
return
Premium.calculatePremiumRay({
IOU.calculatePremiumRay({
premiumShares: spoke.premiumShares,
premiumOffsetRay: spoke.premiumOffsetRay,
drawnIndex: asset.getDrawnIndex()
Expand Down Expand Up @@ -864,11 +889,18 @@ contract Hub is IHub, AccessManaged {
require(spoke.active, SpokeNotActive());
require(!spoke.halted, SpokeHalted());
uint256 drawCap = spoke.drawCap;
uint256 owed = _getSpokeDrawn(asset, spoke) + _getSpokePremium(asset, spoke);
uint256 owed = IOU
.calculateTotalOwedRay({
drawnShares: spoke.drawnShares,
premiumShares: spoke.premiumShares,
premiumOffsetRay: spoke.premiumOffsetRay,
deficitRay: spoke.deficitRay,
drawnIndex: asset.getDrawnIndex()
})
.fromRayUp();
require(
drawCap == MAX_ALLOWED_SPOKE_CAP ||
drawCap * MathUtils.uncheckedExp(10, asset.decimals) >=
owed + amount + uint256(spoke.deficitRay).fromRayUp(),
drawCap * MathUtils.uncheckedExp(10, asset.decimals) >= owed + amount,
DrawCapExceeded(drawCap)
);
}
Expand Down Expand Up @@ -952,7 +984,7 @@ contract Hub is IHub, AccessManaged {
int256 premiumOffsetRay,
PremiumDelta calldata premiumDelta
) internal pure returns (uint120, int200) {
uint256 premiumRayBefore = Premium.calculatePremiumRay({
uint256 premiumRayBefore = IOU.calculatePremiumRay({
premiumShares: premiumShares,
premiumOffsetRay: premiumOffsetRay,
drawnIndex: drawnIndex
Expand All @@ -961,7 +993,7 @@ contract Hub is IHub, AccessManaged {
uint256 newPremiumShares = premiumShares.add(premiumDelta.sharesDelta);
int256 newPremiumOffsetRay = premiumOffsetRay + premiumDelta.offsetRayDelta;

uint256 premiumRayAfter = Premium.calculatePremiumRay({
uint256 premiumRayAfter = IOU.calculatePremiumRay({
premiumShares: newPremiumShares,
premiumOffsetRay: newPremiumOffsetRay,
drawnIndex: drawnIndex
Expand Down
Loading
Loading