From 60ac8c1e72be7dfb6e7cf393eab449d1a935fa59 Mon Sep 17 00:00:00 2001 From: nahem Date: Fri, 8 Mar 2024 18:55:06 +0100 Subject: [PATCH] feat(smart-contracts): add test for try bid works --- .../injective-auction-pool/src/executions.rs | 4 +- .../src/tests/unit_tests.rs | 45 ++++++++++++++++++- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/contracts/injective-auction-pool/src/executions.rs b/contracts/injective-auction-pool/src/executions.rs index 62f4f33..9b259fe 100644 --- a/contracts/injective-auction-pool/src/executions.rs +++ b/contracts/injective-auction-pool/src/executions.rs @@ -140,9 +140,11 @@ pub(crate) fn try_bid( auction_round: u64, basket_value: Uint128, ) -> Result { + cw_utils::nonpayable(&info)?; + // only whitelist addresses or the contract itself can bid on the auction let config = CONFIG.load(deps.storage)?; - if info.sender != env.contract.address || !config.whitelisted_addresses.contains(&info.sender) { + if info.sender != env.contract.address && !config.whitelisted_addresses.contains(&info.sender) { return Err(ContractError::Unauthorized {}); } let current_auction_round_response = query_current_auction(deps.as_ref())?; diff --git a/contracts/injective-auction-pool/src/tests/unit_tests.rs b/contracts/injective-auction-pool/src/tests/unit_tests.rs index f9d6147..417f7bc 100644 --- a/contracts/injective-auction-pool/src/tests/unit_tests.rs +++ b/contracts/injective-auction-pool/src/tests/unit_tests.rs @@ -8,8 +8,9 @@ use cosmwasm_std::{ ContractResult as CwContractResult, CosmosMsg, Decimal, Empty, Env, MemoryStorage, MessageInfo, OwnedDeps, Querier, QuerierResult, QueryRequest, Uint128, WasmMsg, }; -use injective_auction::auction::{Coin, QueryCurrentAuctionBasketResponse}; +use injective_auction::auction::{Coin, MsgBid, QueryCurrentAuctionBasketResponse}; use injective_auction::auction_pool::{ExecuteMsg, InstantiateMsg}; +use prost::Message; use treasurechest::tf::tokenfactory::TokenFactoryType; use crate::contract::{execute, instantiate}; @@ -304,3 +305,45 @@ fn exit_pool_fails() { ); assert_eq!(res.attributes, vec![attr("action", "exit_pool")]); } + +#[test] +fn try_bid_works() { + let (mut deps, env) = init(); + + // join pool with one user & enough funds to be able to outbid default highest bid (20000) + let info = mock_info("robinho", &coins(30_000, "native_denom")); + let msg = ExecuteMsg::JoinPool { + auction_round: 1, + basket_value: Uint128::from(10_000u128), + }; + let _ = execute(deps.as_mut().branch(), env.clone(), info, msg).unwrap(); + + let info = mock_info("bot", &[]); + let msg = ExecuteMsg::TryBid { + auction_round: 1, + basket_value: Uint128::from(10_000u128), + }; + let res = execute(deps.as_mut().branch(), env.clone(), info, msg).unwrap(); + + // check the stargate bid message is correct, should only bid minimum allowed bid amount + assert_eq!( + res.messages[0].msg, + CosmosMsg::Stargate { + type_url: "/injective.auction.v1beta1.MsgBid".to_string(), + value: { + let msg = MsgBid { + sender: env.contract.address.to_string(), + bid_amount: Some(injective_auction::auction::Coin { + denom: "native_denom".to_string(), + amount: "20051".to_string(), + }), + round: 1, + }; + Binary(msg.encode_to_vec()) + }, + } + ); + + // checking attributes are fine + assert_eq!(res.attributes, vec![attr("action", "try_bid"), attr("amount", "20051"),]); +}