Skip to content

Commit

Permalink
Resolve conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
Kayanski committed Oct 1, 2024
2 parents 88dea83 + ea1be9a commit 551d81f
Show file tree
Hide file tree
Showing 21 changed files with 384 additions and 158 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
- Adds an `upload_wasm` function to CosmosSender to upload wasm code associated to no Contract structure
- Update syn to 2.0
- Added cw-plus orchestrator interface to the repo. Pacing the way for more integrations inside this repository in the future
- Add easier way to get PublicKey for `cw_orch_daemon::Wallet`

### Breaking

- Added Support for more mnemonic lengths (at least 24 and 12). This is breaking because of how the mnemonic words are stored and retrieved (`words` method on `PrivateKey`)
- Added Support for more mnemonic lengths (at least 24 and 12). This is breaking because of how the mnemonic words are stored and retrieved (`words` method on `PrivateKey`) (published in cw-orch-daemon 0.26.0)
- Added `Signer` trait for being able to re-use the signing/broadcast flow (Unpublished)

## 0.25.0

Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ cosmos-sdk-proto = { version = "0.24.0", default-features = false }
cw-multi-test = { package = "abstract-cw-multi-test", version = "2.0.2", features = [
"cosmwasm_1_2",
] }
cw20 = { package = "abstract-cw20", version = "3.0.0" }
cw20-base = { package = "abstract-cw20-base", version = "3.0.0" }
cw20 = { version = "2.0.0" }
cw20-base = { version = "2.0.0" }

osmosis-test-tube = { version = "25.0.0" }
neutron-test-tube = { version = "4.2.0" }
Expand Down
4 changes: 2 additions & 2 deletions contracts-ws/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ edition = "2021"

[workspace.dependencies]
cosmwasm-std = "2.0.0"
cw20 = { package = "abstract-cw20", version = "2.0.0" }
cw20-base = { package = "abstract-cw20-base", version = "2.0.0" }
cw20 = { version = "2.0.0" }
cw20-base = { version = "2.0.0" }
cw-storage-plus = { version = "2.0.0" }

