pub fn withdraw_voting_tokens<S: Storage, A: Api, Q: Querier>(
    deps: &mut Extern<S, A, Q>,
    env: Env,
    amount: Option<Uint128>,
) -> HandleResult {
    let sender_address_raw = deps.api.canonical_address(&env.message.sender)?;
    let key = sender_address_raw.as_slice();

    if let Some(mut token_manager) = bank_read(&deps.storage).may_load(key)? {
        let config: Config = config_store(&mut deps.storage).load()?;
        let mut state: State = state_store(&mut deps.storage).load()?;

        // Load total share & total balance except proposal deposit amount
        let total_share = state.total_share.u128();
        let total_locked_balance = state.total_deposit + state.pending_voting_rewards;
        let total_balance = (load_token_balance(
            &deps,
            &deps.api.human_address(&config.mirror_token)?,
            &state.contract_addr,
        )? - total_locked_balance)?
            .u128();

        let user_locked_balance =
            compute_locked_balance(deps, &mut token_manager, &sender_address_raw)?;
        let user_locked_share = user_locked_balance * total_share / total_balance;
        let user_share = token_manager.share.u128();

        let withdraw_share = amount
            .map(|v| std::cmp::max(v.multiply_ratio(total_share, total_balance).u128(), 1u128))
            .unwrap_or_else(|| user_share - user_locked_share);
        let withdraw_amount = amount
            .map(|v| v.u128())
            .unwrap_or_else(|| withdraw_share * total_balance / total_share);

        if user_locked_share + withdraw_share > user_share {
            Err(StdError::generic_err(
                "User is trying to withdraw too many tokens.",
            ))
        } else {
            let share = user_share - withdraw_share;
            token_manager.share = Uint128::from(share);

            bank_store(&mut deps.storage).save(key, &token_manager)?;

            state.total_share = Uint128::from(total_share - withdraw_share);
            state_store(&mut deps.storage).save(&state)?;

            send_tokens(
                &deps.api,
                &config.mirror_token,
                &sender_address_raw,
                withdraw_amount,
                "withdraw",
            )
        }
    } else {
        Err(StdError::generic_err("Nothing staked"))
    }
}
// Withdraw amount if not staked. By default all funds will be withdrawn.
Withdraw amount if not staked. By default all funds will be withdrawn.
Withdraw amount if not staked . By default all funds will be withdrawn .
pub fn withdraw_voting_tokens<S: Storage, A: Api, Q: Querier>(
    deps: &mut Extern<S, A, Q>,
    env: Env,
    amount: Option<Uint128>,
) -> HandleResult {
    let sender_address_raw = deps.api.canonical_address(&env.message.sender)?;
    let key = sender_address_raw.as_slice();
    if let Some(mut token_manager) = bank_read(&deps.storage).may_load(key)? {
        let config: Config = config_store(&mut deps.storage).load()?;
        let mut state: State = state_store(&mut deps.storage).load()?;
        // Load total share & total balance except proposal deposit amount
        let total_share = state.total_share.u128();
        let total_locked_balance = state.total_deposit + state.pending_voting_rewards;
        let total_balance = (load_token_balance(
            &deps,
            &deps.api.human_address(&config.mirror_token)?,
            &state.contract_addr,
        )? - total_locked_balance)?
            .u128();
        let user_locked_balance =
            compute_locked_balance(deps, &mut token_manager, &sender_address_raw)?;
        let user_locked_share = user_locked_balance * total_share / total_balance;
        let user_share = token_manager.share.u128();
        let withdraw_share = amount
            .map(|v| std::cmp::max(v.multiply_ratio(total_share, total_balance).u128(), 1u128))
            .unwrap_or_else(|| user_share - user_locked_share);
        let withdraw_amount = amount
            .map(|v| v.u128())
            .unwrap_or_else(|| withdraw_share * total_balance / total_share);
        if user_locked_share + withdraw_share > user_share {
            Err(StdError::generic_err(
                "User is trying to withdraw too many tokens.",
            ))
        } else {
            let share = user_share - withdraw_share;
            token_manager.share = Uint128::from(share);
            bank_store(&mut deps.storage).save(key, &token_manager)?;
            state.total_share = Uint128::from(total_share - withdraw_share);
            state_store(&mut deps.storage).save(&state)?;
            send_tokens(
                &deps.api,
                &config.mirror_token,
                &sender_address_raw,
                withdraw_amount,
                "withdraw",
            )
        }
    } else {
        Err(StdError::generic_err("Nothing staked"))
    }
}
pub fn withdraw_voting_tokens < S : Storage , A : Api , Q : Querier > ( deps : & mut Extern < S , A , Q > , env : Env , amount : Option < Uint128 > , ) -> HandleResult { let sender_address_raw = deps . api . canonical_address ( & env . message . sender ) ? ; let key = sender_address_raw . as_slice ( ) ; if let Some ( mut token_manager ) = bank_read ( & deps . storage ) . may_load ( key ) ? { let config : Config = config_store ( & mut deps . storage ) . load ( ) ? ; let mut state : State = state_store ( & mut deps . storage ) . load ( ) ? ; // Load total share & total balance except proposal deposit amount let total_share = state . total_share . u128 ( ) ; let total_locked_balance = state . total_deposit + state . pending_voting_rewards ; let total_balance = ( load_token_balance ( & deps , & deps . api . human_address ( & config . mirror_token ) ? , & state . contract_addr , ) ? - total_locked_balance ) ? . u128 ( ) ; let user_locked_balance = compute_locked_balance ( deps , & mut token_manager , & sender_address_raw ) ? ; let user_locked_share = user_locked_balance * total_share / total_balance ; let user_share = token_manager . share . u128 ( ) ; let withdraw_share = amount . map ( | v | std :: cmp :: max ( v . multiply_ratio ( total_share , total_balance ) . u128 ( ) , 1u128 ) ) . unwrap_or_else ( | | user_share - user_locked_share ) ; let withdraw_amount = amount . map ( | v | v . u128 ( ) ) . unwrap_or_else ( | | withdraw_share * total_balance / total_share ) ; if user_locked_share + withdraw_share > user_share { Err ( StdError :: generic_err ( " " , ) ) } else { let share = user_share - withdraw_share ; token_manager . share = Uint128 :: from ( share ) ; bank_store ( & mut deps . storage ) . save ( key , & token_manager ) ? ; state . total_share = Uint128 :: from ( total_share - withdraw_share ) ; state_store ( & mut deps . storage ) . save ( & state ) ? ; send_tokens ( & deps . api , & config . mirror_token , & sender_address_raw , withdraw_amount , " " , ) } } else { Err ( StdError :: generic_err ( " " ) ) } }
Withdraw amount if not staked.
Withdraw amount if not staked .
deps
&mut Extern<S, A, Q>
env
Env
amount
Option<Uint128>