Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/paltalabs/defindex into fea…
Browse files Browse the repository at this point in the history
…t/updgradeRebalanceTests
  • Loading branch information
MattPoblete committed Dec 2, 2024
2 parents 7bb8b29 + cf5421c commit a347942
Show file tree
Hide file tree
Showing 70 changed files with 4,323 additions and 522 deletions.
21 changes: 21 additions & 0 deletions apps/contracts/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion apps/contracts/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["strategies/*", "vault", "factory"]
members = ["common", "strategies/*", "vault", "factory", "integration-test"]
exclude = [
"strategies/external_wasms",
]
Expand All @@ -17,6 +17,7 @@ soroban-sdk = "21.7.6"
soroban-token-sdk = { version = "21.0.1-preview.3" }
# soroswap-library = "0.3.0"
defindex-strategy-core={ path="./strategies/core", package="defindex-strategy-core" }
common={ path="./common", package="common" }

[profile.release]
opt-level = "z"
Expand Down
14 changes: 14 additions & 0 deletions apps/contracts/common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "common"
version = { workspace = true }
authors = ["coderipper <[email protected]>"]
license = { workspace = true }
edition = { workspace = true }
publish = false
repository = { workspace = true }

[dependencies]
soroban-sdk = { workspace = true }

[dev-dependencies]
soroban-sdk = { workspace = true, features = ["testutils"] }
3 changes: 3 additions & 0 deletions apps/contracts/common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#![no_std]

pub mod models;
16 changes: 16 additions & 0 deletions apps/contracts/common/src/models.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use soroban_sdk::{contracttype, Address, String, Vec};

#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Strategy {
pub address: Address,
pub name: String,
pub paused: bool,
}

#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct AssetStrategySet {
pub address: Address,
pub strategies: Vec<Strategy>,
}
2 changes: 2 additions & 0 deletions apps/contracts/factory/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ crate-type = ["cdylib"]

[dependencies]
soroban-sdk = { workspace = true }
defindex-strategy-core = { workspace = true }
common = { workspace = true }

[dev-dependencies]
soroban-sdk = { workspace = true, features = ["testutils"] }
26 changes: 0 additions & 26 deletions apps/contracts/factory/src/defindex.rs

This file was deleted.

2 changes: 1 addition & 1 deletion apps/contracts/factory/src/events.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Definition of the Events used in the contract
use common::models::AssetStrategySet;
use soroban_sdk::{contracttype, symbol_short, Address, Env, Vec};
use crate::defindex::AssetStrategySet;

// INITIALIZED
#[contracttype]
Expand Down
84 changes: 43 additions & 41 deletions apps/contracts/factory/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
#![no_std]

mod defindex;
mod vault;
mod events;
mod storage;
mod error;

use common::models::AssetStrategySet;
use soroban_sdk::{
contract, contractimpl, Address, BytesN, Env, Map, String, Vec
contract, contractimpl, vec, Address, BytesN, Env, Map, String, Symbol, Val, Vec, IntoVal
};
use error::FactoryError;
use defindex::{create_contract, AssetStrategySet};
use storage::{ add_new_defindex, extend_instance_ttl, get_admin, get_defi_wasm_hash, get_defindex_receiver, get_deployed_defindexes, get_fee_rate, has_admin, put_admin, put_defi_wasm_hash, put_defindex_receiver, put_defindex_fee };
pub use vault::create_contract;
use storage::{ add_new_defindex, extend_instance_ttl, get_admin, get_vault_wasm_hash, get_defindex_receiver, get_deployed_defindexes, get_fee_rate, has_admin, put_admin, put_vault_wasm_hash, put_defindex_receiver, put_defindex_fee };

