Skip to content

Commit

Permalink
Merge pull request #25 from xycloo/factory-contract-update
Browse files Browse the repository at this point in the history
Improvements to factory
  • Loading branch information
heytdep committed Jan 5, 2024
2 parents 76cd3fa + 5507adc commit 681c54d
Show file tree
Hide file tree
Showing 18 changed files with 30 additions and 36,086 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
deprecated
.DS_Store
Cargo.lock
test_snapshots
2 changes: 1 addition & 1 deletion factory/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ crate-type = ["cdylib"]
[features]

# Pluggable flash loans and vaults are not included by default
default = []
default = ["pluggable"]

pluggable = []

Expand Down
25 changes: 16 additions & 9 deletions factory/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use soroban_sdk::{contract, contractimpl, Address, BytesN, Env};

use crate::types::Error;
use crate::{pool, storage::*};
use crate::types::{Error, DataKey};
use crate::{pool, storage::*, events};

#[contract]
pub struct XycloansFactory;
Expand All @@ -12,7 +12,7 @@ pub trait PluggableInterface {
/// Plugs in the protocol a vault contract for a certain token.
/// Once both the vault and the associated flash loan are plugged in the proxy, there effictively is a new pool in the protocol.
///
/// [`set_vault()`] must be provided with: /// [`set_vault()`] must be provided with:
/// [`set_pool()`] must be provided with:
/// [`token_address: Address`] Address of the token used by the vault.
/// [`pool_address: Address`] Address of the vault contract.
fn set_pool(env: Env, token_address: Address, pool_address: Address) -> Result<(), Error>;
Expand All @@ -23,17 +23,18 @@ pub trait AdminInterface {

/// [`initialize()`] must be provided with:
/// [`admin: Address`] Address of the proxy's admin
/// [`pool_hash: BytesN<32>`] Hash of the pool

/// The proxy's admin will only be able to plug in and out pools from the protocol
/// without having any control over the deposited funds.
fn initialize(env: Env, admin: Address, pool_hash: BytesN<32>) -> Result<(), Error>;

/// Deploys a flash loan-vault pair and initializes them accordingly.
fn deploy_pair(env: Env, token_address: Address, salt: BytesN<32>) -> Result<Address, Error>;
/// Deploys a pool.
fn deploy_pool(env: Env, token_address: Address, salt: BytesN<32>) -> Result<Address, Error>;
}

pub trait Common {
/// Reads from the storage the flash loan contract for a given token
/// Reads from the storage the pool contract for a given token
fn get_pool_address(env: Env, token_address: Address) -> Result<Address, Error>;
}

Expand All @@ -50,19 +51,25 @@ impl AdminInterface for XycloansFactory {
Ok(())
}

fn deploy_pair(env: Env, token_address: Address, salt: BytesN<32>) -> Result<Address, Error> {
fn deploy_pool(env: Env, token_address: Address, salt: BytesN<32>) -> Result<Address, Error> {
read_admin(&env)?.require_auth();

let key = &DataKey::Pool(token_address.clone());
if env.storage().persistent().has(key) {
return Err(Error::PoolExists)
}

let pool_address = env
.deployer()
.with_address(env.current_contract_address(), salt)
.deploy(read_pool_hash(&env));

let pool = pool::Client::new(&env, &pool_address);

pool.initialize(&token_address);

set_pool(&env, token_address, &pool_address);
events::deployed_pool(&env, &pool_address);

Ok(pool_address)
}
}
Expand All @@ -79,7 +86,7 @@ impl PluggableInterface for XycloansFactory {
fn set_pool(env: Env, token_address: Address, pool_address: Address) -> Result<(), Error> {
read_admin(&env)?.require_auth();

set_vault(&env, token_address, vault_address);
set_pool(&env, token_address, &pool_address);
Ok(())
}
}
6 changes: 6 additions & 0 deletions factory/src/events.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use soroban_sdk::{Env, Address, symbol_short};

pub(crate) fn deployed_pool(env: &Env, contract: &Address) {
let topics = (symbol_short!("deployed"), );
env.events().publish(topics, contract);
}
1 change: 1 addition & 0 deletions factory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
mod contract;
mod storage;
mod types;
mod events;

mod pool {
use soroban_sdk::contractimport;
Expand Down
33 changes: 1 addition & 32 deletions factory/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub(crate) fn read_pool(env: &Env, token_address: Address) -> Result<Address, Er
if let Some(vault_address) = env.storage().persistent().get(key) {
Ok(vault_address)
} else {
Err(Error::VaultDoesntExist)
Err(Error::NoPool)
}
}

Expand All @@ -42,34 +42,3 @@ pub(crate) fn read_pool_hash(env: &Env) -> BytesN<32> {
pub(crate) fn write_pool_hash(env: &Env, hash: &BytesN<32>) {
env.storage().instance().set(&DataKey::PoolHash, hash)
}

/*
Deprecated from 0.2.0
pub(crate) fn vault_withdraw_matured_fees(
env: &Env,
provider: Address,
token_address: Address,
) -> Result<(), Error> {
let vault_client = vault::Client::new(env, &read_vault(env, token_address)?);
vault_client.withdraw_matured(&provider);
Ok(())
}
pub(crate) fn flash_loan_borrow(
env: &Env,
token_address: Address,
amount: i128,
receiver_address: Address,
) -> Result<(), Error> {
let flash_loan_client = flash_loan::Client::new(env, &read_flash_loan(env, token_address)?);
flash_loan_client.borrow(&receiver_address, &amount);
Ok(())
}
*/
4 changes: 2 additions & 2 deletions factory/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ pub enum Error {
AlreadyInitialized = 0,
NotInitialized = 1,
NotAdmin = 2,
VaultDoesntExist = 3,
FlashLoanDoesntExist = 4,
PoolExists = 3,
NoPool = 4
}
Loading

0 comments on commit 681c54d

Please sign in to comment.