Router-wasm-bindings

The wasm bindings for building CosmWasm smart contracts that can run on the Router chain.

Prerequisites

Before starting, make sure you have rustup along with a recent rustc and cargo version installed. Currently, we are testing on 1.62.1+.

And you need to have the wasm32-unknown-unknown target installed as well.

You can check that via:

```sh rustc --version cargo --version rustup target list --installed

if wasm32 is not listed above, run this

rustup target add wasm32-unknown-unknown ```

Context

On the Router chain, We can build two types of contracts. - Contracts that are not interacting with cross-chain contracts. - Contracts that are back and forth sending the data request to cross-chain contracts.

To build second type of contracts that are interacting with other chains, the user/ applications needs to implement router-wasm-binding crate.

```sh

add the following line in the cargo.toml [dependencies] section

router-wasm-bindings = "0.1.2" ```

How to use the Router-Wasm-Bindings

To implement cross-chain interoperability, the contract needs to implement the following functionality - SudoMsg for handling incoming requests from the other chains - RouterMsg to send request to the other chains.

The Contract can write the intermediate business logic inbetween of the incoming request and outbound request. While writing the intermediate business logic, the developer can combine single or multiple incoming request into single or multiple outbound request.

Also, while creating request to other chain, the contract can be developed in such a way where mutiple request can be generated to different chains.

You can find examples for different scenarios in the cw-bridge-contracts repository.

[SudoMsg]

The sudo function is one of the entry point for a router-bridge-contract. It can be called internally by the chain only. It needs to implement to receive incoming request from the other chains. ```sh

import router binding message

use routerwasmbindings::{RouterMsg, SudoMsg};

[cfgattr(not(feature = "library"), entrypoint)]

pub fn sudo(deps: DepsMut, env: Env, msg: SudoMsg) -> StdResult> { match msg { # Sudo msg to handle in coming request from other chains SudoMsg::HandleInboundReq { sender, chaintype, sourcechainid, payload, } => handleinboundrequest(deps, sender, chaintype, sourcechainid, payload), # Sudo msg to handle outbound message acknowledgement SudoMsg::HandleOutboundAck { outboundtxrequestedby, destinationchaintype, destinationchainid, outboundbatchnonce, contractackresponses, } => handleoutboundackrequest( deps, outboundtxrequestedby, destinationchaintype, destinationchainid, outboundbatchnonce, contractackresponses, ), } }

```

[RouterMsg]

The RouterMsg is a enum type of the entry point for a router-bridge-contract. It can be called internally by the chain only. It needs to implement to receive incoming request from the other chains. ```sh

import router binding message

use routerwasmbindings::{RouterMsg, SudoMsg};

let address: String = String::from("destinationcontractaddress"); let payload: Vec = let payload: Vec = b"sample paylaod data".to_vec();

Single Outbound request with single contract call

let contractcall: ContractCall = ContractCall { destinationcontractaddress: address.clone().intobytes(), payload, }; let outboundbatchreq: OutboundBatchRequest = OutboundBatchRequest { destinationchaintype: ChainType::ChainTypeEvm.getchaincode(), destinationchainid: String::from("137"), contractcalls: vec![contractcall], relayerfee: Coin { denom: String::from("router"), amount: Uint128::new(8u128), }, outgoingtxfee: Coin { denom: String::from("router"), amount: Uint128::new(8u128), }, isatomic: false, exptimestamp: None, }; let outboundbatchreqs: RouterMsg = RouterMsg::OutboundBatchRequests { outboundbatchrequests: vec![outboundbatchreq], issequenced: false, };

let res = Response::new() .addmessage(outboundbatch_reqs); Ok(res)

```