use cosmwasm_std::{
to_json_binary, Addr, Binary, Coin, CosmosMsg, DepsMut, StdError, StdResult, WasmMsg,
};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub enum SendType {
Wallet {
receiver: Addr,
},
SteakRewards {
steak: Addr,
receiver: Addr,
},
DistributeSteakRewards {
steak: Addr,
receiver: Addr,
},
TransferSteakRewards {
steak: Addr,
receiver: Addr,
},
}
impl ToString for SendType {
fn to_string(&self) -> String {
match &self {
SendType::Wallet {
receiver,
} => format!("Wallet -> {}", receiver),
SendType::SteakRewards {
steak,
receiver,
} => {
format!("Steak:{} -> {} -", steak, receiver)
},
SendType::DistributeSteakRewards {
steak,
receiver,
} => {
format!("Steak:{} -> {} DISTRIBUTE", steak, receiver)
},
SendType::TransferSteakRewards {
steak,
receiver,
} => {
format!("Steak:{} -> {} Transfer", steak, receiver)
},
}
}
}
impl SendType {
#[deprecated(since = "0.2.9", note = "insufficient checking. use verify_details")]
pub fn verify(&self, address: &Addr) -> bool {
match &self {
SendType::Wallet {
receiver,
} => receiver != address,
SendType::SteakRewards {
receiver,
..
} => receiver != address,
SendType::DistributeSteakRewards {
receiver,
..
} => receiver != address,
SendType::TransferSteakRewards {
receiver,
..
} => receiver != address,
}
}
pub fn verify_details(&self, deps: &DepsMut, address: &Addr) -> Result<(), StdError> {
match &self {
SendType::Wallet {
receiver,
} => {
if receiver != address {
deps.api.addr_validate(receiver.as_str())?;
Ok(())
} else {
Err(StdError::generic_err("address recursion"))
}
},
SendType::SteakRewards {
receiver,
steak,
..
} => {
if receiver != address {
deps.api.addr_validate(receiver.as_str())?;
deps.api.addr_validate(steak.as_str())?;
Ok(())
} else {
Err(StdError::generic_err("address recursion"))
}
},
SendType::DistributeSteakRewards {
receiver,
steak,
} => {
if receiver != address {
deps.api.addr_validate(receiver.as_str())?;
deps.api.addr_validate(steak.as_str())?;
Ok(())
} else {
Err(StdError::generic_err("address recursion"))
}
},
SendType::TransferSteakRewards {
receiver,
steak,
} => {
if receiver != address {
deps.api.addr_validate(receiver.as_str())?;
deps.api.addr_validate(steak.as_str())?;
Ok(())
} else {
Err(StdError::generic_err("address recursion"))
}
},
}
}
}
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, JsonSchema)]
pub struct AllocationDetail {
pub name: String, pub allocation: u8, pub send_after: Coin, pub send_type: SendType, }
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, JsonSchema)]
pub struct AllocationHolding {
pub name: String, pub allocation: u8, pub send_after: Coin, pub send_type: SendType, pub balance: Vec<Coin>,
}
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, JsonSchema)]
pub struct InstantiateMsg {
pub name: String,
pub gov_contract: String,
pub allocation: Vec<AllocationDetail>,
pub init_hook: Option<InitHook>,
}
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, JsonSchema)]
pub struct InitHook {
pub msg: Binary,
pub contract_addr: String,
}
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, JsonSchema)]
pub struct MigrateMsg {}
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum ExecuteMsg {
Deposit {
flush: bool,
},
AddAllocationDetail {
name: String,
allocation: u8,
send_after: Coin,
send_type: SendType,
},
ModifyAllocationDetail {
name: String,
allocation: u8,
send_after: Coin,
send_type: SendType,
},
RemoveAllocationDetail {
name: String,
},
Reconcile {},
TransferGovContract {
gov_contract: String,
blocks: u64,
},
AcceptGovContract {},
AddToFlushWhitelist {
address: String,
},
RemoveFromFlushWhitelist {
address: String,
},
}
impl ExecuteMsg {
pub fn into_binary(self) -> StdResult<Binary> {
to_json_binary(&self)
}
pub fn into_cosmos_msg<T: Into<String>, C>(
self,
contract_addr: T,
funds: Vec<Coin>,
) -> StdResult<CosmosMsg<C>>
where
C: Clone + std::fmt::Debug + PartialEq + JsonSchema,
{
let msg = self.into_binary()?;
let execute = WasmMsg::Execute {
contract_addr: contract_addr.into(),
msg,
funds,
};
Ok(execute.into())
}
}
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum QueryMsg {
Allocations {
start_after: Option<String>,
limit: Option<u32>,
},
Allocation {
name: String,
},
Ownership {},
FlushWhitelist {},
}
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, JsonSchema)]
pub struct OwnershipResponse {
pub owner: String,
pub new_owner: Option<String>,
pub block_height: Option<u64>,
}
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, JsonSchema)]
pub struct WhitelistResponse {
pub allowed: Vec<String>,
}
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, JsonSchema)]
pub struct AllocationResponse {
pub allocations: Vec<AllocationHolding>,
}