serde = { version = "1.0.103", default-features = false, features = ["derive"] }
Expand Down
8 changes: 4 additions & 4 deletions cw-orch-daemon/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ impl<Sender: TxSender> DaemonAsyncBase<Sender> {
contract_address: &Addr,
) -> Result<CosmTxResponse, DaemonError> {
let exec_msg: MsgExecuteContract = MsgExecuteContract {
sender: self.sender().account_id(),
sender: self.sender().msg_sender().map_err(Into::into)?,
contract: AccountId::from_str(contract_address.as_str())?,
msg: serde_json::to_vec(&exec_msg)?,
funds: parse_cw_coins(coins)?,
Expand Down Expand Up @@ -262,7 +262,7 @@ impl<Sender: TxSender> DaemonAsyncBase<Sender> {
code_id,
label: Some(label.unwrap_or("instantiate_contract").to_string()),
admin: admin.map(|a| FromStr::from_str(a.as_str()).unwrap()),
sender: self.sender().account_id(),
sender: self.sender().msg_sender().map_err(Into::into)?,
msg: serde_json::to_vec(&init_msg)?,
funds: parse_cw_coins(coins)?,
};
Expand Down Expand Up @@ -324,7 +324,7 @@ impl<Sender: TxSender> DaemonAsyncBase<Sender> {
contract_address: &Addr,
) -> Result<CosmTxResponse, DaemonError> {
let exec_msg: MsgMigrateContract = MsgMigrateContract {
sender: self.sender().account_id(),
sender: self.sender().msg_sender().map_err(Into::into)?,
contract: AccountId::from_str(contract_address.as_str())?,
msg: serde_json::to_vec(&migrate_msg)?,
code_id: new_code_id,
Expand Down Expand Up @@ -380,7 +380,7 @@ pub async fn upload_wasm<T: TxSender>(
e.write_all(&file_contents)?;
let wasm_byte_code = e.finish()?;
let store_msg = cosmrs::cosmwasm::MsgStoreCode {
sender: sender.account_id(),
sender: sender.msg_sender().map_err(Into::into)?,
wasm_byte_code,
instantiate_permission: access.map(access_config_to_cosmrs).transpose()?,
};
Expand Down
177 changes: 81 additions & 96 deletions cw-orch-daemon/src/senders/cosmos.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use super::{cosmos_options::CosmosWalletKey, query::QuerySender, tx::TxSender};
use super::{
cosmos_options::CosmosWalletKey,
query::QuerySender,
sign::{Signer, SigningAccount},
tx::TxSender,
};
use crate::{
core::parse_cw_coins,
cosmos_modules::{self, auth::BaseAccount},
Expand All @@ -7,10 +12,6 @@ use crate::{
keys::private::PrivateKey,
proto::injective::{InjectiveEthAccount, ETHEREUM_COIN_TYPE},
queriers::{Bank, Node},
tx_broadcaster::{
account_sequence_strategy, assert_broadcast_code_cosm_response, insufficient_fee_strategy,
TxBroadcaster,
},
tx_builder::TxBuilder,
tx_resp::CosmTxResponse,
upload_wasm, CosmosOptions, GrpcChannel,
Expand All @@ -20,9 +21,9 @@ use cosmos_modules::vesting::PeriodicVestingAccount;
use cosmrs::{
bank::MsgSend,
crypto::secp256k1::SigningKey,
proto::{cosmos::authz::v1beta1::MsgExec, traits::Message},
proto::traits::Message,
tendermint::chain::Id,
tx::{self, ModeInfo, Msg, Raw, SignDoc, SignMode, SignerInfo},
tx::{self, Fee, ModeInfo, Msg, Raw, SignDoc, SignMode, SignerInfo, SignerPublicKey},
AccountId, Any,
};
use cosmwasm_std::{coin, Addr, Coin};
Expand Down Expand Up @@ -124,6 +125,10 @@ impl Wallet {
self.options.clone()
}

pub fn public_key(&self) -> Option<SignerPublicKey> {
self.private_key.get_signer_public_key(&self.secp)
}

/// Replaces the private key that the [CosmosSender] is using with key derived from the provided 24-word mnemonic.
/// If you want more control over the derived private key, use [Self::set_private_key]
pub fn set_mnemonic(&mut self, mnemonic: impl Into<String>) -> Result<(), DaemonError> {
Expand Down Expand Up @@ -155,35 +160,15 @@ impl Wallet {
}

pub fn pub_addr_str(&self) -> String {
self.account_id().to_string()
}

pub async fn broadcast_tx(
&self,
tx: Raw,
) -> Result<cosmrs::proto::cosmos::base::abci::v1beta1::TxResponse, DaemonError> {
let mut client = cosmos_modules::tx::service_client::ServiceClient::new(self.channel());
let commit = client
.broadcast_tx(cosmos_modules::tx::BroadcastTxRequest {
tx_bytes: tx.to_bytes()?,
mode: cosmos_modules::tx::BroadcastMode::Sync.into(),
})
.await?;

let commit = commit.into_inner().tx_response.unwrap();
Ok(commit)
Signer::account_id(self).to_string()
}

pub async fn bank_send(
&self,
recipient: &Addr,
coins: Vec<cosmwasm_std::Coin>,
) -> Result<CosmTxResponse, DaemonError> {
let acc_id = if let Some(granter) = self.options.authz_granter.as_ref() {
AccountId::from_str(granter.as_str()).unwrap()
} else {
self.account_id()
};
let acc_id = self.msg_sender()?;

let msg_send = MsgSend {
from_address: acc_id,
Expand Down Expand Up @@ -269,21 +254,6 @@ impl Wallet {
self.commit_tx_any(msgs, memo).await
}

pub fn sign(&self, sign_doc: SignDoc) -> Result<Raw, DaemonError> {
let tx_raw = if self.private_key.coin_type == ETHEREUM_COIN_TYPE {
#[cfg(not(feature = "eth"))]
panic!(
"Coin Type {} not supported without eth feature",
ETHEREUM_COIN_TYPE
);
#[cfg(feature = "eth")]
self.private_key.sign_injective(sign_doc)?
} else {
sign_doc.sign(&self.cosmos_private_key())?
};
Ok(tx_raw)
}

pub async fn base_account(&self) -> Result<BaseAccount, DaemonError> {
let addr = self.address().to_string();

Expand Down Expand Up @@ -433,47 +403,79 @@ impl QuerySender for Wallet {
}
}

impl TxSender for Wallet {
async fn commit_tx_any(
&self,
msgs: Vec<Any>,
memo: Option<&str>,
) -> Result<CosmTxResponse, DaemonError> {
let timeout_height = Node::new_async(self.channel())._block_height().await? + 10u64;
fn get_mnemonic_env(chain_kind: &ChainKind) -> Result<String, CwEnvError> {
match chain_kind {
ChainKind::Local => DaemonEnvVars::local_mnemonic(),
ChainKind::Testnet => DaemonEnvVars::test_mnemonic(),
ChainKind::Mainnet => DaemonEnvVars::main_mnemonic(),
_ => None,
}
.ok_or(CwEnvError::EnvVarNotPresentNamed(
get_mnemonic_env_name(chain_kind).to_string(),
))
}

fn get_mnemonic_env_name(chain_kind: &ChainKind) -> &str {
match chain_kind {
ChainKind::Local => LOCAL_MNEMONIC_ENV_NAME,
ChainKind::Testnet => TEST_MNEMONIC_ENV_NAME,
ChainKind::Mainnet => MAIN_MNEMONIC_ENV_NAME,
_ => panic!("Can't set mnemonic for unspecified chainkind"),
}
}

let msgs = if self.options.authz_granter.is_some() {
// We wrap authz messages
vec![Any {
type_url: "/cosmos.authz.v1beta1.MsgExec".to_string(),
value: MsgExec {
grantee: self.pub_addr_str(),
msgs,
}
.encode_to_vec(),
}]
impl Signer for Wallet {
fn sign(&self, sign_doc: SignDoc) -> Result<Raw, DaemonError> {
let tx_raw = if self.private_key.coin_type == ETHEREUM_COIN_TYPE {
#[cfg(not(feature = "eth"))]
panic!(
"Coin Type {} not supported without eth feature",
ETHEREUM_COIN_TYPE
);
#[cfg(feature = "eth")]
self.private_key.sign_injective(sign_doc)?
} else {
msgs
sign_doc.sign(&self.cosmos_private_key())?
};
Ok(tx_raw)
}

let tx_body = TxBuilder::build_body(msgs, memo, timeout_height);
fn chain_id(&self) -> String {
self.chain_info.chain_id.clone()
}

let tx_builder = TxBuilder::new(tx_body);
fn signer_info(&self, sequence: u64) -> SignerInfo {
SignerInfo {
public_key: self.private_key.get_signer_public_key(&self.secp),
mode_info: ModeInfo::single(SignMode::Direct),
sequence,
}
}

fn build_fee(&self, amount: impl Into<u128>, gas_limit: u64) -> Result<Fee, DaemonError> {
TxBuilder::build_fee(
amount,
&self.get_fee_token(),
gas_limit,
self.options.fee_granter.clone(),
)
}

// We retry broadcasting the tx, with the following strategies
// 1. In case there is an `incorrect account sequence` error, we can retry as much as possible (doesn't cost anything to the user)
// 2. In case there is an insufficient_fee error, we retry once (costs fee to the user everytime we submit this kind of tx)
// 3. In case there is an other error, we fail
let tx_response = TxBroadcaster::default()
.add_strategy(insufficient_fee_strategy())
.add_strategy(account_sequence_strategy())
.broadcast(tx_builder, self)
.await?;
async fn signing_account(&self) -> Result<super::sign::SigningAccount, DaemonError> {
let BaseAccount {
account_number,
sequence,
..
} = self.base_account().await?;

let resp = Node::new_async(self.channel())
._find_tx(tx_response.txhash)
.await?;
Ok(SigningAccount {
account_number,
sequence,
})
}

assert_broadcast_code_cosm_response(resp)
fn gas_price(&self) -> Result<f64, DaemonError> {
Ok(self.chain_info.gas_price)
}

fn account_id(&self) -> AccountId {
Expand All @@ -484,25 +486,8 @@ impl TxSender for Wallet {
// unwrap as address is validated on construction
.unwrap()
}
}

fn get_mnemonic_env(chain_kind: &ChainKind) -> Result<String, CwEnvError> {
match chain_kind {
ChainKind::Local => DaemonEnvVars::local_mnemonic(),
ChainKind::Testnet => DaemonEnvVars::test_mnemonic(),
ChainKind::Mainnet => DaemonEnvVars::main_mnemonic(),
_ => None,
}
.ok_or(CwEnvError::EnvVarNotPresentNamed(
get_mnemonic_env_name(chain_kind).to_string(),
))
}

fn get_mnemonic_env_name(chain_kind: &ChainKind) -> &str {
match chain_kind {
ChainKind::Local => LOCAL_MNEMONIC_ENV_NAME,
ChainKind::Testnet => TEST_MNEMONIC_ENV_NAME,
ChainKind::Mainnet => MAIN_MNEMONIC_ENV_NAME,
_ => panic!("Can't set mnemonic for unspecified chainkind"),
fn authz_granter(&self) -> Option<&Addr> {
self.options.authz_granter.as_ref()
}
}
1 change: 1 addition & 0 deletions cw-orch-daemon/src/senders/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Core Sender traits
pub mod builder;
pub mod query;
pub mod sign;
pub mod tx;

// Senders
Expand Down
Loading

0 comments on commit 551d81f

Please sign in to comment.