Explanation of executing modules inside a CosmWasm contract using sending Native SCRT as an example.
Sending Native SCRT from a contract is realtively trivial and can be done using the BankMsg::Send message.
This is a contract-to-module call. Any native denoms can be send in this way (ex. native IBC tokens).
#[entry_point]pubfnexecute( deps:DepsMut, env:Env, info:MessageInfo, msg:ExecuteMsg// this is a custom struct defined in your msg.rs) ->StdResult<Response> {// example: send 33 uscrtlet coins_to_send:Vec<Coin> =vec![Coin { denom:"uscrt".to_string(), amount:Uint128::from(33u128), }];let message =CosmosMsg::Bank(BankMsg::Send {// replace with recipient of your choice to_address: info.sender.clone().into_string(), amount: coins_to_send, });let res =Response::new().add_message(message);Ok(res)}
pubfnhandle<S:Storage, A:Api, Q:Querier>( _deps:&mutExtern<S, A, Q>, env:Env, _msg:HandleMsg, // this is a custom struct defined in your msg.rs) ->StdResult<HandleResponse> {// example: send 33 uscrtlet coins_to_send:Vec<Coin> =vec![Coin { denom:"uscrt".to_string(), amount:Uint128::from(33u128), }];let res =HandleResponse { messages:vec![CosmosMsg::Bank(BankMsg::Send { from_address: env.contract.address,// replace with recipient of your choice to_address: env.message.sender, amount: coins_to_send, })], log:vec![], data:None, };Ok(res)}
Having Trouble? Always make sure your contract has enough funds to begin with!
You can send some coins to your contract like this:
secretcli tx bank send <from-account> <contract-address> 1000uscrt
You can always see decrypted error messages from the contract like so:
secretcli q compute tx <tx_hash>
Transaction logs
You can see the spent coins as events when you query for the transaction: