diff --git a/README.md b/README.md index f8d7cf8..a7fcddb 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # DefiHackathon-2022 -Demo video: https://youtu.be/kMNxAIWr4T8 \ No newline at end of file + +Hello diff --git a/config.terrain.json b/config.terrain.json index c49c951..bbc25ef 100644 --- a/config.terrain.json +++ b/config.terrain.json @@ -17,7 +17,7 @@ } }, "instantiateMsg": { - "bill": 20000000 + "count": 0 } } } diff --git a/contracts/counter/Cargo.lock b/contracts/counter/Cargo.lock index 6294467..8b9dd73 100644 --- a/contracts/counter/Cargo.lock +++ b/contracts/counter/Cargo.lock @@ -103,7 +103,6 @@ dependencies = [ "cosmwasm-storage", "cw-storage-plus", "cw2", - "cw20", "schemars", "serde", "thiserror", @@ -164,24 +163,10 @@ name = "cw-storage-plus" version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c1e867b9972b83b32e00e878dfbff48299ba26618dabeb19b9c56fae176dc225" - -dependencies = [ - "cosmwasm-std", - "cw-storage-plus", - "schemars", - "serde", -] - -[[package]] -name = "cw0" -version = "0.9.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d759bb5418a3bdf091e1f1be17de2a15d95d2be4fee28045c2e461f4c6d9d1ca" dependencies = [ "cosmwasm-std", "schemars", "serde", - "thiserror", ] [[package]] @@ -196,18 +181,6 @@ dependencies = [ "serde", ] -[[package]] -name = "cw20" -version = "0.9.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac49b013ca1e355fd988cc1926acc9a16d7fd45cfb595ee330455582a788b100" -dependencies = [ - "cosmwasm-std", - "cw0", - "schemars", - "serde", -] - [[package]] name = "der" version = "0.4.1" diff --git a/contracts/counter/Cargo.toml b/contracts/counter/Cargo.toml index 9a0dd8b..7f29341 100644 --- a/contracts/counter/Cargo.toml +++ b/contracts/counter/Cargo.toml @@ -44,7 +44,6 @@ cosmwasm-std = { version = "0.16.2" } cosmwasm-storage = { version = "0.16.0" } cw-storage-plus = "0.8.0" cw2 = "0.8.1" -cw20 = "0.9.0" schemars = "0.8.3" serde = { version = "1.0.127", default-features = false, features = ["derive"] } thiserror = { version = "1.0.26" } diff --git a/contracts/counter/artifacts/checksums.txt b/contracts/counter/artifacts/checksums.txt deleted file mode 100644 index 884699e..0000000 --- a/contracts/counter/artifacts/checksums.txt +++ /dev/null @@ -1 +0,0 @@ -20828f78f4b256f0563c2fbb65b5361504f948dd4910dfe54e981aa5670f73a8 counter.wasm diff --git a/contracts/counter/artifacts/checksums_intermediate.txt b/contracts/counter/artifacts/checksums_intermediate.txt deleted file mode 100644 index ff9a52d..0000000 --- a/contracts/counter/artifacts/checksums_intermediate.txt +++ /dev/null @@ -1 +0,0 @@ -07b18dc271100d7c375e9ff1de8a767899ffa745dd52dbaa0ce9ab587346d78e ./target/wasm32-unknown-unknown/release/counter.wasm diff --git a/contracts/counter/artifacts/counter.wasm b/contracts/counter/artifacts/counter.wasm deleted file mode 100644 index d1b4512..0000000 Binary files a/contracts/counter/artifacts/counter.wasm and /dev/null differ diff --git a/contracts/counter/examples/schema.rs b/contracts/counter/examples/schema.rs index 0bcc70f..fdf606f 100644 --- a/contracts/counter/examples/schema.rs +++ b/contracts/counter/examples/schema.rs @@ -3,7 +3,7 @@ use std::fs::create_dir_all; use cosmwasm_schema::{export_schema, remove_schemas, schema_for}; -use counter::msg::{totalPayersResponse, ExecuteMsg, InstantiateMsg, QueryMsg}; +use counter::msg::{CountResponse, ExecuteMsg, InstantiateMsg, QueryMsg}; use counter::state::State; fn main() { @@ -16,5 +16,5 @@ fn main() { export_schema(&schema_for!(ExecuteMsg), &out_dir); export_schema(&schema_for!(QueryMsg), &out_dir); export_schema(&schema_for!(State), &out_dir); - export_schema(&schema_for!(totalPayersResponse), &out_dir); + export_schema(&schema_for!(CountResponse), &out_dir); } diff --git a/contracts/counter/src/contract.rs b/contracts/counter/src/contract.rs index 53d26c0..4b9dd18 100644 --- a/contracts/counter/src/contract.rs +++ b/contracts/counter/src/contract.rs @@ -1,12 +1,11 @@ #[cfg(not(feature = "library"))] use cosmwasm_std::entry_point; -use cosmwasm_std::{Order, Uint128, Coin, BankMsg, CosmosMsg, Uint256, to_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult, Api, Addr}; +use cosmwasm_std::{to_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult}; use cw2::set_contract_version; use crate::error::ContractError; -use crate::msg::{ExecuteMsg, InstantiateMsg, totalPayersResponse, QueryMsg, self}; -use crate::state::{State, STATE, BALANCES}; - +use crate::msg::{CountResponse, ExecuteMsg, InstantiateMsg, QueryMsg}; +use crate::state::{State, User, STATE, CUSTOMERS}; // version info for migration info const CONTRACT_NAME: &str = "crates.io:counter"; @@ -20,18 +19,16 @@ pub fn instantiate( msg: InstantiateMsg, ) -> Result { let state = State { - storeowner: info.sender.clone(), - bill: msg.bill, - TotalPayers: 0, + count: msg.count, + owner: info.sender.clone(), }; - set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?; STATE.save(deps.storage, &state)?; Ok(Response::new() .add_attribute("method", "instantiate") .add_attribute("owner", info.sender) - ) + .add_attribute("count", msg.count.to_string())) } #[cfg_attr(not(feature = "library"), entry_point)] @@ -42,84 +39,120 @@ pub fn execute( msg: ExecuteMsg, ) -> Result { match msg { - ExecuteMsg::Payup {} => try_payup(deps, info), - ExecuteMsg::Createaccounts {} => create_accounts(deps, info), - + ExecuteMsg::Increment {} => try_increment(deps), + ExecuteMsg::Reset { count } => try_reset(deps, info, count), + //ExecuteMsg::EnterCustomer {} => try_enter_address(deps, info), } - } -pub fn create_accounts(deps: DepsMut, info: MessageInfo) -> Result { - let paid = false; - let address = deps.api.addr_validate(&info.sender.to_string())?; - BALANCES.save(deps.storage, &address, &paid)?; - Ok(Response::new()) +pub fn try_enter_address(deps: DepsMut, info: MessageInfo, entering_address: String) -> Result{ + + let entry_address = deps.api.addr_validate(&entering_address)?; + + //load and save with extra key argument + let empty = CUSTOMERS.may_load(deps.storage, &str.to_string()); + Ok(Response::new()) } -pub fn try_payup(deps: DepsMut, info: MessageInfo) -> Result { - - let all: StdResult> = BALANCES - .range(deps.storage, None, None, Order::Ascending) - .collect(); +pub fn try_increment(deps: DepsMut) -> Result { STATE.update(deps.storage, |mut state| -> Result<_, ContractError> { - state.TotalPayers += all.unwrap_or_default().len() as i32; + state.count += 1; Ok(state) })?; - let config = STATE.load(deps.storage)?; - let deposit_amount: Uint128 = info - .funds - .iter() - .find(|c| c.denom == "uluna") - .map(|c| Uint128::from(c.amount)) - .unwrap_or_else(Uint128::zero); - - if deposit_amount.is_zero() { - return Err(ContractError::ZeroDeposit {}); - } - - let msg = CosmosMsg::Bank(BankMsg::Send { - to_address: config.storeowner.to_string(), - amount: vec![ - Coin { - denom: "uluna".to_string(), - amount: deposit_amount, - }, - ], - }); - - return Ok(Response::new().add_message(msg));; + 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 {}); + } + state.count = count; + Ok(state) + })?; + Ok(Response::new().add_attribute("method", "reset")) } #[cfg_attr(not(feature = "library"), entry_point)] pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult { match msg { - QueryMsg::QueryPayers {} => to_binary(&QueryPayers(deps)?), + QueryMsg::GetCount {} => to_binary(&query_count(deps)?), } } -pub fn QueryPayers(deps: Deps) -> StdResult { +fn query_count(deps: Deps) -> StdResult { let state = STATE.load(deps.storage)?; - Ok(totalPayersResponse { payers: state.TotalPayers}) + Ok(CountResponse { count: state.count }) } -/* +#[cfg(test)] +mod tests { + use super::*; + use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info}; + use cosmwasm_std::{coins, from_binary}; - for index in 0..state.TotalPayers{ - let config = STATE.load(deps.storage)?; - let to_be_paid: (state.bill)*(1/(state.TotalPayers)); + #[test] + fn proper_initialization() { + let mut deps = mock_dependencies(&[]); - let Divided_Amount: Uint = BALANCES[index]; - let deposit_amount: Uint128 = info - .funds - .iter() - .find(|c| c.denom == "uluna") - .map(|c| Uint128::from(c.amount)) - .unwrap_or_else(Uint128::zero); - - if deposit_amount.is_zero() { - return Err(ContractError::ZeroDeposit {}); - } + let msg = InstantiateMsg { count: 17 }; + let info = mock_info("creator", &coins(1000, "earth")); - }*/ + // we can just call .unwrap() to assert this was a success + let res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap(); + assert_eq!(0, res.messages.len()); + + // it worked, let's query the state + let res = query(deps.as_ref(), mock_env(), QueryMsg::GetCount {}).unwrap(); + let value: CountResponse = from_binary(&res).unwrap(); + assert_eq!(17, value.count); + } + + #[test] + fn increment() { + let mut deps = mock_dependencies(&coins(2, "token")); + + let msg = InstantiateMsg { count: 17 }; + let info = mock_info("creator", &coins(2, "token")); + let _res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap(); + + // beneficiary can release it + let info = mock_info("anyone", &coins(2, "token")); + let msg = ExecuteMsg::Increment {}; + let _res = execute(deps.as_mut(), mock_env(), info, msg).unwrap(); + + // should increase counter by 1 + let res = query(deps.as_ref(), mock_env(), QueryMsg::GetCount {}).unwrap(); + let value: CountResponse = from_binary(&res).unwrap(); + assert_eq!(18, value.count); + } + + #[test] + fn reset() { + let mut deps = mock_dependencies(&coins(2, "token")); + + let msg = InstantiateMsg { count: 17 }; + let info = mock_info("creator", &coins(2, "token")); + let _res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap(); + + // beneficiary can release it + let unauth_info = mock_info("anyone", &coins(2, "token")); + let msg = ExecuteMsg::Reset { count: 5 }; + let res = execute(deps.as_mut(), mock_env(), unauth_info, msg); + match res { + Err(ContractError::Unauthorized {}) => {} + _ => panic!("Must return unauthorized error"), + } + + // only the original creator can reset the counter + let auth_info = mock_info("creator", &coins(2, "token")); + let msg = ExecuteMsg::Reset { count: 5 }; + let _res = execute(deps.as_mut(), mock_env(), auth_info, msg).unwrap(); + + // should now be 5 + let res = query(deps.as_ref(), mock_env(), QueryMsg::GetCount {}).unwrap(); + let value: CountResponse = from_binary(&res).unwrap(); + assert_eq!(5, value.count); + } +} diff --git a/contracts/counter/src/error.rs b/contracts/counter/src/error.rs index 4b1dd3e..4a69d8f 100644 --- a/contracts/counter/src/error.rs +++ b/contracts/counter/src/error.rs @@ -8,9 +8,6 @@ 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 da2e1e4..dfedc9d 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; \ No newline at end of file +pub use crate::error::ContractError; diff --git a/contracts/counter/src/msg.rs b/contracts/counter/src/msg.rs index cb76d78..dc8875f 100644 --- a/contracts/counter/src/msg.rs +++ b/contracts/counter/src/msg.rs @@ -3,29 +3,28 @@ use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] pub struct InstantiateMsg { - pub bill: i32, + pub count: i32, //what do we initialize? } #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] #[serde(rename_all = "snake_case")] pub enum ExecuteMsg { - Payup {}, - Createaccounts{} + Increment {}, + Reset { count: i32 }, + EnterCustomer { entering_address: String} + //we will get user address and names. Then divide bill. Then take money from users and put into our own contract. + // } - #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] #[serde(rename_all = "snake_case")] pub enum QueryMsg { // GetCount returns the current count as a json-encoded number - QueryPayers{}, - + GetCount {}, } -//We define a custom struct for each query response +// We define a custom struct for each query response #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] -pub struct totalPayersResponse { - pub payers: i32, +pub struct CountResponse { + pub count: i32, } - -//initiate balances? \ No newline at end of file diff --git a/contracts/counter/src/state.rs b/contracts/counter/src/state.rs index 9a8447f..1c4599f 100644 --- a/contracts/counter/src/state.rs +++ b/contracts/counter/src/state.rs @@ -1,20 +1,23 @@ use schemars::JsonSchema; use serde::{Deserialize, Serialize}; -use cosmwasm_std::{Uint128, Addr}; -use cw_storage_plus::{Item, Map}; +use cosmwasm_std::Addr; +use cw_storage_plus::{Item, Map}; //import map and figure out how to use it #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] pub struct State { - pub storeowner: Addr, - pub bill: i32, - pub TotalPayers: i32, + pub count: i32, + pub owner: Addr, +} + +pub struct User { + pub name: String, + pub address: Addr, } pub const STATE: Item = Item::new("state"); -pub const BALANCES: Map<&Addr, bool> = Map::new("balance"); - +pub const CUSTOMERS: Map<&str, User> = Map::new("customers"); /* add states: @@ -26,4 +29,4 @@ add states: 2 - how to display bill to user Hello -*/ \ No newline at end of file +*/ diff --git a/keys.terrain.js b/keys.terrain.js index a0ac290..b3428ae 100644 --- a/keys.terrain.js +++ b/keys.terrain.js @@ -2,8 +2,11 @@ // to populate secret in CI environment instead of hardcoding module.exports = { - testnet: { + custom_tester_1: { mnemonic: - "cactus wash reveal certain neck guard finger dash tired injury piece remember never load illness off butter lumber velvet off allow control anger return", + "shiver position copy catalog upset verify cheap library enjoy extend second peasant basic kit polar business document shrug pass chuckle lottery blind ecology stand", + }, + custom_tester_2: { + privateKey: "fGl1yNoUnnNUqTUXXhxH9vJU0htlz9lWwBt3fQw+ixw=", }, }; diff --git a/refs.terrain.json b/refs.terrain.json index 9185c8c..9e26dfe 100644 --- a/refs.terrain.json +++ b/refs.terrain.json @@ -1,10 +1 @@ -{ - "localterra": { - "counter": { - "codeId": "13", - "contractAddresses": { - "default": "terra1dazgw2z5sxe7hgt43p0e3xyljnu45tlzwraccz" - } - } - } -} +{} \ No newline at end of file