Skip to content

Commit

Permalink
Server admin (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
Art3miX authored May 13, 2024
1 parent 4b242c7 commit 294db8e
Show file tree
Hide file tree
Showing 12 changed files with 162 additions and 28 deletions.
58 changes: 45 additions & 13 deletions contracts/auction/auctions_manager/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use valence_package::event_indexing::ValenceEvent;

use crate::error::ContractError;
use crate::msg::{ExecuteMsg, InstantiateMsg, MigrateMsg};
use crate::state::AUCTION_CODE_ID;
use crate::state::{AUCTION_CODE_ID, SERVER_ADDR};

const CONTRACT_NAME: &str = "crates.io:auctions-manager";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
Expand All @@ -33,6 +33,7 @@ pub fn instantiate(
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

ADMIN.save(deps.storage, &info.sender)?;
SERVER_ADDR.save(deps.storage, &deps.api.addr_validate(&msg.server_addr)?)?;
AUCTION_CODE_ID.save(deps.storage, &msg.auction_code_id)?;

for min_amount in msg.min_auction_amount {
Expand Down Expand Up @@ -93,13 +94,48 @@ pub fn execute(
Ok(Response::default().add_message(msg))
}
ExecuteMsg::Admin(admin_msg) => admin::handle_msg(deps, env, info, *admin_msg),
ExecuteMsg::Server(server_msg) => server::handle_msg(deps, env, info, server_msg),
ExecuteMsg::ApproveAdminChange {} => {
let event = ValenceEvent::AuctionManagerApproveAdminChange {};
Ok(approve_admin_change(deps, &env, &info)?.add_event(event.into()))
}
}
}

mod server {
use cosmwasm_std::{ensure, to_json_binary, WasmMsg};

use crate::msg::ServerMsgs;

use super::*;

pub fn handle_msg(
deps: DepsMut,
_env: Env,
info: MessageInfo,
msg: ServerMsgs,
) -> Result<Response, ContractError> {
// Verify that the sender is the server
let server_addr = SERVER_ADDR.load(deps.storage)?;
ensure!(info.sender == server_addr, ContractError::NotServer);

match msg {
ServerMsgs::OpenAuction { pair, params } => {
let pair_addr = PAIRS.load(deps.storage, pair)?;
let msg = WasmMsg::Execute {
contract_addr: pair_addr.to_string(),
msg: to_json_binary(&auction::msg::ExecuteMsg::Admin(Box::new(
auction::msg::AdminMsgs::StartAuction(params),
)))?,
funds: vec![],
};

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

mod admin {
use auction_package::helpers::{cancel_admin_change, start_admin_change, verify_admin};
use cosmwasm_std::{to_json_binary, SubMsg, WasmMsg};
Expand Down Expand Up @@ -179,18 +215,6 @@ mod admin {

Ok(Response::default().add_message(msg))
}
AdminMsgs::OpenAuction { pair, params } => {
let pair_addr = PAIRS.load(deps.storage, pair)?;
let msg = WasmMsg::Execute {
contract_addr: pair_addr.to_string(),
msg: to_json_binary(&auction::msg::ExecuteMsg::Admin(Box::new(
auction::msg::AdminMsgs::StartAuction(params),
)))?,
funds: vec![],
};

Ok(Response::default().add_message(msg))
}
AdminMsgs::UpdateAuctionId { code_id } => {
AUCTION_CODE_ID.save(deps.storage, &code_id)?;

Expand Down Expand Up @@ -268,6 +292,13 @@ mod admin {

Ok(Response::default().add_event(event.into()))
}
AdminMsgs::ChangeServerAddr { addr } => {
SERVER_ADDR.save(deps.storage, &deps.api.addr_validate(&addr)?)?;

let event = ValenceEvent::AuctionManagerChangeServerAddr { addr };

Ok(Response::default().add_event(event.into()))
}
AdminMsgs::StartAdminChange { addr, expiration } => {
let event = ValenceEvent::AuctionManagerStartAdminChange {
admin: addr.clone(),
Expand Down Expand Up @@ -317,6 +348,7 @@ pub fn query(deps: Deps, _env: Env, msg: AuctionsManagerQueryMsg) -> StdResult<B
to_json_binary(&MIN_AUCTION_AMOUNT.load(deps.storage, denom)?)
}
AuctionsManagerQueryMsg::GetAdmin => to_json_binary(&ADMIN.load(deps.storage)?),
AuctionsManagerQueryMsg::GetServerAddr => to_json_binary(&SERVER_ADDR.load(deps.storage)?),
}
}

Expand Down
3 changes: 3 additions & 0 deletions contracts/auction/auctions_manager/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ pub enum ContractError {
#[error("Not the new admin")]
NotNewAdmin,

#[error("Sender is not the server")]
NotServer,

#[error("Not the new admin")]
AdminChangeExpired,
}
Expand Down
17 changes: 13 additions & 4 deletions contracts/auction/auctions_manager/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use cw_utils::Expiration;
pub struct InstantiateMsg {
pub auction_code_id: u64,
pub min_auction_amount: Vec<(String, MinAmount)>,
pub server_addr: String,
}

#[cw_serde]
Expand All @@ -18,6 +19,7 @@ pub enum ExecuteMsg {
FinishAuction { pair: Pair, limit: u64 },
ApproveAdminChange {},
Admin(Box<AdminMsgs>),
Server(ServerMsgs),
}

#[cw_serde]
Expand All @@ -26,17 +28,21 @@ pub enum MigrateMsg {
ToV1 {},
}

#[cw_serde]
pub enum ServerMsgs {
OpenAuction {
pair: Pair,
params: NewAuctionParams,
},
}

#[cw_serde]
pub enum AdminMsgs {
NewAuction {
msg: auction::msg::InstantiateMsg,
label: String,
min_amount: Option<MinAmount>,
},
OpenAuction {
pair: Pair,
params: NewAuctionParams,
},
PauseAuction {
pair: Pair,
},
Expand Down Expand Up @@ -70,6 +76,9 @@ pub enum AdminMsgs {
code_id: u64,
msg: auction::msg::MigrateMsg,
},
ChangeServerAddr {
addr: String,
},
StartAdminChange {
addr: String,
expiration: Expiration,
Expand Down
2 changes: 2 additions & 0 deletions contracts/auction/auctions_manager/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use cosmwasm_std::Addr;
use cw_storage_plus::Item;

pub const SERVER_ADDR: Item<Addr> = Item::new("server_addr");
pub const AUCTION_CODE_ID: Item<u64> = Item::new("auction_code_id");
3 changes: 3 additions & 0 deletions packages/auction-package/src/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,7 @@ pub enum AuctionsManagerQueryMsg {

#[returns(Addr)]
GetAdmin,

#[returns(Addr)]
GetServerAddr,
}
6 changes: 6 additions & 0 deletions packages/valence-package/src/event_indexing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ where
AuctionManagerStartAdminChange {
admin: String,
},
AuctionManagerChangeServerAddr {
addr: String,
},
AuctionManagerCancelAdminChange {},
AuctionManagerApproveAdminChange {},

Expand Down Expand Up @@ -325,6 +328,9 @@ impl<E: serde::Serialize> fmt::Display for ValenceGenericEvent<E> {
ValenceGenericEvent::AuctionManagerApproveAdminChange {} => {
write!(f, "auction-manager-approve-admin-change")
}
ValenceGenericEvent::AuctionManagerChangeServerAddr { .. } => {
write!(f, "auction-manager-change-server-addr")
}

// auctions
ValenceGenericEvent::AuctionInit { .. } => write!(f, "auction-init"),
Expand Down
7 changes: 4 additions & 3 deletions tests/rust-tests/src/suite/instantiates/auctions_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ impl From<AuctionsManagerInstantiate> for auctions_manager::msg::InstantiateMsg
}

impl AuctionsManagerInstantiate {
pub fn default(auction_code_id: u64) -> Self {
Self::new(auction_code_id)
pub fn default(auction_code_id: u64, server_addr: String) -> Self {
Self::new(auction_code_id, server_addr)
}

pub fn new(auction_code_id: u64) -> Self {
pub fn new(auction_code_id: u64, server_addr: String) -> Self {
let min_auction_amount = vec![
(
ATOM.to_string(),
Expand Down Expand Up @@ -47,6 +47,7 @@ impl AuctionsManagerInstantiate {
msg: auctions_manager::msg::InstantiateMsg {
auction_code_id,
min_auction_amount,
server_addr,
},
}
}
Expand Down
35 changes: 31 additions & 4 deletions tests/rust-tests/src/suite/suite_auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,17 @@ impl Suite {
end_block: u64,
) -> Result<AppResponse, anyhow::Error> {
self.app.execute_contract(
self.admin.clone(),
self.mm.clone(),
self.auctions_manager_addr.clone(),
&auctions_manager::msg::ExecuteMsg::Admin(Box::new(
auctions_manager::msg::AdminMsgs::OpenAuction {
&auctions_manager::msg::ExecuteMsg::Server(
auctions_manager::msg::ServerMsgs::OpenAuction {
pair,
params: NewAuctionParams {
start_block,
end_block,
},
},
)),
),
&[],
)
}
Expand Down Expand Up @@ -626,6 +626,23 @@ impl Suite {

self.astro_swap(pool_addr, coin)
}

pub fn change_server_addr(&mut self, addr: Addr) -> &mut Self {
self.app
.execute_contract(
self.admin.clone(),
self.auctions_manager_addr.clone(),
&auctions_manager::msg::ExecuteMsg::Admin(Box::new(
auctions_manager::msg::AdminMsgs::ChangeServerAddr {
addr: addr.to_string(),
},
)),
&[],
)
.unwrap();

self
}
}

// Queries
Expand Down Expand Up @@ -771,6 +788,16 @@ impl Suite {
.unwrap();
total_got / Decimal::from_atomics(multiply, 0).unwrap()
}

pub fn query_server_addr(&self) -> Addr {
self.app
.wrap()
.query_wasm_smart(
self.auctions_manager_addr.clone(),
&auction_package::msgs::AuctionsManagerQueryMsg::GetServerAddr {},
)
.unwrap()
}
}

// Helpers
Expand Down
2 changes: 1 addition & 1 deletion tests/rust-tests/src/suite/suite_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ impl SuiteBuilder {
// init Auctions manager contract
let auctions_manager_addr = self.init_auctions_manager(
app,
AuctionsManagerInstantiate::new(self.auction_code_id).into(),
AuctionsManagerInstantiate::new(self.auction_code_id, self.mm.to_string()).into(),
);

// init price_oracle contract
Expand Down
42 changes: 41 additions & 1 deletion tests/rust-tests/src/tests_auctions/test_auction.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use core::panic;

use auction::state::{ActiveAuction, ActiveAuctionStatus};
use auction::{
msg::NewAuctionParams,
state::{ActiveAuction, ActiveAuctionStatus},
};
use auction_package::{error::AuctionError, states::TWAP_PRICES};
use cosmwasm_std::{
coin, coins, from_json, testing::mock_env, Addr, Binary, Decimal, Timestamp, Uint128,
Expand Down Expand Up @@ -976,3 +979,40 @@ fn test_all_pairs_query() {
let pairs = suite.query_auctions_manager_all_pairs();
assert!(!pairs.is_empty())
}

#[test]
fn test_only_server_can_open_auction() {
let mut suite = Suite::default();

suite.auction_funds(
suite.get_account_addr(0),
suite
.auction_addrs
.get(&suite.pair.clone().into())
.unwrap()
.clone(),
&coins(10_u128, suite.pair.0.clone()),
);

let err = suite
.app
.execute_contract(
suite.admin.clone(),
suite.auctions_manager_addr.clone(),
&auctions_manager::msg::ExecuteMsg::Server(
auctions_manager::msg::ServerMsgs::OpenAuction {
pair: suite.pair,
params: NewAuctionParams {
start_block: None,
end_block: suite.app.block_info().height + 1000,
},
},
),
&[],
)
.unwrap_err()
.downcast::<auctions_manager::error::ContractError>()
.unwrap();

assert_eq!(err, auctions_manager::error::ContractError::NotServer)
}
11 changes: 11 additions & 0 deletions tests/rust-tests/src/tests_auctions/test_auctions_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,14 @@ fn test_update_admin_fails() {
)
.unwrap_err();
}

#[test]
fn test_change_server_addr() {
let mut suite = Suite::default();
let new_server_addr = Addr::unchecked("new_server_addr");

suite.change_server_addr(new_server_addr.clone());

let server_addr = suite.query_server_addr();
assert_eq!(server_addr, new_server_addr);
}
4 changes: 2 additions & 2 deletions tests/rust-tests/src/tests_auctions/test_instantiates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn test_instantiate_auction_manager() {

let manager_addr = builder.init_auctions_manager(
app.borrow_mut(),
AuctionsManagerInstantiate::default(builder.auction_code_id).into(),
AuctionsManagerInstantiate::default(builder.auction_code_id, builder.mm.to_string()).into(),
);

let admin_addr = SuiteBuilder::query_wasm_raw_item(&app, manager_addr.clone(), ADMIN);
Expand All @@ -39,7 +39,7 @@ fn test_instantiate_oracle() {

let manager_addr = builder.init_auctions_manager(
app.borrow_mut(),
AuctionsManagerInstantiate::default(builder.auction_code_id).into(),
AuctionsManagerInstantiate::default(builder.auction_code_id, builder.mm.to_string()).into(),
);

let oracle_addr = builder.init_oracle(
Expand Down

0 comments on commit 294db8e

Please sign in to comment.