Skip to content

Commit

Permalink
feat: Create ctx module for context types
Browse files Browse the repository at this point in the history
  • Loading branch information
jawoznia committed Oct 31, 2024
1 parent 7e22598 commit 0a3cc71
Show file tree
Hide file tree
Showing 71 changed files with 286 additions and 79 deletions.
2 changes: 2 additions & 0 deletions examples/contracts/custom/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

use cw_storage_plus::Item;
use sylvia::contract;
use sylvia::cw_std::{CosmosMsg, QueryRequest, Response, StdResult};
Expand Down
2 changes: 2 additions & 0 deletions examples/contracts/custom/src/cw1.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

use cw1::{CanExecuteResp, Cw1};
use sylvia::cw_std::{CosmosMsg, Empty, Response, StdError, StdResult};
use sylvia::types::{ExecCtx, QueryCtx};
Expand Down
2 changes: 2 additions & 0 deletions examples/contracts/cw1-subkeys/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

use cw1_whitelist::contract::Cw1WhitelistContract;
use cw2::set_contract_version;
use cw_storage_plus::{Bound, Map};
Expand Down
2 changes: 2 additions & 0 deletions examples/contracts/cw1-subkeys/src/cw1.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

use cw1::{CanExecuteResp, Cw1};
use sylvia::cw_std::{ensure, Addr, CosmosMsg, Response, StdResult};
use sylvia::types::{CustomMsg, CustomQuery, ExecCtx, QueryCtx};
Expand Down
2 changes: 2 additions & 0 deletions examples/contracts/cw1-subkeys/src/whitelist.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

use sylvia::cw_std::{Response, StdResult};
use sylvia::types::{CustomMsg, CustomQuery, ExecCtx, QueryCtx};
use whitelist::responses::AdminListResponse;
Expand Down
2 changes: 2 additions & 0 deletions examples/contracts/cw1-whitelist/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

use crate::error::ContractError;
use cw2::set_contract_version;
use cw_storage_plus::{Item, Map};
Expand Down
2 changes: 2 additions & 0 deletions examples/contracts/cw1-whitelist/src/cw1.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

use cw1::{CanExecuteResp, Cw1};
use sylvia::cw_std::{Addr, CosmosMsg, Response, StdResult};
use sylvia::types::{CustomMsg, CustomQuery, ExecCtx, QueryCtx};
Expand Down
2 changes: 2 additions & 0 deletions examples/contracts/cw1-whitelist/src/whitelist.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

use sylvia::cw_std::{Empty, Order, Response, StdResult};
use sylvia::types::{CustomMsg, CustomQuery, ExecCtx, QueryCtx};
use whitelist::responses::AdminListResponse;
Expand Down
2 changes: 2 additions & 0 deletions examples/contracts/cw20-base/src/allowances.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

