Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto return stake in tg4 contract #124

Merged
merged 14 commits into from
Sep 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 34 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions contracts/tg4-group/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ backtraces = ["cosmwasm-std/backtraces"]
library = []

[dependencies]
cw0 = { version = "0.8.0" }
cw2 = { version = "0.8.0" }
cw-controllers = { version = "0.8.0" }
cw-storage-plus = { version = "0.8.0" }
cw0 = { version = "0.9.0" }
cw2 = { version = "0.9.0" }
cw-controllers = { version = "0.9.0" }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... this seems like a partial implementation of #142
I guess we merge main into #146 after this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update: I merged this in, worked fine. Now everything is up to v0.9.0

cw-storage-plus = { version = "0.9.0" }
tg4 = { path = "../../packages/tg4", version = "0.3.0" }
tg-controllers = { version = "0.3.0", path = "../../packages/controllers" }
tg-bindings = { version = "0.3.0", path = "../../packages/bindings" }
cosmwasm-std = { version = "0.16.0" }
schemars = "0.8"
serde = { version = "1.0.103", default-features = false, features = ["derive"] }
Expand Down
8 changes: 5 additions & 3 deletions contracts/tg4-group/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
to_binary, Binary, Deps, DepsMut, Env, MessageInfo, Order, Response, StdResult, SubMsg,
};
use cosmwasm_std::{to_binary, Binary, Deps, DepsMut, Env, MessageInfo, Order, StdResult};
use cw0::maybe_addr;
use cw2::set_contract_version;
use cw_storage_plus::{Bound, PrimaryKey, U64Key};
Expand All @@ -14,6 +12,10 @@ use tg4::{
use crate::error::ContractError;
use crate::msg::{ExecuteMsg, InstantiateMsg, PreauthResponse, QueryMsg, SudoMsg};
use crate::state::{members, ADMIN, HOOKS, PREAUTH, TOTAL};
use tg_bindings::TgradeMsg;

pub type Response = cosmwasm_std::Response<TgradeMsg>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to use these types here.
I would use Response unless we make use of Tgrade features.
I think you only add them to tg4-stake.

pub type SubMsg = cosmwasm_std::SubMsg<TgradeMsg>;

// version info for migration info
const CONTRACT_NAME: &str = "crates.io:tg4-group";
Expand Down
11 changes: 6 additions & 5 deletions contracts/tg4-mixer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,21 @@ backtraces = ["cosmwasm-std/backtraces"]
library = []

[dependencies]
cw0 = { version = "0.8.0" }
cw2 = { version = "0.8.0" }
cw20 = { version = "0.8.0" }
cw-storage-plus = { version = "0.8.0" }
cw0 = { version = "0.9.0" }
cw2 = { version = "0.9.0" }
cw20 = { version = "0.9.0" }
cw-storage-plus = { version = "0.9.0" }
tg4 = { path = "../../packages/tg4", version = "0.3.0" }
tg-controllers = { path = "../../packages/controllers", version = "0.3.0" }
tg-bindings = { path = "../../packages/bindings", version = "0.3.0" }
cosmwasm-std = { version = "0.16.0" }
integer-sqrt = { version = "0.1.5" }
schemars = "0.8"
serde = { version = "1.0.103", default-features = false, features = ["derive"] }
thiserror = { version = "1.0.21" }

[dev-dependencies]
cw-multi-test = { version = "0.8.0" }
cw-multi-test = { version = "0.9.0" }
cosmwasm-schema = { version = "0.16.0" }
tg4-group = { path = "../tg4-group", version = "0.3.0", features = ["library"] }
tg4-stake = { path = "../tg4-stake", version = "0.3.0", features = ["library"] }
43 changes: 19 additions & 24 deletions contracts/tg4-mixer/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
to_binary, Binary, Deps, DepsMut, Env, MessageInfo, Order, Response, StdResult, SubMsg,
};
use cosmwasm_std::{to_binary, Binary, Deps, DepsMut, Env, MessageInfo, Order, StdResult};
use cw0::maybe_addr;
use cw2::set_contract_version;
use cw_storage_plus::{Bound, PrimaryKey, U64Key};
use integer_sqrt::IntegerSquareRoot;
use tg_bindings::TgradeMsg;

