Skip to content

Commit

Permalink
refactor: refactored contract
Browse files Browse the repository at this point in the history
  • Loading branch information
srdtrk committed Oct 28, 2023
1 parent a1f1696 commit a58b5c4
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 17 deletions.
14 changes: 5 additions & 9 deletions src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
//! This module handles the execution logic of the contract.
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{to_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult};

use crate::ibc::types::stargate::channel::ica_contract_channel_init;
use crate::types::keys::{CONTRACT_NAME, CONTRACT_VERSION};
use crate::types::msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg};
use crate::types::state::{
CallbackCounter, ChannelState, ContractState, CALLBACK_COUNTER, CHANNEL_STATE, STATE,
};
use crate::types::ContractError;

// version info for migration
const CONTRACT_NAME: &str = "crates.io:cw-ica-controller";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");

/// Instantiates the contract.
#[cfg_attr(not(feature = "library"), entry_point)]
#[entry_point]
pub fn instantiate(
deps: DepsMut,
env: Env,
Expand Down Expand Up @@ -53,7 +49,7 @@ pub fn instantiate(
}

/// Handles the execution of the contract.
#[cfg_attr(not(feature = "library"), entry_point)]
#[entry_point]
pub fn execute(
deps: DepsMut,
env: Env,
Expand Down Expand Up @@ -81,7 +77,7 @@ pub fn execute(
}

/// Handles the query of the contract.
#[cfg_attr(not(feature = "library"), entry_point)]
#[entry_point]
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::GetContractState {} => to_binary(&query::state(deps)?),
Expand All @@ -91,7 +87,7 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
}

/// Migrate contract if version is lower than current version
#[cfg_attr(not(feature = "library"), entry_point)]
#[entry_point]
pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result<Response, ContractError> {
migrate::validate_semver(deps.as_ref())?;

Expand Down
7 changes: 3 additions & 4 deletions src/ibc/handshake.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! This module contains the entry points for the IBC handshake.
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
DepsMut, Env, Ibc3ChannelOpenResponse, IbcBasicResponse, IbcChannel, IbcChannelCloseMsg,
Expand All @@ -15,7 +14,7 @@ use crate::types::{

/// Handles the `OpenInit` and `OpenTry` parts of the IBC handshake.
/// In this application, we only handle `OpenInit` messages since we are the ICA controller
#[cfg_attr(not(feature = "library"), entry_point)]
#[entry_point]
pub fn ibc_channel_open(
deps: DepsMut,
_env: Env,
Expand All @@ -29,7 +28,7 @@ pub fn ibc_channel_open(

/// Handles the `OpenAck` and `OpenConfirm` parts of the IBC handshake.
/// In this application, we only handle `OpenAck` messages since we are the ICA controller
#[cfg_attr(not(feature = "library"), entry_point)]
#[entry_point]
pub fn ibc_channel_connect(
deps: DepsMut,
_env: Env,
Expand All @@ -45,7 +44,7 @@ pub fn ibc_channel_connect(
}

/// Handles the `ChanCloseInit` and `ChanCloseConfirm` for the IBC module.
#[cfg_attr(not(feature = "library"), entry_point)]
#[entry_point]
pub fn ibc_channel_close(
deps: DepsMut,
_env: Env,
Expand Down
2 changes: 2 additions & 0 deletions src/ibc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//!
//! The IBC module is responsible for handling the IBC channel handshake and handling IBC packets.
#[cfg(not(feature = "library"))]
pub mod handshake;
#[cfg(not(feature = "library"))]
pub mod relay;
pub mod types;
7 changes: 3 additions & 4 deletions src/ibc/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//! - The IBC packet timeout.
//! - The IBC packet receive.
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
from_binary, DepsMut, Env, IbcBasicResponse, IbcPacketAckMsg, IbcPacketReceiveMsg,
Expand All @@ -18,7 +17,7 @@ use crate::types::{
use super::types::{events, packet::acknowledgement::AcknowledgementData};

/// Implements the IBC module's `OnAcknowledgementPacket` handler.
#[cfg_attr(not(feature = "library"), entry_point)]
#[entry_point]
pub fn ibc_packet_ack(
deps: DepsMut,
_env: Env,
Expand All @@ -32,7 +31,7 @@ pub fn ibc_packet_ack(
}

/// Handles the `PacketTimeout` for the IBC module.
#[cfg_attr(not(feature = "library"), entry_point)]
#[entry_point]
pub fn ibc_packet_timeout(
deps: DepsMut,
_env: Env,
Expand All @@ -56,7 +55,7 @@ pub fn ibc_packet_timeout(
}

/// Handles the `PacketReceive` for the IBC module.
#[cfg_attr(not(feature = "library"), entry_point)]
#[entry_point]
pub fn ibc_packet_receive(
_deps: DepsMut,
_env: Env,
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![doc = include_str!("../README.md")]
#![deny(missing_docs)]

#[cfg(not(feature = "library"))]
pub mod contract;
pub mod helpers;
pub mod ibc;
Expand Down
9 changes: 9 additions & 0 deletions src/types/keys.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//! # Keys
//!
//! Contains key constants definitions for the contract such as version info for migrations.
/// CONTRACT_NAME is the name of the contract recorded with cw2
pub const CONTRACT_NAME: &str = "crates.io:cw-ica-controller";
/// CONTRACT_VERSION is the version of the cargo package.
/// This is also the version of the contract recorded in cw2
pub const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
1 change: 1 addition & 0 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pub mod cosmos_msg;
mod error;
pub mod keys;
pub mod msg;
pub mod state;

Expand Down

0 comments on commit a58b5c4

Please sign in to comment.