From 627026579fbd93f9a30e53db0e8b2b589a203de7 Mon Sep 17 00:00:00 2001
From: gangov <6922910+gangov@users.noreply.github.com>
Date: Mon, 6 Jan 2025 16:19:29 +0200
Subject: [PATCH 01/15] contructor in factory
---
contracts/factory/src/contract.rs | 113 ++++++++++++--------------
contracts/factory/src/tests.rs | 63 ++++++++------
contracts/factory/src/tests/config.rs | 27 +++---
contracts/factory/src/tests/setup.rs | 29 ++++---
4 files changed, 123 insertions(+), 109 deletions(-)
diff --git a/contracts/factory/src/contract.rs b/contracts/factory/src/contract.rs
index 33e3ac23..fa849dd3 100644
--- a/contracts/factory/src/contract.rs
+++ b/contracts/factory/src/contract.rs
@@ -30,19 +30,6 @@ pub struct Factory;
#[allow(dead_code)]
pub trait FactoryTrait {
- #[allow(clippy::too_many_arguments)]
- fn initialize(
- env: Env,
- admin: Address,
- multihop_wasm_hash: BytesN<32>,
- lp_wasm_hash: BytesN<32>,
- stable_wasm_hash: BytesN<32>,
- stake_wasm_hash: BytesN<32>,
- token_wasm_hash: BytesN<32>,
- whitelisted_accounts: Vec
,
- lp_token_decimals: u32,
- );
-
#[allow(clippy::too_many_arguments)]
fn create_liquidity_pool(
env: Env,
@@ -89,56 +76,6 @@ pub trait FactoryTrait {
#[contractimpl]
impl FactoryTrait for Factory {
- #[allow(clippy::too_many_arguments)]
- fn initialize(
- env: Env,
- admin: Address,
- multihop_wasm_hash: BytesN<32>,
- lp_wasm_hash: BytesN<32>,
- stable_wasm_hash: BytesN<32>,
- stake_wasm_hash: BytesN<32>,
- token_wasm_hash: BytesN<32>,
- whitelisted_accounts: Vec,
- lp_token_decimals: u32,
- ) {
- if is_initialized(&env) {
- log!(
- &env,
- "Factory: Initialize: initializing contract twice is not allowed"
- );
- panic_with_error!(&env, ContractError::AlreadyInitialized);
- }
-
- if whitelisted_accounts.is_empty() {
- log!(&env, "Factory: Initialize: there must be at least one whitelisted account able to create liquidity pools.");
- panic_with_error!(&env, ContractError::WhiteListeEmpty);
- }
-
- set_initialized(&env);
-
- let multihop_address =
- deploy_and_initialize_multihop_contract(env.clone(), admin.clone(), multihop_wasm_hash);
-
- save_config(
- &env,
- Config {
- admin: admin.clone(),
- multihop_address,
- lp_wasm_hash,
- stake_wasm_hash,
- token_wasm_hash,
- whitelisted_accounts,
- lp_token_decimals,
- },
- );
- save_stable_wasm_hash(&env, stable_wasm_hash);
-
- save_lp_vec(&env, Vec::new(&env));
-
- env.events()
- .publish(("initialize", "LP factory contract"), admin);
- }
-
#[allow(clippy::too_many_arguments)]
fn create_liquidity_pool(
env: Env,
@@ -501,6 +438,56 @@ impl FactoryTrait for Factory {
#[contractimpl]
impl Factory {
+ #[allow(clippy::too_many_arguments)]
+ pub fn __constructor(
+ env: Env,
+ admin: Address,
+ multihop_wasm_hash: BytesN<32>,
+ lp_wasm_hash: BytesN<32>,
+ stable_wasm_hash: BytesN<32>,
+ stake_wasm_hash: BytesN<32>,
+ token_wasm_hash: BytesN<32>,
+ whitelisted_accounts: Vec,
+ lp_token_decimals: u32,
+ ) {
+ if is_initialized(&env) {
+ log!(
+ &env,
+ "Factory: Initialize: initializing contract twice is not allowed"
+ );
+ panic_with_error!(&env, ContractError::AlreadyInitialized);
+ }
+
+ if whitelisted_accounts.is_empty() {
+ log!(&env, "Factory: Initialize: there must be at least one whitelisted account able to create liquidity pools.");
+ panic_with_error!(&env, ContractError::WhiteListeEmpty);
+ }
+
+ set_initialized(&env);
+
+ let multihop_address =
+ deploy_and_initialize_multihop_contract(env.clone(), admin.clone(), multihop_wasm_hash);
+
+ save_config(
+ &env,
+ Config {
+ admin: admin.clone(),
+ multihop_address,
+ lp_wasm_hash,
+ stake_wasm_hash,
+ token_wasm_hash,
+ whitelisted_accounts,
+ lp_token_decimals,
+ },
+ );
+ save_stable_wasm_hash(&env, stable_wasm_hash);
+
+ save_lp_vec(&env, Vec::new(&env));
+
+ env.events()
+ .publish(("initialize", "LP factory contract"), admin);
+ }
+
#[allow(dead_code)]
pub fn update(env: Env, new_wasm_hash: BytesN<32>, new_stable_pool_hash: BytesN<32>) {
let admin = get_config(&env).admin;
diff --git a/contracts/factory/src/tests.rs b/contracts/factory/src/tests.rs
index 48f5751a..cea4db58 100644
--- a/contracts/factory/src/tests.rs
+++ b/contracts/factory/src/tests.rs
@@ -1,8 +1,10 @@
-use soroban_sdk::{testutils::Address as _, vec, Address, Env};
+use soroban_sdk::{testutils::Address as _, vec, Address, Env, Vec};
+
+use crate::contract::{Factory, FactoryClient};
use self::setup::{
- deploy_factory_contract, install_lp_contract, install_multihop_wasm, install_stable_lp,
- install_stake_wasm, install_token_wasm,
+ install_lp_contract, install_multihop_wasm, install_stable_lp, install_stake_wasm,
+ install_token_wasm,
};
mod config;
@@ -24,27 +26,42 @@ fn test_deploy_factory_twice_should_fail() {
let stable_wasm_hash = install_stable_lp(&env);
let stake_wasm_hash = install_stake_wasm(&env);
let token_wasm_hash = install_token_wasm(&env);
+ let whitelisted_accounts = vec![&env, auth_user];
+ let contract_addr = Address::generate(&env);
- let factory = deploy_factory_contract(&env, admin.clone());
-
- factory.initialize(
- &admin,
- &multihop_wasm_hash,
- &lp_wasm_hash,
- &stable_wasm_hash,
- &stake_wasm_hash,
- &token_wasm_hash,
- &vec![&env, auth_user.clone()],
- &10u32,
+ let _ = FactoryClient::new(
+ &env,
+ &env.register_at(
+ &contract_addr.clone(),
+ Factory {},
+ (
+ &admin,
+ &multihop_wasm_hash,
+ &lp_wasm_hash,
+ &stable_wasm_hash,
+ &stake_wasm_hash,
+ &token_wasm_hash,
+ whitelisted_accounts.clone(),
+ &10u32,
+ ),
+ ),
);
- factory.initialize(
- &admin,
- &multihop_wasm_hash,
- &lp_wasm_hash,
- &stable_wasm_hash,
- &stake_wasm_hash,
- &token_wasm_hash,
- &vec![&env, auth_user.clone()],
- &10u32,
+
+ let _ = FactoryClient::new(
+ &env,
+ &env.register_at(
+ &contract_addr,
+ Factory {},
+ (
+ &admin,
+ &multihop_wasm_hash,
+ &lp_wasm_hash,
+ &stable_wasm_hash,
+ &stake_wasm_hash,
+ &token_wasm_hash,
+ whitelisted_accounts,
+ &10u32,
+ ),
+ ),
);
}
diff --git a/contracts/factory/src/tests/config.rs b/contracts/factory/src/tests/config.rs
index 06089bf7..75e0241c 100644
--- a/contracts/factory/src/tests/config.rs
+++ b/contracts/factory/src/tests/config.rs
@@ -245,24 +245,29 @@ fn factory_fails_to_init_lp_when_no_whitelisted_accounts() {
env.mock_all_auths();
env.cost_estimate().budget().reset_unlimited();
- let factory = FactoryClient::new(&env, &env.register(Factory, ()));
let multihop_wasm_hash = install_multihop_wasm(&env);
- let whitelisted_accounts = vec![&env];
+ let whitelisted_accounts: soroban_sdk::Vec = vec![&env];
let lp_wasm_hash = install_lp_contract(&env);
let stable_wasm_hash = install_stable_lp(&env);
let stake_wasm_hash = install_stake_wasm(&env);
let token_wasm_hash = install_token_wasm(&env);
- factory.initialize(
- &admin,
- &multihop_wasm_hash,
- &lp_wasm_hash,
- &stable_wasm_hash,
- &stake_wasm_hash,
- &token_wasm_hash,
- &whitelisted_accounts,
- &10u32,
+ let _ = FactoryClient::new(
+ &env,
+ &env.register(
+ Factory,
+ (
+ &admin,
+ &multihop_wasm_hash,
+ &lp_wasm_hash,
+ &stable_wasm_hash,
+ &stake_wasm_hash,
+ &token_wasm_hash,
+ whitelisted_accounts,
+ &10u32,
+ ),
+ ),
);
}
diff --git a/contracts/factory/src/tests/setup.rs b/contracts/factory/src/tests/setup.rs
index 9b8f6f9c..ba0d9641 100644
--- a/contracts/factory/src/tests/setup.rs
+++ b/contracts/factory/src/tests/setup.rs
@@ -3,7 +3,7 @@ use crate::{
token_contract,
};
use phoenix::utils::{LiquidityPoolInitInfo, StakeInitInfo, TokenInitInfo};
-use soroban_sdk::{testutils::Address as _, vec, Address, BytesN, Env, String};
+use soroban_sdk::{testutils::Address as _, vec, Address, BytesN, Env, String, Vec};
pub const ONE_DAY: u64 = 86400;
const TOKEN_WASM: &[u8] =
include_bytes!("../../../../target/wasm32-unknown-unknown/release/soroban_token_contract.wasm");
@@ -62,24 +62,29 @@ pub fn deploy_factory_contract<'a>(
admin: impl Into