Skip to content

Commit

Permalink
refactor upload contracts and add new method to add single contract
Browse files Browse the repository at this point in the history
  • Loading branch information
keyleu committed Sep 11, 2024
1 parent a4ec702 commit 8a02f29
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 15 deletions.
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
57 changes: 47 additions & 10 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,27 +25,46 @@ 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")))?,
path,
chain_name,
self.chain_name
.ok_or(Error::MissingBuilderParam(String::from("chain_name")))?,
local_cache_path,
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,
)
}
}
Expand All @@ -54,10 +74,29 @@ impl TestContext {
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

0 comments on commit 8a02f29

Please sign in to comment.