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

Implement more delegation queries #1788

Merged
merged 20 commits into from
Aug 30, 2023
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
10 changes: 5 additions & 5 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -356,15 +356,15 @@ jobs:
- run:
name: Build library for native target (all features)
working_directory: ~/project/packages/std
command: cargo build --locked --features abort,iterator,staking,stargate,cosmwasm_1_3
command: cargo build --locked --features abort,iterator,staking,stargate,cosmwasm_1_4
- run:
name: Build library for wasm target (all features)
working_directory: ~/project/packages/std
command: cargo wasm --locked --features abort,iterator,staking,stargate,cosmwasm_1_3
command: cargo wasm --locked --features abort,iterator,staking,stargate,cosmwasm_1_4
- run:
name: Run unit tests (all features)
working_directory: ~/project/packages/std
command: cargo test --locked --features abort,iterator,staking,stargate,cosmwasm_1_3
command: cargo test --locked --features abort,iterator,staking,stargate,cosmwasm_1_4
- save_cache:
paths:
- /usr/local/cargo/registry
Expand Down Expand Up @@ -907,7 +907,7 @@ jobs:
- run:
name: Clippy linting on std (all feature flags)
working_directory: ~/project/packages/std
command: cargo clippy --all-targets --features abort,iterator,staking,stargate,cosmwasm_1_3 -- -D warnings
command: cargo clippy --all-targets --features abort,iterator,staking,stargate,cosmwasm_1_4 -- -D warnings
- run:
name: Clippy linting on storage (no feature flags)
working_directory: ~/project/packages/storage
Expand Down Expand Up @@ -984,7 +984,7 @@ jobs:
CRYPTO=" cargo tarpaulin --skip-clean --out Xml --output-dir reports/crypto --packages cosmwasm-crypto"
DERIVE=" cargo tarpaulin --skip-clean --out Xml --output-dir reports/derive --packages cosmwasm-derive"
SCHEMA=" cargo tarpaulin --skip-clean --out Xml --output-dir reports/schema --packages cosmwasm-schema"
STD=" cargo tarpaulin --skip-clean --out Xml --output-dir reports/std --packages cosmwasm-std --features abort,iterator,staking,stargate,cosmwasm_1_3"
STD=" cargo tarpaulin --skip-clean --out Xml --output-dir reports/std --packages cosmwasm-std --features abort,iterator,staking,stargate,cosmwasm_1_4"
STORAGE="cargo tarpaulin --skip-clean --out Xml --output-dir reports/storage --packages cosmwasm-storage"
docker run --security-opt seccomp=unconfined -v "${PWD}:/volume" xd009642/tarpaulin:0.21.0 \
sh -c "$CRYPTO && $DERIVE && $SCHEMA && $STD && $STORAGE"
Expand Down
7 changes: 6 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
{
"rust-analyzer.cargo.features": ["abort", "stargate", "staking", "cosmwasm_1_3"]
"rust-analyzer.cargo.features": [
"abort",
"stargate",
"staking",
"cosmwasm_1_4"
]
}
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ and this project adheres to

## [Unreleased]

### Added

