cauuu 0.1.1

A SDK for the cauuu staking contracts
Documentation
extern crate cosmwasm_schema;
extern crate cosmwasm_std;
extern crate cosmwasm_storage;
extern crate cw_controllers;
extern crate schemars;
extern crate serde;

use self::cosmwasm_schema::{cw_serde, QueryResponses};
use self::cosmwasm_std::{Addr, Coin, Decimal, Timestamp, Uint128};
use self::cw_controllers::AdminResponse;

#[cw_serde]
pub struct MigrateMsg {}

#[cw_serde]
pub struct InstantiateMsg {
    pub underlying_coin_denom: String,
    pub unbonding_period: u64,
    pub protocol_fee: Decimal,
    pub subdenom: String,
    pub blocks_per_year: u64,
    pub target_bonding_ratio: Decimal,
    pub inflation_rate_change: Decimal,
    pub min_inflation: Decimal,
    pub max_inflation: Decimal,
    pub rewards_max_variation: u64,
}

#[cw_serde]
pub struct Parameters {
    pub underlying_coin_denom: String,
    pub unbonding_period: u64,
    pub protocol_fee: Decimal,
    pub ctoken_denom: String,
    pub blocks_per_year: u64,
    pub target_bonding_ratio: Decimal,
    pub inflation_rate_change: Decimal,
    pub min_inflation: Decimal,
    pub max_inflation: Decimal,
    //reward max variation is the maximum variation of the reward that can be minted per each reward call.
    pub rewards_max_variation: u64,
}

#[cw_serde]
pub struct State {
    pub exchange_rate: Decimal,
    pub total_bonded_amount: Uint128,
    pub total_supply: Uint128,
    pub last_index_modification: u64,
    pub current_bonding_ratio: Decimal,
    pub current_inflation: Decimal,
}

impl State {
    pub fn update_exchange_rate(&mut self) {
        if self.total_bonded_amount.is_zero() || self.total_supply.is_zero() {
            self.exchange_rate = Decimal::one()
        } else {
            self.exchange_rate = Decimal::from_ratio(self.total_bonded_amount, self.total_supply);
        }
    }
}

#[cw_serde]
pub struct Config {
    pub protocol_fee_collector: Option<String>,
    pub mint_contract: Option<String>,
}

#[cw_serde]
pub enum ExecuteMsg {
    ////////////////////
    /// Owner's operations
    ////////////////////

    // Pause contract functionalities
    Pause {},
    // Unpause contract functionalities
    Unpause {},

    /// Set the owener
    UpdateConfig {
        mint_token: Option<String>,
        protocol_fee_collector: Option<String>,
    },

    /// Change the admin (must be called by current admin)
    UpdateAdmin {
        admin: String,
    },

    /// update the parameters that is needed for the contract
    UpdateParams {
        unbonding_period: Option<u64>,
        protocol_fee: Option<Decimal>,
        target_bonding_ratio: Option<Decimal>,
        min_inflation: Option<Decimal>,
        max_inflation: Option<Decimal>,
        inflation_rate_change: Option<Decimal>,
        rewards_max_variation: Option<u64>,
    },

    ////////////////////
    /// User's operations
    ////////////////////

    /// Receives `amount` in underlying coin denom from sender.
    /// Issue `amount` / exchange_rate for the user.
    Bond {},

    /// Update global index
    UpdateGlobalIndex {},

    Unbond {
        camount: Uint128,
    },
    /// Send back unbonded coin to the user
    WithdrawUnbonded {},
}

#[cw_serde]
#[derive(QueryResponses)]
pub enum QueryMsg {
    #[returns(ConfigResponse)]
    Config {},
    #[returns(StateResponse)]
    State {},
    #[returns(Uint128)]
    TotalWithdrawable { address: String },
    #[returns(Parameters)]
    Parameters {},
    #[returns(Vec<UnbondingRequest>)]
    UnbondRequests { address: String },
    #[returns(AdminResponse)]
    Admin {},
}

#[cw_serde]
pub struct UnbondingRequest {
    pub id: u64,
    pub requester: Addr,
    pub amount: Uint128,
    pub request_time: Timestamp,
    pub withdrawable_after: Timestamp,
}

#[cw_serde]
pub struct StateResponse {
    pub exchange_rate: Decimal,
    pub total_bond_amount: Uint128,
    pub last_index_modification: u64,
    pub total_supply: Uint128,
    pub inflation: Decimal,
}

#[cw_serde]
pub struct ConfigResponse {
    pub mint_contract: Option<String>,
    pub protocol_fee_collector: Option<String>,
}

#[cw_serde]
pub struct WithdrawableResponse {
    pub withdrawable: Uint128,
}

#[cw_serde]
pub struct BondResponse {
    pub amount_out: Coin,
}