diff --git a/content/2.developers/3.guides/13.cosmwasm-by-example/7.sending-tokens.md b/content/2.developers/3.guides/13.cosmwasm-by-example/7.sending-tokens.md index 692c94c4..2a41b845 100644 --- a/content/2.developers/3.guides/13.cosmwasm-by-example/7.sending-tokens.md +++ b/content/2.developers/3.guides/13.cosmwasm-by-example/7.sending-tokens.md @@ -6,12 +6,14 @@ parentSection: Developers parentSectionPath: /developers --- -# Sending Tokens +# Sending tokens + This section shows a smart contract designed to send a blockchain's native tokens to a recipient specified by the original sender in the execute message. ## Explanation ### send_tokens + Once the main use case of this function is executed (which in this context is void), a bank message is appended for the contract to act upon. It's worth noting that the contract becomes the signer of the transaction, not the initiating sender. ::highlight-card ```rust @@ -28,18 +30,26 @@ pub fn send_tokens( A developer crafts a BankMsg to transmit tokens to a specified address using the native token. The function will fail if the smart contract lacks sufficient tokens. If any error surfaces prior to the response's generation, funds won't be transmitted. */ + + let msg = CosmosMsg::Bank(BankMsg::Send { + to_address: contract_address.into_string(), + amount: vec![ + Coin{ + denom, amount + } + ], + }); Ok(Response::new() .add_attribute("action", "send") - .add_message(BankMsg::Send { - to_address: to.into_string(), - amount: vec![Coin{denom, amount}] - }) + .add_message(msg) ) } ``` :: -### Integration Testing + +### Integration testing + ::highlight-card ```rust // integration_tests.rs @@ -53,7 +63,7 @@ fn balance() { to: Addr::unchecked("receiver") }; - let funds_sent = Coin::new(10u128, "token".to_string()); + let funds_sent = Coin { denom: "token".to_string(), amount: Uint128::new(10) }; let cosmos_msg = cw_template_contract.call(msg, funds_sent).unwrap(); app.execute(Addr::unchecked(USER), cosmos_msg).unwrap(); @@ -93,6 +103,7 @@ pub use crate::error::ContractError; :: ### contract.rs + ::highlight-card ```rust #[cfg(not(feature = "library"))] @@ -125,7 +136,7 @@ pub fn execute( msg: ExecuteMsg, ) -> Result { match msg { - ExecuteMsg::SendTokens {amount, denom, to} => execute::send_tokens(deps, amount, denom, to), + ExecuteMsg::SendTokens {amount, denom, to} => send_tokens(deps, amount, denom, to), } } @@ -136,7 +147,7 @@ pub mod execute { pub fn send_tokens(_deps: DepsMut, amount: Uint128, denom: String, to: Addr) -> Result { - Ok(Response::new().add_attribute("action", "increment") + Ok(Response::new().add_attribute("action", "send") /* Sending tokens is part of the response of a function Developer creates a BankMsg to send tokens to an address with a specific native token Will fail if smart contract does not have this much tokens initially */ @@ -149,6 +160,7 @@ pub mod execute { #[cfg_attr(not(feature = "library"), entry_point)] pub fn query(_deps: Deps, _env: Env, msg: QueryMsg) -> StdResult { match msg { + _ => Err(StdError::generic_err("Unsupported query")), } } @@ -164,6 +176,7 @@ mod tests { :: ### msg.rs + ::highlight-card ```rust use cosmwasm_schema::{cw_serde, QueryResponses}; @@ -192,6 +205,7 @@ pub struct BalanceResponse { :: ### error.rs + ::highlight-card ```rust use cosmwasm_std::StdError; @@ -212,6 +226,7 @@ pub enum ContractError { :: ### state.rs + ::highlight-card ```rust use schemars::JsonSchema; @@ -232,6 +247,7 @@ pub const STATE: Item = Item::new("state"); :: ### helpers.rs + ::highlight-card ```rust use schemars::JsonSchema; @@ -269,6 +285,7 @@ impl CwTemplateContract { :: ### integration_tests.rs + ::highlight-card ```rust #[cfg(test)] @@ -336,7 +353,7 @@ mod tests { let (mut app, cw_template_contract) = proper_instantiate(); let msg = ExecuteMsg::SendTokens { amount: Uint128::new(10), denom: "token".to_string(), to: Addr::unchecked("receiver") } ; - let funds_sent = Coin::new(10u128, "token".to_string()); + let funds_sent = Coin { denom: "token".to_string(), amount: Uint128::new(10) }; let cosmos_msg = cw_template_contract.call(msg, funds_sent).unwrap(); app.execute(Addr::unchecked(USER), cosmos_msg).unwrap(); let balance = app.wrap().query_balance("receiver", "token").unwrap();