Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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();

Expand Down Expand Up @@ -93,6 +103,7 @@ pub use crate::error::ContractError;
::

### contract.rs

::highlight-card
```rust
#[cfg(not(feature = "library"))]
Expand Down Expand Up @@ -125,7 +136,7 @@ pub fn execute(
msg: ExecuteMsg,
) -> Result<Response, ContractError> {
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),
}
}

Expand All @@ -136,7 +147,7 @@ pub mod execute {

pub fn send_tokens(_deps: DepsMut, amount: Uint128, denom: String, to: Addr) -> Result<Response, ContractError> {

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 */
Expand All @@ -149,6 +160,7 @@ pub mod execute {
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(_deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
_ => Err(StdError::generic_err("Unsupported query")),
}
}

Expand All @@ -164,6 +176,7 @@ mod tests {
::

### msg.rs

::highlight-card
```rust
use cosmwasm_schema::{cw_serde, QueryResponses};
Expand Down Expand Up @@ -192,6 +205,7 @@ pub struct BalanceResponse {
::

### error.rs

::highlight-card
```rust
use cosmwasm_std::StdError;
Expand All @@ -212,6 +226,7 @@ pub enum ContractError {
::

### state.rs

::highlight-card
```rust
use schemars::JsonSchema;
Expand All @@ -232,6 +247,7 @@ pub const STATE: Item<State> = Item::new("state");
::

### helpers.rs

::highlight-card
```rust
use schemars::JsonSchema;
Expand Down Expand Up @@ -269,6 +285,7 @@ impl CwTemplateContract {
::

### integration_tests.rs

::highlight-card
```rust
#[cfg(test)]
Expand Down Expand Up @@ -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();
Expand Down