Skip to content

Commit

Permalink
Merge pull request #1 from PFC-Validator/feat/try-bid
Browse files Browse the repository at this point in the history
Merge feat/try-bid into main
  • Loading branch information
nseguias authored Mar 4, 2024
2 parents 8d2cb4a + 024f121 commit a2ef0eb
Show file tree
Hide file tree
Showing 10 changed files with 488 additions and 87 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

82 changes: 63 additions & 19 deletions contracts/injective-auction-pool/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,82 @@
use cosmwasm_std::entry_point;
use cosmwasm_std::{Binary, Deps, DepsMut, Env, MessageInfo, QueryRequest, Response, StdResult};
use std::str::FromStr;

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

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

use crate::error::ContractError;
use crate::executions;
use crate::helpers::{query_current_auction, validate_rewards_fee};
use crate::state::{CONFIG, CURRENT_AUCTION_NUMBER};
use crate::executions::{self, settle_auction};
use crate::helpers::{query_current_auction, validate_percentage};
use crate::state::{Auction, BIDDING_BALANCE, CONFIG, CURRENT_AUCTION};

const CONTRACT_NAME: &str = "crates.io:injective-auction-pool";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");

#[entry_point]
pub fn instantiate(
deps: DepsMut,
_env: Env,
env: Env,
_info: MessageInfo,
msg: InstantiateMsg,
) -> Result<Response, ContractError> {
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

let whitelisted_addresses = msg
.whitelisted_addresses
.iter()
.map(|addr| deps.api.addr_validate(addr))
.collect::<Result<Vec<Addr>, _>>()?;

CONFIG.save(
deps.storage,
&Config {
rewards_fee: validate_rewards_fee(msg.rewards_fee)?,
native_denom: msg.native_denom,
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)?,
bot_address: deps.api.addr_validate(&msg.bot_address)?,
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)?,
},
)?;

let current_auction_round = query_current_auction(deps.as_ref())?
// 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)?;
CURRENT_AUCTION_NUMBER.save(deps.storage, &current_auction_round)?;

//todo mint lp for current auction
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();

Ok(Response::default())
CURRENT_AUCTION.save(
deps.storage,
&Auction {
basket,
auction_round,
lp_subdenom: 0,
closing_time: current_auction_round_response.auction_closing_time(),
},
)?;

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

// create a new denom for the current auction round
let msg = msg
.token_factory_type
.create_denom(env.contract.address.clone(), "0");

Ok(Response::default().add_message(msg))
}

#[entry_point]
Expand All @@ -49,14 +87,20 @@ pub fn execute(
msg: ExecuteMsg,
) -> Result<Response, ContractError> {
match msg {
ExecuteMsg::TryBid {} => executions::try_bid(deps, env, info),
ExecuteMsg::TryBid {
auction_round,
basket_value,
} => executions::try_bid(deps, env, info, auction_round, basket_value),
ExecuteMsg::JoinPool {
auction_round: auction_id,
} => executions::join_pool(deps, env, info, auction_id),
auction_round,
basket_value,
} => executions::join_pool(deps, env, info, auction_round, basket_value),
ExecuteMsg::ExitPool {} => executions::exit_pool(deps, env, info),
ExecuteMsg::SettleAuction {} => {
unimplemented!()
},
ExecuteMsg::SettleAuction {
auction_round,
auction_winner,
auction_winning_bid,
} => settle_auction(deps, env, info, auction_round, auction_winner, auction_winning_bid),
}
}

Expand Down
23 changes: 20 additions & 3 deletions contracts/injective-auction-pool/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cosmwasm_std::StdError;
use cosmwasm_std::{Decimal, OverflowError, StdError};
use cw_utils::PaymentError;
use thiserror::Error;

Expand All @@ -13,8 +13,10 @@ pub enum ContractError {
#[error("Unauthorized")]
Unauthorized {},

#[error("Invalid rewards fee")]
InvalidRewardsFee,
#[error("Invalid rate: {rate}. Rate must be between 0.0 and 1.0")]
InvalidRate {
rate: Decimal,
},

#[error("Invalid auction round. Current auction round: {current_auction_round}, auction round: {auction_round}")]
InvalidAuctionRound {
Expand All @@ -29,4 +31,19 @@ pub enum ContractError {

#[error("Couldn't parse the current auction query response")]
CurrentAuctionQueryError,

#[error("Cannot bid")]
CannotBid,

#[error("No bids found for the current auction round")]
NoBidsFound,

#[error("Auction round has not finished")]
AuctionRoundHasNotFinished,

#[error("Max bid percentage must be between 0 and 100 percent")]
InvalidMaxBidPercentage,

#[error("Overflow error: {0}")]
OverflowError(#[from] OverflowError),
}
Loading

0 comments on commit a2ef0eb

Please sign in to comment.