Skip to content

Commit

Permalink
Merge pull request #7 from valory-xyz/feat/chain
Browse files Browse the repository at this point in the history
Create trader abci
  • Loading branch information
Adamantios authored Jul 11, 2023
2 parents 89a5f9a + 5129499 commit 827bf64
Show file tree
Hide file tree
Showing 15 changed files with 584 additions and 36 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ all-checks: clean format code-checks security generators common-checks-1 common-
fix-abci-app-specs:
autonomy analyse fsm-specs --update --app-class MarketManagerAbciApp --package packages/valory/skills/market_manager_abci
autonomy analyse fsm-specs --update --app-class DecisionMakerAbciApp --package packages/valory/skills/decision_maker_abci
autonomy analyse fsm-specs --update --app-class TraderAbciApp --package packages/valory/skills/trader_abci
echo "Successfully validated abcis!"

protolint_install:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def _calc_buy_amount(self) -> Generator[None, None, bool]:
self.buy_amount = buy_amount
return True

def _get_buy_data(self) -> Generator[None, None, bool]:
def _build_buy_data(self) -> Generator[None, None, bool]:
"""Get the buy tx data encoded."""
response_msg = yield from self.get_contract_api_response(
performative=ContractApiMessage.Performative.GET_RAW_TRANSACTION, # type: ignore
Expand All @@ -129,12 +129,8 @@ def _get_buy_data(self) -> Generator[None, None, bool]:
self.buy_data = buy_data
return True

def _get_safe_tx_hash(self) -> Generator[None, None, bool]:
"""
Prepares and returns the safe tx hash.
:return: the tx hash
"""
def _build_safe_tx_hash(self) -> Generator[None, None, bool]:
"""Prepares and returns the safe tx hash."""
response_msg = yield from self.get_contract_api_response(
performative=ContractApiMessage.Performative.GET_RAW_TRANSACTION, # type: ignore
contract_address=self.synchronized_data.safe_contract_address,
Expand Down Expand Up @@ -197,16 +193,11 @@ def wait_for_condition_with_sleep(
yield from self.sleep(self.params.sleep_time)

def _prepare_safe_tx(self) -> Generator[None, None, str]:
"""
Sample a bet and return its id.
The sampling logic is relatively simple at the moment
It simply selects the unprocessed bet with the largest liquidity.
"""
"""Prepare the safe transaction for placing a bet and return the hex for the tx settlement skill."""
for step in (
self._calc_buy_amount,
self._get_buy_data,
self._get_safe_tx_hash,
self._build_buy_data,
self._build_safe_tx_hash,
):
yield from self.wait_for_condition_with_sleep(step)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def sampled_bet_idx(self) -> int:
The sampling logic is relatively simple at the moment
It simply selects the unprocessed bet with the largest liquidity.
:return: the id of the sampled bet.
"""
max_lq = max(self.available_bets, key=lambda bet: bet.usdLiquidityMeasure)
return self.synchronized_data.bets.index(max_lq)
Expand Down
6 changes: 2 additions & 4 deletions packages/valory/skills/decision_maker_abci/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@
SharedState as BaseSharedState,
)
from packages.valory.skills.decision_maker_abci.rounds import DecisionMakerAbciApp
from packages.valory.skills.market_manager_abci.models import (
MarketManagerParams as BaseParams,
)
from packages.valory.skills.market_manager_abci.models import MarketManagerParams


Requests = BaseRequests
Expand All @@ -44,7 +42,7 @@ class SharedState(BaseSharedState):
abci_app_cls = DecisionMakerAbciApp


class DecisionMakerParams(BaseParams):
class DecisionMakerParams(MarketManagerParams):
"""Decision maker's parameters."""

def __init__(self, *args: Any, **kwargs: Any) -> None:
Expand Down
4 changes: 2 additions & 2 deletions packages/valory/skills/market_manager_abci/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ class SharedState(BaseSharedState):
abci_app_cls = MarketManagerAbciApp


class RandomnessApi(ApiSpecs):
"""A model that wraps ApiSpecs for randomness api specifications."""
class OmenSubgraph(ApiSpecs):
"""A model that wraps ApiSpecs for the OMEN's subgraph specifications."""


class MarketManagerParams(BaseParams):
Expand Down
15 changes: 1 addition & 14 deletions packages/valory/skills/market_manager_abci/skill.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,6 @@ models:
opening_margin: 300
languages: [en_US]
class_name: MarketManagerParams
randomness_api:
args:
api_id: cloudflare
headers: {}
method: GET
parameters: {}
response_key: null
response_type: dict
retries: 5
url: https://drand.cloudflare.com/public/latest
class_name: RandomnessApi
omen_subgraph:
args:
api_id: omen
Expand All @@ -151,7 +140,5 @@ models:
state:
args: {}
class_name: SharedState
dependencies:
open-aea-test-autonomy:
version: ==0.10.7
dependencies: {}
is_abstract: true
5 changes: 5 additions & 0 deletions packages/valory/skills/trader_abci/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Trader abci

## Description

This module contains the trader chained skill for an AEA.
25 changes: 25 additions & 0 deletions packages/valory/skills/trader_abci/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2023 Valory AG
#
# 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 module contains the trader chained skill for an AEA."""

from aea.configurations.base import PublicId


PUBLIC_ID = PublicId.from_str("valory/trader_abci:0.1.0")
61 changes: 61 additions & 0 deletions packages/valory/skills/trader_abci/behaviours.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2023 Valory AG
#
# 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 module contains the behaviours for the trader skill."""

from typing import Set, Type

from packages.valory.skills.abstract_round_abci.behaviours import (
AbstractRoundBehaviour,
BaseBehaviour,
)
from packages.valory.skills.decision_maker_abci.behaviours.round_behaviour import (
AgentDecisionMakerRoundBehaviour,
)
from packages.valory.skills.market_manager_abci.behaviours import (
MarketManagerRoundBehaviour,
)
from packages.valory.skills.registration_abci.behaviours import (
AgentRegistrationRoundBehaviour,
RegistrationStartupBehaviour,
)
from packages.valory.skills.reset_pause_abci.behaviours import (
ResetPauseABCIConsensusBehaviour,
)
from packages.valory.skills.termination_abci.behaviours import (
BackgroundBehaviour,
TerminationAbciBehaviours,
)
from packages.valory.skills.trader_abci.composition import TraderAbciApp


class TraderConsensusBehaviour(AbstractRoundBehaviour):
"""This behaviour manages the consensus stages for the trader."""

initial_behaviour_cls = RegistrationStartupBehaviour
abci_app_cls = TraderAbciApp

behaviours: Set[Type[BaseBehaviour]] = {
*AgentRegistrationRoundBehaviour.behaviours,
*AgentDecisionMakerRoundBehaviour.behaviours,
*MarketManagerRoundBehaviour.behaviours,
*ResetPauseABCIConsensusBehaviour.behaviours,
*TerminationAbciBehaviours.behaviours,
}
background_behaviour_cls = BackgroundBehaviour
76 changes: 76 additions & 0 deletions packages/valory/skills/trader_abci/composition.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2023 Valory AG
#
# 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 module contains the trader ABCI application."""

from packages.valory.skills.abstract_round_abci.abci_app_chain import (
AbciAppTransitionMapping,
chain,
)
from packages.valory.skills.decision_maker_abci.rounds import DecisionMakerAbciApp
from packages.valory.skills.decision_maker_abci.states.final_states import (
FinishedDecisionMakerRound,
FinishedWithoutDecisionRound,
)
from packages.valory.skills.decision_maker_abci.states.sampling import SamplingRound
from packages.valory.skills.market_manager_abci.rounds import (
FailedMarketManagerRound,
FinishedMarketManagerRound,
MarketManagerAbciApp,
UpdateBetsRound,
)
from packages.valory.skills.registration_abci.rounds import (
AgentRegistrationAbciApp,
FinishedRegistrationRound,
RegistrationRound,
)
from packages.valory.skills.reset_pause_abci.rounds import (
FinishedResetAndPauseErrorRound,
FinishedResetAndPauseRound,
ResetAndPauseRound,
ResetPauseAbciApp,
)
from packages.valory.skills.termination_abci.rounds import BackgroundRound
from packages.valory.skills.termination_abci.rounds import Event as TerminationEvent
from packages.valory.skills.termination_abci.rounds import TerminationAbciApp


abci_app_transition_mapping: AbciAppTransitionMapping = {
FinishedRegistrationRound: UpdateBetsRound,
FinishedMarketManagerRound: SamplingRound,
FailedMarketManagerRound: ResetAndPauseRound,
FinishedDecisionMakerRound: ResetAndPauseRound,
FinishedWithoutDecisionRound: ResetAndPauseRound,
FinishedResetAndPauseRound: UpdateBetsRound,
FinishedResetAndPauseErrorRound: RegistrationRound,
}

TraderAbciApp = chain(
(
AgentRegistrationAbciApp,
DecisionMakerAbciApp,
MarketManagerAbciApp,
ResetPauseAbciApp,
),
abci_app_transition_mapping,
).add_termination(
background_round_cls=BackgroundRound,
termination_event=TerminationEvent.TERMINATE,
termination_abci_app=TerminationAbciApp,
)
90 changes: 90 additions & 0 deletions packages/valory/skills/trader_abci/dialogues.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2023 Valory AG
#
# 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 module contains the classes required for dialogue management."""

from packages.valory.skills.abstract_round_abci.dialogues import (
AbciDialogue as BaseAbciDialogue,
)
from packages.valory.skills.abstract_round_abci.dialogues import (
AbciDialogues as BaseAbciDialogues,
)
from packages.valory.skills.abstract_round_abci.dialogues import (
ContractApiDialogue as BaseContractApiDialogue,
)
from packages.valory.skills.abstract_round_abci.dialogues import (
ContractApiDialogues as BaseContractApiDialogues,
)
from packages.valory.skills.abstract_round_abci.dialogues import (
HttpDialogue as BaseHttpDialogue,
)
from packages.valory.skills.abstract_round_abci.dialogues import (
HttpDialogues as BaseHttpDialogues,
)
from packages.valory.skills.abstract_round_abci.dialogues import (
IpfsDialogue as BaseIpfsDialogue,
)
from packages.valory.skills.abstract_round_abci.dialogues import (
IpfsDialogues as BaseIpfsDialogues,
)
from packages.valory.skills.abstract_round_abci.dialogues import (
LedgerApiDialogue as BaseLedgerApiDialogue,
)
from packages.valory.skills.abstract_round_abci.dialogues import (
LedgerApiDialogues as BaseLedgerApiDialogues,
)
from packages.valory.skills.abstract_round_abci.dialogues import (
SigningDialogue as BaseSigningDialogue,
)
from packages.valory.skills.abstract_round_abci.dialogues import (
SigningDialogues as BaseSigningDialogues,
)
from packages.valory.skills.abstract_round_abci.dialogues import (
TendermintDialogue as BaseTendermintDialogue,
)
from packages.valory.skills.abstract_round_abci.dialogues import (
TendermintDialogues as BaseTendermintDialogues,
)


AbciDialogue = BaseAbciDialogue
AbciDialogues = BaseAbciDialogues


HttpDialogue = BaseHttpDialogue
HttpDialogues = BaseHttpDialogues


SigningDialogue = BaseSigningDialogue
SigningDialogues = BaseSigningDialogues


LedgerApiDialogue = BaseLedgerApiDialogue
LedgerApiDialogues = BaseLedgerApiDialogues


ContractApiDialogue = BaseContractApiDialogue
ContractApiDialogues = BaseContractApiDialogues

TendermintDialogue = BaseTendermintDialogue
TendermintDialogues = BaseTendermintDialogues


IpfsDialogue = BaseIpfsDialogue
IpfsDialogues = BaseIpfsDialogues
Loading

0 comments on commit 827bf64

Please sign in to comment.