fn execute_redeem(
    deps: DepsMut,
    env: Env,
    info: MessageInfo,
    amount: Uint128,
    code: String,
    sender: Option<String>,
    memo: String,
) -> Result<Response, ContractError> {
    if REEDEMS.has(deps.storage, &code) {
        return Err(ContractError::RedeemCodeUsed {});
    }

    if amount == Uint128::zero() {
        return Err(Cw20ContractError::InvalidZeroAmount {}.into());
    }

    // lower balance
    BALANCES.update(
        deps.storage,
        &info.sender,
        |balance: Option<Uint128>| -> Result<_, ContractError> {
            let balance = balance.unwrap_or_default();
            balance
                .checked_sub(amount)
                .map_err(|_| ContractError::RedeemOverBalance(balance))
        },
    )?;

    // reduce total_supply
    TOKEN_INFO.update(deps.storage, |mut info| -> StdResult<_> {
        info.total_supply = info.total_supply.checked_sub(amount)?;
        Ok(info)
    })?;

    REEDEMS.save(
        deps.storage,
        &code,
        &Redeem {
            sender: info.sender.clone(),
            amount,
            memo: memo.clone(),
            timestamp: env.block.time,
        },
    )?;

    let sender = if let Some(sender) = sender {
        deps.api.addr_validate(&sender)?;
        sender
    } else {
        info.sender.to_string()
    };

    let event = Event::new("redeem")
        .add_attribute("code", code)
        .add_attribute("sender", sender)
        .add_attribute("amount", amount)
        .add_attribute("memo", memo);

    Ok(Response::new()
        .add_event(event)
        .add_attribute("action", "redeem")
        .add_attribute("from", info.sender)
        .add_attribute("amount", amount))
}
/// Redeems token effectively burning them and storing information about redeem internally. This
/// also triggers custom `redeem` event with details of process. Before redeeming, sender should
/// make sure, that token provider is aware about such possibility and is willing to cover redeem
/// off-chain, otherwise this may be equivalent to destrotying commodity.
Redeems token effectively burning them and storing information about redeem internally. This
also triggers custom `redeem` event with details of process. Before redeeming, sender should
make sure, that token provider is aware about such possibility and is willing to cover redeem
off-chain, otherwise this may be equivalent to destrotying commodity.
Redeems token effectively burning them and storing information about redeem internally . This also triggers custom ` redeem ` event with details of process . Before redeeming sender should make sure that token provider is aware about such possibility and is willing to cover redeem off - chain otherwise this may be equivalent to destrotying commodity .
fn execute_redeem(
    deps: DepsMut,
    env: Env,
    info: MessageInfo,
    amount: Uint128,
    code: String,
    sender: Option<String>,
    memo: String,
) -> Result<Response, ContractError> {
    if REEDEMS.has(deps.storage, &code) {
        return Err(ContractError::RedeemCodeUsed {});
    }
    if amount == Uint128::zero() {
        return Err(Cw20ContractError::InvalidZeroAmount {}.into());
    }
    // lower balance
    BALANCES.update(
        deps.storage,
        &info.sender,
        |balance: Option<Uint128>| -> Result<_, ContractError> {
            let balance = balance.unwrap_or_default();
            balance
                .checked_sub(amount)
                .map_err(|_| ContractError::RedeemOverBalance(balance))
        },
    )?;
    // reduce total_supply
    TOKEN_INFO.update(deps.storage, |mut info| -> StdResult<_> {
        info.total_supply = info.total_supply.checked_sub(amount)?;
        Ok(info)
    })?;
    REEDEMS.save(
        deps.storage,
        &code,
        &Redeem {
            sender: info.sender.clone(),
            amount,
            memo: memo.clone(),
            timestamp: env.block.time,
        },
    )?;
    let sender = if let Some(sender) = sender {
        deps.api.addr_validate(&sender)?;
        sender
    } else {
        info.sender.to_string()
    };
    let event = Event::new("redeem")
        .add_attribute("code", code)
        .add_attribute("sender", sender)
        .add_attribute("amount", amount)
        .add_attribute("memo", memo);
    Ok(Response::new()
        .add_event(event)
        .add_attribute("action", "redeem")
        .add_attribute("from", info.sender)
        .add_attribute("amount", amount))
}
fn execute_redeem ( deps : DepsMut , env : Env , info : MessageInfo , amount : Uint128 , code : String , sender : Option < String > , memo : String , ) -> Result < Response , ContractError > { if REEDEMS . has ( deps . storage , & code ) { return Err ( ContractError :: RedeemCodeUsed { } ) ; } if amount == Uint128 :: zero ( ) { return Err ( Cw20ContractError :: InvalidZeroAmount { } . into ( ) ) ; } // lower balance BALANCES . update ( deps . storage , & info . sender , | balance : Option < Uint128 > | -> Result < _ , ContractError > { let balance = balance . unwrap_or_default ( ) ; balance . checked_sub ( amount ) . map_err ( | _ | ContractError :: RedeemOverBalance ( balance ) ) } , ) ? ; // reduce total_supply TOKEN_INFO . update ( deps . storage , | mut info | -> StdResult < _ > { info . total_supply = info . total_supply . checked_sub ( amount ) ? ; Ok ( info ) } ) ? ; REEDEMS . save ( deps . storage , & code , & Redeem { sender : info . sender . clone ( ) , amount , memo : memo . clone ( ) , timestamp : env . block . time , } , ) ? ; let sender = if let Some ( sender ) = sender { deps . api . addr_validate ( & sender ) ? ; sender } else { info . sender . to_string ( ) } ; let event = Event :: new ( " " ) . add_attribute ( " " , code ) . add_attribute ( " " , sender ) . add_attribute ( " " , amount ) . add_attribute ( " " , memo ) ; Ok ( Response :: new ( ) . add_event ( event ) . add_attribute ( " " , " " ) . add_attribute ( " " , info . sender ) . add_attribute ( " " , amount ) ) }
Redeems token effectively burning them and storing information about redeem internally.
Redeems token effectively burning them and storing information about redeem internally .
deps
DepsMut
env
Env
info
MessageInfo
amount
Uint128
code
String
sender
Option<String>
memo
String
deps
DepsMut
env
Env
info
MessageInfo
amount
Uint128
code
String
sender
Option<String>
memo
String