Skip to content

Commit

Permalink
feat: Allow specifying salt for contract address
Browse files Browse the repository at this point in the history
  • Loading branch information
jawoznia committed Nov 16, 2023
1 parent 4b7fbdb commit 36d39b4
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 36 deletions.
22 changes: 7 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 54 additions & 17 deletions sylvia-derive/src/multitest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,14 +489,15 @@ where
}

pub fn instantiate(
&self,#(#fields,)*
&self, #(#fields,)*
) -> InstantiateProxy<'_, 'app, #(#generics,)* #mt_app > {
let msg = #instantiate_msg {#(#fields_names,)*};
InstantiateProxy::< #(#generics,)* _> {
code_id: self,
funds: &[],
label: "Contract",
admin: None,
salt: None,
msg,
}
}
Expand All @@ -507,6 +508,7 @@ where
funds: &'proxy [#sylvia ::cw_std::Coin],
label: &'proxy str,
admin: Option<String>,
salt: Option<&'proxy [u8]>,
msg: InstantiateMsg #bracketed_used_generics,
}

Expand All @@ -528,24 +530,59 @@ where
Self { admin, ..self }
}

pub fn with_salt(self, salt: impl Into<Option<&'proxy [u8]>>) -> Self {
let salt = salt.into();
Self { salt, ..self }
}

#[track_caller]
pub fn call(self, sender: &str) -> Result<#proxy_name<'app, MtApp, #(#generics,)* >, #error_type> {
(*self.code_id.app)
.app_mut()
.instantiate_contract(
self.code_id.code_id,
#sylvia ::cw_std::Addr::unchecked(sender),
&self.msg,
self.funds,
self.label,
self.admin,
)
.map_err(|err| err.downcast().unwrap())
.map(|addr| #proxy_name {
contract_addr: addr,
app: self.code_id.app,
_phantom: std::marker::PhantomData::default(),
})
let Self {code_id, funds, label, admin, salt, msg} = self;

match salt {
Some(salt) => {
let msg = #sylvia ::cw_std::to_json_binary(&msg)
.map_err(Into::< #error_type >::into)?;
let sender = #sylvia ::cw_std::Addr::unchecked(sender);

let msg = #sylvia ::cw_std::WasmMsg::Instantiate2 {
admin,
code_id: code_id.code_id,
msg,
funds: funds.to_owned(),
label: label.to_owned(),
salt: salt.into(),
};
let app_response = (*code_id.app)
.app_mut()
.execute(sender.clone(), msg.into())
.map_err(|err| err.downcast::< #error_type >().unwrap())?;

#sylvia:: cw_utils::parse_instantiate_response_data(app_response.data.unwrap().as_slice())
.map_err(|err| Into::into( #sylvia ::cw_std::StdError::generic_err(err.to_string())))
.map(|data| #proxy_name {
contract_addr: #sylvia ::cw_std::Addr::unchecked(data.contract_address),
app: code_id.app,
_phantom: std::marker::PhantomData::default(),
})
},
None => (*code_id.app)
.app_mut()
.instantiate_contract(
code_id.code_id,
#sylvia ::cw_std::Addr::unchecked(sender),
&msg,
funds,
label,
admin,
)
.map_err(|err| err.downcast().unwrap())
.map(|addr| #proxy_name {
contract_addr: addr,
app: code_id.app,
_phantom: std::marker::PhantomData::default(),
}),
}
}
}
}
Expand Down
20 changes: 16 additions & 4 deletions sylvia/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ keywords = ["cosmwasm", "blockchain", "confio"]
categories = ["api-bindings", "wasm"]

[features]
mt = ["sylvia-derive/mt", "dep:cw-multi-test", "dep:anyhow", "dep:derivative"]
mt = [
"sylvia-derive/mt",
"dep:cw-multi-test",
"dep:anyhow",
"dep:derivative",
"cosmwasm-std/cosmwasm_1_2",
"dep:cw-utils",
]
stargate = ["cosmwasm-std/stargate", "cw-multi-test?/stargate"]
staking = ["cosmwasm-std/staking", "cw-multi-test?/staking"]

Expand All @@ -24,12 +31,17 @@ serde = { version = "1.0", default-features = false, features = ["derive"] }
serde-cw-value = "0.7.0"
serde-json-wasm = "1.0.0"
konst = "0.3"
cw-multi-test = { version = "0.18.0", optional = true }
cw-multi-test = { version = "0.18.0", optional = true, features = [
"cosmwasm_1_2",
] }
anyhow = { version = "1.0", optional = true }
derivative = { version = "2.2.0", optional = true }
cw-utils = { version = "1.0.2", optional = true }

[dev-dependencies]
anyhow = "1.0"
cw-storage-plus = "1.0.1"
cosmwasm-std = { version = "1.5", features = ["staking", "cosmwasm_1_2"] }
cw-multi-test = "0.18"
thiserror = "1.0.38"
cw-storage-plus = "1.2.0"
cw-utils = "1.0.2"
thiserror = "1.0.50"
2 changes: 2 additions & 0 deletions sylvia/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ pub use cosmwasm_std as cw_std;
#[cfg(feature = "mt")]
pub use cw_multi_test;
#[cfg(feature = "mt")]
pub use cw_utils;
#[cfg(feature = "mt")]
pub use derivative;
pub use schemars;
pub use serde;
Expand Down
44 changes: 44 additions & 0 deletions sylvia/tests/multitest.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#![cfg(feature = "mt")]

use cosmwasm_std::{Empty, Response, StdResult};
use std::marker::PhantomData;
use sylvia::multitest::App;
use sylvia::types::InstantiateCtx;
use sylvia_derive::contract;

pub struct SomeContract<ParamT> {
_phantom: PhantomData<ParamT>,
}

#[contract]
impl<ParamT> SomeContract<ParamT>

Check warning on line 14 in sylvia/tests/multitest.rs

View check run for this annotation

Codecov / codecov/patch

sylvia/tests/multitest.rs#L14

Added line #L14 was not covered by tests
where
ParamT: sylvia::types::CustomMsg + 'static,
{
pub const fn new() -> Self {
Self {
_phantom: PhantomData,
}
}

#[msg(instantiate)]
pub fn instantiate(&self, _ctx: InstantiateCtx, _param: ParamT) -> StdResult<Response> {
Ok(Response::new())
}
}

#[test]
fn instantiate_with_salt() {
let owner = "owner";
let salt = "sylvia OP".as_bytes();

let app = App::default();

let code_id = sv::multitest_utils::CodeId::<Empty, _>::store_code(&app);

let _: sv::multitest_utils::SomeContractProxy<_, Empty> = code_id
.instantiate(Empty {})
.with_salt(salt)
.call(owner)
.unwrap();
}

0 comments on commit 36d39b4

Please sign in to comment.