Skip to content

Commit

Permalink
add permissionless settle auction function
Browse files Browse the repository at this point in the history
  • Loading branch information
nseguias committed Sep 23, 2024
1 parent 2f02db6 commit 03beb27
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 23 deletions.
13 changes: 6 additions & 7 deletions Cargo.lock

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

12 changes: 2 additions & 10 deletions contracts/injective-auction-pool/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,8 @@ pub fn execute(
auction_round,
auction_winner,
auction_winning_bid,
basket_rewards,
} => 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),
ExecuteMsg::TrySettleAuction {} => executions::try_settle_auction(deps, env, info),
}
}

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

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

#[error("Empty auction result")]
EmptyAuctionResult {},

#[error(
"Auction round missmatch. Unsettled auction round: {unsettled}. Latest auction round: {latest}"
)]
AuctionRoundMismatch {
unsettled: u64,
latest: u64,
},
}

impl From<semver::Error> for ContractError {
Expand Down
44 changes: 41 additions & 3 deletions contracts/injective-auction-pool/src/executions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use injective_std::types::cosmos::base::v1beta1::Coin;
use injective_std::types::injective::auction::v1beta1::MsgBid;

use crate::{
helpers::{new_auction_round, query_current_auction, validate_percentage},
helpers::{
new_auction_round, query_current_auction, query_latest_auction_result, validate_percentage,
},
state::{
Whitelisted, BIDDING_BALANCE, CONFIG, FUNDS_LOCKED, UNSETTLED_AUCTION,
WHITELISTED_ADDRESSES,
Expand Down Expand Up @@ -294,7 +296,6 @@ 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 @@ -329,11 +330,48 @@ pub fn settle_auction(
info,
Some(auction_winner),
Some(auction_winning_bid),
basket_reward,
unsettled_auction.basket,
)?;

Ok(Response::default()
.add_attribute("action", "settle_auction")
.add_messages(messages)
.add_attributes(attributes))
}

/// Tries to settle the latest auction permissionlessly
pub fn try_settle_auction(
deps: DepsMut,
env: Env,
info: MessageInfo,
) -> Result<Response, ContractError> {
let unsettled_auction = UNSETTLED_AUCTION.load(deps.storage)?;

let latest_auction_result_response = query_latest_auction_result(deps.as_ref())?
.last_auction_result
.ok_or(ContractError::EmptyAuctionResult {})?;

// prevents the contract from settling the auction round
if unsettled_auction.auction_round != latest_auction_result_response.round {
return Err(ContractError::AuctionRoundMismatch {
unsettled: unsettled_auction.auction_round,
latest: latest_auction_result_response.round,
});
}

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

let (messages, attributes) = new_auction_round(
deps,
&env,
info,
Some(latest_auction_result_response.winner),
Some(latest_auction_result_response.amount.parse()?),
unsettled_auction.basket,
)?;

Ok(Response::default()
.add_attribute("action", "try_settle_auction")
.add_messages(messages)
.add_attributes(attributes))
}
27 changes: 27 additions & 0 deletions contracts/injective-auction-pool/src/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
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,
OverflowError, QueryRequest, StdResult, Uint128, WasmMsg,
};
use injective_std::types::injective::auction::v1beta1::QueryLastAuctionResultResponse;

use crate::{
state::{Auction, BIDDING_BALANCE, CONFIG, TREASURE_CHEST_CONTRACTS, UNSETTLED_AUCTION},
Expand Down Expand Up @@ -60,6 +63,16 @@ pub(crate) fn new_auction_round(

let current_auction_round = current_auction_round_response.auction_round;

let new_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 Down Expand Up @@ -191,6 +204,7 @@ pub(crate) fn new_auction_round(
UNSETTLED_AUCTION.save(
deps.storage,
&Auction {
basket: new_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 @@ -214,6 +228,7 @@ pub(crate) fn new_auction_round(
UNSETTLED_AUCTION.save(
deps.storage,
&Auction {
basket: new_basket,
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 @@ -233,6 +248,7 @@ pub(crate) fn new_auction_round(
UNSETTLED_AUCTION.save(
deps.storage,
&Auction {
basket: new_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 Expand Up @@ -284,3 +300,14 @@ fn add_coin_to_basket(basket: &mut Vec<Coin>, coin: Coin) {
basket.push(coin);
}
}

/// Queries the latest auction result
pub(crate) fn query_latest_auction_result(deps: Deps) -> StdResult<QueryLastAuctionResultResponse> {
let last_auction_result_response: QueryLastAuctionResultResponse =
deps.querier.query(&QueryRequest::Stargate {
path: "/injective.auction.v1beta1.Query/LastAuctionResult".to_string(),
data: [].into(),
})?;

Ok(last_auction_result_response)
}
2 changes: 2 additions & 0 deletions contracts/injective-auction-pool/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ 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
4 changes: 1 addition & 3 deletions packages/injective_auction/src/auction_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,8 @@ 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>,
},
TrySettleAuction {},
}

#[cw_ownable_query]
Expand Down

0 comments on commit 03beb27

Please sign in to comment.