Skip to content

Commit

Permalink
compile and fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianElvis committed Jan 7, 2025
1 parent 3d12c3a commit 77bad10
Show file tree
Hide file tree
Showing 15 changed files with 73 additions and 90 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.

10 changes: 6 additions & 4 deletions contracts/babylon/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use babylon_bindings::query::{get_babylon_sdk_params, BabylonQuery};
use cosmwasm_std::{
to_json_binary, to_json_string, Addr, Binary, Deps, DepsMut, Empty, Env, IbcMsg, MessageInfo,
QueryResponse, Response, SubMsg, SubMsgResponse, WasmMsg,
to_json_binary, to_json_string, Deps, DepsMut, Empty, Env, IbcMsg, MessageInfo, QueryResponse,
Response, WasmMsg,
};
use cw2::set_contract_version;
use cw_utils::must_pay;

use babylon_apis::{btc_staking_api, finality_api, to_bech32_addr, to_module_canonical_addr};
use babylon_apis::{btc_staking_api, to_bech32_addr, to_module_canonical_addr};
use babylon_bindings::BabylonMsg;

use crate::error::ContractError;
Expand Down Expand Up @@ -229,9 +229,11 @@ pub fn execute(
#[cfg(test)]
mod tests {
use super::*;
use babylon_bindings_test::mock_dependencies;
use babylon_bitcoin::BlockHeader;
use cosmwasm_std::testing::message_info;
use cosmwasm_std::testing::{mock_dependencies, mock_env};
use cosmwasm_std::testing::mock_env;
use cosmwasm_std::Binary;

const CREATOR: &str = "creator";

Expand Down
7 changes: 3 additions & 4 deletions contracts/babylon/src/ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,7 @@ mod tests {
use cosmwasm_std::OwnedDeps;

const CREATOR: &str = "creator";

fn setup() -> OwnedDeps<MockStorage, MockApi, MockQuerier<BabylonQuery>> {
fn setup() -> OwnedDeps<MockStorage, MockApi, MockQuerier<BabylonQuery>, BabylonQuery> {
let mut deps = mock_dependencies();
let msg = InstantiateMsg {
network: babylon_bitcoin::chain_params::Network::Regtest,
Expand All @@ -354,15 +353,15 @@ mod tests {
let info = message_info(&deps.api.addr_make(CREATOR), &[]);
let res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap();
assert_eq!(0, res.messages.len());
deps.as_mut()
deps
}

#[test]
fn enforce_version_in_handshake() {
let mut deps = setup();

let wrong_order = mock_ibc_channel_open_try("channel-12", IbcOrder::Unordered, IBC_VERSION);
ibc_channel_open(deps, mock_env(), wrong_order).unwrap_err();
ibc_channel_open(deps.as_mut(), mock_env(), wrong_order).unwrap_err();

let wrong_version = mock_ibc_channel_open_try("channel-12", IbcOrder::Ordered, "reflect");
ibc_channel_open(deps.as_mut(), mock_env(), wrong_version).unwrap_err();
Expand Down
2 changes: 1 addition & 1 deletion contracts/babylon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use cosmwasm_std::entry_point;
use cosmwasm_std::{
Binary, Deps, DepsMut, Empty, Env, IbcBasicResponse, IbcChannelCloseMsg, IbcChannelConnectMsg,
IbcChannelOpenMsg, IbcChannelOpenResponse, IbcPacketAckMsg, IbcPacketReceiveMsg,
IbcPacketTimeoutMsg, IbcReceiveResponse, MessageInfo, Never, Reply, Response, StdResult,
IbcPacketTimeoutMsg, IbcReceiveResponse, MessageInfo, Never, Response, StdResult,
};

use babylon_bindings::{query::BabylonQuery, BabylonMsg};
Expand Down
2 changes: 1 addition & 1 deletion contracts/babylon/src/msg/contract.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::Uint128;
use cosmwasm_std::{Binary, StdError, StdResult};
use cosmwasm_std::{StdError, StdResult};

use babylon_apis::finality_api::Evidence;

Expand Down
2 changes: 1 addition & 1 deletion contracts/babylon/src/queries/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ pub(crate) fn transfer_info(
mod tests {
use super::*;
use crate::state::btc_light_client::{init, tests::setup};
use cosmwasm_std::testing::mock_dependencies;
use babylon_bindings_test::mock_dependencies;
use test_utils::get_btc_lc_headers;

#[test]
Expand Down
40 changes: 5 additions & 35 deletions contracts/btc-finality/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ fn send_rewards_msg(
pub fn get_activated_height(querier: &QuerierWrapper<BabylonQuery>) -> StdResult<u64> {
// TODO: Use a raw query
let query = encode_smart_query(
&get_babylon_sdk_params(&querier)?.btc_staking_contract_address,
&get_babylon_sdk_params(querier)?.btc_staking_contract_address,
&btc_staking::msg::QueryMsg::ActivatedHeight {},
)?;
let res: ActivatedHeightResponse = querier.query(&query)?;
Expand All @@ -345,52 +345,22 @@ pub(crate) fn encode_smart_query<Q: CustomQuery>(

#[cfg(test)]
pub(crate) mod tests {
use std::marker::PhantomData;


use super::*;

use babylon_bindings::query::ParamsResponse;

use babylon_bindings_test::mock_dependencies;
use cosmwasm_std::{
from_json,
testing::{message_info, mock_env, MockApi, MockQuerier, MockStorage},
Coin, ContractResult, OwnedDeps, SystemResult,
testing::{message_info, mock_env},
};
use cw_controllers::AdminResponse;

pub(crate) const CREATOR: &str = "creator";
pub(crate) const INIT_ADMIN: &str = "initial_admin";
const NEW_ADMIN: &str = "new_admin";

pub fn mock_dependencies(
) -> OwnedDeps<MockStorage, MockApi, MockQuerier<BabylonQuery>, BabylonQuery> {
let custom_querier: MockQuerier<BabylonQuery> = MockQuerier::new(&[("", &[])])
.with_custom_handler(|query| {
// Handle your custom query type here
match query {
BabylonQuery::Params {} => {
// Return a mock response for the custom query
let response = ParamsResponse {
babylon_contract_address: Addr::unchecked(""),
btc_staking_contract_address: Addr::unchecked(""),
btc_finality_contract_address: Addr::unchecked(""),
babylon_contract_code_id: 0,
btc_staking_contract_code_id: 0,
btc_finality_contract_code_id: 0,
max_gas_begin_blocker: 0,
};
SystemResult::Ok(ContractResult::Ok(to_json_binary(&response).unwrap()))
}
_ => panic!("Unsupported query type"),
}
});
OwnedDeps {
storage: MockStorage::default(),
api: MockApi::default(),
querier: custom_querier,
custom_query_type: PhantomData,
}
}

#[test]
fn instantiate_without_admin() {
let mut deps = mock_dependencies();
Expand Down
9 changes: 3 additions & 6 deletions contracts/btc-finality/src/multitest.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
mod suite;

use cosmwasm_std::Addr;
use suite::SuiteBuilder;

// Some multi-test default settings
Expand All @@ -20,13 +19,11 @@ mod instantiation {
let suite = SuiteBuilder::new().build();

// Confirm the btc-staking contract has been instantiated and set
let config = suite.get_babylon_config();
let _config = suite.get_babylon_config();
// Check that the btc-staking contract was initialized correctly
let btc_staking_config = suite.get_btc_staking_config();
assert_eq!(btc_staking_config.babylon, Addr::unchecked(CONTRACT0_ADDR));

let _btc_staking_config = suite.get_btc_staking_config();
// Check that the btc-finality contract was initialized correctly
let btc_finality_config = suite.get_btc_finality_config();
let _btc_finality_config = suite.get_btc_finality_config();
}
}

Expand Down
2 changes: 1 addition & 1 deletion contracts/btc-finality/src/multitest/suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use babylon_bindings::query::BabylonQuery;
use derivative::Derivative;
use hex::ToHex;

use cosmwasm_std::{to_json_binary, Addr, Coin};
use cosmwasm_std::{Addr, Coin};

use cw_multi_test::{AppResponse, Contract, ContractWrapper, Executor};

Expand Down
1 change: 1 addition & 0 deletions contracts/btc-staking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ full-validation = []
[dependencies]
babylon-apis = { path = "../../packages/apis" }
babylon-bindings = { path = "../../packages/bindings" }
babylon-bindings-test = { path = "../../packages/bindings-test" }
babylon-contract = { path = "../babylon", features = [ "library" ] }
babylon-merkle = { path = "../../packages/merkle" }
babylon-proto = { path = "../../packages/proto" }
Expand Down
22 changes: 13 additions & 9 deletions contracts/btc-staking/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use babylon_bindings::query::BabylonQuery;
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
Expand All @@ -20,15 +21,13 @@ pub const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn instantiate(
mut deps: DepsMut,
mut deps: DepsMut<BabylonQuery>,
_env: Env,
info: MessageInfo,
msg: InstantiateMsg,
) -> Result<Response<BabylonMsg>, ContractError> {
nonpayable(&info)?;
let config = Config {
babylon: info.sender,
};
let config = Config {};
CONFIG.save(deps.storage, &config)?;

let api = deps.api;
Expand All @@ -43,12 +42,16 @@ pub fn instantiate(
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn reply(_deps: DepsMut, _env: Env, _reply: Reply) -> StdResult<Response> {
pub fn reply(_deps: DepsMut<BabylonQuery>, _env: Env, _reply: Reply) -> StdResult<Response> {
Ok(Response::default())
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> Result<QueryResponse, ContractError> {
pub fn query(
deps: Deps<BabylonQuery>,
_env: Env,
msg: QueryMsg,
) -> Result<QueryResponse, ContractError> {
match msg {
QueryMsg::Config {} => Ok(to_json_binary(&queries::config(deps)?)?),
QueryMsg::Params {} => Ok(to_json_binary(&queries::params(deps)?)?),
Expand Down Expand Up @@ -90,13 +93,13 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> Result<QueryResponse, Cont

/// This is a no-op just to test how this integrates with wasmd
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(_deps: DepsMut, _env: Env, _msg: Empty) -> StdResult<Response> {
pub fn migrate(_deps: DepsMut<BabylonQuery>, _env: Env, _msg: Empty) -> StdResult<Response> {
Ok(Response::default())
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn execute(
deps: DepsMut,
deps: DepsMut<BabylonQuery>,
env: Env,
info: MessageInfo,
msg: ExecuteMsg,
Expand Down Expand Up @@ -128,9 +131,10 @@ pub fn execute(
pub mod tests {
use super::*;

use babylon_bindings_test::mock_dependencies;
use cosmwasm_std::{
from_json,
testing::{message_info, mock_dependencies, mock_env},
testing::{message_info, mock_env},
};
use cw_controllers::AdminResponse;

Expand Down
34 changes: 22 additions & 12 deletions contracts/btc-staking/src/queries.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::str::FromStr;

use babylon_bindings::query::BabylonQuery;
use bitcoin::hashes::Hash;
use bitcoin::Txid;

Expand All @@ -20,15 +21,18 @@ use crate::state::staking::{
fps, BtcDelegation, FinalityProviderState, ACTIVATED_HEIGHT, DELEGATIONS, FPS, FP_DELEGATIONS,
};

pub fn config(deps: Deps) -> StdResult<Config> {
pub fn config(deps: Deps<BabylonQuery>) -> StdResult<Config> {
CONFIG.load(deps.storage)
}

pub fn params(deps: Deps) -> StdResult<Params> {
pub fn params(deps: Deps<BabylonQuery>) -> StdResult<Params> {
PARAMS.load(deps.storage)
}

pub fn finality_provider(deps: Deps, btc_pk_hex: String) -> StdResult<FinalityProvider> {
pub fn finality_provider(
deps: Deps<BabylonQuery>,
btc_pk_hex: String,
) -> StdResult<FinalityProvider> {
FPS.load(deps.storage, &btc_pk_hex)
}

Expand All @@ -37,7 +41,7 @@ const MAX_LIMIT: u32 = 30;
const DEFAULT_LIMIT: u32 = 10;

pub fn finality_providers(
deps: Deps,
deps: Deps<BabylonQuery>,
start_after: Option<String>,
limit: Option<u32>,
) -> StdResult<FinalityProvidersResponse> {
Expand All @@ -53,7 +57,10 @@ pub fn finality_providers(

/// Get the delegation info by staking tx hash.
/// `staking_tx_hash_hex`: The (reversed) staking tx hash, in hex
pub fn delegation(deps: Deps, staking_tx_hash_hex: String) -> Result<BtcDelegation, ContractError> {
pub fn delegation(
deps: Deps<BabylonQuery>,
staking_tx_hash_hex: String,
) -> Result<BtcDelegation, ContractError> {
let staking_tx_hash = Txid::from_str(&staking_tx_hash_hex)?;
Ok(DELEGATIONS.load(deps.storage, staking_tx_hash.as_ref())?)
}
Expand All @@ -62,7 +69,7 @@ pub fn delegation(deps: Deps, staking_tx_hash_hex: String) -> Result<BtcDelegati
/// `start_after`: The (reversed) associated staking tx hash of the delegation in hex, if provided.
/// `active`: List only active delegations if true, otherwise list all delegations.
pub fn delegations(
deps: Deps,
deps: Deps<BabylonQuery>,
start_after: Option<String>,
limit: Option<u32>,
active: Option<bool>,
Expand Down Expand Up @@ -94,7 +101,7 @@ pub fn delegations(
///
/// `btc_pk_hex`: The BTC public key of the finality provider, in hex
pub fn delegations_by_fp(
deps: Deps,
deps: Deps<BabylonQuery>,
btc_pk_hex: String,
) -> Result<DelegationsByFPResponse, ContractError> {
let tx_hashes = FP_DELEGATIONS.load(deps.storage, &btc_pk_hex)?;
Expand All @@ -113,7 +120,7 @@ pub fn delegations_by_fp(
/// `btc_pk_hex`: The BTC public key of the finality provider, in hex.
/// `active` is a filter to return only active delegations
pub fn active_delegations_by_fp(
deps: Deps,
deps: Deps<BabylonQuery>,
btc_pk_hex: String,
active: bool,
) -> Result<BtcDelegationsResponse, ContractError> {
Expand All @@ -133,7 +140,7 @@ pub fn active_delegations_by_fp(
}

pub fn finality_provider_info(
deps: Deps,
deps: Deps<BabylonQuery>,
btc_pk_hex: String,
height: Option<u64>,
) -> Result<FinalityProviderInfo, ContractError> {
Expand All @@ -150,7 +157,7 @@ pub fn finality_provider_info(
}

pub fn finality_providers_by_power(
deps: Deps,
deps: Deps<BabylonQuery>,
start_after: Option<FinalityProviderInfo>,
limit: Option<u32>,
) -> StdResult<FinalityProvidersByPowerResponse> {
Expand All @@ -170,7 +177,9 @@ pub fn finality_providers_by_power(
Ok(FinalityProvidersByPowerResponse { fps })
}

pub fn activated_height(deps: Deps) -> Result<ActivatedHeightResponse, ContractError> {
pub fn activated_height(
deps: Deps<BabylonQuery>,
) -> Result<ActivatedHeightResponse, ContractError> {
let activated_height = ACTIVATED_HEIGHT.may_load(deps.storage)?.unwrap_or_default();
Ok(ActivatedHeightResponse {
height: activated_height,
Expand All @@ -179,9 +188,10 @@ pub fn activated_height(deps: Deps) -> Result<ActivatedHeightResponse, ContractE

#[cfg(test)]
mod tests {
use babylon_bindings_test::mock_dependencies;
use cosmwasm_std::storage_keys::namespace_with_key;
use cosmwasm_std::testing::message_info;
use cosmwasm_std::testing::{mock_dependencies, mock_env};
use cosmwasm_std::testing::mock_env;
use cosmwasm_std::StdError::NotFound;
use cosmwasm_std::{from_json, Env, Storage};

Expand Down
Loading

0 comments on commit 77bad10

Please sign in to comment.