fn check_initialized(e: &Env) -> Result<(), FactoryError> {
if !has_admin(e) {
Expand All @@ -27,7 +28,7 @@ pub trait FactoryTrait {
/// * `admin` - The address of the contract administrator, who can manage settings.
/// * `defindex_receiver` - The default address designated to receive a portion of fees.
/// * `defindex_fee` - The initial annual fee rate (in basis points).
/// * `defindex_wasm_hash` - The hash of the DeFindex Vault's WASM file for deploying new vaults.
/// * `vault_wasm_hash` - The hash of the DeFindex Vault's WASM file for deploying new vaults.
///
/// # Returns
/// * `Result<(), FactoryError>` - Returns Ok(()) if successful, otherwise an error.
Expand All @@ -36,7 +37,7 @@ pub trait FactoryTrait {
admin: Address,
defindex_receiver: Address,
defindex_fee: u32,
defindex_wasm_hash: BytesN<32>
vault_wasm_hash: BytesN<32>
) -> Result<(), FactoryError>;

/// Creates a new DeFindex Vault with specified parameters.
Expand Down Expand Up @@ -180,7 +181,7 @@ impl FactoryTrait for DeFindexFactory {
/// * `admin` - The address of the contract administrator, who can manage settings.
/// * `defindex_receiver` - The default address designated to receive a portion of fees.
/// * `defindex_fee` - The initial annual fee rate (in basis points).
/// * `defindex_wasm_hash` - The hash of the DeFindex Vault's WASM file for deploying new vaults.
/// * `vault_wasm_hash` - The hash of the DeFindex Vault's WASM file for deploying new vaults.
///
/// # Returns
/// * `Result<(), FactoryError>` - Returns Ok(()) if successful, otherwise an error.
Expand All @@ -189,15 +190,15 @@ impl FactoryTrait for DeFindexFactory {
admin: Address,
defindex_receiver: Address,
defindex_fee: u32,
defi_wasm_hash: BytesN<32>
vault_wasm_hash: BytesN<32>
) -> Result<(), FactoryError> {
if has_admin(&e) {
return Err(FactoryError::AlreadyInitialized);
}

put_admin(&e, &admin);
put_defindex_receiver(&e, &defindex_receiver);
put_defi_wasm_hash(&e, defi_wasm_hash);
put_vault_wasm_hash(&e, vault_wasm_hash);
put_defindex_fee(&e, &defindex_fee);

events::emit_initialized(&e, admin, defindex_receiver, defindex_fee);
Expand Down Expand Up @@ -233,22 +234,23 @@ impl FactoryTrait for DeFindexFactory {

let current_contract = e.current_contract_address();

let defi_wasm_hash = get_defi_wasm_hash(&e)?;
let defindex_address = create_contract(&e, defi_wasm_hash, salt);
let vault_wasm_hash = get_vault_wasm_hash(&e)?;
let defindex_address = create_contract(&e, vault_wasm_hash, salt);

let defindex_receiver = get_defindex_receiver(&e);

defindex::Client::new(&e, &defindex_address).initialize(
&assets,
&manager,
&emergency_manager,
&fee_receiver,
&vault_fee,
&defindex_receiver,
&current_contract,
&vault_name,
&vault_symbol,
);
let mut init_args: Vec<Val> = vec![&e];
init_args.push_back(assets.to_val());
init_args.push_back(manager.to_val());
init_args.push_back(emergency_manager.to_val());
init_args.push_back(fee_receiver.to_val());
init_args.push_back(vault_fee.into_val(&e));
init_args.push_back(defindex_receiver.to_val());
init_args.push_back(current_contract.to_val());
init_args.push_back(vault_name.to_val());
init_args.push_back(vault_symbol.to_val());

e.invoke_contract::<Val>(&defindex_address, &Symbol::new(&e, "initialize"), init_args);

add_new_defindex(&e, defindex_address.clone());
events::emit_create_defindex_vault(&e, emergency_manager, fee_receiver, manager, vault_fee, assets);
Expand Down Expand Up @@ -293,35 +295,35 @@ impl FactoryTrait for DeFindexFactory {

let current_contract = e.current_contract_address();

let defi_wasm_hash = get_defi_wasm_hash(&e)?;
let defindex_address = create_contract(&e, defi_wasm_hash, salt);
let vault_wasm_hash = get_vault_wasm_hash(&e)?;
let defindex_address = create_contract(&e, vault_wasm_hash, salt);

let defindex_receiver = get_defindex_receiver(&e);

let defindex_client = defindex::Client::new(&e, &defindex_address);
let mut init_args: Vec<Val> = vec![&e];
init_args.push_back(assets.to_val());
init_args.push_back(manager.to_val());
init_args.push_back(emergency_manager.to_val());
init_args.push_back(fee_receiver.to_val());
init_args.push_back(vault_fee.into_val(&e));
init_args.push_back(defindex_receiver.to_val());
init_args.push_back(current_contract.to_val());
init_args.push_back(vault_name.to_val());
init_args.push_back(vault_symbol.to_val());

defindex_client.initialize(
&assets,
&manager,
&emergency_manager,
&fee_receiver,
&vault_fee,
&defindex_receiver,
&current_contract,
&vault_name,
&vault_symbol,
);
e.invoke_contract::<Val>(&defindex_address, &Symbol::new(&e, "initialize"), init_args);

let mut amounts_min = Vec::new(&e);
for _ in 0..amounts.len() {
amounts_min.push_back(0i128);
}

defindex_client.deposit(
&amounts,
&amounts_min,
&caller
);
let mut deposit_args: Vec<Val> = vec![&e];
deposit_args.push_back(amounts.to_val());
deposit_args.push_back(amounts_min.to_val());
deposit_args.push_back(caller.to_val());

e.invoke_contract::<Val>(&defindex_address, &Symbol::new(&e, "deposit"), deposit_args);

add_new_defindex(&e, defindex_address.clone());
events::emit_create_defindex_vault(&e, emergency_manager, fee_receiver, manager, vault_fee, assets);
Expand Down
6 changes: 3 additions & 3 deletions apps/contracts/factory/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ fn get_persistent_extend_or_error<V: TryFromVal<Env, Val>>(
}
}

pub fn get_defi_wasm_hash(e: &Env) -> Result<BytesN<32>, FactoryError>{
pub fn get_vault_wasm_hash(e: &Env) -> Result<BytesN<32>, FactoryError>{
let key = DataKey::DeFindexWasmHash;
get_persistent_extend_or_error(&e, &key, FactoryError::NotInitialized)
}

pub fn put_defi_wasm_hash(e: &Env, pair_wasm_hash: BytesN<32>) {
pub fn put_vault_wasm_hash(e: &Env, vault_wasm_hash: BytesN<32>) {
let key = DataKey::DeFindexWasmHash;
e.storage().persistent().set(&key, &pair_wasm_hash);
e.storage().persistent().set(&key, &vault_wasm_hash);
e.storage()
.persistent()
.extend_ttl(&key, PERSISTENT_LIFETIME_THRESHOLD, PERSISTENT_BUMP_AMOUNT)
Expand Down
5 changes: 2 additions & 3 deletions apps/contracts/factory/src/test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![cfg(test)]
extern crate std;
use crate::defindex::{AssetStrategySet, Strategy};
use crate::{DeFindexFactory, DeFindexFactoryClient};
use common::models::{AssetStrategySet, Strategy};
use soroban_sdk::token::{
StellarAssetClient as SorobanTokenAdminClient, TokenClient as SorobanTokenClient,
};
Expand Down Expand Up @@ -157,5 +157,4 @@ impl<'a> DeFindexFactoryTest<'a> {

mod admin;
mod initialize;
mod create_defindex;
mod all_flow;
mod create_defindex;
Loading

0 comments on commit a347942

Please sign in to comment.