Skip to content

Commit b582756

Browse files
test: Price change affecting rp
1 parent 996ae7c commit b582756

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

snapshots/Spoke.Operations.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"supply: 1 debt": "170331",
88
"supply: 2 debt": "170342",
99
"supply: 3 debt": "170295",
10-
"usingAsCollateral": "50661",
10+
"usingAsCollateral": "50683",
1111
"withdraw: full": "215505",
1212
"withdraw: partial": "226456"
1313
}

tests/Base.t.sol

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {IERC20} from 'src/dependencies/openzeppelin/IERC20.sol';
2626
import {WETH9} from 'src/dependencies/weth/WETH9.sol';
2727

2828
abstract contract Base is Test {
29+
using WadRayMath for uint256;
2930
using WadRayMathExtended for uint256;
3031
using SharesMath for uint256;
3132
using PercentageMath for uint256;

tests/unit/Spoke/Spoke.RiskPremium.EdgeCases.t.sol

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,4 +1068,73 @@ contract SpokeRiskPremiumEdgeCasesTest is SpokeBase {
10681068
'Bob user risk premium after supplying lower LP collateral'
10691069
);
10701070
}
1071+
1072+
/// Initially debt is covered by 2 collaterals, then due to price change, debt is covered by 1 collateral
1073+
function test_riskPremium_priceChangeReducesRP(uint256 daiSupplyAmount, uint256 newPrice) public {
1074+
daiSupplyAmount = bound(daiSupplyAmount, 1e18, MAX_SUPPLY_AMOUNT);
1075+
uint256 startingPrice = spoke2.getReservePrice(_daiReserveId(spoke2));
1076+
newPrice = bound(newPrice, startingPrice + 1, 1e16);
1077+
1078+
// Supply dai and dai2 collaterals to cover weth debt. Dai increases in price to fully cover weth debt
1079+
uint256 dai2SupplyAmount = MAX_SUPPLY_AMOUNT;
1080+
uint256 borrowAmount = _calcEquivalentAssetAmount(daiAssetId, daiSupplyAmount, wethAssetId) + 1; // Borrow more than dai supply value so 2 collaterals cover debt
1081+
1082+
// Deploy liquidity for weth borrow
1083+
_deployLiquidity(spoke2, _wethReserveId(spoke2), MAX_SUPPLY_AMOUNT);
1084+
1085+
// Deal bob dai to cover dai and dai2 supply
1086+
deal(address(tokenList.dai), bob, MAX_SUPPLY_AMOUNT * 2);
1087+
1088+
// Bob supplies dai and dai2 collaterals
1089+
Utils.supplyCollateral({
1090+
spoke: spoke2,
1091+
reserveId: _daiReserveId(spoke2),
1092+
user: bob,
1093+
amount: daiSupplyAmount,
1094+
onBehalfOf: bob
1095+
});
1096+
Utils.supplyCollateral({
1097+
spoke: spoke2,
1098+
reserveId: _dai2ReserveId(spoke2),
1099+
user: bob,
1100+
amount: dai2SupplyAmount,
1101+
onBehalfOf: bob
1102+
});
1103+
1104+
// Bob borrows weth
1105+
Utils.borrow({
1106+
spoke: spoke2,
1107+
reserveId: _wethReserveId(spoke2),
1108+
user: bob,
1109+
amount: borrowAmount,
1110+
onBehalfOf: bob
1111+
});
1112+
1113+
// Bob's current risk premium should be greater than or equal to liquidity premium of dai, since debt is not fully covered by it (and due to rounding)
1114+
assertLt(
1115+
_getValueInBaseCurrency(daiAssetId, daiSupplyAmount),
1116+
_getValueInBaseCurrency(wethAssetId, borrowAmount),
1117+
'Bob dai collateral less than weth debt'
1118+
);
1119+
assertGe(
1120+
spoke2.getUserRiskPremium(bob),
1121+
spoke2.getLiquidityPremium(_daiReserveId(spoke2)),
1122+
'Bob user rp greater than or equal dai lp'
1123+
);
1124+
1125+
// Now change the price of dai
1126+
oracle.setAssetPrice(daiAssetId, newPrice);
1127+
1128+
// Now risk premium should equal LP of dai since debt is fully covered by it
1129+
assertGe(
1130+
_getValueInBaseCurrency(daiAssetId, daiSupplyAmount),
1131+
_getValueInBaseCurrency(wethAssetId, borrowAmount),
1132+
'Bob dai collateral greater than weth debt'
1133+
);
1134+
assertEq(
1135+
spoke2.getUserRiskPremium(bob),
1136+
spoke2.getLiquidityPremium(_daiReserveId(spoke2)),
1137+
'Bob user risk premium matches dai lp after price change'
1138+
);
1139+
}
10711140
}

0 commit comments

Comments
 (0)