Skip to content

Commit

Permalink
feat: Use SSR oracle, update naming convention (SC-693) (#40)
Browse files Browse the repository at this point in the history
* chre: rm old oracle

* forge install: xchain-ssr-oracle

v1.2.0

* feat: update DSR language

* fix: update param name
  • Loading branch information
lucas-manuel authored Sep 27, 2024
1 parent f574cef commit 1b6384b
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 44 deletions.
6 changes: 3 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
[submodule "lib/erc20-helpers"]
path = lib/erc20-helpers
url = https://github.com/marsfoundation/erc20-helpers
[submodule "lib/xchain-dsr-oracle"]
path = lib/xchain-dsr-oracle
url = https://github.com/marsfoundation/xchain-dsr-oracle
[submodule "lib/openzeppelin-contracts"]
path = lib/openzeppelin-contracts
url = https://github.com/openzeppelin/openzeppelin-contracts
[submodule "lib/xchain-ssr-oracle"]
path = lib/xchain-ssr-oracle
url = https://github.com/marsfoundation/xchain-ssr-oracle
1 change: 0 additions & 1 deletion lib/xchain-dsr-oracle
Submodule xchain-dsr-oracle deleted from a02e59
1 change: 1 addition & 0 deletions lib/xchain-ssr-oracle
Submodule xchain-ssr-oracle added at dc9fc3
2 changes: 1 addition & 1 deletion test/PSMTestBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ contract PSMTestBase is Test {
MockERC20 public usdc;
MockERC20 public susds;

IRateProviderLike public rateProvider; // Can be overridden by dsrOracle using same interface
IRateProviderLike public rateProvider; // Can be overridden by ssrOracle using same interface

MockRateProvider public mockRateProvider; // Interface used for mocking

Expand Down
46 changes: 23 additions & 23 deletions test/invariant/Invariants.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ pragma solidity ^0.8.13;

import "forge-std/Test.sol";

import { DSRAuthOracle } from "lib/xchain-dsr-oracle/src/DSRAuthOracle.sol";
import { IDSROracle } from "lib/xchain-dsr-oracle/src/interfaces/IDSROracle.sol";
import { SSRAuthOracle } from "lib/xchain-ssr-oracle/src/SSRAuthOracle.sol";
import { ISSROracle } from "lib/xchain-ssr-oracle/src/interfaces/ISSROracle.sol";

import { PSM3 } from "src/PSM3.sol";

Expand Down Expand Up @@ -541,36 +541,36 @@ contract PSMInvariants_TimeBasedRateSetting_NoTransfer is PSMInvariantTestBase {
function setUp() public override {
super.setUp();

DSRAuthOracle dsrOracle = new DSRAuthOracle();
SSRAuthOracle ssrOracle = new SSRAuthOracle();

// Workaround to initialize PSM with an oracle that does not return zero
// This gets overwritten by the handler
dsrOracle.grantRole(dsrOracle.DATA_PROVIDER_ROLE(), address(this));
dsrOracle.setPotData(IDSROracle.PotData({
dsr: uint96(1e27),
ssrOracle.grantRole(ssrOracle.DATA_PROVIDER_ROLE(), address(this));
ssrOracle.setSUSDSData(ISSROracle.SUSDSData({
ssr: uint96(1e27),
chi: uint120(1e27),
rho: uint40(block.timestamp)
}));
dsrOracle.revokeRole(dsrOracle.DATA_PROVIDER_ROLE(), address(this));
ssrOracle.revokeRole(ssrOracle.DATA_PROVIDER_ROLE(), address(this));

// Redeploy PSM with new rate provider
psm = new PSM3(owner, address(usdc), address(usds), address(susds), address(dsrOracle));
psm = new PSM3(owner, address(usdc), address(usds), address(susds), address(ssrOracle));

// Seed the new PSM with 1e18 shares (1e18 of value)
_deposit(address(usds), BURN_ADDRESS, 1e18);

lpHandler = new LpHandler(psm, usdc, usds, susds, 3);
swapperHandler = new SwapperHandler(psm, usdc, usds, susds, 3);
timeBasedRateHandler = new TimeBasedRateHandler(psm, dsrOracle);
timeBasedRateHandler = new TimeBasedRateHandler(psm, ssrOracle);

// Handler acts in the same way as a receiver on L2, so add as a data provider to the
// oracle.
dsrOracle.grantRole(dsrOracle.DATA_PROVIDER_ROLE(), address(timeBasedRateHandler));
ssrOracle.grantRole(ssrOracle.DATA_PROVIDER_ROLE(), address(timeBasedRateHandler));

rateProvider = IRateProviderLike(address(dsrOracle));
rateProvider = IRateProviderLike(address(ssrOracle));

// Manually set initial values for the oracle through the handler to start
timeBasedRateHandler.setPotData(1e27);
timeBasedRateHandler.setSUSDSData(1e27);

targetContract(address(lpHandler));
targetContract(address(swapperHandler));
Expand All @@ -580,7 +580,7 @@ contract PSMInvariants_TimeBasedRateSetting_NoTransfer is PSMInvariantTestBase {
assertEq(swapperHandler.lp0(), lpHandler.lps(0));
}

function invariant_A_test() public view {
function invariant_A() public view {
_checkInvariant_A();
}

Expand Down Expand Up @@ -620,37 +620,37 @@ contract PSMInvariants_TimeBasedRateSetting_WithTransfers is PSMInvariantTestBas
function setUp() public override {
super.setUp();

DSRAuthOracle dsrOracle = new DSRAuthOracle();
SSRAuthOracle ssrOracle = new SSRAuthOracle();

// Workaround to initialize PSM with an oracle that does not return zero
// This gets overwritten by the handler
dsrOracle.grantRole(dsrOracle.DATA_PROVIDER_ROLE(), address(this));
dsrOracle.setPotData(IDSROracle.PotData({
dsr: uint96(1e27),
ssrOracle.grantRole(ssrOracle.DATA_PROVIDER_ROLE(), address(this));
ssrOracle.setSUSDSData(ISSROracle.SUSDSData({
ssr: uint96(1e27),
chi: uint120(1e27),
rho: uint40(block.timestamp)
}));
dsrOracle.revokeRole(dsrOracle.DATA_PROVIDER_ROLE(), address(this));
ssrOracle.revokeRole(ssrOracle.DATA_PROVIDER_ROLE(), address(this));

// Redeploy PSM with new rate provider
psm = new PSM3(owner, address(usdc), address(usds), address(susds), address(dsrOracle));
psm = new PSM3(owner, address(usdc), address(usds), address(susds), address(ssrOracle));

// Seed the new PSM with 1e18 shares (1e18 of value)
_deposit(address(usds), BURN_ADDRESS, 1e18);

lpHandler = new LpHandler(psm, usdc, usds, susds, 3);
swapperHandler = new SwapperHandler(psm, usdc, usds, susds, 3);
timeBasedRateHandler = new TimeBasedRateHandler(psm, dsrOracle);
timeBasedRateHandler = new TimeBasedRateHandler(psm, ssrOracle);
transferHandler = new TransferHandler(psm, usdc, usds, susds);

// Handler acts in the same way as a receiver on L2, so add as a data provider to the
// oracle.
dsrOracle.grantRole(dsrOracle.DATA_PROVIDER_ROLE(), address(timeBasedRateHandler));
ssrOracle.grantRole(ssrOracle.DATA_PROVIDER_ROLE(), address(timeBasedRateHandler));

rateProvider = IRateProviderLike(address(dsrOracle));
rateProvider = IRateProviderLike(address(ssrOracle));

// Manually set initial values for the oracle through the handler to start
timeBasedRateHandler.setPotData(1e27);
timeBasedRateHandler.setSUSDSData(1e27);

targetContract(address(lpHandler));
targetContract(address(swapperHandler));
Expand Down
32 changes: 16 additions & 16 deletions test/invariant/handlers/TimeBasedRateHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,40 @@ import { HandlerBase, PSM3 } from "test/invariant/handlers/HandlerBase.sol";

import { StdCheats } from "forge-std/StdCheats.sol";

import { DSRAuthOracle } from "lib/xchain-dsr-oracle/src/DSRAuthOracle.sol";
import { IDSROracle } from "lib/xchain-dsr-oracle/src/interfaces/IDSROracle.sol";
import { SSRAuthOracle } from "lib/xchain-ssr-oracle/src/SSRAuthOracle.sol";
import { ISSROracle } from "lib/xchain-ssr-oracle/src/interfaces/ISSROracle.sol";

contract TimeBasedRateHandler is HandlerBase, StdCheats {

uint256 public dsr;
uint256 public ssr;

uint256 constant TWENTY_PCT_APY_DSR = 1.000000005781378656804591712e27;
uint256 constant TWENTY_PCT_APY_SSR = 1.000000005781378656804591712e27;

DSRAuthOracle public dsrOracle;
SSRAuthOracle public ssrOracle;

uint256 public setPotDataCount;
uint256 public setSUSDSDataCount;
uint256 public warpCount;

constructor(PSM3 psm_, DSRAuthOracle dsrOracle_) HandlerBase(psm_) {
dsrOracle = dsrOracle_;
constructor(PSM3 psm_, SSRAuthOracle ssrOracle_) HandlerBase(psm_) {
ssrOracle = ssrOracle_;
}

// This acts as a receiver on an L2.
function setPotData(uint256 newDsr) external {
function setSUSDSData(uint256 newSsr) external {
// 1. Setup and bounds
dsr = _bound(newDsr, 1e27, TWENTY_PCT_APY_DSR);
ssr = _bound(newSsr, 1e27, TWENTY_PCT_APY_SSR);

// Update rho to be current, update chi based on current rate
uint256 rho = block.timestamp;
uint256 chi = dsrOracle.getConversionRate(rho);
uint256 chi = ssrOracle.getConversionRate(rho);

// 2. Cache starting state
uint256 startingConversion = psm.convertToAssetValue(1e18);
uint256 startingValue = psm.totalAssets();

// 3. Perform action against protocol
dsrOracle.setPotData(IDSROracle.PotData({
dsr: uint96(dsr),
ssrOracle.setSUSDSData(ISSROracle.SUSDSData({
ssr: uint96(ssr),
chi: uint120(chi),
rho: uint40(rho)
}));
Expand All @@ -47,17 +47,17 @@ contract TimeBasedRateHandler is HandlerBase, StdCheats {
assertGe(
psm.convertToAssetValue(1e18) + 1,
startingConversion,
"TimeBasedRateHandler/setPotData/conversion-rate-decrease"
"TimeBasedRateHandler/getSUSDSData/conversion-rate-decrease"
);

assertGe(
psm.totalAssets() + 1,
startingValue,
"TimeBasedRateHandler/setPotData/psm-total-value-decrease"
"TimeBasedRateHandler/getSUSDSData/psm-total-value-decrease"
);

// 5. Update metrics tracking state
setPotDataCount++;
setSUSDSDataCount++;
}

function warp(uint256 skipTime) external {
Expand Down

0 comments on commit 1b6384b

Please sign in to comment.