Skip to content

Commit

Permalink
Merge pull request #114 from skalenetwork/enhancement/SKALE-2261-upda…
Browse files Browse the repository at this point in the history
…te-delegation-functionality

SKALE-2261 Move everything to the latest delegation contracts
  • Loading branch information
dmytrotkk authored Mar 2, 2020
2 parents 3e1ac6a + 42c63aa commit 73ecc20
Show file tree
Hide file tree
Showing 26 changed files with 695 additions and 523 deletions.
1 change: 0 additions & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ jobs:
env:
ETH_PRIVATE_KEY: ${{ secrets.ETH_PRIVATE_KEY }}
ENDPOINT: ${{ secrets.ENDPOINT }}
MANAGER_BRANCH: "delegation-manual"
PIP_USERNAME: ${{ secrets.PIP_USERNAME }}
PIP_PASSWORD: ${{ secrets.PIP_PASSWORD }}
steps:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
ETH_PRIVATE_KEY: ${{ secrets.ETH_PRIVATE_KEY }}
ENDPOINT: ${{ secrets.ENDPOINT }}
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
MANAGER_BRANCH: "delegation-manual"
MANAGER_BRANCH: "delegation-v2"
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ schain.json

abi.json
test_abi.json
unique.json

.DS_Store

Expand Down
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [3.0.0] - Unreleased
## [3.1]

Updated delegation functionality

## [3.0.0]

### Added

Expand Down
19 changes: 13 additions & 6 deletions scripts/deploy_manager.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@ set -e
: "${ETH_PRIVATE_KEY?Need to set ETH_PRIVATE_KEY}"
: "${MANAGER_BRANCH?Need to set MANAGER_BRANCH}"

docker run -d --network host --name ganache trufflesuite/ganache-cli:v6.8.1-beta.0 \
--account="${ETH_PRIVATE_KEY},100000000000000000000000000" -l 80000000

export DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
export DOCKER_NETWORK_ENDPOINT=http://ganache:8545

docker rm -f ganache || true
docker network create testnet || true

docker run -d --network testnet -p 8545:8545 -p 8546:8546 \
--name ganache trufflesuite/ganache-cli:v6.8.1-beta.0 \
--account="0x${ETH_PRIVATE_KEY},100000000000000000000000000" -l 80000000

docker run \
-v $DIR/contracts_data:/usr/src/manager/data \
--network host \
--network testnet \
-e ENDPOINT=$DOCKER_NETWORK_ENDPOINT \
-e PRIVATE_KEY=$ETH_PRIVATE_KEY \
skalenetwork/skale-manager:$MANAGER_BRANCH-latest \
npx truffle migrate --network test
npx truffle migrate --network unique

cp $DIR/contracts_data/test.json $DIR/../test_abi.json
cp $DIR/contracts_data/unique.json $DIR/../test_abi.json
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

