How to deposit(receive) native SCRT and tokens at my secret contract

I cannot find an example about depositing/receiving SCRT and tokens in the doc.
I guess it should be like cross contract communication Cross Contract Communication | Secret Network, right?
Could someone give me an example?

  //let funds: Vec<Coin> = info.funds;
  if info.funds.len() != 1 {
    return Err(StdError::generic_err("Only One Token is accepted"));
  }
  if info.funds[0].denom != "uscrt" {
    return Err(StdError::generic_err("Only SCRT is accepted"));
  }
  let amount = info.funds[0].amount.u128();

All my contracts use snips so unfortunately I don’t have a personal example but

MessageInfo in cosmwasm_std - Rust contains funds and you can use that info in an execute contract flow like so

pub fn execute(
    deps: DepsMut,
    _env: Env,
    info: MessageInfo,
    msg: ExecuteMsg,
) -> StdResult<Response> {
    match msg {
        ExecuteMsg::ReceiveNative {} => {
            // Check if funds were sent
            if info.funds.is_empty() {
                return Err(StdError::generic_err("No funds sent"));
            }

            // Validate the received tokens (e.g., check denomination and amount)
            for coin in info.funds.iter() {
                if coin.denom != "uscrt" {
                    return Err(StdError::generic_err("Invalid denomination"));
                }
                if coin.amount.is_zero() {
                    return Err(StdError::generic_err("Amount must be greater than zero"));
                }
            }

            // Process the received tokens (e.g., update state or perform actions)
            // Example: Log the received amount
            let amount = info.funds[0].amount;
            Ok(Response::new()
                .add_attribute("action", "receive_native")
                .add_attribute("amount", amount.to_string())
                .add_attribute("denom", "uscrt"))
        }
    }
}
1 Like