mirror of
https://github.com/BiPhan4/DefiHackathon-2022.git
synced 2025-04-02 10:41:42 -04:00
pay_up works
This commit is contained in:
parent
c4e8bd2dae
commit
c061918f78
9 changed files with 49 additions and 108 deletions
|
@ -17,7 +17,7 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"instantiateMsg": {
|
"instantiateMsg": {
|
||||||
"count": 0
|
"bill": 20000000
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
1
contracts/counter/artifacts/checksums.txt
Normal file
1
contracts/counter/artifacts/checksums.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
9c11206ae9184a6c56fea84d97c362971ebfce471c080a45b435a002146e18e1 counter.wasm
|
1
contracts/counter/artifacts/checksums_intermediate.txt
Normal file
1
contracts/counter/artifacts/checksums_intermediate.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
08e18ced1337e3a64711dc4ecbeecb8abb5fc677ba99167697248450bff6d0f5 ./target/wasm32-unknown-unknown/release/counter.wasm
|
BIN
contracts/counter/artifacts/counter.wasm
Normal file
BIN
contracts/counter/artifacts/counter.wasm
Normal file
Binary file not shown.
|
@ -1,6 +1,6 @@
|
||||||
#[cfg(not(feature = "library"))]
|
#[cfg(not(feature = "library"))]
|
||||||
use cosmwasm_std::entry_point;
|
use cosmwasm_std::entry_point;
|
||||||
use cosmwasm_std::{to_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult};
|
use cosmwasm_std::{Uint128, Coin, BankMsg, CosmosMsg, Uint256, 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;
|
||||||
|
@ -21,6 +21,16 @@ pub fn instantiate(
|
||||||
let state = State {
|
let state = State {
|
||||||
storeowner: info.sender.clone(),
|
storeowner: info.sender.clone(),
|
||||||
bill: msg.bill
|
bill: msg.bill
|
||||||
|
/*logic to split the bill
|
||||||
|
put contract callers into
|
||||||
|
store owner specifies bill amount
|
||||||
|
and users call join.
|
||||||
|
Need join function: join function adds their addresses to map
|
||||||
|
then once everyone joins have the pay up method split bill
|
||||||
|
based on map length
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
};
|
};
|
||||||
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)?;
|
||||||
|
@ -28,7 +38,7 @@ pub fn instantiate(
|
||||||
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)]
|
||||||
|
@ -44,122 +54,35 @@ pub fn execute(
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn try_increment(deps: DepsMut) -> Result<Response, ContractError> {
|
pub fn try_payup(deps: DepsMut, info: MessageInfo) -> Result<Response, ContractError> {
|
||||||
STATE.update(deps.storage, |mut state| -> Result<_, ContractError> {
|
|
||||||
state.count += 1;
|
|
||||||
Ok(state)
|
|
||||||
})?;
|
|
||||||
|
|
||||||
Ok(Response::new().add_attribute("method", "try_increment"))
|
|
||||||
}
|
|
||||||
pub fn try_payup(deps: DepsMut, info: MessageInfo: i32) -> Result<Response, ContractError> {
|
|
||||||
let config = STATE.load(deps.storage)?;
|
let config = STATE.load(deps.storage)?;
|
||||||
let deposit_amount: Uint256 = info
|
let deposit_amount: Uint128 = info
|
||||||
.funds
|
.funds
|
||||||
.iter()
|
.iter()
|
||||||
.find(|c| c.denom == "uluna")
|
.find(|c| c.denom == "uluna")
|
||||||
.map(|c| Uint256::from(c.amount))
|
.map(|c| Uint128::from(c.amount))
|
||||||
.unwrap_or_else(Uint256::zero);
|
.unwrap_or_else(Uint128::zero);
|
||||||
if deposit_amount.is_zero() {
|
if deposit_amount.is_zero() {
|
||||||
return Err(ContractError::ZeroDeposit());
|
return Err(ContractError::ZeroDeposit {});
|
||||||
}
|
}
|
||||||
|
/*is depost amount 50% of config.bill
|
||||||
|
*/
|
||||||
|
|
||||||
let msg = CosmosMsg::Bank(BankMsg::Send {
|
let msg = CosmosMsg::Bank(BankMsg::Send {
|
||||||
to_address: config.storeowner,
|
to_address: config.storeowner.to_string(),
|
||||||
amount: vec![deduct_tax(
|
amount: vec![
|
||||||
deps.as_ref(),
|
|
||||||
Coin {
|
Coin {
|
||||||
denom: "uusd".to_string(),
|
denom: "uluna".to_string(),
|
||||||
amount: balance,
|
amount: deposit_amount,
|
||||||
},
|
},
|
||||||
)?],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
Ok(Response::new().add_message(msg))
|
Ok(Response::new().add_message(msg))
|
||||||
}
|
}
|
||||||
pub createorder(deps: DepsMut, info: MessageInfo: i32) -> Result<Response, ContractError>){
|
|
||||||
|
|
||||||
}
|
//pub createorder(deps: DepsMut, info: MessageInfo: i32) -> Result<Response, ContractError>{
|
||||||
|
|
||||||
#[cfg_attr(not(feature = "library"), entry_point)]
|
|
||||||
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
|
|
||||||
match msg {
|
|
||||||
QueryMsg::GetCount {} => to_binary(&query_count(deps)?),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn query_count(deps: Deps) -> StdResult<CountResponse> {
|
//}
|
||||||
let state = STATE.load(deps.storage)?;
|
|
||||||
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};
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn proper_initialization() {
|
|
||||||
let mut deps = mock_dependencies(&[]);
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ 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 count: i32,
|
pub bill: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
|
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
|
||||||
|
@ -25,3 +25,5 @@ pub enum QueryMsg {
|
||||||
pub struct CountResponse {
|
pub struct CountResponse {
|
||||||
pub count: i32,
|
pub count: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//initiate balances?
|
|
@ -2,11 +2,16 @@ use schemars::JsonSchema;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use cosmwasm_std::Addr;
|
use cosmwasm_std::Addr;
|
||||||
use cw_storage_plus::Item;
|
use cw_storage_plus::{Item, Map};
|
||||||
|
|
||||||
|
use crate::msg;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
|
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
|
||||||
pub struct State {
|
pub struct State {
|
||||||
pub storeowner: Addr,
|
pub storeowner: Addr,
|
||||||
|
pub bill: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
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");
|
|
@ -4,6 +4,6 @@
|
||||||
module.exports = {
|
module.exports = {
|
||||||
testnet: {
|
testnet: {
|
||||||
mnemonic:
|
mnemonic:
|
||||||
"rose affair garlic decorate awesome round upgrade distance novel strong this congress jelly title also trigger proof stand between humble primary short area people",
|
"cactus wash reveal certain neck guard finger dash tired injury piece remember never load illness off butter lumber velvet off allow control anger return",
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -1 +1,10 @@
|
||||||
{}
|
{
|
||||||
|
"localterra": {
|
||||||
|
"counter": {
|
||||||
|
"codeId": "6",
|
||||||
|
"contractAddresses": {
|
||||||
|
"default": "terra1xzlgeyuuyqje79ma6vllregprkmgwgavjx2h6m"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue