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 wasm function to sender #487

Merged
merged 12 commits into from
Sep 16, 2024
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

## Unpublished

- Add methods to set the private key and mnemonic of an existing sender
- Deprecate `authz_granter` and `fee_granter` on `Daemon` struct
- Add a method on `TxHandler` to select instantiation permissions on Wasm upload
- Add methods to set the private key and mnemonic of an existing sender
- Deprecate `authz_granter` and `fee_granter` on `Daemon` struct
- Add a method on `TxHandler` to select instantiation permissions on Wasm upload
- Adds an `upload_wasm` function to CosmosSender to upload wasm code associated to no Contract structure

### Breaking

Expand Down
22 changes: 3 additions & 19 deletions cw-orch-daemon/src/core.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
queriers::CosmWasm,
senders::{builder::SenderBuilder, query::QuerySender},
senders::{builder::SenderBuilder, query::QuerySender, upload_wasm},
DaemonAsyncBuilder, DaemonState,
};

Expand All @@ -22,13 +22,11 @@ use cw_orch_core::{
},
log::transaction_target,
};
use flate2::{write, Compression};
use prost::Message;
use serde::{de::DeserializeOwned, Serialize};
use serde_json::from_str;
use std::{
fmt::Debug,
io::Write,
ops::Deref,
str::{from_utf8, FromStr},
time::Duration,
Expand Down Expand Up @@ -360,21 +358,7 @@ impl<Sender: TxSender> DaemonAsyncBase<Sender> {

log::debug!(target: &transaction_target(), "Uploading file at {:?}", wasm_path);

let file_contents = std::fs::read(wasm_path.path())?;
let mut e = write::GzEncoder::new(Vec::new(), Compression::default());
Kayanski marked this conversation as resolved.
Show resolved Hide resolved
e.write_all(&file_contents)?;
let wasm_byte_code = e.finish()?;
let store_msg = cosmrs::cosmwasm::MsgStoreCode {
sender: self.sender().account_id(),
wasm_byte_code,
instantiate_permission: access.map(access_config_to_cosmrs).transpose()?,
};

let result = self
.sender()
.commit_tx(vec![store_msg], None)
.await
.map_err(Into::into)?;
let result = upload_wasm(self.sender(), wasm_path, access).await?;

log::info!(target: &transaction_target(), "Uploading done: {:?}", result.txhash);

Expand All @@ -389,7 +373,7 @@ impl<Sender: TxSender> DaemonAsyncBase<Sender> {
}
}

fn access_config_to_cosmrs(
pub(crate) fn access_config_to_cosmrs(
access_config: AccessConfig,
) -> Result<cosmrs::cosmwasm::AccessConfig, DaemonError> {
let response = match access_config {
Expand Down
45 changes: 43 additions & 2 deletions cw-orch-daemon/src/senders/cosmos.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
access_config_to_cosmrs,
env::DaemonEnvVars,
proto::injective::ETHEREUM_COIN_TYPE,
queriers::Bank,
Expand Down Expand Up @@ -32,13 +33,15 @@ use cosmrs::{
};
use cosmwasm_std::{coin, Addr, Coin};
use cw_orch_core::{
environment::{ChainInfoOwned, ChainKind},
contract::WasmPath,
environment::{AccessConfig, ChainInfoOwned, ChainKind},
CoreEnvVars, CwEnvError,
};
use flate2::{write, Compression};

use crate::env::{LOCAL_MNEMONIC_ENV_NAME, MAIN_MNEMONIC_ENV_NAME, TEST_MNEMONIC_ENV_NAME};
use bitcoin::secp256k1::{All, Secp256k1, Signing};
use std::{str::FromStr, sync::Arc};
use std::{io::Write, str::FromStr, sync::Arc};

use cosmos_modules::vesting::PeriodicVestingAccount;
use tonic::transport::Channel;
Expand Down Expand Up @@ -415,6 +418,44 @@ impl Wallet {
}
}

// Helpers to facilitate some rare operations
impl Wallet {
/// Uploads the `WasmPath` path specifier on chain.
/// The resulting code_id can be extracted from the Transaction result using [cw_orch_core::environment::IndexResponse::uploaded_code_id] and returns the resulting code_id
pub async fn upload_wasm(&self, wasm_path: WasmPath) -> Result<CosmTxResponse, DaemonError> {
self.upload_with_access_config(wasm_path, None).await
}

pub async fn upload_with_access_config(
&self,
wasm_path: WasmPath,
access: Option<AccessConfig>,
) -> Result<CosmTxResponse, DaemonError> {
upload_wasm(self, wasm_path, access).await
}
}

pub async fn upload_wasm<T: TxSender>(
Kayanski marked this conversation as resolved.
Show resolved Hide resolved
sender: &T,
wasm_path: WasmPath,
access: Option<AccessConfig>,
) -> Result<CosmTxResponse, DaemonError> {
let file_contents = std::fs::read(wasm_path.path())?;
let mut e = write::GzEncoder::new(Vec::new(), Compression::default());
e.write_all(&file_contents)?;
let wasm_byte_code = e.finish()?;
let store_msg = cosmrs::cosmwasm::MsgStoreCode {
sender: sender.account_id(),
wasm_byte_code,
instantiate_permission: access.map(access_config_to_cosmrs).transpose()?,
};

sender
.commit_tx(vec![store_msg], None)
.await
.map_err(Into::into)
}

impl QuerySender for Wallet {
type Error = DaemonError;
type Options = CosmosOptions;
Expand Down
1 change: 1 addition & 0 deletions cw-orch-daemon/src/senders/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod cosmos_batch;
mod cosmos_options;
mod query_only;

pub(crate) use cosmos::upload_wasm;
pub use {
cosmos::{CosmosSender, Wallet},
cosmos_batch::{options::CosmosBatchOptions, BatchDaemon, CosmosBatchSender},
Expand Down
Loading