Skip to content

Commit

Permalink
feat: Add UpdateAdmin and ClearAdmin constructors in the Remote
Browse files Browse the repository at this point in the history
  • Loading branch information
jawoznia committed Sep 27, 2024
1 parent 8140714 commit 735bfeb
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 19 deletions.
13 changes: 13 additions & 0 deletions sylvia/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,19 @@ impl<'a, Contract: ?Sized> Remote<'a, Contract> {
pub fn executor(&self) -> ExecutorBuilder<(EmptyExecutorBuilderState, Contract)> {
ExecutorBuilder::<(EmptyExecutorBuilderState, Contract)>::new(&self.addr)
}

pub fn update_admin(&self, new_admin: String) -> WasmMsg {

Check warning on line 438 in sylvia/src/types.rs

View check run for this annotation

Codecov / codecov/patch

sylvia/src/types.rs#L438

Added line #L438 was not covered by tests
WasmMsg::UpdateAdmin {
contract_addr: self.addr.to_string(),

Check warning on line 440 in sylvia/src/types.rs

View check run for this annotation

Codecov / codecov/patch

sylvia/src/types.rs#L440

Added line #L440 was not covered by tests
admin: new_admin,
}
}

pub fn clear_admin(&self) -> WasmMsg {

Check warning on line 445 in sylvia/src/types.rs

View check run for this annotation

Codecov / codecov/patch

sylvia/src/types.rs#L445

Added line #L445 was not covered by tests
WasmMsg::ClearAdmin {
contract_addr: self.addr.to_string(),

Check warning on line 447 in sylvia/src/types.rs

View check run for this annotation

Codecov / codecov/patch

sylvia/src/types.rs#L447

Added line #L447 was not covered by tests
}
}
}

impl<'a, Contract: ?Sized> AsRef<cosmwasm_std::Addr> for Remote<'a, Contract> {
Expand Down
110 changes: 91 additions & 19 deletions sylvia/tests/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ where
}

pub mod manager {
use cosmwasm_std::WasmMsg;
use cw_storage_plus::Item;
use schemars::JsonSchema;
use serde::de::DeserializeOwned;
Expand Down Expand Up @@ -257,12 +258,42 @@ pub mod manager {

Ok(count)
}

#[sv::msg(exec)]
fn update_admin(

Check warning on line 263 in sylvia/tests/remote.rs

View check run for this annotation

Codecov / codecov/patch

sylvia/tests/remote.rs#L263

Added line #L263 was not covered by tests
&self,
ctx: ExecCtx<ExampleQuery>,
new_admin: String,
) -> Result<Response<ExampleMsg>, StdError> {
let wasm = self
.remote_counter
.load(ctx.deps.storage)?
.interface_remote
.update_admin(new_admin);
let resp = Response::new().add_message(wasm);
Ok(resp)

Check warning on line 274 in sylvia/tests/remote.rs

View check run for this annotation

Codecov / codecov/patch

sylvia/tests/remote.rs#L268-L274

Added lines #L268 - L274 were not covered by tests
}

#[sv::msg(exec)]
fn clear_admin(

Check warning on line 278 in sylvia/tests/remote.rs

View check run for this annotation

Codecov / codecov/patch

sylvia/tests/remote.rs#L278

Added line #L278 was not covered by tests
&self,
ctx: ExecCtx<ExampleQuery>,
) -> Result<Response<ExampleMsg>, StdError> {
let wasm = self
.remote_counter
.load(ctx.deps.storage)?
.interface_remote

Check warning on line 285 in sylvia/tests/remote.rs

View check run for this annotation

Codecov / codecov/patch

sylvia/tests/remote.rs#L282-L285

Added lines #L282 - L285 were not covered by tests
.clear_admin();
let resp = Response::new().add_message(wasm);
Ok(resp)

Check warning on line 288 in sylvia/tests/remote.rs

View check run for this annotation

Codecov / codecov/patch

sylvia/tests/remote.rs#L287-L288

Added lines #L287 - L288 were not covered by tests
}
}
}