setup(
name='skale.py',
version='3.0',
version='3.1',
description='SKALE client tools',
long_description_markdown_filename='README.md',
author='SKALE Labs',
Expand Down
1 change: 1 addition & 0 deletions skale/contracts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
from skale.contracts.delegation.delegation_controller import DelegationController
from skale.contracts.delegation.validator_service import ValidatorService
from skale.contracts.delegation.token_state import TokenState
from skale.contracts.delegation.distributor import Distributor

from skale.contracts.dkg import DKG
148 changes: 146 additions & 2 deletions skale/contracts/delegation/delegation_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,18 @@
# You should have received a copy of the GNU Affero General Public License
# along with SKALE.py. If not, see <https://www.gnu.org/licenses/>.

from skale.contracts import BaseContract
from skale.contracts import BaseContract, transaction_method
from skale.utils.helper import format_fields
from skale.transactions.tools import post_transaction
from skale.dataclasses.tx_res import TxRes
from skale.utils.constants import GAS
from skale.dataclasses.delegation_status import DelegationStatus

FIELDS = ['address', 'validator_id', 'amount', 'delegation_period', 'created', 'info']

FIELDS = [
'address', 'validator_id', 'amount', 'delegation_period', 'created',
'started', 'finished', 'info'
]


class DelegationController(BaseContract):
Expand All @@ -35,10 +43,146 @@ def get_delegation(self, delegation_id: int) -> dict:
"""
return self.__raw_get_delegation(delegation_id)

def get_delegation_full(self, delegation_id: int) -> dict:
"""Returns delegation structure with ID and status fields.
:returns: Info about delegation request
:rtype: dict
"""
delegation = self.get_delegation(delegation_id)
delegation['id'] = delegation_id
delegation['status'] = self._get_delegation_status(delegation_id)
return delegation

def __raw_get_delegation(self, delegation_id: int) -> list:
"""Returns raw delegation fields.
:returns: Info about delegation request
:rtype: list
"""
return self.contract.functions.getDelegation(delegation_id).call()

def _get_delegation_ids_by_validator(self, validator_id: int) -> list:
delegation_ids_len = self._get_delegation_ids_len_by_validator(validator_id)
return [
self.contract.functions.delegationsByValidator(validator_id, _id).call()
for _id in range(delegation_ids_len)
]

def _get_delegation_ids_by_holder(self, address: str) -> list:
delegation_ids_len = self._get_delegation_ids_len_by_holder(address)
return [
self.contract.functions.delegationsByHolder(address, _id).call()
for _id in range(delegation_ids_len)
]

def _get_delegation_ids_len_by_validator(self, validator_id: int) -> list:
return self.contract.functions.getDelegationsByValidatorLength(validator_id).call()

def _get_delegation_ids_len_by_holder(self, address: str) -> list:
return self.contract.functions.getDelegationsByHolderLength(address).call()

def _get_delegation_state_index(self, delegation_id: int) -> str:
return self.contract.functions.getState(delegation_id).call()

def _get_delegation_status(self, delegation_id: int) -> str:
index = self._get_delegation_state_index(delegation_id)
return DelegationStatus(index).name

def get_all_delegations(self, delegation_ids: list) -> list:
"""Returns list of formatted delegations with particular status.
:param delegation_ids: List of delegation IDs
:type address: list
:returns: List of formatted delegations
:rtype: list
"""
return [
self.skale.delegation_controller.get_delegation_full(_id)
for _id in delegation_ids
]

def get_all_delegations_by_holder(self, address: str) -> list:
"""Returns list of formatted delegations for token holder.
:param address: Ethereum address
:type address: str
:returns: List of formatted delegation requests
:rtype: list
"""
delegation_ids = self._get_delegation_ids_by_holder(address)
return self.get_all_delegations(delegation_ids)

def get_all_delegations_by_validator(self, validator_id: int) -> list:
"""Returns list of formatted delegations for validator.
:param validator_id: ID of the validator
:type address: int
:returns: List of formatted delegations
:rtype: list
"""
validator_id = int(validator_id)
delegation_ids = self._get_delegation_ids_by_validator(validator_id)
return self.get_all_delegations(delegation_ids)

@transaction_method
def delegate(self, validator_id: int, amount: int, delegation_period: int, info: str) -> TxRes:
"""Creates request to delegate amount of tokens to validator_id.
:param validator_id: ID of the validator to delegate tokens
:type validator_id: int
:param amount: Amount of tokens to delegate
:type amount: int
:param delegation_period: Period of delegation
:type delegation_period: int
:param info: Delegation request information
:type info: str
:returns: Transaction results
:rtype: TxRes
"""
func = self.contract.functions.delegate(validator_id, amount, delegation_period, info)
return post_transaction(self.skale.wallet, func, GAS['delegate'])

@transaction_method
def accept_pending_delegation(self, delegation_id: int) -> TxRes:
"""Accepts a pending delegation by delegation ID.
:param delegation_id: Delegation ID to accept
:type delegation_id: int
:returns: Transaction results
:rtype: TxRes
"""
func = self.contract.functions.acceptPendingDelegation(delegation_id)
return post_transaction(self.skale.wallet, func, GAS['accept_pending_delegation'])

@transaction_method
def cancel_pending_delegation(self, delegation_id: int) -> TxRes:
"""Cancel pending delegation request.
:param delegation_id: ID of the delegation to cancel
:type delegation_id: int
:returns: Transaction results
:rtype: TxRes
"""
func = self.contract.functions.cancelPendingDelegation(delegation_id)
return post_transaction(self.skale.wallet, func, GAS['cancel_pending_delegation'])

def get_delegated_to_validator_now(self, validator_id: int) -> int:
"""Amount of delegated tokens to the validator
:param validator_id: ID of the validator
:type validator_id: int
:returns: Amount of delegated tokens
:rtype: int
"""
return self.contract.functions.getAndUpdateDelegatedToValidatorNow(validator_id).call()

def get_delegated_amount(self, address: str) -> int:
"""Amount of delegated tokens by token holder
:param address: Token holder address
:type address: str
:returns: Amount of delegated tokens
:rtype: int
"""
return self.contract.functions.getAndUpdateDelegatedAmount(address).call()
Loading

0 comments on commit 73ecc20

Please sign in to comment.