Skip to content

Commit

Permalink
refactor: use address_provider interface instead or address in storage
Browse files Browse the repository at this point in the history
  • Loading branch information
FabienCoutant committed Jan 5, 2024
1 parent 2b2810d commit 37652e5
Showing 1 changed file with 24 additions and 32 deletions.
56 changes: 24 additions & 32 deletions src/core/debt_token.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@ trait IDebtToken<TContractState> {

#[starknet::contract]
mod DebtToken {
use core::starknet::event::EventEmitter;
use starknet::{ContractAddress, get_caller_address};
use openzeppelin::access::ownable::ownable::OwnableComponent::InternalTrait as OwnableInternalTrait;
use shisui::core::debt_token::IDebtToken;
use openzeppelin::token::erc20::erc20::ERC20Component::InternalTrait as ERC20InternalTrait;
use shisui::core::address_provider::IAddressProviderDispatcherTrait;
use starknet::{ContractAddress, get_caller_address};
use openzeppelin::{
access::ownable::{OwnableComponent, OwnableComponent::InternalImpl},
token::erc20::ERC20Component
};
use shisui::{
utils::errors::{CommunErrors, DebtTokenErrors},
core::address_provider::{IAddressProviderDispatcher, AddressesKey}
core::address_provider::{
IAddressProviderDispatcher, IAddressProviderDispatcherTrait, AddressesKey
}
};
use shisui::core::debt_token::IDebtToken;

component!(path: OwnableComponent, storage: ownable, event: OwnableEvent);
component!(path: ERC20Component, storage: erc20, event: ERC20Event);
Expand All @@ -45,7 +45,7 @@ mod DebtToken {

#[storage]
struct Storage {
address_provider: ContractAddress,
address_provider: IAddressProviderDispatcher,
emergency_stop_minting_collateral: LegacyMap::<ContractAddress, bool>,
whitelisted_contracts: LegacyMap::<ContractAddress, bool>,
#[substorage(v0)]
Expand All @@ -70,7 +70,7 @@ mod DebtToken {
}

#[constructor]
fn constructor(ref self: ContractState, address_provider: ContractAddress) {
fn constructor(ref self: ContractState, address_provider: IAddressProviderDispatcher) {
self.ownable.initializer(get_caller_address());
self.address_provider.write(address_provider);
}
Expand All @@ -79,22 +79,22 @@ mod DebtToken {
#[external(v0)]
impl DebtTokenImpl of super::IDebtToken<ContractState> {
fn mint(ref self: ContractState, account: ContractAddress, amount: u256) {
self.require_caller_is_borrower_operations();
self.assert_caller_is_borrower_operations();
self.erc20._mint(account, amount);
}

fn mint_from_whitelisted_contract(ref self: ContractState, amount: u256) {
self.require_caller_is_whitelisted_contract();
self.assert_caller_is_whitelisted_contract();
self.erc20._mint(get_caller_address(), amount);
}

fn burn_from_whitelisted_contract(ref self: ContractState, amount: u256) {
self.require_caller_is_whitelisted_contract();
self.assert_caller_is_whitelisted_contract();
self.erc20._burn(get_caller_address(), amount);
}

fn burn(ref self: ContractState, account: ContractAddress, amount: u256) {
self.require_caller_is_bo_or_vesselm_or_sp();
self.assert_caller_is_bo_or_vesselm_or_sp();
self.erc20._burn(account, amount);
}

Expand All @@ -119,31 +119,26 @@ mod DebtToken {
#[generate_trait]
impl InternalFunctions of InternalFunctionsTrait {
#[inline(always)]
fn require_caller_is_whitelisted_contract(self: @ContractState) {
fn assert_caller_is_whitelisted_contract(self: @ContractState) {
let caller = get_caller_address();
assert(
self.is_whitelisted(caller), CommunErrors::CommunErrors__CallerNotAuthorized
);
assert(self.is_whitelisted(caller), CommunErrors::CommunErrors__CallerNotAuthorized);
}
#[inline(always)]
fn require_caller_is_borrower_operations(self: @ContractState) {
fn assert_caller_is_borrower_operations(self: @ContractState) {
let caller = get_caller_address();
let address_provider = IAddressProviderDispatcher {
contract_address: (self.address_provider.read())
};
let borrower_operations_manager = address_provider
let borrower_operations_manager = self
.address_provider
.read()
.get_address(AddressesKey::borrower_operations);
assert(
caller == borrower_operations_manager,
CommunErrors::CommunErrors__CallerNotAuthorized
);
}
#[inline(always)]
fn require_caller_is_bo_or_vesselm_or_sp(self: @ContractState) {
fn assert_caller_is_bo_or_vesselm_or_sp(self: @ContractState) {
let caller = get_caller_address();
let address_provider = IAddressProviderDispatcher {
contract_address: (self.address_provider.read())
};
let address_provider = self.address_provider.read();
let borrower_operations_manager = address_provider
.get_address(AddressesKey::borrower_operations);
let vessel_manager = address_provider.get_address(AddressesKey::vessel_manager);
Expand All @@ -156,20 +151,17 @@ mod DebtToken {
);
}
#[inline(always)]
fn require_caller_is_stability_pool(self: @ContractState) {
fn assert_caller_is_stability_pool(self: @ContractState) {
let caller = get_caller_address();
let address_provider = IAddressProviderDispatcher {
contract_address: (self.address_provider.read())
};
let address_provider = self.address_provider.read();
let stability_pool = address_provider.get_address(AddressesKey::stability_pool);
assert(caller == stability_pool, CommunErrors::CommunErrors__CallerNotAuthorized);
}

#[inline(always)]
fn require_caller_is_vesselm_or_sp(self: @ContractState) {
fn assert_caller_is_vesselm_or_sp(self: @ContractState) {
let caller = get_caller_address();
let address_provider = IAddressProviderDispatcher {
contract_address: (self.address_provider.read())
};
let address_provider = self.address_provider.read();
let vessel_manager = address_provider.get_address(AddressesKey::vessel_manager);
let stability_pool = address_provider.get_address(AddressesKey::stability_pool);
assert(
Expand Down

0 comments on commit 37652e5

Please sign in to comment.