Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chainlink oracle contract #400

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions src/ERC7540Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -409,12 +409,12 @@ contract ERC7540Vault is Auth, IERC7540Vault {
}

// --- Helpers ---
/// @notice Price of 1 unit of share, quoted in the decimals of the asset.
/// @inheritdoc IERC7540Vault
function pricePerShare() external view returns (uint256) {
return convertToAssets(10 ** _shareDecimals);
}

/// @notice Returns timestamp of the last share price update.
/// @inheritdoc IERC7540Vault
function priceLastUpdated() external view returns (uint64) {
return manager.priceLastUpdated(address(this));
}
Expand Down
65 changes: 65 additions & 0 deletions src/factories/AggregatorV3OracleFactory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.20;

import {IAggregatorV3} from "src/interfaces/factories/IAggregatorV3.sol";
import {Auth} from "src/Auth.sol";
import {IERC20Metadata} from "src/interfaces/IERC20.sol";
import {IERC7540Vault} from "src/interfaces/IERC7540.sol";

contract VaultOracle is Auth, IAggregatorV3 {
uint80 public constant ROUND_ID = 0;

uint256 public immutable override version = 1;
uint8 public immutable override decimals;

IERC7540Vault public vault;

// --- Events ---
event File(bytes32 indexed what, address data);

constructor(address vault_) Auth(msg.sender) {
vault = IERC7540Vault(vault_);
decimals = IERC20Metadata(vault.asset()).decimals();
}

// --- Administration ---
function file(bytes32 what, address data) public auth {
if (what == "vault") {
require(
address(vault) == address(0)
|| (IERC7540Vault(data).share() == vault.share() && IERC7540Vault(data).asset() == vault.asset()),
"VaultOracle/mismatching-asset-or-share"
);
vault = IERC7540Vault(data);
emit File(what, data);
} else {
revert("VaultOracle/file-unrecognized-param");
}
}

// --- Price computation ---
function latestRoundData()
public
view
override
returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound)
{
uint256 priceLastUpdated = vault.priceLastUpdated();
return (ROUND_ID, int256(vault.pricePerShare()), priceLastUpdated, priceLastUpdated, ROUND_ID);
}

function getRoundData(uint80 /* roundId */ )
external
view
returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound)
{
return latestRoundData();
}

// --- View methods ---
function description() external view returns (string memory) {
string memory shareSymbol = IERC20Metadata(vault.share()).symbol();
string memory assetSymbol = IERC20Metadata(vault.asset()).symbol();
return string.concat(shareSymbol, " / ", assetSymbol);
}
}
6 changes: 6 additions & 0 deletions src/interfaces/IERC7540.sol
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,12 @@ interface IERC7540Vault is
/// @dev MUST be called by endorsed sender
function setEndorsedOperator(address owner, bool approved) external;

/// @notice Price of 1 unit of share, quoted in the decimals of the asset.
function pricePerShare() external view returns (uint256);

/// @notice Returns timestamp of the last share price update.
function priceLastUpdated() external view returns (uint64);

/// @notice Callback when a redeem Request is triggered externally;
function onRedeemRequest(address controller, address owner, uint256 shares) external;

Expand Down
20 changes: 20 additions & 0 deletions src/interfaces/factories/IAggregatorV3.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.5.0;

interface IAggregatorV3 {
function decimals() external view returns (uint8);

function description() external view returns (string memory);

function version() external view returns (uint256);

function getRoundData(uint80 _roundId)
external
view
returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound);

function latestRoundData()
external
view
returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound);
}
Loading