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

Maintainability issues fix #6

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion admin/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct AdminContract;
#[contractclient(name = "AdminClient")]
pub trait Admin {

/// Initializes the bridge oracle
/// Initializes the AdminContract
/// # Arguments
/// * `admin` - The admin address
/// * `treasury` - The treasury address
Expand Down
28 changes: 14 additions & 14 deletions pegkeeper/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ pub struct PegkeeperContract;
#[contractclient(name="PegkeeperClient")]
pub trait Pegkeeper {

/// Initialize the treasury
/// Initializes the PegKeeper contract
///
/// ### Arguments
/// * `admin` - The Address for the admin
/// * `maximum_duration` - The maximum_duration for swap transaction
fn initialize(e: Env, admin: Address, router: Address);
/// * `treasury` - The Address of the treasury
/// * `router` - The address of the soroswap router
fn initialize(e: Env, treasury: Address, router: Address);

/// Execute operation
///
Expand All @@ -32,35 +32,35 @@ pub trait Pegkeeper {
#[contractimpl]
impl Pegkeeper for PegkeeperContract {

fn initialize(e: Env, admin: Address, router: Address) {
fn initialize(e: Env, treasury: Address, router: Address) {
storage::extend_instance(&e);

if storage::is_init(&e) {
panic_with_error!(&e, PegkeeperError::AlreadyInitializedError);
}

storage::set_router(&e, &router);
storage::set_admin(&e, &admin);
e.events().publish(("Pegkeeper", Symbol::new(&e, "init")), (admin.clone(), router.clone()));
storage::set_treasury(&e, &treasury);
e.events().publish(("Pegkeeper", Symbol::new(&e, "init")), (treasury.clone(), router.clone()));
}

fn fl_receive(e: Env, token: Address, amount: i128, blend_pool: Address, auction: Address, collateral_token: Address, lot_amount: i128, liq_amount: i128, amm: Address, fee_taker: Address) {
storage::extend_instance(&e);

let admin = storage::get_admin(&e);
admin.require_auth();
let treasury = storage::get_treasury(&e);
treasury.require_auth();

let token_client = token::Client::new(&e, &token);
let collateral_client = token::Client::new(&e, &collateral_token);
let token_client = token::TokenClient::new(&e, &token);
let collateral_client = token::TokenClient::new(&e, &collateral_token);
let balance_before = token_client.balance(&e.current_contract_address());
let collateral_balance = collateral_client.balance(&e.current_contract_address());

helper::liquidate(&e, auction, token.clone(), amount.clone(), collateral_token.clone(), lot_amount.clone(), blend_pool.clone(), liq_amount.clone());

let collateral_balance_after = collateral_client.balance(&e.current_contract_address());
let lot_amount = collateral_balance_after - collateral_balance;
let to_swap = collateral_balance_after - collateral_balance;

helper::swap(&e, amm, collateral_token.clone(), token.clone(), lot_amount.clone(), 0);
helper::swap(&e, amm, collateral_token.clone(), token.clone(), to_swap.clone(), 0);

let balance_after = token_client.balance(&e.current_contract_address());

Expand All @@ -73,7 +73,7 @@ impl Pegkeeper for PegkeeperContract {

token_client.approve(
&e.current_contract_address(),
&admin,
&treasury,
&amount,
&(e.ledger().sequence() + 1),
);
Expand Down
12 changes: 8 additions & 4 deletions pegkeeper/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,25 @@ use crate::dependencies::{
};
use crate::storage;

const REQUEST_TYPE_LIQUIDATION_AUCTION: u32 = 6;
const REQUEST_TYPE_REPAY: u32 = 5;
const REQUEST_TYPE_COLLATERAL_WITHDRAWAL: u32 = 3;

pub fn liquidate(e: &Env, auction_creator: Address, token_a: Address, token_a_bid_amount: i128, token_b: Address, token_b_lot_amount: i128, blend_pool: Address, liq_amount: i128) {
let fill_requests = vec![
e,
Request {
request_type: 6 as u32, // liquidationAuction
request_type: REQUEST_TYPE_LIQUIDATION_AUCTION,
address: auction_creator.clone(),
amount: liq_amount.clone(),
},
Request {
request_type: 5 as u32, // Repay
request_type: REQUEST_TYPE_REPAY,
address: token_a.clone(),
amount: token_a_bid_amount,
},
Request {
request_type: 3 as u32, // Withdraw
request_type: REQUEST_TYPE_COLLATERAL_WITHDRAWAL,
address: token_b.clone(),
amount: token_b_lot_amount,
},
Expand Down Expand Up @@ -74,5 +78,5 @@ pub fn swap(e: &Env, pair: Address, token_a: Address, token_b: Address, amount_a
sub_invocations: vec![e]
})
]);
router_client.swap_exact_tokens_for_tokens(&amount_a, &amount_b, &path, &e.current_contract_address(), &u64::MAX);
let _ = router_client.swap_exact_tokens_for_tokens(&amount_a, &amount_b, &path, &e.current_contract_address(), &u64::MAX);
}
12 changes: 6 additions & 6 deletions pegkeeper/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const LEDGER_BUMP_INSTANCE: u32 = LEDGER_THRESHOLD_INSTANCE + ONE_DAY_LEDGERS; /
#[derive(Clone)]
#[contracttype]
pub enum PegkeeperDataKey {
ADMIN,
TREASURY,
ROUTER,
}

Expand All @@ -17,19 +17,19 @@ pub fn extend_instance(e: &Env) {
.extend_ttl(LEDGER_THRESHOLD_INSTANCE, LEDGER_BUMP_INSTANCE);
}

pub fn is_init(e: &Env) -> bool { e.storage().instance().has(&PegkeeperDataKey::ADMIN) }
pub fn is_init(e: &Env) -> bool { e.storage().instance().has(&PegkeeperDataKey::TREASURY) }

pub fn get_admin(e: &Env) -> Address {
pub fn get_treasury(e: &Env) -> Address {
e.storage()
.instance()
.get(&PegkeeperDataKey::ADMIN)
.get(&PegkeeperDataKey::TREASURY)
.unwrap_optimized()
}

pub fn set_admin(e: &Env, new_admin: &Address) {
pub fn set_treasury(e: &Env, new_admin: &Address) {
e.storage()
.instance()
.set(&PegkeeperDataKey::ADMIN, new_admin);
.set(&PegkeeperDataKey::TREASURY, new_admin);
}

pub fn get_router(e: &Env) -> Address {
Expand Down
7 changes: 5 additions & 2 deletions treasury/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ use soroban_sdk::{contract, contractclient, contractimpl, panic_with_error, toke
use soroban_sdk::auth::{ContractContext, InvokerContractAuthEntry, SubContractInvocation};
use crate::errors::TreasuryError;

const REQUEST_TYPE_SUPPLY: u32 = 0;
const REQUEST_TYPE_WITHDRAW: u32 = 1;

#[contract]
pub struct TreasuryContract;

Expand Down Expand Up @@ -132,7 +135,7 @@ impl Treasury for TreasuryContract {
PoolClient::new(&e, &blend).submit(&e.current_contract_address(), &e.current_contract_address(), &e.current_contract_address(), &vec![
&e,
Request {
request_type: 0_u32, // SUPPLY RequestType
request_type: REQUEST_TYPE_SUPPLY,
address: token.clone(),
amount,
},
Expand All @@ -157,7 +160,7 @@ impl Treasury for TreasuryContract {
PoolClient::new(&e, &blend).submit(&e.current_contract_address(), &e.current_contract_address(), &e.current_contract_address(), &vec![
&e,
Request {
request_type: 1_u32, // WITHDRAW RequestType
request_type: REQUEST_TYPE_WITHDRAW,
address: token.clone(),
amount,
},
Expand Down