-
Notifications
You must be signed in to change notification settings - Fork 1
feat: frontend data provider #571
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
Merged
Merged
Changes from 10 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
816c057
add sketch
tserg 13bad2d
rename file
tserg 9c13fd7
minor fixes
tserg 1468405
change yang idx to u32 in sentinel
tserg cdde63f
add more data; add custom types
tserg 5248913
fix comments
tserg bb555cc
add yin and rm info
tserg 7e56647
add comments
tserg b829724
rename types
tserg e02aea0
add upgradeable
tserg a7a8eb6
move types to new file
tserg 236659d
Merge branch 'main' of https://github.com/lindy-labs/opus_contracts i…
tserg 98a420e
add to deployment script
tserg 00b725f
fix admin in ctor
tserg 3156acb
update scripts
tserg 7203e83
apply mc suggestions
tserg 2997f5f
add print
tserg 4366474
add sepolia deployment
tserg 4012653
modify deploy script
tserg 17ccdf2
update state file and sepolia addresses
tserg 1eb1b41
clean up
tserg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
#[starknet::contract] | ||
pub mod frontend_data_provider { | ||
use access_control::access_control_component; | ||
use opus::interfaces::IGate::{IGateDispatcher, IGateDispatcherTrait}; | ||
use opus::interfaces::ISentinel::{ISentinelDispatcher, ISentinelDispatcherTrait}; | ||
use opus::interfaces::IShrine::{IShrineDispatcher, IShrineDispatcherTrait}; | ||
use opus::periphery::interfaces::IFrontendDataProvider; | ||
use opus::periphery::roles::frontend_data_provider_roles; | ||
use opus::types::{Health, RecoveryModeInfo, ShrineAssetInfo, TroveAssetInfo, YangBalance, YinInfo}; | ||
use opus::utils::upgradeable::{IUpgradeable, upgradeable_component}; | ||
use starknet::{ClassHash, ContractAddress}; | ||
use wadray::{Ray, Wad}; | ||
|
||
// | ||
// Components | ||
// | ||
|
||
component!(path: access_control_component, storage: access_control, event: AccessControlEvent); | ||
|
||
#[abi(embed_v0)] | ||
impl AccessControlPublic = access_control_component::AccessControl<ContractState>; | ||
impl AccessControlHelpers = access_control_component::AccessControlHelpers<ContractState>; | ||
|
||
component!(path: upgradeable_component, storage: upgradeable, event: UpgradeableEvent); | ||
|
||
impl UpgradeableHelpers = upgradeable_component::UpgradeableHelpers<ContractState>; | ||
|
||
// | ||
// Storage | ||
// | ||
|
||
#[storage] | ||
struct Storage { | ||
// components | ||
#[substorage(v0)] | ||
access_control: access_control_component::Storage, | ||
#[substorage(v0)] | ||
upgradeable: upgradeable_component::Storage, | ||
// Sentinel associated with the Shrine | ||
sentinel: ISentinelDispatcher, | ||
shrine: IShrineDispatcher, | ||
} | ||
|
||
// | ||
// Events | ||
// | ||
|
||
#[event] | ||
#[derive(Copy, Drop, starknet::Event, PartialEq)] | ||
pub enum Event { | ||
AccessControlEvent: access_control_component::Event, | ||
UpgradeableEvent: upgradeable_component::Event, | ||
} | ||
|
||
// | ||
// Constructor | ||
// | ||
|
||
#[constructor] | ||
fn constructor(ref self: ContractState, shrine: ContractAddress, sentinel: ContractAddress) { | ||
self.shrine.write(IShrineDispatcher { contract_address: shrine }); | ||
self.sentinel.write(ISentinelDispatcher { contract_address: sentinel }); | ||
} | ||
|
||
// | ||
// Upgradeable | ||
// | ||
|
||
#[abi(embed_v0)] | ||
impl IUpgradeableImpl of IUpgradeable<ContractState> { | ||
fn upgrade(ref self: ContractState, new_class_hash: ClassHash) { | ||
self.access_control.assert_has_role(frontend_data_provider_roles::UPGRADE); | ||
self.upgradeable.upgrade(new_class_hash); | ||
} | ||
} | ||
|
||
// | ||
// External functions | ||
// | ||
|
||
#[abi(embed_v0)] | ||
impl IFrontendDataProviderImpl of IFrontendDataProvider<ContractState> { | ||
fn get_yin_info(self: @ContractState) -> YinInfo { | ||
let shrine: IShrineDispatcher = self.shrine.read(); | ||
|
||
YinInfo { | ||
yin_spot_price: shrine.get_yin_spot_price(), | ||
yin_total_supply: shrine.get_total_yin(), | ||
yin_ceiling: shrine.get_debt_ceiling(), | ||
} | ||
} | ||
|
||
fn get_recovery_mode_info(self: @ContractState) -> RecoveryModeInfo { | ||
let shrine: IShrineDispatcher = self.shrine.read(); | ||
let shrine_health: Health = shrine.get_shrine_health(); | ||
|
||
let target_factor: Ray = shrine.get_recovery_mode_target_factor(); | ||
let buffer_factor: Ray = shrine.get_recovery_mode_buffer_factor(); | ||
let target_ltv: Ray = target_factor * shrine_health.threshold; | ||
let buffer_ltv: Ray = (target_factor + buffer_factor) * shrine_health.threshold; | ||
|
||
RecoveryModeInfo { is_recovery_mode: shrine.is_recovery_mode(), target_ltv, buffer_ltv } | ||
} | ||
|
||
// Returns an ordered array of TroveAssetInfo struct for a trove | ||
fn get_trove_assets_info(self: @ContractState, trove_id: u64) -> Span<TroveAssetInfo> { | ||
let shrine: IShrineDispatcher = self.shrine.read(); | ||
let sentinel: ISentinelDispatcher = self.sentinel.read(); | ||
|
||
let mut trove_yang_balances: Span<YangBalance> = shrine.get_trove_deposits(trove_id); | ||
let mut yang_addresses: Span<ContractAddress> = sentinel.get_yang_addresses(); | ||
|
||
assert(trove_yang_balances.len() == yang_addresses.len(), 'FDP: Length mismatch'); | ||
|
||
let mut yang_infos: Array<TroveAssetInfo> = ArrayTrait::new(); | ||
let current_rate_era: u64 = shrine.get_current_rate_era(); | ||
loop { | ||
match trove_yang_balances.pop_front() { | ||
Option::Some(yang_balance) => { | ||
let yang: ContractAddress = *yang_addresses.pop_front().unwrap(); | ||
assert(sentinel.get_yang(*yang_balance.yang_id) == yang, 'FDP: Address mismatch'); | ||
|
||
let (shrine_asset_info, yang_price) = self | ||
.get_shrine_yang_info_helper( | ||
shrine, sentinel, yang, *yang_balance.amount, current_rate_era | ||
); | ||
|
||
let asset_amt: u128 = sentinel.convert_to_assets(yang, *yang_balance.amount); | ||
let trove_yang_info = TroveAssetInfo { | ||
shrine_asset_info, amount: asset_amt, value: *yang_balance.amount * yang_price, | ||
}; | ||
yang_infos.append(trove_yang_info); | ||
}, | ||
Option::None => { break yang_infos.span(); } | ||
} | ||
} | ||
} | ||
|
||
// Returns an ordered array of ShrineAssetInfo struct for the Shrine | ||
fn get_shrine_assets_info(self: @ContractState) -> Span<ShrineAssetInfo> { | ||
let shrine: IShrineDispatcher = self.shrine.read(); | ||
let sentinel: ISentinelDispatcher = self.sentinel.read(); | ||
|
||
let mut shrine_yang_balances: Span<YangBalance> = shrine.get_shrine_deposits(); | ||
let mut yang_addresses: Span<ContractAddress> = sentinel.get_yang_addresses(); | ||
|
||
assert(shrine_yang_balances.len() == yang_addresses.len(), 'FDP: Length mismatch'); | ||
|
||
let mut yang_infos: Array<ShrineAssetInfo> = ArrayTrait::new(); | ||
tserg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let current_rate_era: u64 = shrine.get_current_rate_era(); | ||
loop { | ||
match shrine_yang_balances.pop_front() { | ||
Option::Some(yang_balance) => { | ||
let yang: ContractAddress = *yang_addresses.pop_front().unwrap(); | ||
assert(sentinel.get_yang(*yang_balance.yang_id) == yang, 'FDP: Address mismatch'); | ||
|
||
let (shrine_yang_info, _) = self | ||
.get_shrine_yang_info_helper( | ||
shrine, sentinel, yang, *yang_balance.amount, current_rate_era | ||
); | ||
yang_infos.append(shrine_yang_info); | ||
}, | ||
Option::None => { break yang_infos.span(); } | ||
} | ||
} | ||
} | ||
} | ||
|
||
// | ||
// Internal functions | ||
// | ||
|
||
#[generate_trait] | ||
impl FrontendDataProviderHelpers of FrontendDataProviderHelpersTrait { | ||
// Helper function to generate a ShrineAssetInfo struct for a yang. | ||
// Returns a tuple of a ShrineAssetInfo struct and the yang price | ||
fn get_shrine_yang_info_helper( | ||
tserg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self: @ContractState, | ||
shrine: IShrineDispatcher, | ||
sentinel: ISentinelDispatcher, | ||
yang: ContractAddress, | ||
yang_amt: Wad, | ||
current_rate_era: u64 | ||
) -> (ShrineAssetInfo, Wad) { | ||
let gate = IGateDispatcher { contract_address: sentinel.get_gate_address(yang) }; | ||
let deposited: u128 = gate.get_total_assets(); | ||
|
||
let (yang_price, _, _) = shrine.get_current_yang_price(yang); | ||
let yang_value: Wad = yang_amt * yang_price; | ||
let asset_price: Wad = yang_value / deposited.into(); | ||
|
||
let threshold: Ray = shrine.get_yang_threshold(yang); | ||
let base_rate: Ray = shrine.get_yang_rate(yang, current_rate_era); | ||
let ceiling: u128 = sentinel.get_yang_asset_max(yang); | ||
|
||
( | ||
ShrineAssetInfo { | ||
address: yang, | ||
price: asset_price, | ||
threshold, | ||
base_rate, | ||
deposited, | ||
ceiling, | ||
deposited_value: yang_value | ||
}, | ||
yang_price | ||
) | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.