Skip to content

Commit

Permalink
Merge pull request #3 from PFC-Validator/fix/pfc-feedback
Browse files Browse the repository at this point in the history
fix(smart-contracts): refactor contract to address pfc comments
  • Loading branch information
PFC-developer authored Mar 15, 2024
2 parents 1a00d52 + bcb32e5 commit 57e8a1e
Show file tree
Hide file tree
Showing 11 changed files with 639 additions and 315 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions contracts/injective-auction-pool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ prost = { workspace = true }
cw-utils = { workspace = true }
treasurechest = { workspace = true }
cw-paginate-storage = { workspace = true }
cw-ownable = { workspace = true }

[dev-dependencies]
cw-multi-test = { workspace = true }
87 changes: 39 additions & 48 deletions contracts/injective-auction-pool/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
use std::str::FromStr;

use cosmwasm_std::{entry_point, Addr, Coin, Uint128};
use cosmwasm_std::{attr, entry_point, to_json_binary, Attribute};
use cosmwasm_std::{Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult};
use cw2::set_contract_version;

use injective_auction::auction_pool::{Config, ExecuteMsg, InstantiateMsg, QueryMsg};

use crate::error::ContractError;
use crate::executions::{self, settle_auction};
use crate::helpers::{query_current_auction, validate_percentage};
use crate::helpers::{new_auction_round, validate_percentage};
use crate::queries;
use crate::state::{Auction, BIDDING_BALANCE, CONFIG, UNSETTLED_AUCTION};
use crate::state::{Whitelisted, CONFIG, FUNDS_LOCKED, WHITELISTED_ADDRESSES};

const CONTRACT_NAME: &str = "crates.io:injective-auction-pool";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
Expand All @@ -25,64 +23,47 @@ pub fn instantiate(
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

let owner = deps.api.addr_validate(&msg.owner.unwrap_or(info.sender.to_string()))?;
cw_ownable::initialize_owner(deps.storage, deps.api, Some(owner.to_string().as_str()))?;

// Ensure that the contract is funded with at least the minimum balance
let amount = cw_utils::must_pay(&info, &msg.native_denom)?;
if amount < msg.min_balance {
return Err(ContractError::InsufficientFunds {
native_denom: msg.native_denom,
min_balance: msg.min_balance,
});
}

let whitelisted_addresses = msg
.whitelisted_addresses
.iter()
.map(|addr| deps.api.addr_validate(addr))
.collect::<Result<Vec<Addr>, _>>()?;
let mut whitelisted: Vec<Attribute> = vec![];

for addr in msg.whitelisted_addresses {
let addr = deps.api.addr_validate(&addr)?;
WHITELISTED_ADDRESSES.save(deps.storage, &addr, &Whitelisted {})?;
whitelisted.push(attr("whitelisted_address", addr.to_string()));
}

CONFIG.save(
deps.storage,
&Config {
owner,
native_denom: msg.native_denom,
min_balance: msg.min_balance,
token_factory_type: msg.token_factory_type.clone(),
rewards_fee: validate_percentage(msg.rewards_fee)?,
rewards_fee_addr: deps.api.addr_validate(&msg.rewards_fee_addr)?,
whitelisted_addresses,
min_next_bid_increment_rate: validate_percentage(msg.min_next_bid_increment_rate)?,
treasury_chest_code_id: msg.treasury_chest_code_id,
min_return: validate_percentage(msg.min_return)?,
},
)?;

// fetch current auction details and save them in the contract state
let current_auction_round_response = query_current_auction(deps.as_ref())?;

let auction_round = current_auction_round_response
.auction_round
.ok_or(ContractError::CurrentAuctionQueryError)?;

let basket = current_auction_round_response
.amount
.iter()
.map(|coin| Coin {
amount: Uint128::from_str(&coin.amount).expect("Failed to parse coin amount"),
denom: coin.denom.clone(),
})
.collect();

UNSETTLED_AUCTION.save(
deps.storage,
&Auction {
basket,
auction_round,
lp_subdenom: 1,
closing_time: current_auction_round_response.auction_closing_time(),
},
)?;

BIDDING_BALANCE.save(deps.storage, &Uint128::zero())?;
FUNDS_LOCKED.save(deps.storage, &false)?;

// create a new denom for the current auction round
let msg = msg.token_factory_type.create_denom(env.contract.address.clone(), "1");
let (messages, attributes) = new_auction_round(deps, &env, info, None, None)?;

Ok(Response::default()
.add_message(msg)
.add_messages(messages)
.add_attribute("action", "instantiate")
.add_attribute("auction_round", auction_round.to_string())
.add_attribute("lp_subdenom", "1"))
.add_attributes(attributes))
}

#[entry_point]
Expand All @@ -94,23 +75,27 @@ pub fn execute(
) -> Result<Response, ContractError> {
match msg {
ExecuteMsg::UpdateConfig {
owner,
rewards_fee,
rewards_fee_addr,
whitelist_addresses,
min_next_bid_increment_rate,
min_return,
} => executions::update_config(
deps,
env,
info,
owner,
rewards_fee,
rewards_fee_addr,
whitelist_addresses,
min_next_bid_increment_rate,
min_return,
),
ExecuteMsg::UpdateOwnership(action) => {
cw_ownable::update_ownership(deps, &env.block, &info.sender, action)?;
Ok(Response::default())
},
ExecuteMsg::UpdateWhiteListedAddresses {
remove,
add,
} => executions::update_whitelisted_addresses(deps, env, info, remove, add),
ExecuteMsg::TryBid {
auction_round,
basket_value,
Expand All @@ -132,10 +117,16 @@ pub fn execute(
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::Config {} => queries::query_config(deps),
QueryMsg::WhitelistedAddresses {} => queries::query_whitelisted_addresses(deps),
QueryMsg::Ownership {} => {
let ownership = cw_ownable::get_ownership(deps.storage)?;
to_json_binary(&ownership)
},
QueryMsg::TreasureChestContracts {
start_after,
limit,
} => queries::query_treasure_chest_contracts(deps, start_after, limit),
QueryMsg::BiddingBalance {} => queries::query_bidding_balance(deps),
QueryMsg::FundsLocked {} => to_json_binary(&FUNDS_LOCKED.load(deps.storage)?),
}
}
28 changes: 27 additions & 1 deletion contracts/injective-auction-pool/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use cosmwasm_std::{Decimal, Instantiate2AddressError, OverflowError, StdError};
use cosmwasm_std::{Decimal, Instantiate2AddressError, OverflowError, StdError, Uint128};
use cw_ownable::OwnershipError;
use cw_utils::PaymentError;
use thiserror::Error;

Expand Down Expand Up @@ -49,4 +50,29 @@ pub enum ContractError {

#[error("Instantiate address error: {0}")]
Instantiate2AddressError(#[from] Instantiate2AddressError),

#[error("Missing auction winner")]
MissingAuctionWinner,

#[error("Missing auction winning bid")]
MissingAuctionWinningBid,

#[error("Insufficient funds. Must deposit at least {min_balance} {native_denom} to instantiate the contract")]
InsufficientFunds {
native_denom: String,
min_balance: Uint128,
},

#[error(transparent)]
Ownership(#[from] OwnershipError),

#[error("Address already whitelisted: {address}")]
AddressAlreadyWhitelisted {
address: String,
},

#[error("Address not whitelisted: {address}")]
AddressNotWhitelisted {
address: String,
},
}
Loading

0 comments on commit 57e8a1e

Please sign in to comment.