use cw20_allowances::responses::{
AllAccountsResponse, AllAllowancesResponse, AllSpenderAllowancesResponse, AllowanceInfo,
AllowanceResponse, SpenderAllowanceInfo,
Expand Down
2 changes: 2 additions & 0 deletions examples/contracts/cw20-base/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

use crate::error::ContractError;
use crate::responses::{BalanceResponse, Cw20Coin, Cw20ReceiveMsg, TokenInfoResponse};
use crate::validation::{validate_accounts, validate_msg, verify_logo};
Expand Down
2 changes: 2 additions & 0 deletions examples/contracts/cw20-base/src/marketing.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

use crate::contract::Cw20Base;
use crate::error::ContractError;
use crate::validation::verify_logo;
Expand Down
2 changes: 2 additions & 0 deletions examples/contracts/cw20-base/src/minting.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

use crate::contract::{Cw20Base, MinterData};
use crate::error::ContractError;
use cw20_minting::responses::MinterResponse;
Expand Down
2 changes: 2 additions & 0 deletions examples/contracts/cw20-base/src/multitest/receiver.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

use sylvia::cw_std::{Binary, Response, StdError, Uint128};
use sylvia::interface;
use sylvia::types::ExecCtx;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

use sylvia::contract;
use sylvia::cw_std::{Response, StdResult};
use sylvia::types::InstantiateCtx;
Expand Down
2 changes: 2 additions & 0 deletions examples/contracts/entry-points-overriding/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

use cw_storage_plus::Item;
use sylvia::contract;
use sylvia::cw_std::{Response, StdError, StdResult};
Expand Down
2 changes: 2 additions & 0 deletions examples/contracts/entry-points-overriding/src/messages.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

use sylvia::cw_schema::cw_serde;
use sylvia::cw_std::{DepsMut, Env, MessageInfo, Response, StdError, StdResult};
use sylvia::types::ExecCtx;
Expand Down
5 changes: 3 additions & 2 deletions examples/contracts/generic_contract/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#![allow(deprecated)]

use cw_storage_plus::Item;
use sylvia::contract;
use sylvia::ctx::ReplyCtx;
use sylvia::cw_std::{Binary, Response, StdResult, SubMsgResult};
#[allow(deprecated)]
use sylvia::replies::ReplyCtx;
use sylvia::serde::Deserialize;
use sylvia::types::{CustomMsg, ExecCtx, InstantiateCtx, MigrateCtx, QueryCtx, SudoCtx};

Expand Down
2 changes: 2 additions & 0 deletions examples/contracts/generic_contract/src/custom_and_generic.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

use custom_and_generic::CustomAndGeneric;
use sylvia::cw_std::{CosmosMsg, Response, StdError, StdResult};
use sylvia::types::{ExecCtx, QueryCtx, SudoCtx};
Expand Down
2 changes: 2 additions & 0 deletions examples/contracts/generic_contract/src/cw1.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

use crate::contract::GenericContract;
use cw1::{CanExecuteResp, Cw1};
use sylvia::cw_std::{CosmosMsg, Empty, Response, StdError, StdResult};
Expand Down
2 changes: 2 additions & 0 deletions examples/contracts/generic_contract/src/generic.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

use generic::Generic;
use sylvia::cw_std::{CosmosMsg, Response, StdError, StdResult};
use sylvia::serde::Deserialize;
Expand Down
2 changes: 2 additions & 0 deletions examples/contracts/generic_iface_on_contract/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

use sylvia::contract;
use sylvia::cw_std::{Response, StdResult};
use sylvia::types::InstantiateCtx;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

use crate::contract::{SvCustomMsg, SvCustomQuery};
use custom_and_generic::CustomAndGeneric;
use sylvia::cw_std::{CosmosMsg, Response, StdError, StdResult};
Expand Down
2 changes: 2 additions & 0 deletions examples/contracts/generic_iface_on_contract/src/cw1.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

use cw1::{CanExecuteResp, Cw1};
use sylvia::cw_std::{CosmosMsg, Empty, Response, StdError, StdResult};
use sylvia::types::{ExecCtx, QueryCtx};
Expand Down
2 changes: 2 additions & 0 deletions examples/contracts/generic_iface_on_contract/src/generic.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

use generic::Generic;
use sylvia::cw_std::{CosmosMsg, Response, StdError, StdResult};
use sylvia::types::{ExecCtx, QueryCtx, SudoCtx};
Expand Down
5 changes: 2 additions & 3 deletions examples/contracts/generics_forwarded/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#![allow(clippy::type_complexity)]
#![allow(clippy::type_complexity, deprecated)]

use crate::error::ContractError;
use cw_storage_plus::Item;
use sylvia::contract;
use sylvia::ctx::ReplyCtx;
use sylvia::cw_std::{Binary, Response, SubMsgResult};
#[allow(deprecated)]
use sylvia::replies::ReplyCtx;
use sylvia::serde::Deserialize;
use sylvia::types::{
CustomMsg, CustomQuery, ExecCtx, InstantiateCtx, MigrateCtx, QueryCtx, SudoCtx,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

use custom_and_generic::CustomAndGeneric;
use sylvia::cw_std::{CosmosMsg, Response};
use sylvia::serde::Deserialize;
Expand Down
2 changes: 2 additions & 0 deletions examples/contracts/generics_forwarded/src/cw1.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

use cw1::{CanExecuteResp, Cw1};
use sylvia::cw_schema::schemars::JsonSchema;
use sylvia::cw_std::{CosmosMsg, CustomMsg, Empty, Response, StdResult};
Expand Down
2 changes: 2 additions & 0 deletions examples/contracts/generics_forwarded/src/generic.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

use generic::Generic;
use sylvia::cw_std::{CosmosMsg, Response};
use sylvia::serde::Deserialize;
Expand Down
2 changes: 2 additions & 0 deletions examples/interfaces/custom-and-generic/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

use sylvia::cw_std::{CosmosMsg, Response, StdError};

use sylvia::interface;
Expand Down
2 changes: 2 additions & 0 deletions examples/interfaces/cw1/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

use sylvia::cw_std::{CosmosMsg, Response, StdError, StdResult};
use sylvia::serde::{Deserialize, Serialize};
use sylvia::types::{CustomMsg, CustomQuery, ExecCtx, QueryCtx};
Expand Down
2 changes: 2 additions & 0 deletions examples/interfaces/cw20-allowances/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

pub mod responses;

use cw_utils::Expiration;
Expand Down
2 changes: 2 additions & 0 deletions examples/interfaces/cw20-marketing/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

pub mod responses;

use responses::{DownloadLogoResponse, MarketingInfoResponse};
Expand Down
2 changes: 2 additions & 0 deletions examples/interfaces/cw20-minting/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

pub mod responses;

use responses::MinterResponse;
Expand Down
2 changes: 2 additions & 0 deletions examples/interfaces/cw4/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

pub mod responses;

use responses::{
Expand Down
2 changes: 2 additions & 0 deletions examples/interfaces/generic/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

use sylvia::cw_std::{CosmosMsg, Response, StdError};

use sylvia::interface;
Expand Down
2 changes: 2 additions & 0 deletions examples/interfaces/whitelist/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

use responses::AdminListResponse;
use sylvia::cw_std::{Response, StdError, StdResult};
use sylvia::interface;
Expand Down
6 changes: 3 additions & 3 deletions sylvia-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ fn interface_impl(_attr: TokenStream2, item: TokenStream2) -> TokenStream2 {
/// ## Example usage
/// ```rust
/// # use sylvia::types::{ExecCtx, InstantiateCtx, MigrateCtx, QueryCtx, SudoCtx};
/// # use sylvia::replies::ReplyCtx;
/// # use sylvia::ctx::ReplyCtx;
/// # use sylvia::cw_std::{Binary, Response, StdError, SubMsgResult};
/// # use cw_storage_plus::Item;
/// # use thiserror::Error;
Expand Down Expand Up @@ -328,7 +328,7 @@ fn interface_impl(_attr: TokenStream2, item: TokenStream2) -> TokenStream2 {
///
/// ```rust
/// # use sylvia::types::{ExecCtx, InstantiateCtx, MigrateCtx, QueryCtx, SudoCtx};
/// # use sylvia::replies::ReplyCtx;
/// # use sylvia::ctx::ReplyCtx;
/// # use sylvia::cw_std::{Binary, Response, StdError, SubMsgResult};
/// # use cw_storage_plus::Item;
/// # use thiserror::Error;
Expand Down Expand Up @@ -673,7 +673,7 @@ fn contract_impl(attr: TokenStream2, item: TokenStream2) -> TokenStream2 {
/// ## Example usage
/// ```rust
/// # use sylvia::types::{ExecCtx, InstantiateCtx, MigrateCtx, QueryCtx, SudoCtx};
/// # use sylvia::replies::ReplyCtx;
/// # use sylvia::ctx::ReplyCtx;
/// # use sylvia::cw_std::{Binary, Reply, Response, StdResult, SubMsgResult};
/// #
/// pub struct SvContract;
Expand Down
2 changes: 2 additions & 0 deletions sylvia/examples/basic.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

use anyhow::Error;
use cosmwasm_std::{Addr, Response};
use cw_storage_plus::{Item, Map};
Expand Down
131 changes: 131 additions & 0 deletions sylvia/src/ctx.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
use cosmwasm_std::{Deps, DepsMut, Empty, Env, Event, MessageInfo, MsgResponse};

/// Represantation of `reply` context received in entry point.
#[non_exhaustive]
pub struct ReplyCtx<'a, C: cosmwasm_std::CustomQuery = Empty> {
pub deps: DepsMut<'a, C>,
pub env: Env,
pub gas_used: u64,
pub events: Vec<Event>,
pub msg_responses: Vec<MsgResponse>,
}

/// Represantation of `migrate` context received in entry point.
#[non_exhaustive]
pub struct MigrateCtx<'a, C: cosmwasm_std::CustomQuery = Empty> {
pub deps: DepsMut<'a, C>,
pub env: Env,
}

/// Represantation of `execute` context received in entry point.
#[non_exhaustive]
pub struct ExecCtx<'a, C: cosmwasm_std::CustomQuery = Empty> {
pub deps: DepsMut<'a, C>,
pub env: Env,
pub info: MessageInfo,
}

/// Represantation of `instantiate` context received in entry point.
#[non_exhaustive]
pub struct InstantiateCtx<'a, C: cosmwasm_std::CustomQuery = Empty> {
pub deps: DepsMut<'a, C>,
pub env: Env,
pub info: MessageInfo,
}

/// Represantation of `query` context received in entry point.
#[non_exhaustive]
pub struct QueryCtx<'a, C: cosmwasm_std::CustomQuery = Empty> {
pub deps: Deps<'a, C>,
pub env: Env,
}

/// Represantation of `sudo` context received in entry point.
#[non_exhaustive]
pub struct SudoCtx<'a, C: cosmwasm_std::CustomQuery = Empty> {
pub deps: DepsMut<'a, C>,
pub env: Env,
}

