diff --git a/README.md b/README.md index a7fcddb..f8d7cf8 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,2 @@ # DefiHackathon-2022 - -Hello +Demo video: https://youtu.be/kMNxAIWr4T8 \ No newline at end of file diff --git a/config.terrain.json b/config.terrain.json index bbc25ef..c49c951 100644 --- a/config.terrain.json +++ b/config.terrain.json @@ -17,7 +17,7 @@ } }, "instantiateMsg": { - "count": 0 + "bill": 20000000 } } } diff --git a/contracts/counter/Cargo.lock b/contracts/counter/Cargo.lock index 8b9dd73..6294467 100644 --- a/contracts/counter/Cargo.lock +++ b/contracts/counter/Cargo.lock @@ -103,6 +103,7 @@ dependencies = [ "cosmwasm-storage", "cw-storage-plus", "cw2", + "cw20", "schemars", "serde", "thiserror", @@ -163,10 +164,24 @@ 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]] @@ -181,6 +196,18 @@ 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 7f29341..9a0dd8b 100644 --- a/contracts/counter/Cargo.toml +++ b/contracts/counter/Cargo.toml @@ -44,6 +44,7 @@ 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 new file mode 100644 index 0000000..884699e --- /dev/null +++ b/contracts/counter/artifacts/checksums.txt @@ -0,0 +1 @@ +20828f78f4b256f0563c2fbb65b5361504f948dd4910dfe54e981aa5670f73a8 counter.wasm diff --git a/contracts/counter/artifacts/checksums_intermediate.txt b/contracts/counter/artifacts/checksums_intermediate.txt new file mode 100644 index 0000000..ff9a52d --- /dev/null +++ b/contracts/counter/artifacts/checksums_intermediate.txt @@ -0,0 +1 @@ +07b18dc271100d7c375e9ff1de8a767899ffa745dd52dbaa0ce9ab587346d78e ./target/wasm32-unknown-unknown/release/counter.wasm diff --git a/contracts/counter/artifacts/counter.wasm b/contracts/counter/artifacts/counter.wasm new file mode 100644 index 0000000..d1b4512 Binary files /dev/null and b/contracts/counter/artifacts/counter.wasm differ diff --git a/contracts/counter/examples/schema.rs b/contracts/counter/examples/schema.rs index fdf606f..0bcc70f 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::{CountResponse, ExecuteMsg, InstantiateMsg, QueryMsg}; +use counter::msg::{totalPayersResponse, 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!(CountResponse), &out_dir); + export_schema(&schema_for!(totalPayersResponse), &out_dir); } diff --git a/contracts/counter/src/contract.rs b/contracts/counter/src/contract.rs index 4b9dd18..53d26c0 100644 --- a/contracts/counter/src/contract.rs +++ b/contracts/counter/src/contract.rs @@ -1,11 +1,12 @@ #[cfg(not(feature = "library"))] use cosmwasm_std::entry_point; -use cosmwasm_std::{to_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult}; +use cosmwasm_std::{Order, Uint128, Coin, BankMsg, CosmosMsg, Uint256, to_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult, Api, Addr}; use cw2::set_contract_version; use crate::error::ContractError; -use crate::msg::{CountResponse, ExecuteMsg, InstantiateMsg, QueryMsg}; -use crate::state::{State, User, STATE, CUSTOMERS}; +use crate::msg::{ExecuteMsg, InstantiateMsg, totalPayersResponse, QueryMsg, self}; +use crate::state::{State, STATE, BALANCES}; + // version info for migration info const CONTRACT_NAME: &str = "crates.io:counter"; @@ -19,16 +20,18 @@ pub fn instantiate( msg: InstantiateMsg, ) -> Result { let state = State { - count: msg.count, - owner: info.sender.clone(), + storeowner: info.sender.clone(), + bill: msg.bill, + TotalPayers: 0, }; + 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)] @@ -39,120 +42,84 @@ pub fn execute( msg: ExecuteMsg, ) -> Result { match msg { - ExecuteMsg::Increment {} => try_increment(deps), - ExecuteMsg::Reset { count } => try_reset(deps, info, count), - //ExecuteMsg::EnterCustomer {} => try_enter_address(deps, info), + ExecuteMsg::Payup {} => try_payup(deps, info), + ExecuteMsg::Createaccounts {} => create_accounts(deps, info), + } + } -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 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_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.count += 1; + state.TotalPayers += all.unwrap_or_default().len() as i32; Ok(state) })?; - 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")) + 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));; } #[cfg_attr(not(feature = "library"), entry_point)] pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult { match msg { - QueryMsg::GetCount {} => to_binary(&query_count(deps)?), + QueryMsg::QueryPayers {} => to_binary(&QueryPayers(deps)?), } } -fn query_count(deps: Deps) -> StdResult { +pub fn QueryPayers(deps: Deps) -> StdResult { let state = STATE.load(deps.storage)?; - Ok(CountResponse { count: state.count }) + Ok(totalPayersResponse { payers: state.TotalPayers}) } -#[cfg(test)] -mod tests { - use super::*; - use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info}; - use cosmwasm_std::{coins, from_binary}; +/* - #[test] - fn proper_initialization() { - let mut deps = mock_dependencies(&[]); + for index in 0..state.TotalPayers{ + let config = STATE.load(deps.storage)?; + let to_be_paid: (state.bill)*(1/(state.TotalPayers)); - let msg = InstantiateMsg { count: 17 }; - let info = mock_info("creator", &coins(1000, "earth")); + 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 {}); + } - // 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 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 dc8875f..cb76d78 100644 --- a/contracts/counter/src/msg.rs +++ b/contracts/counter/src/msg.rs @@ -3,28 +3,29 @@ use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] pub struct InstantiateMsg { - pub count: i32, //what do we initialize? + pub bill: i32, } #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] #[serde(rename_all = "snake_case")] pub enum ExecuteMsg { - 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. - // + Payup {}, + Createaccounts{} } + #[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 - GetCount {}, + QueryPayers{}, + } -// 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 CountResponse { - pub count: i32, +pub struct totalPayersResponse { + pub payers: i32, } + +//initiate balances? \ No newline at end of file diff --git a/contracts/counter/src/state.rs b/contracts/counter/src/state.rs index 1c4599f..9a8447f 100644 --- a/contracts/counter/src/state.rs +++ b/contracts/counter/src/state.rs @@ -1,23 +1,20 @@ use schemars::JsonSchema; use serde::{Deserialize, Serialize}; -use cosmwasm_std::Addr; -use cw_storage_plus::{Item, Map}; //import map and figure out how to use it +use cosmwasm_std::{Uint128, Addr}; +use cw_storage_plus::{Item, Map}; #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] pub struct State { - pub count: i32, - pub owner: Addr, -} - -pub struct User { - pub name: String, - pub address: Addr, + pub storeowner: Addr, + pub bill: i32, + pub TotalPayers: i32, } pub const STATE: Item = Item::new("state"); -pub const CUSTOMERS: Map<&str, User> = Map::new("customers"); +pub const BALANCES: Map<&Addr, bool> = Map::new("balance"); + /* add states: @@ -29,4 +26,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 b3428ae..a0ac290 100644 --- a/keys.terrain.js +++ b/keys.terrain.js @@ -2,11 +2,8 @@ // to populate secret in CI environment instead of hardcoding module.exports = { - custom_tester_1: { + testnet: { mnemonic: - "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=", + "cactus wash reveal certain neck guard finger dash tired injury piece remember never load illness off butter lumber velvet off allow control anger return", }, }; diff --git a/refs.terrain.json b/refs.terrain.json index 9e26dfe..9185c8c 100644 --- a/refs.terrain.json +++ b/refs.terrain.json @@ -1 +1,10 @@ -{} \ No newline at end of file +{ + "localterra": { + "counter": { + "codeId": "13", + "contractAddresses": { + "default": "terra1dazgw2z5sxe7hgt43p0e3xyljnu45tlzwraccz" + } + } + } +}