valkyrie/
message_factories.rs

1use cosmwasm_std::{
2    Addr, BankMsg, Binary, Coin, CosmosMsg, QuerierWrapper, StdResult, to_binary, Uint128, WasmMsg,
3};
4use cw20::Cw20ExecuteMsg;
5use serde::Serialize;
6
7use crate::terra::extract_tax;
8
9pub fn native_send(
10    querier: &QuerierWrapper,
11    denom: String,
12    recipient: &Addr,
13    amount_with_tax: Uint128,
14) -> StdResult<CosmosMsg> {
15    let tax = extract_tax(querier, denom.to_string(), amount_with_tax)?;
16
17    Ok(CosmosMsg::Bank(BankMsg::Send {
18        to_address: recipient.to_string(),
19        amount: vec![Coin {
20            amount: amount_with_tax.checked_sub(tax)?,
21            denom,
22        }],
23    }))
24}
25
26pub fn cw20_transfer(token: &Addr, recipient: &Addr, amount: Uint128) -> CosmosMsg {
27    CosmosMsg::Wasm(WasmMsg::Execute {
28        contract_addr: token.to_string(),
29        funds: vec![],
30        msg: to_binary(&Cw20ExecuteMsg::Transfer {
31            recipient: recipient.to_string(),
32            amount,
33        })
34            .unwrap(),
35    })
36}
37
38pub fn wasm_instantiate(code_id: u64, admin: Option<Addr>, msg: Binary) -> CosmosMsg {
39    CosmosMsg::Wasm(WasmMsg::Instantiate {
40        admin: admin.map(|v| v.to_string()),
41        code_id,
42        msg,
43        funds: vec![],
44        label: String::new(),
45    })
46}
47
48pub fn wasm_execute<T>(contract: &Addr, msg: &T) -> CosmosMsg
49where
50    T: Serialize + ?Sized {
51    wasm_execute_bin(contract, to_binary(&msg).unwrap())
52}
53
54pub fn wasm_execute_with_funds<T>(contract: &Addr, funds: Vec<Coin>, msg: &T) -> CosmosMsg
55where
56    T: Serialize + ?Sized {
57    wasm_execute_bin_with_funds(contract, funds, to_binary(msg).unwrap())
58}
59
60pub fn wasm_execute_bin(contract: &Addr, msg: Binary) -> CosmosMsg {
61    wasm_execute_bin_with_funds(contract, vec![], msg)
62}
63
64pub fn wasm_execute_bin_with_funds(contract: &Addr, funds: Vec<Coin>, msg: Binary) -> CosmosMsg {
65    CosmosMsg::Wasm(WasmMsg::Execute {
66        contract_addr: contract.to_string(),
67        funds,
68        msg,
69    })
70}