impl<C: cosmwasm_std::CustomQuery> ExecCtx<'_, C> {
pub fn branch(&'_ mut self) -> ExecCtx<'_, C> {

Check warning on line 51 in sylvia/src/ctx.rs

View check run for this annotation

Codecov / codecov/patch

sylvia/src/ctx.rs#L51

Added line #L51 was not covered by tests
ExecCtx {
deps: self.deps.branch(),
env: self.env.clone(),
info: self.info.clone(),

Check warning on line 55 in sylvia/src/ctx.rs

View check run for this annotation

Codecov / codecov/patch

sylvia/src/ctx.rs#L53-L55

Added lines #L53 - L55 were not covered by tests
}
}
}

impl<C: cosmwasm_std::CustomQuery> InstantiateCtx<'_, C> {
pub fn branch(&'_ mut self) -> InstantiateCtx<'_, C> {

Check warning on line 61 in sylvia/src/ctx.rs

View check run for this annotation

Codecov / codecov/patch

sylvia/src/ctx.rs#L61

Added line #L61 was not covered by tests
InstantiateCtx {
deps: self.deps.branch(),
env: self.env.clone(),
info: self.info.clone(),

Check warning on line 65 in sylvia/src/ctx.rs

View check run for this annotation

Codecov / codecov/patch

sylvia/src/ctx.rs#L63-L65

Added lines #L63 - L65 were not covered by tests
}
}
}

