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

Factory: Add verified/non-verified state to the pool #59

Merged
merged 15 commits into from
May 13, 2024
Merged
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
40 changes: 27 additions & 13 deletions contracts/factory/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ use crate::{
querier::query_pair_info,
state::{
check_asset_infos, pair_key, read_pairs, Config, TmpPoolInfo, CONFIG, OWNERSHIP_PROPOSAL,
PAIRS, PAIRS_TO_MIGRATE, PAIR_CONFIGS, PERMISSIONLESS_DEPOSIT, STAKING_ADDRESSES,
TMP_PAIR_INFO,
PAIRS, PAIRS_TO_MIGRATE, PAIR_CONFIGS, STAKING_ADDRESSES, TMP_PAIR_INFO,
},
};

Expand Down Expand Up @@ -77,7 +76,8 @@ pub fn instantiate(
fee_address: addr_opt_validate(deps.api, &msg.fee_address)?,
max_referral_commission: msg.max_referral_commission,
default_stake_config: msg.default_stake_config,
only_owner_can_create_pools: true,
only_owner_can_create_pools: false,
pool_creation_fee: msg.pool_creation_fee,
trading_starts: msg.trading_starts,
};

Expand Down Expand Up @@ -182,7 +182,6 @@ pub fn execute(
total_fee_bps,
staking_config,
Vec::new(),
false,
),
ExecuteMsg::Deregister { asset_infos } => {
deregister_pool_and_staking(deps, info, asset_infos)
Expand Down Expand Up @@ -243,7 +242,6 @@ pub fn execute(
total_fee_bps,
staking_config,
distribution_flows,
false,
),
ExecuteMsg::CreateDistributionFlow {
asset_infos,
Expand All @@ -260,9 +258,7 @@ fn receive_cw20_message(
info: MessageInfo,
msg: Cw20ReceiveMsg,
) -> Result<Response, ContractError> {
let required_deposit = PERMISSIONLESS_DEPOSIT
.load(deps.storage)
.map_err(|_| ContractError::DepositNotSet {})?;
let required_deposit = CONFIG.load(deps.storage)?.pool_creation_fee;
let deposit = Asset {
info: AssetInfo::Cw20Token(info.sender.to_string()),
amount: msg.amount,
Expand Down Expand Up @@ -292,7 +288,6 @@ fn receive_cw20_message(
total_fee_bps,
staking_config,
Vec::new(),
true,
),
ReceiveMsg::CreatePoolAndDistributionFlows {
pool_type,
Expand All @@ -311,7 +306,6 @@ fn receive_cw20_message(
total_fee_bps,
staking_config,
distribution_flows,
true,
),
}
}
Expand Down Expand Up @@ -473,7 +467,6 @@ pub fn execute_create_pair(
total_fee_bps: Option<u16>,
staking_config: PartialStakeConfig,
distribution_flows: Vec<DistributionFlow>,
deposit_sent: bool,
) -> Result<Response, ContractError> {
let asset_infos = check_asset_infos(deps.api, &asset_infos)?;

Expand All @@ -482,10 +475,14 @@ pub fn execute_create_pair(
if config.only_owner_can_create_pools && info.sender != config.owner {
return Err(ContractError::Unauthorized {});
}
if !config.only_owner_can_create_pools && !deposit_sent {

if !config.only_owner_can_create_pools && !permissionless_fee_sent(&deps, &info) {
return Err(ContractError::PermissionlessRequiresDeposit {});
}

// pool is verified if it's created by the admin/owner of the contract
let verified = info.sender == config.owner;

if PAIRS.has(deps.storage, &pair_key(&asset_infos)) {
return Err(ContractError::PoolWasCreated {});
}
Expand Down Expand Up @@ -530,6 +527,7 @@ pub fn execute_create_pair(
total_fee_bps: total_fee_bps.unwrap_or(pair_config.fee_config.total_fee_bps),
protocol_fee_bps: pair_config.fee_config.protocol_fee_bps,
},
verified,
circuit_breaker: None,
})?,
funds: vec![],
Expand Down Expand Up @@ -589,6 +587,14 @@ pub fn reply(
reply::instantiate_pair(deps, env, res)
}

fn permissionless_fee_sent(deps: &DepsMut<CoreumQueries>, info: &MessageInfo) -> bool {
let deposit_required = CONFIG.load(deps.storage).unwrap().pool_creation_fee;

info.funds.iter().any(|coin| {
coin.amount >= deposit_required.amount && coin.denom == deposit_required.info.to_string()
})
}

pub mod reply {
use cosmwasm_std::wasm_execute;
use cw_utils::MsgInstantiateContractResponse;
Expand Down Expand Up @@ -727,6 +733,8 @@ pub fn deregister_pool_and_staking(
/// * **QueryMsg::BlacklistedPoolTypes {}** Returns a vector that contains blacklisted pair types (pair types that cannot get ASTRO emissions).
///
/// * **QueryMsg::PoolsToMigrate {}** Returns a vector that contains pair addresses that are not migrated.
///
/// * **QueryMsg::PoolsType { address }** Returns boolean.`true` if the pool is verified, `false` if non-verified
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps<CoreumQueries>, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
Expand Down Expand Up @@ -838,7 +846,13 @@ pub fn migrate(
ensure_from_older_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
}
MigrateMsg::AddPermissionlessPoolDeposit(asset) => {
PERMISSIONLESS_DEPOSIT.save(deps.storage, &asset)?;
CONFIG.update(deps.storage, |old_config| -> StdResult<_> {
let new_config = Config {
pool_creation_fee: asset,
..old_config
};
Ok(new_config)
})?;
}
};

Expand Down
5 changes: 2 additions & 3 deletions contracts/factory/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub struct Config {
pub default_stake_config: DefaultStakeConfig,
/// When this is set to `true`, only the owner can create pairs
pub only_owner_can_create_pools: bool,
/// Fee required for the pool to be established by a non-admin
pub pool_creation_fee: Asset,
/// The block time until which trading is disabled
pub trading_starts: Option<u64>,
}
Expand All @@ -44,9 +46,6 @@ pub const TMP_PAIR_INFO: Item<TmpPoolInfo> = Item::new("tmp_pair_info");
/// Saves factory settings
pub const CONFIG: Item<Config> = Item::new("config");

/// If factory is permissionless, require deposit to create a pool
pub const PERMISSIONLESS_DEPOSIT: Item<Asset> = Item::new("permissionless_deposit");

/// Saves created pairs (from olders to latest)
pub const PAIRS: Map<&[u8], Addr> = Map::new("pair_info");

Expand Down
Loading
Loading