mirror of
https://github.com/BiPhan4/DefiHackathon-2022.git
synced 2025-04-02 10:41:42 -04:00
parent
c2e75206df
commit
253a8939aa
15 changed files with 132 additions and 135 deletions
|
@ -1,2 +1,3 @@
|
||||||
# DefiHackathon-2022
|
# DefiHackathon-2022
|
||||||
Demo video: https://youtu.be/kMNxAIWr4T8
|
|
||||||
|
Hello
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"instantiateMsg": {
|
"instantiateMsg": {
|
||||||
"bill": 20000000
|
"count": 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
27
contracts/counter/Cargo.lock
generated
27
contracts/counter/Cargo.lock
generated
|
@ -103,7 +103,6 @@ dependencies = [
|
||||||
"cosmwasm-storage",
|
"cosmwasm-storage",
|
||||||
"cw-storage-plus",
|
"cw-storage-plus",
|
||||||
"cw2",
|
"cw2",
|
||||||
"cw20",
|
|
||||||
"schemars",
|
"schemars",
|
||||||
"serde",
|
"serde",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
|
@ -164,24 +163,10 @@ name = "cw-storage-plus"
|
||||||
version = "0.8.1"
|
version = "0.8.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c1e867b9972b83b32e00e878dfbff48299ba26618dabeb19b9c56fae176dc225"
|
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 = [
|
dependencies = [
|
||||||
"cosmwasm-std",
|
"cosmwasm-std",
|
||||||
"schemars",
|
"schemars",
|
||||||
"serde",
|
"serde",
|
||||||
"thiserror",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -196,18 +181,6 @@ dependencies = [
|
||||||
"serde",
|
"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]]
|
[[package]]
|
||||||
name = "der"
|
name = "der"
|
||||||
version = "0.4.1"
|
version = "0.4.1"
|
||||||
|
|
|
@ -44,7 +44,6 @@ cosmwasm-std = { version = "0.16.2" }
|
||||||
cosmwasm-storage = { version = "0.16.0" }
|
cosmwasm-storage = { version = "0.16.0" }
|
||||||
cw-storage-plus = "0.8.0"
|
cw-storage-plus = "0.8.0"
|
||||||
cw2 = "0.8.1"
|
cw2 = "0.8.1"
|
||||||
cw20 = "0.9.0"
|
|
||||||
schemars = "0.8.3"
|
schemars = "0.8.3"
|
||||||
serde = { version = "1.0.127", default-features = false, features = ["derive"] }
|
serde = { version = "1.0.127", default-features = false, features = ["derive"] }
|
||||||
thiserror = { version = "1.0.26" }
|
thiserror = { version = "1.0.26" }
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
20828f78f4b256f0563c2fbb65b5361504f948dd4910dfe54e981aa5670f73a8 counter.wasm
|
|
|
@ -1 +0,0 @@
|
||||||
07b18dc271100d7c375e9ff1de8a767899ffa745dd52dbaa0ce9ab587346d78e ./target/wasm32-unknown-unknown/release/counter.wasm
|
|
Binary file not shown.
|
@ -3,7 +3,7 @@ use std::fs::create_dir_all;
|
||||||
|
|
||||||
use cosmwasm_schema::{export_schema, remove_schemas, schema_for};
|
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;
|
use counter::state::State;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
@ -16,5 +16,5 @@ fn main() {
|
||||||
export_schema(&schema_for!(ExecuteMsg), &out_dir);
|
export_schema(&schema_for!(ExecuteMsg), &out_dir);
|
||||||
export_schema(&schema_for!(QueryMsg), &out_dir);
|
export_schema(&schema_for!(QueryMsg), &out_dir);
|
||||||
export_schema(&schema_for!(State), &out_dir);
|
export_schema(&schema_for!(State), &out_dir);
|
||||||
export_schema(&schema_for!(totalPayersResponse), &out_dir);
|
export_schema(&schema_for!(CountResponse), &out_dir);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
#[cfg(not(feature = "library"))]
|
#[cfg(not(feature = "library"))]
|
||||||
use cosmwasm_std::entry_point;
|
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 cw2::set_contract_version;
|
||||||
|
|
||||||
use crate::error::ContractError;
|
use crate::error::ContractError;
|
||||||
use crate::msg::{ExecuteMsg, InstantiateMsg, totalPayersResponse, QueryMsg, self};
|
use crate::msg::{CountResponse, ExecuteMsg, InstantiateMsg, QueryMsg};
|
||||||
use crate::state::{State, STATE, BALANCES};
|
use crate::state::{State, User, STATE, CUSTOMERS};
|
||||||
|
|
||||||
|
|
||||||
// version info for migration info
|
// version info for migration info
|
||||||
const CONTRACT_NAME: &str = "crates.io:counter";
|
const CONTRACT_NAME: &str = "crates.io:counter";
|
||||||
|
@ -20,18 +19,16 @@ pub fn instantiate(
|
||||||
msg: InstantiateMsg,
|
msg: InstantiateMsg,
|
||||||
) -> Result<Response, ContractError> {
|
) -> Result<Response, ContractError> {
|
||||||
let state = State {
|
let state = State {
|
||||||
storeowner: info.sender.clone(),
|
count: msg.count,
|
||||||
bill: msg.bill,
|
owner: info.sender.clone(),
|
||||||
TotalPayers: 0,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
|
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
|
||||||
STATE.save(deps.storage, &state)?;
|
STATE.save(deps.storage, &state)?;
|
||||||
|
|
||||||
Ok(Response::new()
|
Ok(Response::new()
|
||||||
.add_attribute("method", "instantiate")
|
.add_attribute("method", "instantiate")
|
||||||
.add_attribute("owner", info.sender)
|
.add_attribute("owner", info.sender)
|
||||||
)
|
.add_attribute("count", msg.count.to_string()))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg_attr(not(feature = "library"), entry_point)]
|
#[cfg_attr(not(feature = "library"), entry_point)]
|
||||||
|
@ -42,84 +39,120 @@ pub fn execute(
|
||||||
msg: ExecuteMsg,
|
msg: ExecuteMsg,
|
||||||
) -> Result<Response, ContractError> {
|
) -> Result<Response, ContractError> {
|
||||||
match msg {
|
match msg {
|
||||||
ExecuteMsg::Payup {} => try_payup(deps, info),
|
ExecuteMsg::Increment {} => try_increment(deps),
|
||||||
ExecuteMsg::Createaccounts {} => create_accounts(deps, info),
|
ExecuteMsg::Reset { count } => try_reset(deps, info, count),
|
||||||
|
//ExecuteMsg::EnterCustomer {} => try_enter_address(deps, info),
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_accounts(deps: DepsMut, info: MessageInfo) -> Result<Response, ContractError> {
|
pub fn try_enter_address(deps: DepsMut, info: MessageInfo, entering_address: String) -> Result<Response, ContractError>{
|
||||||
let paid = false;
|
|
||||||
let address = deps.api.addr_validate(&info.sender.to_string())?;
|
let entry_address = deps.api.addr_validate(&entering_address)?;
|
||||||
BALANCES.save(deps.storage, &address, &paid)?;
|
|
||||||
|
//load and save with extra key argument
|
||||||
|
let empty = CUSTOMERS.may_load(deps.storage, &str.to_string());
|
||||||
Ok(Response::new())
|
Ok(Response::new())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn try_payup(deps: DepsMut, info: MessageInfo) -> Result<Response, ContractError> {
|
|
||||||
|
|
||||||
let all: StdResult<Vec<_>> = BALANCES
|
|
||||||
.range(deps.storage, None, None, Order::Ascending)
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
|
pub fn try_increment(deps: DepsMut) -> Result<Response, ContractError> {
|
||||||
STATE.update(deps.storage, |mut state| -> Result<_, ContractError> {
|
STATE.update(deps.storage, |mut state| -> Result<_, ContractError> {
|
||||||
state.TotalPayers += all.unwrap_or_default().len() as i32;
|
state.count += 1;
|
||||||
Ok(state)
|
Ok(state)
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let config = STATE.load(deps.storage)?;
|
Ok(Response::new().add_attribute("method", "try_increment"))
|
||||||
let deposit_amount: Uint128 = info
|
}
|
||||||
.funds
|
pub fn try_reset(deps: DepsMut, info: MessageInfo, count: i32) -> Result<Response, ContractError> {
|
||||||
.iter()
|
STATE.update(deps.storage, |mut state| -> Result<_, ContractError> {
|
||||||
.find(|c| c.denom == "uluna")
|
if info.sender != state.owner {
|
||||||
.map(|c| Uint128::from(c.amount))
|
return Err(ContractError::Unauthorized {});
|
||||||
.unwrap_or_else(Uint128::zero);
|
|
||||||
|
|
||||||
if deposit_amount.is_zero() {
|
|
||||||
return Err(ContractError::ZeroDeposit {});
|
|
||||||
}
|
}
|
||||||
|
state.count = count;
|
||||||
let msg = CosmosMsg::Bank(BankMsg::Send {
|
Ok(state)
|
||||||
to_address: config.storeowner.to_string(),
|
})?;
|
||||||
amount: vec![
|
Ok(Response::new().add_attribute("method", "reset"))
|
||||||
Coin {
|
|
||||||
denom: "uluna".to_string(),
|
|
||||||
amount: deposit_amount,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
|
|
||||||
return Ok(Response::new().add_message(msg));;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg_attr(not(feature = "library"), entry_point)]
|
#[cfg_attr(not(feature = "library"), entry_point)]
|
||||||
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
|
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
|
||||||
match msg {
|
match msg {
|
||||||
QueryMsg::QueryPayers {} => to_binary(&QueryPayers(deps)?),
|
QueryMsg::GetCount {} => to_binary(&query_count(deps)?),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn QueryPayers(deps: Deps) -> StdResult<totalPayersResponse> {
|
fn query_count(deps: Deps) -> StdResult<CountResponse> {
|
||||||
let state = STATE.load(deps.storage)?;
|
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{
|
#[test]
|
||||||
let config = STATE.load(deps.storage)?;
|
fn proper_initialization() {
|
||||||
let to_be_paid: (state.bill)*(1/(state.TotalPayers));
|
let mut deps = mock_dependencies(&[]);
|
||||||
|
|
||||||
let Divided_Amount: Uint = BALANCES[index];
|
let msg = InstantiateMsg { count: 17 };
|
||||||
let deposit_amount: Uint128 = info
|
let info = mock_info("creator", &coins(1000, "earth"));
|
||||||
.funds
|
|
||||||
.iter()
|
|
||||||
.find(|c| c.denom == "uluna")
|
|
||||||
.map(|c| Uint128::from(c.amount))
|
|
||||||
.unwrap_or_else(Uint128::zero);
|
|
||||||
|
|
||||||
if deposit_amount.is_zero() {
|
// we can just call .unwrap() to assert this was a success
|
||||||
return Err(ContractError::ZeroDeposit {});
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -8,9 +8,6 @@ pub enum ContractError {
|
||||||
|
|
||||||
#[error("Unauthorized")]
|
#[error("Unauthorized")]
|
||||||
Unauthorized {},
|
Unauthorized {},
|
||||||
|
|
||||||
#[error("you cant deposit $0")]
|
|
||||||
ZeroDeposit {},
|
|
||||||
// Add any other custom errors you like here.
|
// Add any other custom errors you like here.
|
||||||
// Look at https://docs.rs/thiserror/1.0.21/thiserror/ for details.
|
// Look at https://docs.rs/thiserror/1.0.21/thiserror/ for details.
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,29 +3,28 @@ use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
|
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
|
||||||
pub struct InstantiateMsg {
|
pub struct InstantiateMsg {
|
||||||
pub bill: i32,
|
pub count: i32, //what do we initialize?
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
|
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
|
||||||
#[serde(rename_all = "snake_case")]
|
#[serde(rename_all = "snake_case")]
|
||||||
pub enum ExecuteMsg {
|
pub enum ExecuteMsg {
|
||||||
Payup {},
|
Increment {},
|
||||||
Createaccounts{}
|
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)]
|
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
|
||||||
#[serde(rename_all = "snake_case")]
|
#[serde(rename_all = "snake_case")]
|
||||||
pub enum QueryMsg {
|
pub enum QueryMsg {
|
||||||
// GetCount returns the current count as a json-encoded number
|
// 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)]
|
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
|
||||||
pub struct totalPayersResponse {
|
pub struct CountResponse {
|
||||||
pub payers: i32,
|
pub count: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
//initiate balances?
|
|
|
@ -1,20 +1,23 @@
|
||||||
use schemars::JsonSchema;
|
use schemars::JsonSchema;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use cosmwasm_std::{Uint128, Addr};
|
use cosmwasm_std::Addr;
|
||||||
use cw_storage_plus::{Item, Map};
|
use cw_storage_plus::{Item, Map}; //import map and figure out how to use it
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
|
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
|
||||||
pub struct State {
|
pub struct State {
|
||||||
pub storeowner: Addr,
|
pub count: i32,
|
||||||
pub bill: i32,
|
pub owner: Addr,
|
||||||
pub TotalPayers: i32,
|
}
|
||||||
|
|
||||||
|
pub struct User {
|
||||||
|
pub name: String,
|
||||||
|
pub address: Addr,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const STATE: Item<State> = Item::new("state");
|
pub const STATE: Item<State> = Item::new("state");
|
||||||
|
|
||||||
pub const BALANCES: Map<&Addr, bool> = Map::new("balance");
|
pub const CUSTOMERS: Map<&str, User> = Map::new("customers");
|
||||||
|
|
||||||
/*
|
/*
|
||||||
add states:
|
add states:
|
||||||
|
|
||||||
|
|
|
@ -2,8 +2,11 @@
|
||||||
// to populate secret in CI environment instead of hardcoding
|
// to populate secret in CI environment instead of hardcoding
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
testnet: {
|
custom_tester_1: {
|
||||||
mnemonic:
|
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=",
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,10 +1 @@
|
||||||
{
|
{}
|
||||||
"localterra": {
|
|
||||||
"counter": {
|
|
||||||
"codeId": "13",
|
|
||||||
"contractAddresses": {
|
|
||||||
"default": "terra1dazgw2z5sxe7hgt43p0e3xyljnu45tlzwraccz"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Add table
Reference in a new issue