Skip to content

Commit

Permalink
add compatible adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
eukadish committed Nov 23, 2024
1 parent f263565 commit 04def9d
Showing 1 changed file with 42 additions and 25 deletions.
67 changes: 42 additions & 25 deletions src/adapters/EthenaUSDEAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ pragma solidity ^0.8.24;

import {AccessControl} from "openzeppelin-contracts/contracts/access/AccessControl.sol";

import {IERC4626} from "openzeppelin-contracts/contracts/interfaces/IERC4626.sol";
import {IERC20} from "openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
import {IERC4626} from "openzeppelin-contracts/contracts/interfaces/IERC4626.sol";

// import {Stablecoin} from "../Stablecoin.sol";

import {Stablecoin} from "../Stablecoin.sol";
import {IOracle} from "src/interfaces/IOracle.sol";

import {IAssetAdapter} from "src/interfaces/IAssetAdapter.sol";

contract EthenaSUSDEAdapter is IAssetAdapter, AccessControl {
Expand All @@ -18,41 +20,59 @@ contract EthenaSUSDEAdapter is IAssetAdapter, AccessControl {
bytes32 public constant CONTROLLER =
keccak256(abi.encode("asset.adapter.controller"));

IERC4626 public immutable fund;
IERC20 public immutable underlying;
IERC4626 public immutable vault;

address public immutable holder;

uint256 public immutable duration;

IOracle public immutable underlyingPriceOracle;
IOracle public immutable fundPriceOracle;
IOracle public immutable underlyingPriceOracle;

uint256 public underlyingRiskWeight; // 100% = 1e6
uint256 public fundRiskWeight; // 100% = 1e6
uint256 public underlyingRiskWeight; // 100% = 1e6

constructor(
address _admin,
address _holder,
address _fundAddr,
address _underlyingAddr,
address _vaultAddr,
// address _underlyingPriceOracleAddr,
// address _fundPriceOracleAddr,
uint256 _duration
) {
_grantRole(DEFAULT_ADMIN_ROLE, _admin);

fund = IERC4626(_fundAddr);
underlying = IERC20(_underlyingAddr);
vault = IERC4626(_vaultAddr);

holder = _holder;

duration = _duration;
}

underlyingPriceOracle = IOracle(_underlyingPriceOracleAddr);
fundPriceOracle = IOracle(_fundPriceOracleAddr);
function allocate(uint256 _assets) external {
underlying.transferFrom(msg.sender, address(this), _assets);

emit Allocate(msg.sender, _assets, block.timestamp);
}

function withdraw(uint256 _assets) external {
underlying.transfer(msg.sender, _assets);

emit Withdraw(msg.sender, _assets, block.timestamp);
}

function allocate(uint256) external {}
function deposit(uint256 amount) public onlyRole(CONTROLLER) {
// TODO: Replace with swap and deposit

function withdraw(uint256) external {}
underlying.transfer(holder, amount);

function deposit(uint256) external {}
emit Deposit(msg.sender, amount, block.timestamp);
}

function redeem(uint256) external {}
function redeem(uint256) public onlyRole(CONTROLLER) {
// TODO: Replace with redeem and swap
}

function setUnderlyingRiskWeight(
uint256 _riskWeight
Expand All @@ -73,13 +93,13 @@ contract EthenaSUSDEAdapter is IAssetAdapter, AccessControl {
}

function totalValue() external view returns (uint256 total) {
total += _underlyingTotalValue();
total += _fundTotalValue();
total += _underlyingTotalValue();
}

function totalRiskValue() external view returns (uint256 total) {
total += _underlyingTotalRiskValue();
total += _fundTotalRiskValue();
total += _underlyingTotalRiskValue();
}

function underlyingTotalRiskValue() external view returns (uint256) {
Expand Down Expand Up @@ -156,31 +176,28 @@ contract EthenaSUSDEAdapter is IAssetAdapter, AccessControl {

function _fundValue(uint256 amount) private view returns (uint256) {
// return (_fundPriceOracleLatestAnswer() * amount) / 1e8;
return vault.previewDeposit(underlying.totalBalance(address(this)));
// return fund.previewDeposit(underlying.balanceOf(holder)); // wrong underlying
return fund.previewRedeem(amount); // wrong underlying
}

function fundBalance() external view returns (uint256) {
return _fundBalance();
}

function _fundBalance() private view returns (uint256) {
return vault.balanceOf(address(this));
return fund.balanceOf(holder);
}

function _underlyingPriceOracleLatestAnswer()
private
view
returns (uint256)
{
int256 latestAnswer = underlyingPriceOracle.latestAnswer();

return latestAnswer > 0 ? uint256(latestAnswer) : 0;
return 1e8;
}

function _fundPriceOracleLatestAnswer() private view returns (uint256) {
int256 latestAnswer = fundPriceOracle.latestAnswer();

return latestAnswer > 0 ? uint256(latestAnswer) : 0;
return fund.previewRedeem(1e18);
}

function recover(address _token) external onlyRole(MANAGER) {
Expand Down

0 comments on commit 04def9d

Please sign in to comment.