Skip to content

Commit

Permalink
Add example for invoking another contract and handling the reply
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed May 21, 2024
1 parent 5dd3110 commit fa27144
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 27 deletions.
1 change: 0 additions & 1 deletion docs-test-gen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use strum::{AsRefStr, EnumIter, IntoEnumIterator};

static TEMPLATES: phf::Map<&'static str, &'static str> = phf_map! {
"core" => include_str!("../templates/core.tpl"),
"core-migrate" => include_str!("../templates/core-migrate.tpl"),
"execute" => include_str!("../templates/execute.tpl"),
};

Expand Down
25 changes: 0 additions & 25 deletions docs-test-gen/templates/core-migrate.tpl

This file was deleted.

20 changes: 20 additions & 0 deletions docs-test-gen/templates/core.tpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use cosmwasm_std::*;
use cosmwasm_schema::cw_serde;

#[cw_serde]
enum OtherContractMsg {
DoSomething {}
}

#[cw_serde]
struct InstantiateMsg {}

Expand All @@ -16,6 +21,21 @@ struct MigrateMsg {}
#[cw_serde]
struct SudoMsg {}

fn from_slice<T: Default>(_data: &[u8]) -> StdResult<T> {
Ok(T::default())
}

fn to_vec<T>(_data: &T) -> StdResult<Vec<u8>> {
Ok(Vec::new())
}

fn transform(_old_data: OldData) -> () {
()
}

#[derive(Default)]
struct OldData {}

#[test]
fn doctest() {
{{code}}
Expand Down
2 changes: 1 addition & 1 deletion src/pages/core/entrypoints/migrate.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ the storage in there.

## Example

```rust filename="contract.rs" template="core-migrate"
```rust filename="contract.rs" template="core"
const STATE_VERSION: u64 = 1;

#[entry_point]
Expand Down
37 changes: 37 additions & 0 deletions src/pages/core/entrypoints/reply.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,43 @@ This endpoint gets invoked when you receive a response to a message you sent out
over the chain. The reply parameter then allows you to inspect and work with the
response that was produced.

## Request a reply

To request a reply, you need to use the `reply_on` field in the message you send
and set it to one of the following values:

- `ReplyOn::Always`
- `ReplyOn::Error`
- `ReplyOn::Success`

```rust filename="contract.rs" template="core"
const CONTRACT_ADDR: &str = "other_contract";
const SUBMSG_ID: u64 = 1;

#[entry_point]
pub fn reply(deps: DepsMut, env: Env, msg: cosmwasm_std::Reply) -> StdResult<Response> {
if msg.id != SUBMSG_ID {
return Err(StdError::generic_err("Invalid submsg id"));
}

// We received a message! From the contract we invoked earlier.

Ok(Response::default())
}

#[entry_point]
pub fn execute(deps: DepsMut, env: Env, msg: ExecuteMsg) -> StdResult<Response> {
let msg = WasmMsg::Execute {
contract_addr: CONTRACT_ADDR.into(),
msg: to_json_binary(&OtherContractMsg::DoSomething {}).unwrap(),
funds: vec![],
};

let submsg = SubMsg::reply_always(msg, SUBMSG_ID);
Ok(Response::new().add_submessage(submsg))
}
```

## Definition

```rust filename="contract.rs" template="core"
Expand Down

0 comments on commit fa27144

Please sign in to comment.