- cosmwasm-std: Add
`DistributionQuery::{DelegationRewards, DelegationTotalRewards, DelegatorValidators}`.
This requires the `cosmwasm_1_4` feature to be enabled. ([#1788])

[#1788]: https://github.com/CosmWasm/cosmwasm/pull/1788

## [1.4.0-beta.1] - 2023-08-29

### Added
Expand Down
4 changes: 4 additions & 0 deletions docs/CAPABILITIES-BUILT-IN.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ might define others.
`BankQuery::DenomMetadata` and `DistributionQuery::DelegatorWithdrawAddress`
queries, as well as `DistributionMsg::FundCommunityPool`. Only chains running
CosmWasm `1.3.0` or higher support this.
- `cosmwasm_1_4` enables the `DistributionQuery::DelegationRewards`,
`DistributionQuery::DelegationTotalRewards` and
`DistributionQuery::DelegatorValidators` queries. Only chains running CosmWasm
`1.4.0` or higher support this.
1 change: 1 addition & 0 deletions docs/USING_COSMWASM_STD.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ The libarary comes with the following features:
| cosmwasm_1_1 | | Features that require CosmWasm 1.1+ on the chain |
| cosmwasm_1_2 | | Features that require CosmWasm 1.2+ on the chain |
| cosmwasm_1_3 | | Features that require CosmWasm 1.3+ on the chain |
| cosmwasm_1_4 | | Features that require CosmWasm 1.4+ on the chain |

## The cosmwasm-std dependency for contract developers

Expand Down
2 changes: 1 addition & 1 deletion packages/check/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use cosmwasm_vm::capabilities_from_csv;
use cosmwasm_vm::internals::{check_wasm, compile, make_compiling_engine};

const DEFAULT_AVAILABLE_CAPABILITIES: &str =
"iterator,staking,stargate,cosmwasm_1_1,cosmwasm_1_2,cosmwasm_1_3";
"iterator,staking,stargate,cosmwasm_1_1,cosmwasm_1_2,cosmwasm_1_3,cosmwasm_1_4";

pub fn main() {
let matches = Command::new("Contract checking")
Expand Down
2 changes: 1 addition & 1 deletion packages/go-gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ publish = false

[dependencies]
schemars = "0.8.3"
cosmwasm-std = { path = "../std", version = "1.4.0-beta.1", features = ["cosmwasm_1_3", "staking", "stargate", "ibc3"] }
cosmwasm-std = { path = "../std", version = "1.4.0-beta.1", features = ["cosmwasm_1_4", "staking", "stargate", "ibc3"] }
cosmwasm-schema = { path = "../schema", version = "1.4.0-beta.1" }
anyhow = "1"
Inflector = "0.11.4"
Expand Down
3 changes: 3 additions & 0 deletions packages/go-gen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,9 @@ mod tests {
// compare_codes!(cosmwasm_std::ValidatorResponse); // does not use "omitempty" for `Validator` field
// distribution
compare_codes!(cosmwasm_std::DelegatorWithdrawAddressResponse);
compare_codes!(cosmwasm_std::DelegationRewardsResponse);
compare_codes!(cosmwasm_std::DelegationTotalRewardsResponse);
compare_codes!(cosmwasm_std::DelegatorValidatorsResponse);
// wasm
compare_codes!(cosmwasm_std::ContractInfoResponse);
// compare_codes!(cosmwasm_std::CodeInfoResponse); // TODO: Checksum type and "omitempty"
Expand Down
1 change: 1 addition & 0 deletions packages/go-gen/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ pub fn custom_type_of(ty: &str) -> Option<&str> {
"HexBinary" => Some("Checksum"),
"Addr" => Some("string"),
"Decimal" => Some("string"),
"Decimal256" => Some("string"),
_ => None,
}
}
Expand Down
10 changes: 10 additions & 0 deletions packages/go-gen/tests/cosmwasm_std__DelegationRewardsResponse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// See <https://github.com/cosmos/cosmos-sdk/blob/c74e2887b0b73e81d48c2f33e6b1020090089ee0/proto/cosmos/distribution/v1beta1/query.proto#L169-L178>
type DelegationRewardsResponse struct {
Rewards []DecCoin `json:"rewards"`
}

// A coin type with decimal amount. Modeled after the Cosmos SDK's [DecCoin](https://github.com/cosmos/cosmos-sdk/blob/c74e2887b0b73e81d48c2f33e6b1020090089ee0/proto/cosmos/base/v1beta1/coin.proto#L32-L41) type
type DecCoin struct {
Amount string `json:"amount"`
Denom string `json:"denom"`
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// See <https://github.com/cosmos/cosmos-sdk/blob/c74e2887b0b73e81d48c2f33e6b1020090089ee0/proto/cosmos/distribution/v1beta1/query.proto#L189-L200>
type DelegationTotalRewardsResponse struct {
Rewards []DelegatorReward `json:"rewards"`
Total []DecCoin `json:"total"`
}

// A coin type with decimal amount. Modeled after the Cosmos SDK's [DecCoin](https://github.com/cosmos/cosmos-sdk/blob/c74e2887b0b73e81d48c2f33e6b1020090089ee0/proto/cosmos/base/v1beta1/coin.proto#L32-L41) type
type DecCoin struct {
Amount string `json:"amount"`
Denom string `json:"denom"`
}

type DelegatorReward struct {
Reward []DecCoin `json:"reward"`
ValidatorAddress string `json:"validator_address"`
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// See <https://github.com/cosmos/cosmos-sdk/blob/b0acf60e6c39f7ab023841841fc0b751a12c13ff/proto/cosmos/distribution/v1beta1/query.proto#L212-L220>
type DelegatorValidatorsResponse struct {
Validators []string `json:"validators"`
}
18 changes: 17 additions & 1 deletion packages/go-gen/tests/cosmwasm_std__DistributionQuery.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@

type DelegatorWithdrawAddressQuery struct {
DelegatorAddress string `json:"delegator_address"`
}
type DelegationRewardsQuery struct {
DelegatorAddress string `json:"delegator_address"`
ValidatorAddress string `json:"validator_address"`
}
type DelegationTotalRewardsQuery struct {
DelegatorAddress string `json:"delegator_address"`
}
type DelegatorValidatorsQuery struct {
DelegatorAddress string `json:"delegator_address"`
}

type DistributionQuery struct {
// See <https://github.com/cosmos/cosmos-sdk/blob/c74e2887b0b73e81d48c2f33e6b1020090089ee0/proto/cosmos/distribution/v1beta1/query.proto#L222-L230>
DelegatorWithdrawAddress *DelegatorWithdrawAddressQuery `json:"delegator_withdraw_address,omitempty"`
// See <https://github.com/cosmos/cosmos-sdk/blob/c74e2887b0b73e81d48c2f33e6b1020090089ee0/proto/cosmos/distribution/v1beta1/query.proto#L157-L167>
DelegationRewards *DelegationRewardsQuery `json:"delegation_rewards,omitempty"`
// See <https://github.com/cosmos/cosmos-sdk/blob/c74e2887b0b73e81d48c2f33e6b1020090089ee0/proto/cosmos/distribution/v1beta1/query.proto#L180-L187>
DelegationTotalRewards *DelegationTotalRewardsQuery `json:"delegation_total_rewards,omitempty"`
// See <https://github.com/cosmos/cosmos-sdk/blob/b0acf60e6c39f7ab023841841fc0b751a12c13ff/proto/cosmos/distribution/v1beta1/query.proto#L202-L210>
DelegatorValidators *DelegatorValidatorsQuery `json:"delegator_validators,omitempty"`
}
2 changes: 2 additions & 0 deletions packages/std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ cosmwasm_1_2 = ["cosmwasm_1_1"]
cosmwasm_1_3 = ["cosmwasm_1_2"]
# Together with the `iterator` feature this enables additional imports for more
# efficient iteration over DB keys or values.
# It also makes `DistributionQuery::{DelegationRewards, DelegationTotalRewards, DelegatorValidators}`
# available for the contract to call.
# It requires the host blockchain to run CosmWasm `1.4.0` or higher.
cosmwasm_1_4 = ["cosmwasm_1_3"]
chipshort marked this conversation as resolved.
Show resolved Hide resolved

Expand Down
4 changes: 4 additions & 0 deletions packages/std/src/exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ extern "C" fn requires_cosmwasm_1_2() -> () {}
#[no_mangle]
extern "C" fn requires_cosmwasm_1_3() -> () {}

#[cfg(feature = "cosmwasm_1_4")]
#[no_mangle]
extern "C" fn requires_cosmwasm_1_4() -> () {}

/// interface_version_* exports mark which Wasm VM interface level this contract is compiled for.
/// They can be checked by cosmwasm_vm.
/// Update this whenever the Wasm VM interface breaks.
Expand Down
9 changes: 5 additions & 4 deletions packages/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@ pub use crate::pagination::PageRequest;
pub use crate::query::{
AllBalanceResponse, AllDelegationsResponse, AllDenomMetadataResponse, AllValidatorsResponse,
BalanceResponse, BankQuery, BondedDenomResponse, ChannelResponse, CodeInfoResponse,
ContractInfoResponse, CustomQuery, Delegation, DelegationResponse,
DelegatorWithdrawAddressResponse, DenomMetadataResponse, DistributionQuery, FullDelegation,
IbcQuery, ListChannelsResponse, PortIdResponse, QueryRequest, StakingQuery, SupplyResponse,
Validator, ValidatorResponse, WasmQuery,
ContractInfoResponse, CustomQuery, DecCoin, Delegation, DelegationResponse,
DelegationRewardsResponse, DelegationTotalRewardsResponse, DelegatorReward,
DelegatorValidatorsResponse, DelegatorWithdrawAddressResponse, DenomMetadataResponse,
DistributionQuery, FullDelegation, IbcQuery, ListChannelsResponse, PortIdResponse,
QueryRequest, StakingQuery, SupplyResponse, Validator, ValidatorResponse, WasmQuery,
};
#[allow(deprecated)]
pub use crate::results::SubMsgExecutionResponse;
Expand Down
86 changes: 83 additions & 3 deletions packages/std/src/query/distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,23 @@ use super::query_response::QueryResponseType;
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum DistributionQuery {
// https://github.com/cosmos/cosmos-sdk/blob/4f6f6c00021f4b5ee486bbb71ae2071a8ceb47c9/x/distribution/types/query.pb.go#L792-L795
/// See <https://github.com/cosmos/cosmos-sdk/blob/c74e2887b0b73e81d48c2f33e6b1020090089ee0/proto/cosmos/distribution/v1beta1/query.proto#L222-L230>
DelegatorWithdrawAddress { delegator_address: String },
/// See <https://github.com/cosmos/cosmos-sdk/blob/c74e2887b0b73e81d48c2f33e6b1020090089ee0/proto/cosmos/distribution/v1beta1/query.proto#L157-L167>
#[cfg(feature = "cosmwasm_1_4")]
DelegationRewards {
delegator_address: String,
validator_address: String,
},
/// See <https://github.com/cosmos/cosmos-sdk/blob/c74e2887b0b73e81d48c2f33e6b1020090089ee0/proto/cosmos/distribution/v1beta1/query.proto#L180-L187>
#[cfg(feature = "cosmwasm_1_4")]
DelegationTotalRewards { delegator_address: String },
/// See <https://github.com/cosmos/cosmos-sdk/blob/b0acf60e6c39f7ab023841841fc0b751a12c13ff/proto/cosmos/distribution/v1beta1/query.proto#L202-L210>
#[cfg(feature = "cosmwasm_1_4")]
DelegatorValidators { delegator_address: String },
}

// https://github.com/cosmos/cosmos-sdk/blob/4f6f6c00021f4b5ee486bbb71ae2071a8ceb47c9/x/distribution/types/query.pb.go#L832-L835
/// See <https://github.com/cosmos/cosmos-sdk/blob/c74e2887b0b73e81d48c2f33e6b1020090089ee0/proto/cosmos/distribution/v1beta1/query.proto#L232-L240>
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
Expand All @@ -22,5 +34,73 @@ pub struct DelegatorWithdrawAddressResponse {
}

impl_response_constructor!(DelegatorWithdrawAddressResponse, withdraw_address: Addr);

impl QueryResponseType for DelegatorWithdrawAddressResponse {}

/// See <https://github.com/cosmos/cosmos-sdk/blob/c74e2887b0b73e81d48c2f33e6b1020090089ee0/proto/cosmos/distribution/v1beta1/query.proto#L169-L178>
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct DelegationRewardsResponse {
pub rewards: Vec<DecCoin>,
}

impl_response_constructor!(DelegationRewardsResponse, rewards: Vec<DecCoin>);
impl QueryResponseType for DelegationRewardsResponse {}

/// A coin type with decimal amount.
/// Modeled after the Cosmos SDK's [DecCoin] type.
/// However, in contrast to the Cosmos SDK the `amount` string MUST always have a dot at JSON level,
/// see <https://github.com/cosmos/cosmos-sdk/issues/10863>.
/// Also if Cosmos SDK choses to migrate away from fixed point decimals
/// (as shown [here](https://github.com/cosmos/cosmos-sdk/blob/v0.47.4/x/group/internal/math/dec.go#L13-L21 and discussed [here](https://github.com/cosmos/cosmos-sdk/issues/11783)),
/// wasmd needs to truncate the decimal places to 18.
///
/// [DecCoin]: (https://github.com/cosmos/cosmos-sdk/blob/v0.47.4/proto/cosmos/base/v1beta1/coin.proto#L28-L38)
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct DecCoin {
pub denom: String,
/// An amount in the base denom of the distributed token.
///
/// Some chains have choosen atto (10^-18) for their token's base denomination. If we used `Decimal` here, we could only store
/// 340282366920938463463.374607431768211455atoken which is 340.28 TOKEN.
pub amount: crate::Decimal256,
}
webmaster128 marked this conversation as resolved.
Show resolved Hide resolved

impl DecCoin {
pub fn new(amount: crate::Decimal256, denom: impl Into<String>) -> Self {
Self {
denom: denom.into(),
amount,
}
}
}

/// See <https://github.com/cosmos/cosmos-sdk/blob/c74e2887b0b73e81d48c2f33e6b1020090089ee0/proto/cosmos/distribution/v1beta1/query.proto#L189-L200>
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[non_exhaustive]
pub struct DelegationTotalRewardsResponse {
pub rewards: Vec<DelegatorReward>,
pub total: Vec<DecCoin>,
}

impl_response_constructor!(
DelegationTotalRewardsResponse,
rewards: Vec<DelegatorReward>,
total: Vec<DecCoin>
);
impl QueryResponseType for DelegationTotalRewardsResponse {}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct DelegatorReward {
pub validator_address: String,
pub reward: Vec<DecCoin>,
}

/// See <https://github.com/cosmos/cosmos-sdk/blob/b0acf60e6c39f7ab023841841fc0b751a12c13ff/proto/cosmos/distribution/v1beta1/query.proto#L212-L220>
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct DelegatorValidatorsResponse {
pub validators: Vec<String>,
}

impl_response_constructor!(DelegatorValidatorsResponse, validators: Vec<String>);
impl QueryResponseType for DelegatorValidatorsResponse {}
Loading
Loading