Skip to content

Commit 6fef990

Browse files
committed
Add new Staking messages and queries under feature flag
1 parent e0c3c90 commit 6fef990

File tree

6 files changed

+109
-4
lines changed

6 files changed

+109
-4
lines changed

.circleci/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ jobs:
7878
working_directory: ~/project/packages/std
7979
command: cargo wasm --locked --features iterator
8080
- run:
81-
name: Run unit tests (with iterator support)
81+
name: Run unit tests (with iterator and staking support)
8282
working_directory: ~/project/packages/std
83-
command: cargo test --locked --features iterator
83+
command: cargo test --locked --features iterator,staking
8484
- run:
8585
name: Build and run schema generator
8686
working_directory: ~/project/packages/std

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,13 @@
9191
- `ExternalStorage.get` now returns an empty vector if a storage entry exists
9292
but has an empty value. Before, this was normalized to `None`.
9393
- Reorganize `CosmosMsg` enum types. They are now split by modules:
94-
`CosmosMsg::Bank(BankMsg)`, `CosmosMsg::Custom(T)`,
95-
`CosmosMsg::Wasm(WasmMsg)`
94+
`CosmosMsg::Bank(BankMsg)`, `CosmosMsg::Custom(T)`, `CosmosMsg::Wasm(WasmMsg)`
9695
- CosmosMsg is now generic over the content of `Custom` variant. This allows
9796
blockchains to support custom native calls in their Cosmos-SDK apps and
9897
developers to make use of them in CosmWasm apps without forking the
9998
`cosmwasm-vm` and `go-cosmwasm` runtime.
99+
- Add `staking` feature flag to expose new `StakingMsg` types under `CosmosMsg`
100+
and new `StakingRequest` types under `QueryRequest`.
100101

101102
**cosmwasm-vm**
102103

packages/std/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ maintenance = { status = "actively-developed" }
1818
# given Ethereum 1.0, 2.0, Substrate, and other major projects use Tries
1919
# we keep this optional, to allow possible future integration (or different Cosmos Backends)
2020
iterator = []
21+
# staking exposes bindings to a required staking moudle in the runtime, via new
22+
# CosmosMsg types, and new QueryRequest types. This should only be enabled on contracts
23+
# that require these types, so other contracts can be used on systems with eg. PoA consensus
24+
staking = []
2125
# backtraces provides much better context at runtime errors (in non-wasm code)
2226
# at the cost of a bit of code size and performance.
2327
backtraces = ["snafu/backtraces"]

packages/std/src/init_handle.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ where
2020
// by default we use RawMsg, but a contract can override that
2121
// to call into more app-specific code (whatever they define)
2222
Custom(T),
23+
#[cfg(feature = "staking")]
24+
Staking(StakingMsg),
2325
Wasm(WasmMsg),
2426
}
2527

@@ -39,6 +41,35 @@ pub enum BankMsg {
3941
/// those contracts that don't explicitly set a custom message.
4042
pub enum NoMsg {}
4143

44+
#[cfg(feature = "staking")]
45+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
46+
#[serde(rename_all = "snake_case")]
47+
pub enum StakingMsg {
48+
Delegate {
49+
// delegator is automatically set to address of the calling contract
50+
validator: HumanAddr,
51+
amount: Coin,
52+
},
53+
Undelegate {
54+
// delegator is automatically set to address of the calling contract
55+
validator: HumanAddr,
56+
amount: Coin,
57+
},
58+
Withdraw {
59+
// delegator is automatically set to address of the calling contract
60+
validator: HumanAddr,
61+
// this is the "withdraw address", the one that should receive the rewards
62+
// if None, then use delegator address
63+
recipient: Option<HumanAddr>,
64+
},
65+
Redelegate {
66+
// delegator is automatically set to address of the calling contract
67+
src_validator: HumanAddr,
68+
dst_validator: HumanAddr,
69+
amount: Coin,
70+
},
71+
}
72+
4273
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
4374
#[serde(rename_all = "snake_case")]
4475
pub enum WasmMsg {

packages/std/src/mock.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ impl Querier for MockQuerier {
166166
let api_res = to_vec(&bank_res).map(Binary).map_err(|e| e.into());
167167
Ok(api_res)
168168
}
169+
#[cfg(feature = "staking")]
170+
QueryRequest::Staking(_) => Err(ApiSystemError::InvalidRequest {
171+
error: "staking not yet implemented".to_string(),
172+
}),
169173
QueryRequest::Wasm(msg) => {
170174
let addr = match msg {
171175
WasmQuery::Smart { contract_addr, .. } => contract_addr,

packages/std/src/query.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ pub type QueryResult = ApiResult<QueryResponse>;
1414
#[serde(rename_all = "snake_case")]
1515
pub enum QueryRequest {
1616
Bank(BankQuery),
17+
#[cfg(feature = "staking")]
18+
Staking(StakingRequest),
1719
Wasm(WasmQuery),
1820
}
1921

@@ -61,3 +63,66 @@ pub struct AllBalanceResponse {
6163
// Returns all non-zero coins held by this account.
6264
pub amount: Vec<Coin>,
6365
}
66+
67+
#[cfg(feature = "staking")]
68+
pub use staking::{Delegation, DelegationsResponse, StakingRequest, Validator, ValidatorsResponse};
69+
70+
#[cfg(feature = "staking")]
71+
mod staking {
72+
use schemars::JsonSchema;
73+
use serde::{Deserialize, Serialize};
74+
75+
use crate::coins::Coin;
76+
use crate::types::HumanAddr;
77+
78+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
79+
#[serde(rename_all = "snake_case")]
80+
pub enum StakingRequest {
81+
Validators {},
82+
// Delegations will return all delegations by the delegator,
83+
// or just those to the given validator (if set)
84+
Delegations {
85+
delegator: HumanAddr,
86+
validator: Option<HumanAddr>,
87+
},
88+
}
89+
90+
#[cfg(feature = "staking")]
91+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
92+
/// ValidatorsResponse is data format returned from StakingRequest::Validators query
93+
pub struct ValidatorsResponse {
94+
pub validators: Vec<Validator>,
95+
}
96+
97+
#[cfg(feature = "staking")]
98+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
99+
pub struct Validator {
100+
pub address: HumanAddr,
101+
// rates are denominated in 10^-6 - 1_000_000 (max) = 100%, 10_000 = 1%
102+
// TODO: capture this in some Dec type?
103+
pub commission: u64,
104+
pub max_commission: u64,
105+
// what units are these (in terms of time)?
106+
pub max_change_rate: u64,
107+
}
108+
109+
#[cfg(feature = "staking")]
110+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
111+
#[serde(rename_all = "snake_case")]
112+
/// DelegationsResponse is data format returned from StakingRequest::Delegations query
113+
pub struct DelegationsResponse {
114+
pub delegations: Vec<Delegation>,
115+
}
116+
117+
#[cfg(feature = "staking")]
118+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
119+
pub struct Delegation {
120+
pub delegator: HumanAddr,
121+
pub validator: HumanAddr,
122+
pub amount: Coin,
123+
pub can_redelegate: bool,
124+
// Review this: this is how much we can withdraw
125+
pub accumulated_rewards: Coin,
126+
// TODO: do we want to expose more info?
127+
}
128+
}

0 commit comments

Comments
 (0)