-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add distribution module * Create distribution rest client * Add tests for distribution module * Remove unused import * run black and isort Signed-off-by: David Pierret <[email protected]> * Update regexp to accept external modifications Signed-off-by: David Pierret <[email protected]> * remove not needed parameter Signed-off-by: David Pierret <[email protected]> * add slashes content in tests Signed-off-by: David Pierret <[email protected]> * Add a test on parameter values Check the values are correctly encoded/decoded by the protobuf code. Signed-off-by: David Pierret <[email protected]> * chores: fix docstrings Co-authored-by: David Pierret <[email protected]> Co-authored-by: Flavien Binet <[email protected]>
- Loading branch information
1 parent
822f288
commit b43c96b
Showing
6 changed files
with
600 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# -*- coding: utf-8 -*- | ||
# ------------------------------------------------------------------------------ | ||
# | ||
# Copyright 2018-2021 Fetch.AI Limited | ||
# Modifications copyright (C) 2022 Cros-Nest | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
# ------------------------------------------------------------------------------ | ||
|
||
"""This package contains the Distribution modules.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
# -*- coding: utf-8 -*- | ||
# ------------------------------------------------------------------------------ | ||
# | ||
# Copyright 2018-2021 Fetch.AI Limited | ||
# Modifications copyright (C) 2022 Cros-Nest | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
# ------------------------------------------------------------------------------ | ||
|
||
"""Interface for the Distribution functionality of CosmosSDK.""" | ||
|
||
from abc import ABC, abstractmethod | ||
|
||
from cosmpy.protos.cosmos.distribution.v1beta1.query_pb2 import ( | ||
QueryCommunityPoolResponse, | ||
QueryDelegationRewardsRequest, | ||
QueryDelegationRewardsResponse, | ||
QueryDelegationTotalRewardsRequest, | ||
QueryDelegationTotalRewardsResponse, | ||
QueryDelegatorValidatorsRequest, | ||
QueryDelegatorValidatorsResponse, | ||
QueryDelegatorWithdrawAddressRequest, | ||
QueryDelegatorWithdrawAddressResponse, | ||
QueryParamsResponse, | ||
QueryValidatorCommissionRequest, | ||
QueryValidatorCommissionResponse, | ||
QueryValidatorOutstandingRewardsRequest, | ||
QueryValidatorOutstandingRewardsResponse, | ||
QueryValidatorSlashesRequest, | ||
QueryValidatorSlashesResponse, | ||
) | ||
|
||
|
||
class Distribution(ABC): | ||
"""Distribution abstract class.""" | ||
|
||
@abstractmethod | ||
def CommunityPool(self) -> QueryCommunityPoolResponse: | ||
""" | ||
CommunityPool queries the community pool coins. | ||
:return: a QueryCommunityPoolResponse instance | ||
""" | ||
|
||
@abstractmethod | ||
def DelegationTotalRewards( | ||
self, request: QueryDelegationTotalRewardsRequest | ||
) -> QueryDelegationTotalRewardsResponse: | ||
""" | ||
DelegationTotalRewards queries the total rewards accrued by a each validator. | ||
:param request: a QueryDelegationTotalRewardsRequest instance | ||
:return: a QueryDelegationTotalRewardsResponse instance | ||
""" | ||
|
||
@abstractmethod | ||
def DelegationRewards( | ||
self, request: QueryDelegationRewardsRequest | ||
) -> QueryDelegationRewardsResponse: | ||
""" | ||
DelegationRewards queries the total rewards accrued by a delegation. | ||
:param request: a QueryDelegationRewardsRequest instance | ||
:return: a QueryDelegationRewardsResponse instance | ||
""" | ||
|
||
@abstractmethod | ||
def DelegatorValidators( | ||
self, request: QueryDelegatorValidatorsRequest | ||
) -> QueryDelegatorValidatorsResponse: | ||
""" | ||
DelegatorValidators queries the validators of a delegator. | ||
:param request: a QueryDelegatorValidatorsRequest instance | ||
:return: a QueryDelegatorValidatorsResponse instance | ||
""" | ||
|
||
@abstractmethod | ||
def DelegatorWithdrawAddress( | ||
self, request: QueryDelegatorWithdrawAddressRequest | ||
) -> QueryDelegatorWithdrawAddressResponse: | ||
""" | ||
DelegatorWithdrawAddress queries withdraw address of a delegator. | ||
:param request: a QueryDelegatorWithdrawAddressRequest instance | ||
:return: a QueryDelegatorWithdrawAddressResponse instance | ||
""" | ||
|
||
@abstractmethod | ||
def Params(self) -> QueryParamsResponse: | ||
""" | ||
Params queries params of the distribution module. | ||
:return: a QueryParamsResponse instance | ||
""" | ||
|
||
@abstractmethod | ||
def ValidatorCommission( | ||
self, request: QueryValidatorCommissionRequest | ||
) -> QueryValidatorCommissionResponse: | ||
""" | ||
ValidatorCommission queries accumulated commission for a validator. | ||
:param request: QueryValidatorCommissionRequest | ||
:return: QueryValidatorCommissionResponse | ||
""" | ||
|
||
@abstractmethod | ||
def ValidatorOutstandingRewards( | ||
self, request: QueryValidatorOutstandingRewardsRequest | ||
) -> QueryValidatorOutstandingRewardsResponse: | ||
""" | ||
ValidatorOutstandingRewards queries rewards of a validator address. | ||
:param request: QueryValidatorOutstandingRewardsRequest | ||
:return: QueryValidatorOutstandingRewardsResponse | ||
""" | ||
|
||
@abstractmethod | ||
def ValidatorSlashes( | ||
self, request: QueryValidatorSlashesRequest | ||
) -> QueryValidatorSlashesResponse: | ||
""" | ||
ValidatorSlashes queries slash events of a validator. | ||
:param request: QueryValidatorSlashesRequest | ||
:return: QueryValidatorSlashesResponse | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
# -*- coding: utf-8 -*- | ||
# ------------------------------------------------------------------------------ | ||
# | ||
# Copyright 2018-2021 Fetch.AI Limited | ||
# Modifications copyright (C) 2022 Cros-Nest | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
# ------------------------------------------------------------------------------ | ||
|
||
"""Implementation of Distribution interface using REST.""" | ||
|
||
from google.protobuf.json_format import Parse | ||
|
||
from cosmpy.common.rest_client import RestClient | ||
from cosmpy.distribution.interface import Distribution | ||
from cosmpy.protos.cosmos.distribution.v1beta1.query_pb2 import ( | ||
QueryCommunityPoolResponse, | ||
QueryDelegationRewardsRequest, | ||
QueryDelegationRewardsResponse, | ||
QueryDelegationTotalRewardsRequest, | ||
QueryDelegationTotalRewardsResponse, | ||
QueryDelegatorValidatorsRequest, | ||
QueryDelegatorValidatorsResponse, | ||
QueryDelegatorWithdrawAddressRequest, | ||
QueryDelegatorWithdrawAddressResponse, | ||
QueryParamsResponse, | ||
QueryValidatorCommissionRequest, | ||
QueryValidatorCommissionResponse, | ||
QueryValidatorOutstandingRewardsRequest, | ||
QueryValidatorOutstandingRewardsResponse, | ||
QueryValidatorSlashesRequest, | ||
QueryValidatorSlashesResponse, | ||
) | ||
|
||
|
||
class DistributionRestClient(Distribution): | ||
"""Distribution REST client.""" | ||
|
||
API_URL = "/cosmos/distribution/v1beta1" | ||
|
||
def __init__(self, rest_api: RestClient) -> None: | ||
""" | ||
Initialize. | ||
:param rest_api: RestClient api | ||
""" | ||
self._rest_api = rest_api | ||
|
||
def CommunityPool(self) -> QueryCommunityPoolResponse: | ||
""" | ||
CommunityPool queries the community pool coins. | ||
:return: a QueryCommunityPoolResponse instance | ||
""" | ||
json_response = self._rest_api.get(f"{self.API_URL}/community_pool") | ||
return Parse(json_response, QueryCommunityPoolResponse()) | ||
|
||
def DelegationTotalRewards( | ||
self, request: QueryDelegationTotalRewardsRequest | ||
) -> QueryDelegationTotalRewardsResponse: | ||
""" | ||
DelegationTotalRewards queries the total rewards accrued by a each validator. | ||
:param request: a QueryDelegationTotalRewardsRequest instance | ||
:return: a QueryDelegationTotalRewardsResponse instance | ||
""" | ||
json_response = self._rest_api.get( | ||
f"{self.API_URL}/delegators/{request.delegator_address}/rewards" | ||
) | ||
return Parse(json_response, QueryDelegationTotalRewardsResponse()) | ||
|
||
def DelegationRewards( | ||
self, request: QueryDelegationRewardsRequest | ||
) -> QueryDelegationRewardsResponse: | ||
""" | ||
DelegationRewards queries the total rewards accrued by a delegation. | ||
:param request: a QueryDelegationRewardsRequest instance | ||
:return: a QueryDelegationRewardsResponse instance | ||
""" | ||
json_response = self._rest_api.get( | ||
f"{self.API_URL}/delegators/{request.delegator_address}/rewards/{request.validator_address}" | ||
) | ||
return Parse(json_response, QueryDelegationRewardsResponse()) | ||
|
||
def DelegatorValidators( | ||
self, request: QueryDelegatorValidatorsRequest | ||
) -> QueryDelegatorValidatorsResponse: | ||
""" | ||
DelegatorValidators queries the validators of a delegator. | ||
:param request: a QueryDelegatorValidatorsRequest instance | ||
:return: a QueryDelegatorValidatorsResponse instance | ||
""" | ||
json_response = self._rest_api.get( | ||
f"{self.API_URL}/delegators/{request.delegator_address}/validators" | ||
) | ||
return Parse(json_response, QueryDelegatorValidatorsResponse()) | ||
|
||
def DelegatorWithdrawAddress( | ||
self, request: QueryDelegatorWithdrawAddressRequest | ||
) -> QueryDelegatorWithdrawAddressResponse: | ||
""" | ||
DelegatorWithdrawAddress queries withdraw address of a delegator. | ||
:param request: a QueryDelegatorWithdrawAddressRequest instance | ||
:return: a QueryDelegatorWithdrawAddressResponse instance | ||
""" | ||
json_response = self._rest_api.get( | ||
f"{self.API_URL}/delegators/{request.delegator_address}/withdraw_address" | ||
) | ||
return Parse(json_response, QueryDelegatorWithdrawAddressResponse()) | ||
|
||
def Params(self) -> QueryParamsResponse: | ||
""" | ||
Params queries params of the distribution module. | ||
:return: a QueryParamsResponse instance | ||
""" | ||
json_response = self._rest_api.get(f"{self.API_URL}/params") | ||
return Parse(json_response, QueryParamsResponse()) | ||
|
||
def ValidatorCommission( | ||
self, request: QueryValidatorCommissionRequest | ||
) -> QueryValidatorCommissionResponse: | ||
""" | ||
ValidatorCommission queries accumulated commission for a validator. | ||
:param request: QueryValidatorCommissionRequest | ||
:return: QueryValidatorCommissionResponse | ||
""" | ||
json_response = self._rest_api.get( | ||
f"{self.API_URL}/validators/{request.validator_address}/commission" | ||
) | ||
return Parse(json_response, QueryValidatorCommissionResponse()) | ||
|
||
def ValidatorOutstandingRewards( | ||
self, request: QueryValidatorOutstandingRewardsRequest | ||
) -> QueryValidatorOutstandingRewardsResponse: | ||
""" | ||
ValidatorOutstandingRewards queries rewards of a validator address. | ||
:param request: QueryValidatorOutstandingRewardsRequest | ||
:return: QueryValidatorOutstandingRewardsResponse | ||
""" | ||
json_response = self._rest_api.get( | ||
f"{self.API_URL}/validators/{request.validator_address}/outstanding_rewards" | ||
) | ||
return Parse(json_response, QueryValidatorOutstandingRewardsResponse()) | ||
|
||
def ValidatorSlashes( | ||
self, request: QueryValidatorSlashesRequest | ||
) -> QueryValidatorSlashesResponse: | ||
""" | ||
ValidatorSlashes queries slash events of a validator. | ||
:param request: QueryValidatorSlashesRequest | ||
:return: QueryValidatorSlashesResponse | ||
""" | ||
json_response = self._rest_api.get( | ||
f"{self.API_URL}/validators/{request.validator_address}/slashes", | ||
request, | ||
["validatorAddress"], | ||
) | ||
return Parse(json_response, QueryValidatorSlashesResponse()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# -*- coding: utf-8 -*- | ||
# ------------------------------------------------------------------------------ | ||
# | ||
# Copyright 2018-2021 Fetch.AI Limited | ||
# Modifications copyright (C) 2022 Cros-Nest | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
# ------------------------------------------------------------------------------ | ||
|
||
"""This package contains test for the Distribution modules.""" |
Oops, something went wrong.