Skip to content

Commit

Permalink
feat: create functionality to fetch and update bets
Browse files Browse the repository at this point in the history
  • Loading branch information
Adamantios committed Jul 5, 2023
1 parent 2d056bb commit cbd6f67
Show file tree
Hide file tree
Showing 15 changed files with 987 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ all-checks: clean format code-checks security generators common-checks-1 common-

.PHONY: fix-abci-app-specs
fix-abci-app-specs:
#autonomy analyse fsm-specs --update --app-class AbciApp --package packages/valory/skills/abci
autonomy analyse fsm-specs --update --app-class MarketManagerAbciApp --package packages/valory/skills/market_manager_abci
echo "Successfully validated abcis!"

protolint_install:
Expand Down
5 changes: 5 additions & 0 deletions packages/valory/skills/market_manager_abci/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Market manager abci

## Description

This module contains the MarketManager skill for an AEA.
25 changes: 25 additions & 0 deletions packages/valory/skills/market_manager_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 MarketManager skill for an AEA."""

from aea.configurations.base import PublicId


PUBLIC_ID = PublicId.from_str("valory/market_manager_abci:0.1.0")
88 changes: 88 additions & 0 deletions packages/valory/skills/market_manager_abci/behaviours.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# -*- 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 MarketManager skill."""

from abc import ABC
from typing import Generator, Set, Type, cast

from packages.valory.skills.abstract_round_abci.behaviour_utils import BaseBehaviour
from packages.valory.skills.abstract_round_abci.behaviours import AbstractRoundBehaviour
from packages.valory.skills.market_manager_abci.graph_tooling.requests import (
QueryingBehaviour,
)
from packages.valory.skills.market_manager_abci.models import (
MarketManagerParams,
SharedState,
)
from packages.valory.skills.market_manager_abci.payloads import UpdateBetsPayload
from packages.valory.skills.market_manager_abci.rounds import (
MarketManagerAbciApp,
SynchronizedData,
UpdateBetsRound,
)


class MarketManagerBaseBehaviour(BaseBehaviour, ABC):
"""Base behaviour for the MarketManager skill."""

@property
def params(self) -> MarketManagerParams:
"""Return the params."""
return cast(MarketManagerParams, self.context.params)

@property
def shared_state(self) -> SharedState:
"""Get the shared state."""
return cast(SharedState, self.context.state)

@property
def synchronized_data(self) -> SynchronizedData:
"""Return the synchronized data."""
return cast(SynchronizedData, super().synchronized_data)


class UpdateBetsBehaviour(QueryingBehaviour):
"""Behaviour that fetches and updates the bets."""

matching_round = UpdateBetsRound

def async_act(self) -> Generator:
"""Do the action."""
with self.context.benchmark_tool.measure(self.behaviour_id).local():
succeeded = yield from self._update_bets()
bets_hash = self.shared_state.bets_hash if succeeded else ""
payload = UpdateBetsPayload(
self.context.agent_address, succeeded, bets_hash
)

with self.context.benchmark_tool.measure(self.behaviour_id).consensus():
yield from self.send_a2a_transaction(payload)
yield from self.wait_until_round_end()
self.set_done()


class MarketManagerRoundBehaviour(AbstractRoundBehaviour):
"""This behaviour manages the consensus stages for the MarketManager behaviour."""

initial_behaviour_cls = UpdateBetsBehaviour
abci_app_cls = MarketManagerAbciApp
behaviours: Set[Type[BaseBehaviour]] = {
UpdateBetsBehaviour, # type: ignore
}
90 changes: 90 additions & 0 deletions packages/valory/skills/market_manager_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
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- 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 functionality for interacting with a GraphQL API."""
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- 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.
#
# ------------------------------------------------------------------------------

"""GraphQL queries."""
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# -*- 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.
#
# ------------------------------------------------------------------------------

"""Omen queries."""

import json
from string import Template


questions = Template(
"""
{
fixedProductMarketMakers(
where: {
creator_in: ${creators},
outcomeSlotCount: ${slot_count},
openingTimestamp_gt: ${opening_threshold},
language_in: ${languages},
isPendingArbitration: false
},
orderBy: creationTimestamp
orderDirection: desc
){
id
title
creator
fee
openingTimestamp
outcomeSlotCount
outcomeTokenAmounts
outcomeTokenMarginalPrices
outcomes
}
}
"""
)


def to_content(query: str) -> bytes:
"""Convert the given query string to payload content, i.e., add it under a `queries` key and convert it to bytes."""
finalized_query = {"query": query}
encoded_query = json.dumps(finalized_query, sort_keys=True).encode("utf-8")

return encoded_query
Loading

0 comments on commit cbd6f67

Please sign in to comment.