From c4e8bd2daeed44c9073509ecafcfe88882d3cc85 Mon Sep 17 00:00:00 2001 From: alex324998301 Date: Sat, 26 Mar 2022 20:47:53 -0400 Subject: [PATCH] added payup funtion and other stuff related to that in other files --- contracts/counter/src/contract.rs | 42 ++++++++++++++++++++++--------- contracts/counter/src/error.rs | 3 +++ contracts/counter/src/lib.rs | 2 +- contracts/counter/src/msg.rs | 4 +-- contracts/counter/src/state.rs | 3 +-- 5 files changed, 37 insertions(+), 17 deletions(-) diff --git a/contracts/counter/src/contract.rs b/contracts/counter/src/contract.rs index 4d0fa6b..9d90809 100644 --- a/contracts/counter/src/contract.rs +++ b/contracts/counter/src/contract.rs @@ -19,8 +19,8 @@ pub fn instantiate( msg: InstantiateMsg, ) -> Result { let state = State { - count: msg.count, - owner: info.sender.clone(), + storeowner: info.sender.clone(), + bill: msg.bill }; set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?; STATE.save(deps.storage, &state)?; @@ -39,9 +39,9 @@ pub fn execute( msg: ExecuteMsg, ) -> Result { match msg { - ExecuteMsg::Increment {} => try_increment(deps), - ExecuteMsg::Reset { count } => try_reset(deps, info, count), + ExecuteMsg::Payup {} => try_payup(deps, info), } + } pub fn try_increment(deps: DepsMut) -> Result { @@ -52,15 +52,33 @@ pub fn try_increment(deps: DepsMut) -> Result { Ok(Response::new().add_attribute("method", "try_increment")) } -pub fn try_reset(deps: DepsMut, info: MessageInfo, count: i32) -> Result { - STATE.update(deps.storage, |mut state| -> Result<_, ContractError> { - if info.sender != state.owner { - return Err(ContractError::Unauthorized {}); +pub fn try_payup(deps: DepsMut, info: MessageInfo: i32) -> Result { + let config = STATE.load(deps.storage)?; + let deposit_amount: Uint256 = info + .funds + .iter() + .find(|c| c.denom == "uluna") + .map(|c| Uint256::from(c.amount)) + .unwrap_or_else(Uint256::zero); + if deposit_amount.is_zero() { + return Err(ContractError::ZeroDeposit()); } - state.count = count; - Ok(state) - })?; - Ok(Response::new().add_attribute("method", "reset")) + + let msg = CosmosMsg::Bank(BankMsg::Send { + to_address: config.storeowner, + amount: vec![deduct_tax( + deps.as_ref(), + Coin { + denom: "uusd".to_string(), + amount: balance, + }, + )?], + }); + + Ok(Response::new().add_message(msg)) +} +pub createorder(deps: DepsMut, info: MessageInfo: i32) -> Result){ + } #[cfg_attr(not(feature = "library"), entry_point)] diff --git a/contracts/counter/src/error.rs b/contracts/counter/src/error.rs index 4a69d8f..4b1dd3e 100644 --- a/contracts/counter/src/error.rs +++ b/contracts/counter/src/error.rs @@ -8,6 +8,9 @@ pub enum ContractError { #[error("Unauthorized")] Unauthorized {}, + + #[error("you cant deposit $0")] + ZeroDeposit {}, // Add any other custom errors you like here. // Look at https://docs.rs/thiserror/1.0.21/thiserror/ for details. } diff --git a/contracts/counter/src/lib.rs b/contracts/counter/src/lib.rs index dfedc9d..da2e1e4 100644 --- a/contracts/counter/src/lib.rs +++ b/contracts/counter/src/lib.rs @@ -3,4 +3,4 @@ mod error; pub mod msg; pub mod state; -pub use crate::error::ContractError; +pub use crate::error::ContractError; \ No newline at end of file diff --git a/contracts/counter/src/msg.rs b/contracts/counter/src/msg.rs index 4cec36d..fed76d7 100644 --- a/contracts/counter/src/msg.rs +++ b/contracts/counter/src/msg.rs @@ -9,10 +9,10 @@ pub struct InstantiateMsg { #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] #[serde(rename_all = "snake_case")] pub enum ExecuteMsg { - Increment {}, - Reset { count: i32 }, + Payup {} } + #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] #[serde(rename_all = "snake_case")] pub enum QueryMsg { diff --git a/contracts/counter/src/state.rs b/contracts/counter/src/state.rs index 1b426f9..893adeb 100644 --- a/contracts/counter/src/state.rs +++ b/contracts/counter/src/state.rs @@ -6,8 +6,7 @@ use cw_storage_plus::Item; #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] pub struct State { - pub count: i32, - pub owner: Addr, + pub storeowner: Addr, } pub const STATE: Item = Item::new("state");