use tg4::{
HooksResponse, Member, MemberChangedHookMsg, MemberDiff, MemberListResponse, MemberResponse,
Expand All @@ -17,6 +16,9 @@ use crate::error::ContractError;
use crate::msg::{ExecuteMsg, GroupsResponse, InstantiateMsg, PreauthResponse, QueryMsg};
use crate::state::{members, Groups, GROUPS, HOOKS, PREAUTH, TOTAL};

pub type Response = cosmwasm_std::Response<TgradeMsg>;
pub type SubMsg = cosmwasm_std::SubMsg<TgradeMsg>;

// version info for migration info
const CONTRACT_NAME: &str = "crates.io:tg4-mixer";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
Expand Down Expand Up @@ -325,11 +327,11 @@ fn list_members_by_weight(
#[cfg(test)]
mod tests {
use super::*;
use cosmwasm_std::testing::{mock_env, MockApi, MockStorage};
use cosmwasm_std::{coins, Addr, Empty, Uint128};
use cosmwasm_std::{coins, Addr, Uint128};
use cw20::Denom;
use cw_multi_test::{next_block, App, BankKeeper, Contract, ContractWrapper, Executor};
use cw_multi_test::{next_block, App, AppBuilder, Contract, ContractWrapper, Executor};
use tg4_stake::state::Duration;
use tg_bindings::TgradeMsg;

const STAKE_DENOM: &str = "utgd";
const OWNER: &str = "owner";
Expand All @@ -346,7 +348,7 @@ mod tests {
}
}

pub fn contract_mixer() -> Box<dyn Contract<Empty>> {
pub fn contract_mixer() -> Box<dyn Contract<TgradeMsg>> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to change the contracts to make the types match.

There is ContractWrapper::new_with_empty, which takes a contract that returns Response and converts it to Contract for any T.

I'd prefer this over artificially adding the Tgrade Response types to tg4-group and tg4-mixer.

But yeah, in the multitest that uses them all (this file), we need to use adaptors so the App supports TgradeMsg (as I describe on the first line).

let contract = ContractWrapper::new(
crate::contract::execute,
crate::contract::instantiate,
Expand All @@ -355,7 +357,7 @@ mod tests {
Box::new(contract)
}

pub fn contract_group() -> Box<dyn Contract<Empty>> {
pub fn contract_group() -> Box<dyn Contract<TgradeMsg>> {
let contract = ContractWrapper::new(
tg4_group::contract::execute,
tg4_group::contract::instantiate,
Expand All @@ -364,7 +366,7 @@ mod tests {
Box::new(contract)
}

pub fn contract_staking() -> Box<dyn Contract<Empty>> {
pub fn contract_staking() -> Box<dyn Contract<TgradeMsg>> {
let contract = ContractWrapper::new(
tg4_stake::contract::execute,
tg4_stake::contract::instantiate,
Expand All @@ -373,16 +375,8 @@ mod tests {
Box::new(contract)
}

fn mock_app() -> App {
let env = mock_env();
let api = MockApi::default();
let bank = BankKeeper::new();

App::new(api, env.block, bank, MockStorage::new())
}

// uploads code and returns address of group contract
fn instantiate_group(app: &mut App, members: Vec<Member>) -> Addr {
fn instantiate_group(app: &mut App<TgradeMsg>, members: Vec<Member>) -> Addr {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is needed. (and all App changes)

let admin = Some(OWNER.into());
let group_id = app.store_code(contract_group());
let msg = tg4_group::msg::InstantiateMsg {
Expand All @@ -395,7 +389,7 @@ mod tests {
}

// uploads code and returns address of group contract
fn instantiate_staking(app: &mut App, stakers: Vec<Member>) -> Addr {
fn instantiate_staking(app: &mut App<TgradeMsg>, stakers: Vec<Member>) -> Addr {
let admin = Some(OWNER.into());
let group_id = app.store_code(contract_staking());
let msg = tg4_stake::msg::InstantiateMsg {
Expand All @@ -405,6 +399,7 @@ mod tests {
unbonding_period: Duration::new_from_seconds(3600),
admin: admin.clone(),
preauths: Some(1),
auto_return_limit: 0,
};
let contract = app
.instantiate_contract(
Expand Down Expand Up @@ -433,7 +428,7 @@ mod tests {
contract
}

fn instantiate_mixer(app: &mut App, left: &Addr, right: &Addr) -> Addr {
fn instantiate_mixer(app: &mut App<TgradeMsg>, left: &Addr, right: &Addr) -> Addr {
let flex_id = app.store_code(contract_mixer());
let msg = crate::msg::InstantiateMsg {
left_group: left.to_string(),
Expand All @@ -449,7 +444,7 @@ mod tests {
/// and connectioning them all to the mixer.
///
/// Returns (mixer address, group address, staking address).
fn setup_test_case(app: &mut App, stakers: Vec<Member>) -> (Addr, Addr, Addr) {
fn setup_test_case(app: &mut App<TgradeMsg>, stakers: Vec<Member>) -> (Addr, Addr, Addr) {
// 1. Instantiate group contract with members (and OWNER as admin)
let members = vec![
member(OWNER, 0),
Expand All @@ -475,7 +470,7 @@ mod tests {

#[allow(clippy::too_many_arguments)]
fn check_membership(
app: &App,
app: &App<TgradeMsg>,
mixer_addr: &Addr,
owner: Option<u64>,
voter1: Option<u64>,
Expand Down Expand Up @@ -508,7 +503,7 @@ mod tests {

#[test]
fn basic_init() {
let mut app = mock_app();
let mut app = AppBuilder::new().build();
let stakers = vec![
member(OWNER, 88888888888), // 0 weight -> 0 mixed
member(VOTER1, 10000), // 10000 stake, 100 weight -> 1000 mixed
Expand All @@ -532,7 +527,7 @@ mod tests {

#[test]
fn update_with_upstream_change() {
let mut app = mock_app();
let mut app = AppBuilder::new().build();
let stakers = vec![
member(VOTER1, 10000), // 10000 stake, 100 weight -> 1000 mixed
member(VOTER3, 7500), // 7500 stake, 300 weight -> 1500 mixed
Expand Down
12 changes: 7 additions & 5 deletions contracts/tg4-stake/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,19 @@ backtraces = ["cosmwasm-std/backtraces"]
library = []

[dependencies]
cw0 = { version = "0.8.0" }
cw2 = { version = "0.8.0" }
cw20 = { version = "0.8.0" }
cw-controllers = { version = "0.8.0" }
cw-storage-plus = { version = "0.8.0" }
cw0 = { version = "0.9.0" }
cw2 = { version = "0.9.0" }
cw20 = { version = "0.9.0" }
cw-controllers = { version = "0.9.0" }
cw-storage-plus = { version = "0.9.0" }
tg4 = { path = "../../packages/tg4", version = "0.3.0" }
tg-controllers = { path = "../../packages/controllers", version = "0.3.0" }
tg-bindings = { path = "../../packages/bindings", version = "0.3.0" }
cosmwasm-std = { version = "0.16.0" }
schemars = "0.8.1"
serde = { version = "1.0.103", default-features = false, features = ["derive"] }
thiserror = { version = "1.0.21" }
itertools = "0.10"

[dev-dependencies]
cosmwasm-schema = { version = "0.16.0" }
Loading