Skip to content

Commit

Permalink
fix: add validation on settle_auction to make sure the previous aucti…
Browse files Browse the repository at this point in the history
…on has been settled; remove rewards_basket from state as it will always be outdated, pass it on settle auction as input parameter now
  • Loading branch information
nseguias committed Sep 19, 2024
1 parent 22e546a commit b79b69d
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 49 deletions.
13 changes: 11 additions & 2 deletions contracts/injective-auction-pool/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub fn instantiate(

FUNDS_LOCKED.save(deps.storage, &false)?;

let (messages, attributes) = new_auction_round(deps, &env, info, None, None)?;
let (messages, attributes) = new_auction_round(deps, &env, info, None, None, vec![])?;

Ok(Response::default()
.add_messages(messages)
Expand Down Expand Up @@ -111,7 +111,16 @@ pub fn execute(
auction_round,
auction_winner,
auction_winning_bid,
} => settle_auction(deps, env, info, auction_round, auction_winner, auction_winning_bid),
basket_rewards,
} => settle_auction(
deps,
env,
info,
auction_round,
auction_winner,
auction_winning_bid,
basket_rewards,
),
}
}

Expand Down
12 changes: 12 additions & 0 deletions contracts/injective-auction-pool/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ pub enum ContractError {

#[error("Semver parsing error: {0}")]
SemVer(String),

#[error(
"Previous auction round has not been settled. Unsettled auction round: {unsettled_auction_round}.\
Current auction round: {current_auction_round}"
)]
AuctionRoundNotSettled {
unsettled_auction_round: u64,
current_auction_round: u64,
},

#[error("Basket rewards is empty")]
EmptyBasketRewards {},
}

impl From<semver::Error> for ContractError {
Expand Down
26 changes: 22 additions & 4 deletions contracts/injective-auction-pool/src/executions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,21 @@ pub(crate) fn join_pool(
});
}

let mut messages = vec![];
let unsettled_auction = UNSETTLED_AUCTION.load(deps.storage)?;

// if the current auction round is different from the unsettled auction round,
// prevent the user from joining the pool
if unsettled_auction.auction_round != current_auction_round.u64() {
return Err(ContractError::AuctionRoundNotSettled {
unsettled_auction_round: unsettled_auction.auction_round,
current_auction_round: current_auction_round.u64(),
});
}

// mint the lp token and send it to the user
let lp_subdenom = UNSETTLED_AUCTION.load(deps.storage)?.lp_subdenom;
let mut messages = vec![];
let lp_subdenom = unsettled_auction.lp_subdenom;

messages.push(config.token_factory_type.mint(
env.contract.address.clone(),
format!("factory/{}/auction.{}", env.contract.address.clone(), lp_subdenom).as_str(),
Expand Down Expand Up @@ -283,6 +294,7 @@ pub fn settle_auction(
auction_round: u64,
auction_winner: String,
auction_winning_bid: Uint128,
basket_reward: Vec<cosmwasm_std::Coin>,
) -> Result<Response, ContractError> {
// only whitelist addresses can settle the auction for now until the
// contract can query the aunction module for a specific auction round
Expand Down Expand Up @@ -311,8 +323,14 @@ pub fn settle_auction(

FUNDS_LOCKED.save(deps.storage, &false)?;

let (messages, attributes) =
new_auction_round(deps, &env, info, Some(auction_winner), Some(auction_winning_bid))?;
let (messages, attributes) = new_auction_round(
deps,
&env,
info,
Some(auction_winner),
Some(auction_winning_bid),
basket_reward,
)?;

Ok(Response::default()
.add_attribute("action", "settle_auction")
Expand Down
46 changes: 6 additions & 40 deletions contracts/injective-auction-pool/src/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::str::FromStr;

use cosmwasm_std::{
attr, instantiate2_address, to_json_binary, Addr, Attribute, BankMsg, Binary, CanonicalAddr,
CodeInfoResponse, Coin, CosmosMsg, CustomQuery, Decimal, Deps, DepsMut, Env, MessageInfo,
Expand Down Expand Up @@ -53,29 +51,15 @@ pub(crate) fn new_auction_round(
_info: MessageInfo,
auction_winner: Option<String>,
auction_winning_bid: Option<Uint128>,
basket_reward: Vec<Coin>,
) -> Result<(Vec<CosmosMsg>, Vec<Attribute>), ContractError> {
let config = CONFIG.load(deps.storage)?;

// TODO: check that info.sender is whitelisted & test it
// if !WHITELISTED_ADDRESSES.has(deps.storage, &_info.sender) {
// return Err(ContractError::Unauthorized {});
// }

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

let current_auction_round = current_auction_round_response.auction_round;

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

let unsettled_auction = UNSETTLED_AUCTION.may_load(deps.storage)?;

let mut attributes = vec![];
Expand All @@ -100,7 +84,6 @@ pub(crate) fn new_auction_round(
}),
)?;

let unsettled_basket = unsettled_auction.basket;
let mut basket_fees = vec![];
let mut basket_to_treasure_chest = vec![];

Expand All @@ -117,7 +100,11 @@ pub(crate) fn new_auction_round(
}

// split the basket, taking the rewards fees into account
for coin in unsettled_basket.iter() {
if basket_reward.is_empty() {
return Err(ContractError::EmptyBasketRewards {});
}

for coin in basket_reward.iter() {
let fee = coin.amount * config.rewards_fee;
basket_fees.push(Coin {
denom: coin.denom.clone(),
Expand Down Expand Up @@ -189,20 +176,9 @@ pub(crate) fn new_auction_round(
format!("auction.{}", new_subdenom).as_str(),
));

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

UNSETTLED_AUCTION.save(
deps.storage,
&Auction {
basket,
auction_round: current_auction_round_response.auction_round.u64(),
lp_subdenom: new_subdenom,
closing_time: current_auction_round_response.auction_closing_time.i64()
Expand All @@ -226,15 +202,6 @@ pub(crate) fn new_auction_round(
UNSETTLED_AUCTION.save(
deps.storage,
&Auction {
basket: current_auction_round_response
.amount
.iter()
.map(|coin| Coin {
amount: Uint128::from_str(&coin.amount.to_string())
.expect("Failed to parse coin amount"),
denom: coin.denom.clone(),
})
.collect(),
auction_round: current_auction_round_response.auction_round.u64(),
lp_subdenom: unsettled_auction.lp_subdenom,
closing_time: current_auction_round_response.auction_closing_time.i64()
Expand All @@ -254,7 +221,6 @@ pub(crate) fn new_auction_round(
UNSETTLED_AUCTION.save(
deps.storage,
&Auction {
basket: current_basket,
auction_round: current_auction_round_response.auction_round.u64(),
lp_subdenom: 0,
closing_time: current_auction_round_response.auction_closing_time.i64() as u64,
Expand Down
2 changes: 0 additions & 2 deletions contracts/injective-auction-pool/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ use injective_auction::auction_pool::Config;

#[cw_serde]
pub struct Auction {
/// The coins in the basket being auctioned
pub basket: Vec<Coin>,
/// The auction round number
pub auction_round: u64,
/// A unique number that is used to create new token factory denoms
Expand Down
6 changes: 5 additions & 1 deletion packages/injective_auction/src/auction_pool.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::Coin;
use cosmwasm_std::{Addr, Decimal, Uint128};
use cw_ownable::{cw_ownable_execute, cw_ownable_query};
use treasurechest::tf::{injective::denom::Coin, tokenfactory::TokenFactoryType};
use treasurechest::tf::tokenfactory::TokenFactoryType;

#[cw_serde]
pub struct InstantiateMsg {
Expand Down Expand Up @@ -61,6 +62,9 @@ pub enum ExecuteMsg {
auction_winner: String,
/// The amount bid by the winner of the auction
auction_winning_bid: Uint128,
/// The rewards to be sent to the vault. As rewards are being added to the auction module
/// on each block, the eaact amount can only be know once the auction is finished
basket_rewards: Vec<Coin>,
},
}

Expand Down

0 comments on commit b79b69d

Please sign in to comment.