Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added upload_with_access #485

2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unpublished

- Add a method on `TxHandler` to select instantiation permissions on Wasm upload

### Breaking

## 0.25.0
Expand Down
26 changes: 24 additions & 2 deletions cw-orch-daemon/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ use cosmrs::{
use cosmwasm_std::{Addr, Binary, Coin};
use cw_orch_core::{
contract::interface_traits::Uploadable,
environment::{AsyncWasmQuerier, ChainInfoOwned, ChainState, IndexResponse, Querier},
environment::{
AccessConfig, AsyncWasmQuerier, ChainInfoOwned, ChainState, IndexResponse, Querier,
},
log::transaction_target,
};
use flate2::{write, Compression};
Expand Down Expand Up @@ -342,8 +344,17 @@ impl<Sender: TxSender> DaemonAsyncBase<Sender> {

/// Upload a contract to the chain.
pub async fn upload<T: Uploadable>(
&self,
uploadable: &T,
) -> Result<CosmTxResponse, DaemonError> {
self.upload_with_access_config(uploadable, None).await
}

/// Upload a contract to the chain and specify the permissions for instantiating
pub async fn upload_with_access_config<T: Uploadable>(
&self,
_uploadable: &T,
access: Option<AccessConfig>,
) -> Result<CosmTxResponse, DaemonError> {
let wasm_path = <T as Uploadable>::wasm(self.chain_info());

Expand All @@ -356,7 +367,18 @@ impl<Sender: TxSender> DaemonAsyncBase<Sender> {
let store_msg = cosmrs::cosmwasm::MsgStoreCode {
sender: self.sender().account_id(),
wasm_byte_code,
instantiate_permission: None,
instantiate_permission: access
.map(|a| {
Ok::<_, DaemonError>(cosmrs::cosmwasm::AccessConfig {
permission: a.permission.try_into().unwrap(),
addresses: a
.addresses
.into_iter()
.map(|a| a.parse())
.collect::<Result<_, _>>()?,
})
})
.transpose()?,
};

let result = self
Expand Down
11 changes: 11 additions & 0 deletions cw-orch-daemon/src/sync/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,17 @@ impl<Sender: TxSender> TxHandler for DaemonBase<Sender> {
.instantiate2(code_id, init_msg, label, admin, coins, salt),
)
}

fn upload_with_access_config<T: Uploadable>(
&self,
contract_source: &T,
access_config: Option<cw_orch_core::environment::AccessConfig>,
) -> Result<Self::Response, Self::Error> {
self.rt_handle.block_on(
self.daemon
.upload_with_access_config(contract_source, access_config),
)
}
}

impl<Sender: TxSender> Stargate for DaemonBase<Sender> {
Expand Down
19 changes: 16 additions & 3 deletions packages/cw-orch-core/src/contract/contract_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{
};

use crate::environment::QueryHandler;
use cosmos_sdk_proto::cosmwasm::wasm::v1::AccessConfig;
use cosmwasm_std::{Addr, Binary, Coin};
use serde::{de::DeserializeOwned, Serialize};
use std::fmt::Debug;
Expand Down Expand Up @@ -102,15 +103,22 @@ impl<Chain: ChainState> Contract<Chain> {
impl<Chain: TxHandler> Contract<Chain> {
// Chain interfaces

/// Upload a contract given its source
pub fn upload(&self, source: &impl Uploadable) -> Result<TxResponse<Chain>, CwEnvError> {
/// Upload a contract given its source and specify the permissions for instantiating
pub fn upload_with_access_config(
&self,
source: &impl Uploadable,
access_config: Option<AccessConfig>,
) -> Result<TxResponse<Chain>, CwEnvError> {
log::info!(
target: &contract_target(),
"[{}][Upload]",
self.id,
);

let resp = self.chain.upload(source).map_err(Into::into)?;
let resp = self
.chain
.upload_with_access_config(source, access_config)
.map_err(Into::into)?;
let code_id = resp.uploaded_code_id()?;
self.set_code_id(code_id);
log::info!(
Expand All @@ -128,6 +136,11 @@ impl<Chain: TxHandler> Contract<Chain> {
Ok(resp)
}

/// Upload a contract given its source
pub fn upload(&self, source: &impl Uploadable) -> Result<TxResponse<Chain>, CwEnvError> {
self.upload_with_access_config(source, None)
}

/// Executes an operation on the contract
pub fn execute<E: Serialize + Debug>(
&self,
Expand Down
10 changes: 10 additions & 0 deletions packages/cw-orch-core/src/contract/interface_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
error::CwEnvError,
log::contract_target,
};
use cosmos_sdk_proto::cosmwasm::wasm::v1::AccessConfig;
use cosmwasm_std::{Addr, Binary, Coin, Empty};
use cw_multi_test::Contract as MockContract;
use cw_storage_plus::{Item, Map, PrimaryKey};
Expand Down Expand Up @@ -240,9 +241,9 @@

impl<T: MigratableContract + ContractInstance<Chain>, Chain: TxHandler> CwOrchMigrate<Chain> for T {}

/// Trait to implement on the contract to enable it to be uploaded
/// Should return [`WasmPath`](crate::contract::interface_traits::WasmPath) for `Chain = Daemon`
/// and [`Box<&dyn Contract>`] for `Chain = Mock`

Check failure on line 246 in packages/cw-orch-core/src/contract/interface_traits.rs

View workflow job for this annotation

GitHub Actions / clippy

first doc comment paragraph is too long

error: first doc comment paragraph is too long --> packages/cw-orch-core/src/contract/interface_traits.rs:244:1 | 244 | / /// Trait to implement on the contract to enable it to be uploaded 245 | | /// Should return [`WasmPath`](crate::contract::interface_traits::WasmPath) for `Chain = Daemon` 246 | | /// and [`Box<&dyn Contract>`] for `Chain = Mock` | |_ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#too_long_first_doc_paragraph = note: `-D clippy::too-long-first-doc-paragraph` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::too_long_first_doc_paragraph)]`
pub trait Uploadable {
/// Return an object that can be used to upload the contract to a WASM-supported environment.
fn wasm(_chain: &ChainInfoOwned) -> WasmPath {
Expand All @@ -261,6 +262,15 @@
fn upload(&self) -> Result<Chain::Response, CwEnvError> {
self.as_instance().upload(self)
}

/// upload the contract to the configured environment and specify the permissions for instantiating
fn upload_with_access_config(
&self,
access_config: Option<AccessConfig>,
) -> Result<Chain::Response, CwEnvError> {
self.as_instance()
.upload_with_access_config(self, access_config)
}
}

/// enable `.upload()` for contracts that implement `Uploadable` for that environment.
Expand Down
2 changes: 1 addition & 1 deletion packages/cw-orch-core/src/environment/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ pub use queriers::{
DefaultQueriers, Querier, QuerierGetter, QueryHandler,
};
pub use state::{ChainState, StateInterface};
pub use tx_handler::{TxHandler, TxResponse};
pub use tx_handler::{AccessConfig, TxHandler, TxResponse};
Kayanski marked this conversation as resolved.
Show resolved Hide resolved
17 changes: 17 additions & 0 deletions packages/cw-orch-core/src/environment/tx_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use cosmwasm_std::{Addr, Binary, Coin};
use serde::Serialize;
use std::fmt::Debug;

pub use cosmos_sdk_proto::cosmwasm::wasm::v1::AccessConfig;

/// Response type for actions on an environment
pub type TxResponse<Chain> = <Chain as TxHandler>::Response;

Expand Down Expand Up @@ -35,6 +37,13 @@ pub trait TxHandler: ChainState + Clone {
/// Uploads a contract to the chain.
fn upload<T: Uploadable>(&self, contract_source: &T) -> Result<Self::Response, Self::Error>;

/// Uploads a contract to the chain and specify the permissions for instantiating
fn upload_with_access_config<T: Uploadable>(
&self,
contract_source: &T,
access_config: Option<AccessConfig>,
) -> Result<Self::Response, Self::Error>;

/// Send a InstantiateMsg to a contract.
fn instantiate<I: Serialize + Debug>(
&self,
Expand Down Expand Up @@ -203,6 +212,14 @@ mod tests {
) -> Result<Self::Response, Self::Error> {
unimplemented!()
}

fn upload_with_access_config<T: Uploadable>(
&self,
_contract_source: &T,
_access_config: Option<AccessConfig>,
) -> Result<Self::Response, Self::Error> {
unimplemented!()
}
}

fn associated_error<T: TxHandler>(t: T) -> anyhow::Result<()> {
Expand Down
11 changes: 10 additions & 1 deletion packages/cw-orch-mock/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use serde::Serialize;
use super::state::MockState;
use cw_orch_core::{
contract::interface_traits::Uploadable,
environment::{ChainState, IndexResponse, StateInterface, TxHandler},
environment::{AccessConfig, ChainState, IndexResponse, StateInterface, TxHandler},
CwEnvError,
};

Expand Down Expand Up @@ -248,6 +248,15 @@ impl<A: Api, S: StateInterface> TxHandler for MockBase<A, S> {
)
.map_err(From::from)
}

fn upload_with_access_config<T: Uploadable>(
&self,
contract_source: &T,
_access_config: Option<AccessConfig>,
) -> Result<Self::Response, Self::Error> {
log::debug!("Uploading with access is not enforced when using Mock testing");
self.upload(contract_source)
}
}

#[cfg(test)]
Expand Down
Loading