Skip to content

Commit

Permalink
Refactor UploadContractsBuilder (#31)
Browse files Browse the repository at this point in the history
* refactor upload contracts and add new method to add single contract

* fix parameters

* update readme

* update readme

* update readme

* fix readme
  • Loading branch information
keyleu authored Sep 12, 2024
1 parent a4ec702 commit e2801c6
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 18 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ Note that most `tx_*` helper functions expose a `.with_key(key: &str)` builder f

#### General utility functions

* `.build_tx_upload_contracts` - Uploads all contracts in the specified artifacts dir to Neutron.
* `.build_tx_upload_contracts` - Uploads all contracts in the specified artifacts dir to Neutron by default.
* No required builder calls
* No notable optional builder calls

* Notable optional builder calls:
* `.with_chain_name(chain_name: impl Into<String>)` - Should be one of `"osmosis" | "neutron"` or one of the registered chain names from `.with_chain`
#### Wasm

* `.build_tx_instantiate2` - Predictably instantiates a CosmWasm contract.
Expand Down
8 changes: 3 additions & 5 deletions examples/neutron.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ fn main() -> Result<(), Box<dyn Error>> {
.build()?;

// Upload contracts
ctx.build_tx_upload_contracts().send_with_local_cache(
"contracts",
NEUTRON_CHAIN_NAME,
LOCAL_CODE_ID_CACHE_PATH,
)?;
ctx.build_tx_upload_contracts()
.with_chain_name(NEUTRON_CHAIN_NAME)
.send_with_local_cache("contracts", LOCAL_CODE_ID_CACHE_PATH)?;

// Create a token in the tokenfactory
ctx.build_tx_create_tokenfactory_token()
Expand Down
55 changes: 46 additions & 9 deletions src/utils/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use std::{
pub struct UploadContractsTxBuilder<'a> {
key: Option<&'a str>,
test_ctx: &'a mut TestContext,
chain_name: Option<&'a str>,
}

impl<'a> UploadContractsTxBuilder<'a> {
Expand All @@ -24,40 +25,78 @@ impl<'a> UploadContractsTxBuilder<'a> {
self
}

pub fn with_chain_name(&mut self, chain_name: &'a str) -> &mut Self {
self.chain_name = Some(chain_name);

self
}

/// Sends the transaction.
pub fn send(&mut self) -> Result<(), Error> {
self.test_ctx.tx_upload_contracts(
self.key
.ok_or(Error::MissingBuilderParam(String::from("key")))?,
self.chain_name
.ok_or(Error::MissingBuilderParam(String::from("chain_name")))?,
)
}

/// Sends the transaction using a path, chain and local cache path
pub fn send_with_local_cache(
&mut self,
path: &str,
chain_name: &str,
local_cache_path: &str,
) -> Result<(), Error> {
self.test_ctx.tx_upload_contracts_with_local_cache(
self.key
.ok_or(Error::MissingBuilderParam(String::from("key")))?,
self.chain_name
.ok_or(Error::MissingBuilderParam(String::from("chain_name")))?,
path,
chain_name,
local_cache_path,
)
}

/// Sends the transaction using a single contract file
pub fn send_single_contract(&mut self, path: &str) -> Result<(), Error> {
self.test_ctx.tx_upload_contract(
self.key
.ok_or(Error::MissingBuilderParam(String::from("key")))?,
self.chain_name
.ok_or(Error::MissingBuilderParam(String::from("chain_name")))?,
path,
)
}
}

impl TestContext {
pub fn build_tx_upload_contracts(&mut self) -> UploadContractsTxBuilder {
UploadContractsTxBuilder {
key: Some(DEFAULT_KEY),
test_ctx: self,
chain_name: Some(NEUTRON_CHAIN_NAME),
}
}

fn tx_upload_contracts(&mut self, key: &str) -> Result<(), Error> {
fn tx_upload_contract(&mut self, key: &str, chain_name: &str, path: &str) -> Result<(), Error> {
let path = fs::canonicalize(path)?;

let local_chain = self.get_mut_chain(chain_name);

let mut cw = CosmWasm::new(&local_chain.rb);

let code_id = cw.store(key, &path)?;

let id = path
.file_stem()
.and_then(|stem| stem.to_str())
.ok_or(Error::Misc(String::from("failed to format file path")))?;
local_chain.contract_codes.insert(id.to_string(), code_id);

Ok(())
}

fn tx_upload_contracts(&mut self, key: &str, chain_name: &str) -> Result<(), Error> {
fs::read_dir(&self.artifacts_dir)?
.filter_map(|dir_ent| dir_ent.ok())
.filter(|dir_ent| {
Expand All @@ -67,19 +106,17 @@ impl TestContext {
.map(fs::canonicalize)
.try_for_each(|maybe_abs_path| {
let path = maybe_abs_path?;
let neutron_local_chain = self.get_mut_chain(NEUTRON_CHAIN_NAME);
let local_chain = self.get_mut_chain(chain_name);

let mut cw = CosmWasm::new(&neutron_local_chain.rb);
let mut cw = CosmWasm::new(&local_chain.rb);

let code_id = cw.store(key, &path)?;

let id = path
.file_stem()
.and_then(|stem| stem.to_str())
.ok_or(Error::Misc(String::from("failed to format file path")))?;
neutron_local_chain
.contract_codes
.insert(id.to_string(), code_id);
local_chain.contract_codes.insert(id.to_string(), code_id);

Ok(())
})
Expand All @@ -88,8 +125,8 @@ impl TestContext {
fn tx_upload_contracts_with_local_cache(
&mut self,
key: &str,
path: &str,
chain_name: &str,
path: &str,
local_cache_path: &str,
) -> Result<(), Error> {
if fs::metadata(path).is_ok_and(|m| m.is_dir()) {
Expand Down
3 changes: 2 additions & 1 deletion src/utils/setup/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl<'a> Instantiate2TxBuilder<'a> {
}

impl TestContext {
pub fn build_tx_instantiate2<'a>(&'a mut self) -> Instantiate2TxBuilder<'a> {
pub fn build_tx_instantiate2(&mut self) -> Instantiate2TxBuilder<'_> {
Instantiate2TxBuilder {
key: DEFAULT_KEY,
chain_name: NEUTRON_CHAIN_NAME,
Expand All @@ -119,6 +119,7 @@ impl TestContext {
}
}

#[allow(clippy::too_many_arguments)]
fn tx_instantiate2(
&mut self,
key: &str,
Expand Down

0 comments on commit e2801c6

Please sign in to comment.