#[cfg(test)]
mod tests {
use cw_multi_test::{BasicApp, IntoBech32};
use cosmwasm_std::{CosmosMsg, WasmMsg};
use cw_multi_test::{BasicApp, Executor, IntoAddr};
use sylvia::cw_std::{Addr, StdError};
use sylvia::multitest::{App, Proxy};
use sylvia::types::Remote;
Expand All @@ -277,7 +308,6 @@ mod tests {
use crate::{ExampleMsg, ExampleQuery};

type ExampleApp = BasicApp<ExampleMsg, ExampleQuery>;
const OWNER: &str = "owner";

#[test]
fn remote_generation() {
Expand All @@ -302,27 +332,28 @@ mod tests {
assert_eq!(&addr, borrowed_remote.as_ref());
}

fn setup(
app: &App<ExampleApp>,
fn setup<'a>(
app: &'a App<ExampleApp>,
owner: &'a Addr,
) -> (
Proxy<ExampleApp, ManagerContract<i32>>,
Proxy<ExampleApp, ManagerContract<u32>>,
Proxy<'a, ExampleApp, ManagerContract<i32>>,
Proxy<'a, ExampleApp, ManagerContract<u32>>,
) {
// Manager operating on signed numbers
let signed_counter_code_id = SignedCounterCodeId::store_code(app);

let signed_counter_contract = signed_counter_code_id
.instantiate()
.with_label("Signed counter contract")
.call(&OWNER.into_bech32())
.call(owner)
.unwrap();

let manager_code_id = ManagerCodeId::store_code(app);

let signed_manager_contract = manager_code_id
.instantiate(signed_counter_contract.contract_addr.clone())
.with_label("Manager contract")
.call(&OWNER.into_bech32())
.call(owner)
.unwrap();

// Manager operating on unsigned numbers
Expand All @@ -331,39 +362,80 @@ mod tests {
let unsigned_counter_contract = unsigned_counter_code_id
.instantiate()
.with_label("Unsigned counter contract")
.call(&OWNER.into_bech32())
.call(owner)
.unwrap();

let manager_code_id = ManagerCodeId::store_code(app);

let unsigned_manager_contract = manager_code_id
.instantiate(unsigned_counter_contract.contract_addr.clone())
.with_label("Manager contract")
.call(&OWNER.into_bech32())
.with_admin(Some(owner.as_str()))
.call(owner)
.unwrap();

(signed_manager_contract, unsigned_manager_contract)
}

#[test]
fn call_remote() {
let owner = "owner".into_addr();
let app = App::<cw_multi_test::BasicApp<ExampleMsg, ExampleQuery>>::custom(|_, _, _| {});
let (signed_manager_contract, unsigned_manager_contract) = setup(&app);
let (signed_manager_contract, unsigned_manager_contract) = setup(&app, &owner);
let new_admin = "new_admin".into_addr();

assert_eq!(signed_manager_contract.count().unwrap(), 0);

signed_manager_contract
.add(5)
.call(&OWNER.into_bech32())
.unwrap();
signed_manager_contract.add(5).call(&owner).unwrap();
assert_eq!(signed_manager_contract.count().unwrap(), 5);

assert_eq!(unsigned_manager_contract.count().unwrap(), 0);

unsigned_manager_contract
.add(5)
.call(&OWNER.into_bech32())
.unwrap();
unsigned_manager_contract.add(5).call(&owner).unwrap();
assert_eq!(unsigned_manager_contract.count().unwrap(), 5);

// Not sure if this is intended in the MultiTest, but it seems that the
// `WasmMsg::ClearAdmin` and `WasmMsg::UpdateAdmin` messages cannot be sent from the
// contract and has to be called directly on the App.
//
// Add new admin
// unsigned_manager_contract
// .update_admin(new_admin.to_string())
// .call(&owner)
// .unwrap();

app.app_mut()
.execute(
owner.clone(),
CosmosMsg::Wasm(WasmMsg::UpdateAdmin {
contract_addr: unsigned_manager_contract.contract_addr.to_string(),
admin: new_admin.to_string(),
}),
)
.unwrap();
let contract_info = app
.querier()
.query_wasm_contract_info(unsigned_manager_contract.contract_addr.clone())
.unwrap();
assert_eq!(contract_info.admin, Some(new_admin.clone()));

// Clear admin
// unsigned_manager_contract
// .clear_admin()
// .call(&owner)
// .unwrap();
app.app_mut()
.execute(
new_admin.clone(),
CosmosMsg::Wasm(WasmMsg::ClearAdmin {
contract_addr: unsigned_manager_contract.contract_addr.to_string(),
}),
)
.unwrap();
let contract_info = app
.querier()
.query_wasm_contract_info(unsigned_manager_contract.contract_addr)
.unwrap();
assert_eq!(contract_info.admin, None);
}
}

0 comments on commit 735bfeb

Please sign in to comment.