impl<C: cosmwasm_std::CustomQuery> SudoCtx<'_, C> {
pub fn branch(&'_ mut self) -> SudoCtx<'_, C> {

Check warning on line 71 in sylvia/src/ctx.rs

View check run for this annotation

Codecov / codecov/patch

sylvia/src/ctx.rs#L71

Added line #L71 was not covered by tests
SudoCtx {
deps: self.deps.branch(),
env: self.env.clone(),

Check warning on line 74 in sylvia/src/ctx.rs

View check run for this annotation

Codecov / codecov/patch

sylvia/src/ctx.rs#L73-L74

Added lines #L73 - L74 were not covered by tests
}
}
}

impl<'a, C: cosmwasm_std::CustomQuery> From<(DepsMut<'a, C>, Env)> for MigrateCtx<'a, C> {
fn from((deps, env): (DepsMut<'a, C>, Env)) -> Self {

Check warning on line 80 in sylvia/src/ctx.rs

View check run for this annotation

Codecov / codecov/patch

sylvia/src/ctx.rs#L80

Added line #L80 was not covered by tests
Self { deps, env }
}
}

impl<'a, C: cosmwasm_std::CustomQuery>
From<(DepsMut<'a, C>, Env, u64, Vec<Event>, Vec<MsgResponse>)> for ReplyCtx<'a, C>
{
fn from(
(deps, env, gas_used, events, msg_responses): (
DepsMut<'a, C>,
Env,
u64,
Vec<Event>,
Vec<MsgResponse>,
),
) -> Self {
Self {
deps,
env,
gas_used,
events,
msg_responses,
}
}
}

impl<'a, C: cosmwasm_std::CustomQuery> From<(DepsMut<'a, C>, Env, MessageInfo)> for ExecCtx<'a, C> {
fn from((deps, env, info): (DepsMut<'a, C>, Env, MessageInfo)) -> Self {

Check warning on line 108 in sylvia/src/ctx.rs

View check run for this annotation

Codecov / codecov/patch

sylvia/src/ctx.rs#L108

Added line #L108 was not covered by tests
Self { deps, env, info }
}
}

impl<'a, C: cosmwasm_std::CustomQuery> From<(DepsMut<'a, C>, Env, MessageInfo)>
for InstantiateCtx<'a, C>
{
fn from((deps, env, info): (DepsMut<'a, C>, Env, MessageInfo)) -> Self {

Check warning on line 116 in sylvia/src/ctx.rs

View check run for this annotation

Codecov / codecov/patch

sylvia/src/ctx.rs#L116

Added line #L116 was not covered by tests
Self { deps, env, info }
}
}

impl<'a, C: cosmwasm_std::CustomQuery> From<(Deps<'a, C>, Env)> for QueryCtx<'a, C> {
fn from((deps, env): (Deps<'a, C>, Env)) -> Self {

Check warning on line 122 in sylvia/src/ctx.rs

View check run for this annotation

Codecov / codecov/patch

sylvia/src/ctx.rs#L122

Added line #L122 was not covered by tests
Self { deps, env }
}
}

impl<'a, C: cosmwasm_std::CustomQuery> From<(DepsMut<'a, C>, Env)> for SudoCtx<'a, C> {
fn from((deps, env): (DepsMut<'a, C>, Env)) -> Self {

Check warning on line 128 in sylvia/src/ctx.rs

View check run for this annotation

Codecov / codecov/patch

sylvia/src/ctx.rs#L128

Added line #L128 was not covered by tests
Self { deps, env }
}
}
Loading

0 comments on commit 0a3cc71

Please sign in to comment.