Skip to content

Commit

Permalink
Merge pull request #78 from valory-xyz/fix/close-markets
Browse files Browse the repository at this point in the history
fix: misc. issues with closing
  • Loading branch information
0xArdi authored Dec 25, 2023
2 parents 18dd291 + e74335e commit 06967d6
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 26 deletions.
8 changes: 4 additions & 4 deletions packages/packages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"contract/valory/conditional_tokens/0.1.0": "bafybeifbhritgoqecuilj35uzrrokm2ngfmwkoafbmzxgvpa4q3wpi6i64",
"contract/valory/fpmm_deterministic_factory/0.1.0": "bafybeih5yb4df6plwsxicp4jtywvpqmjnvfw55zw35yuycwxrx7mehoowy",
"contract/valory/realtio/0.1.0": "bafybeihfdvkrjkhezqeaxj3eluv2v4syvnxni6jbuc3knqb4rawrcivcke",
"skill/valory/market_creation_manager_abci/0.1.0": "bafybeicdnq5rioqb3f7yl6jbvdkv3bmzwdhtrs6rrq5zhuivb7yn2r7qle",
"skill/valory/market_maker_abci/0.1.0": "bafybeiho32ca4b7wueh6na7bxra4oo2ctfiq2o7w2tu6nkuucqbmfcfnyi",
"agent/valory/market_maker/0.1.0": "bafybeighrxkgi3c2er55l5sweurslb4qk35bb4uzq4fru6lvperhkq3kni",
"service/valory/market_maker/0.1.0": "bafybeibftdqkqcrxlqdpc6hw3pthyvvvy2hl2kwgaobuwr56ud25gxi22q",
"skill/valory/market_creation_manager_abci/0.1.0": "bafybeibcaibxcnza7yovemtr2b24dr5fjjyowcj32vymu4ltnz7xut5xt4",
"skill/valory/market_maker_abci/0.1.0": "bafybeifzjlqhksee2vurv2vllz7ztks3hhwiyrnqst3t4diabomwnccnru",
"agent/valory/market_maker/0.1.0": "bafybeiglbnqnpryz5dsuc2c2qynja3ms2luemcywktflwjxbfazsq33hdy",
"service/valory/market_maker/0.1.0": "bafybeidcec23o4gyjah2vdj73ceobhqov6lrdbupq6ppcy5iwdj5r4rtbu",
"contract/valory/wxdai/0.1.0": "bafybeidalocwbhmbto6ii6adldtpcughtdt6j3v4tv36utevjk2wrdyqie",
"contract/valory/fpmm/0.1.0": "bafybeiai2ruj27nnglvn7yc5atojyojo3fkmofw6wrjgz2ybps2uwdizx4"
},
Expand Down
5 changes: 3 additions & 2 deletions packages/valory/agents/market_maker/aea-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ protocols:
skills:
- valory/abstract_abci:0.1.0:bafybeiflcfufixmsrhobf56bn5745m2iipcfqyulwk2qegtnagb3kvaaxi
- valory/abstract_round_abci:0.1.0:bafybeiaqcl7h2famylusiffigwem7tevkcsyocdu5xd42jkmgq6kvowzgq
- valory/market_maker_abci:0.1.0:bafybeiho32ca4b7wueh6na7bxra4oo2ctfiq2o7w2tu6nkuucqbmfcfnyi
- valory/market_maker_abci:0.1.0:bafybeifzjlqhksee2vurv2vllz7ztks3hhwiyrnqst3t4diabomwnccnru
- valory/registration_abci:0.1.0:bafybeic2ynseiak7jpta7jfwuqwyp453b4p7lolr4wihxmpn633uekv5am
- valory/market_creation_manager_abci:0.1.0:bafybeicdnq5rioqb3f7yl6jbvdkv3bmzwdhtrs6rrq5zhuivb7yn2r7qle
- valory/market_creation_manager_abci:0.1.0:bafybeibcaibxcnza7yovemtr2b24dr5fjjyowcj32vymu4ltnz7xut5xt4
- valory/reset_pause_abci:0.1.0:bafybeidzajbe3erygeh2xbd6lrjv7nsptznjuzrt24ykgvhgotdeyhfnba
- valory/termination_abci:0.1.0:bafybeie4zvjfxvdu7qrulmur3chpjz3kpj5m4bjsxvpk4gvj5zbyyayfaa
- valory/transaction_settlement_abci:0.1.0:bafybeiaefgqbs7zsn5xe5kdwrujj7ivygkn3ujpw6crnvi3knvxw75qmja
Expand Down Expand Up @@ -203,4 +203,5 @@ models:
slash_threshold_amount: ${int:10000000000000000}
light_slash_unit_amount: ${int:5000000000000000}
serious_slash_unit_amount: ${int:8000000000000000}
xdai_threshold: ${int:1000000000000000000}
is_abstract: false
3 changes: 2 additions & 1 deletion packages/valory/services/market_maker/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ license: Apache-2.0
fingerprint:
README.md: bafybeibwz3af6326msp4h3kqehijvmyhaytvyfbo3o2npc2w4b6zrg6pfq
fingerprint_ignore_patterns: []
agent: valory/market_maker:0.1.0:bafybeighrxkgi3c2er55l5sweurslb4qk35bb4uzq4fru6lvperhkq3kni
agent: valory/market_maker:0.1.0:bafybeiglbnqnpryz5dsuc2c2qynja3ms2luemcywktflwjxbfazsq33hdy
number_of_agents: 1
deployment:
agent:
Expand Down Expand Up @@ -86,6 +86,7 @@ models:
tendermint_url: ${TENDERMINT_URL:str:http://localhost:26657}
tendermint_p2p_url: ${TENDERMINT_P2P_URL:str:localhost:26656}
termination_sleep: ${TERMINATION_SLEEP:int:900}
xdai_threshold: ${XDAI_THRESHOLD:int:1000000000000000000}
tx_timeout: ${TX_TIMEOUT:float:10.0}
use_termination: ${USE_TERMINATION:bool:false}
round_timeout_seconds: ${ROUND_TIMEOUT_SECONDS:float:120}
Expand Down
65 changes: 61 additions & 4 deletions packages/valory/skills/market_creation_manager_abci/behaviours.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ def last_synced_timestamp(self) -> int:
)
return int(last_timestamp)

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

def _calculate_condition_id(
self,
oracle_contract: str,
Expand Down Expand Up @@ -911,10 +916,14 @@ def get_payload(self) -> Generator[None, None, str]:
# something went wrong
return DepositDaiRound.ERROR_PAYLOAD

if balance == 0:
# no balance in the safe
# check if the balance is below the threshold
if balance < self.params.xdai_threshold:
# not enough balance in the safe
return DepositDaiRound.NO_TX_PAYLOAD

# leave xdai threshold in the safe for non-market creation purposes of the safe
balance_to_deposit = balance - self.params.xdai_threshold

# in case there is balance in the safe, fully deposit it to the wxDAI contract
wxdai_address = self.params.collateral_tokens_contract
tx_data = yield from self._get_deposit_tx(wxdai_address)
Expand All @@ -931,7 +940,7 @@ def get_payload(self) -> Generator[None, None, str]:

tx_payload_data = hash_payload_to_hex(
safe_tx_hash=safe_tx_hash,
ether_value=balance,
ether_value=balance_to_deposit,
safe_tx_gas=SAFE_TX_GAS,
to_address=wxdai_address,
data=tx_data,
Expand Down Expand Up @@ -2274,6 +2283,24 @@ def _get_answer_tx(
"data": data,
}

def _get_balance(self, account: str) -> Generator[None, None, Optional[int]]:
"""Get the balance of an account"""
ledger_api_response = yield from self.get_ledger_api_response(
performative=LedgerApiMessage.Performative.GET_STATE,
ledger_callable="get_balance",
account=account,
)
if ledger_api_response.performative != LedgerApiMessage.Performative.STATE:
# something went wrong
self.context.logger.error(
f"Couldn't get balance for account {account}. "
f"Expected response performative {LedgerApiMessage.Performative.STATE.value}, " # type: ignore
f"Received {ledger_api_response.performative.value}." # type: ignore
)
return None
balance = cast(int, ledger_api_response.state.body.get("get_balance_result"))
return balance

def get_payload(self) -> Generator[None, None, str]:
"""Get the transaction payload"""
# get the questions to that need to be answered
Expand All @@ -2290,16 +2317,42 @@ def get_payload(self) -> Generator[None, None, str]:
f"Got {len(questions)} questions to close. " f"Questions: {questions}"
)

safe_address = self.synchronized_data.safe_contract_address
balance = yield from self._get_balance(safe_address)
if balance is None:
self.context.logger.info("Couldn't get balance")
return CloseMarketsRound.NO_TX

self.context.logger.info(f"Address {safe_address!r} has balance {balance}.")
max_num_questions = min(
len(questions), self.params.questions_to_close_batch_size
)
bond_required = self.params.close_question_bond * max_num_questions
if balance < bond_required:
# not enough balance to close the questions
self.context.logger.info(
f"Not enough balance to close {max_num_questions} questions. "
f"Balance {balance}, required {bond_required}"
)
return CloseMarketsRound.NO_TX

# get the answers for those questions
question_to_answer = {}
for question in questions:
question_id = question["question"]["id"]
if question_id.lower() in self.shared_state.processed_question_ids:
# we already processed this question, skip it
self.context.logger.info(
f"Question {question_id} already processed, skipping it."
)
continue
answer = yield from self._get_answer(question["title"])
if answer is None:
self.context.logger.warning(
f"Couldn't get answer for question {question}"
)
continue
question_to_answer[question["question"]["id"]] = answer
question_to_answer[question_id] = answer

if len(question_to_answer) == self.params.questions_to_close_batch_size:
break
Expand All @@ -2322,6 +2375,10 @@ def get_payload(self) -> Generator[None, None, str]:
)
continue
txs.append(tx)
# mark this question as processed. This is to avoid the situation where we
# try to answer the same question multiple times due to a out-of-sync issue
# between the subgraph and the realitio contract.
self.shared_state.processed_question_ids.add(question_id.lower())

if len(txs) == 0:
# something went wrong, respond with ERROR payload for now
Expand Down
13 changes: 11 additions & 2 deletions packages/valory/skills/market_creation_manager_abci/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@

"""This module contains the shared state for the abci skill of MarketCreationManagerAbciApp."""

from typing import Any, List
from typing import Any, List, Set, Type

from aea.skills.base import SkillContext

from packages.valory.skills.abstract_round_abci.base import AbciApp
from packages.valory.skills.abstract_round_abci.models import ApiSpecs, BaseParams
from packages.valory.skills.abstract_round_abci.models import (
BenchmarkTool as BaseBenchmarkTool,
Expand All @@ -37,7 +40,12 @@
class SharedState(BaseSharedState):
"""Keep the current shared state of the skill."""

abci_app_cls = MarketCreationManagerAbciApp
abci_app_cls: Type[AbciApp] = MarketCreationManagerAbciApp

def __init__(self, *args: Any, skill_context: SkillContext, **kwargs: Any) -> None:
"""Initialize the shared state object."""
self.processed_question_ids: Set[str] = set()
super().__init__(*args, skill_context=skill_context, **kwargs)


class MarketCreationManagerParams(BaseParams):
Expand Down Expand Up @@ -141,6 +149,7 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
"market_proposal_round_timeout_seconds_per_day", kwargs, type_=int
)
self.initial_funds = self._ensure("initial_funds", kwargs, type_=float)
self.xdai_threshold = self._ensure("xdai_threshold", kwargs, type_=int)
super().__init__(*args, **kwargs)


Expand Down
26 changes: 24 additions & 2 deletions packages/valory/skills/market_creation_manager_abci/rounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -704,12 +704,29 @@ class CloseMarketsRound(CollectSameUntilThresholdRound):

def end_block(self) -> Optional[Tuple[BaseSynchronizedData, Enum]]:
"""End block."""

if self.threshold_reached:
if self.most_voted_payload == self.ERROR_PAYLOAD:
return self.synchronized_data, Event.ERROR
if self.most_voted_payload == self.NO_TX:
return self.synchronized_data, Event.NO_TX
state = self.synchronized_data.update(
synced_data = cast(SynchronizedData, self.synchronized_data)

# Fix to ensure properties are present on the SynchronizedData
# before ResetAndPause round.
synced_data = synced_data.ensure_property_is_set(
get_name(SynchronizedData.approved_markets_count)
)
synced_data = synced_data.ensure_property_is_set(
get_name(SynchronizedData.proposed_markets_count)
)
synced_data = synced_data.ensure_property_is_set(
get_name(SynchronizedData.proposed_markets_data)
)
synced_data = synced_data.ensure_property_is_set(
get_name(SynchronizedData.approved_markets_timestamp)
)
state = synced_data.update(
synchronized_data_class=self.synchronized_data_class,
**{
get_name(
Expand Down Expand Up @@ -746,7 +763,11 @@ class MarketCreationManagerAbciApp(AbciApp[Event]):
"""MarketCreationManagerAbciApp"""

initial_round_cls: AppState = CollectRandomnessRound
initial_states: Set[AppState] = {CollectRandomnessRound, PostTransactionRound}
initial_states: Set[AppState] = {
CollectRandomnessRound,
PostTransactionRound,
CloseMarketsRound,
}
transition_function: AbciAppTransitionFunction = {
PostTransactionRound: {
Event.DONE: CloseMarketsRound,
Expand Down Expand Up @@ -854,6 +875,7 @@ class MarketCreationManagerAbciApp(AbciApp[Event]):
get_name(SynchronizedData.approved_markets_timestamp),
} # type: ignore
db_pre_conditions: Dict[AppState, Set[str]] = {
CloseMarketsRound: set(),
CollectRandomnessRound: set(),
PostTransactionRound: set(),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ aea_version: '>=1.0.0, <2.0.0'
fingerprint:
.gitignore: bafybeihdfdezgtr3s2lzq5y3oaitfrdy4u4pehionju2bdez35wcjgqx6y
__init__.py: bafybeibkyjt4covc4yhd22aw7kav45zozk3exdv344emt3lilln64soaxm
behaviours.py: bafybeidzore7pigov5i2v74mre6ezvbqneh3dd4iawtn2sjsydin3s4dvq
behaviours.py: bafybeib6ef2ntjcokb7476unn2iza6fqgc2jqfuvtxvcffpucq5q7djtfe
dialogues.py: bafybeicmaufkl7vdomnfciv7lw4536ssld7x4uemdapuhsyvfpd4ncibza
fsm_specification.yaml: bafybeiglegr5e55k3kra4movl5klfoqc6c2lm4cbh3fzicilwq6lhmnmxa
handlers.py: bafybeietxjfli2i57kb7heoy772rcq2znusl36gg7jjj5g3pddw7egny3q
models.py: bafybeigoed3bmxhd4twapnbuyqwztm4hx5cdfenhljfaxa325lktiwky2a
models.py: bafybeif5wcgzqfkq55hzo3cq2fzwr74s5jeozknxpzizqabcsfr3sd6xry
payloads.py: bafybeiadaz4jtimdq6ppgej55xezqkri5bjhjwvwbqipd2fwep2gkq2vvu
prompts.py: bafybeigbvslbtm463rw77nt4jqwkzbhnwhmicaacxgcrq55m3tww5ytmfy
rounds.py: bafybeif7bmei2l2tdhag32yb4shnltoecqssyae7h5lacsdq6gis2rjsdu
rounds.py: bafybeia4onql3sl6zcf2blklnwjqcvob7qokdwsib26gfu33h6a4oalq3m
tests/__init__.py: bafybeihfxvqnyfly72tbxnnnglshcilm2kanihqnjiasvcz3ec3csw32ti
fingerprint_ignore_patterns: []
connections:
Expand Down Expand Up @@ -206,6 +206,7 @@ models:
use_termination: false
use_slashing: false
slash_cooldown_hours: 3
xdai_threshold: 1000000000000000000
slash_threshold_amount: 10000000000000000
light_slash_unit_amount: 5000000000000000
serious_slash_unit_amount: 8000000000000000
Expand Down
4 changes: 2 additions & 2 deletions packages/valory/skills/market_maker_abci/composition.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@


abci_app_transition_mapping: AbciAppTransitionMapping = {
FinishedRegistrationRound: MarketCreationManagerAbci.CollectRandomnessRound,
FinishedRegistrationRound: MarketCreationManagerAbci.CloseMarketsRound,
MarketCreationManagerAbci.FinishedWithoutTxRound: ResetAndPauseRound,
MarketCreationManagerAbci.FinishedWithDepositDaiRound: TransactionSettlementAbci.RandomnessTransactionSubmissionRound,
MarketCreationManagerAbci.FinishedMarketCreationManagerRound: TransactionSettlementAbci.RandomnessTransactionSubmissionRound,
MarketCreationManagerAbci.FinishedWithRemoveFundingRound: TransactionSettlementAbci.RandomnessTransactionSubmissionRound,
TransactionSettlementAbci.FinishedTransactionSubmissionRound: MarketCreationManagerAbci.PostTransactionRound,
TransactionSettlementAbci.FailedRound: ResetAndPauseRound,
FinishedResetAndPauseRound: MarketCreationManagerAbci.CollectRandomnessRound,
FinishedResetAndPauseRound: MarketCreationManagerAbci.CloseMarketsRound,
FinishedResetAndPauseErrorRound: RegistrationRound,
}

Expand Down
6 changes: 3 additions & 3 deletions packages/valory/skills/market_maker_abci/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
BenchmarkTool as BaseBenchmarkTool,
)
from packages.valory.skills.abstract_round_abci.models import Requests as BaseRequests
from packages.valory.skills.abstract_round_abci.models import (
SharedState as BaseSharedState,
)
from packages.valory.skills.market_creation_manager_abci.models import (
MarketCreationManagerParams,
)
Expand All @@ -35,6 +32,9 @@
from packages.valory.skills.market_creation_manager_abci.models import (
RandomnessApi as MarketCreationManagerRandomnessApi,
)
from packages.valory.skills.market_creation_manager_abci.models import (
SharedState as BaseSharedState,
)
from packages.valory.skills.market_creation_manager_abci.rounds import (
Event as MarketCreationManagerEvent,
)
Expand Down
9 changes: 6 additions & 3 deletions packages/valory/skills/market_maker_abci/skill.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ fingerprint:
README.md: bafybeiaa46uuvl2cbxvgdqrqhfh5te6z2iinqktaoz3sycjd6wpar2nfhy
__init__.py: bafybeie36nhohbz4t6hngy42nw7coe2hwrprywo4n6xmwqjkfz7ni3n6sq
behaviours.py: bafybeibzkj5b7zymcz4c2befwyodkyyisvuni742rjtozqxlezktelk6fi
composition.py: bafybeifbikpmoxy7fxamfdo67a3ctp3zuijwprp6motpmijc4c5ecwiasi
composition.py: bafybeify7znoqv7lpd55dupbp55nlilyt2pwew6vynprp55mjj6nvbaqdm
dialogues.py: bafybeicintyylxt4nd5gcufh3rehbxr5jvdn6un7wvaoel7hvj3dmurlpy
fsm_specification.yaml: bafybeiafsjardaq6mudqjmvkm5f3raxt7t26gwpgxy4pseqcuvtzv4gdey
handlers.py: bafybeidlgouig5odju36dkjl5vdqx6mqrukaoyn2chnl5dv2ca6owqlecm
models.py: bafybeie4rqji6a2hmlvk7jamfatwhkc2dd7oj5qd43fk524defc3lowxe4
models.py: bafybeiefhdhm5p6mugsucxxmzbxtuiakb7bp5dofvef7ixrq3cqwivimyu
tests/__init__.py: bafybeig7noiiz2y2b2scjeaalby56ft24eqj4o433blm2oze2pu3lljp44
fingerprint_ignore_patterns: []
connections: []
Expand All @@ -24,7 +24,7 @@ skills:
- valory/abstract_round_abci:0.1.0:bafybeiaqcl7h2famylusiffigwem7tevkcsyocdu5xd42jkmgq6kvowzgq
- valory/registration_abci:0.1.0:bafybeic2ynseiak7jpta7jfwuqwyp453b4p7lolr4wihxmpn633uekv5am
- valory/reset_pause_abci:0.1.0:bafybeidzajbe3erygeh2xbd6lrjv7nsptznjuzrt24ykgvhgotdeyhfnba
- valory/market_creation_manager_abci:0.1.0:bafybeicdnq5rioqb3f7yl6jbvdkv3bmzwdhtrs6rrq5zhuivb7yn2r7qle
- valory/market_creation_manager_abci:0.1.0:bafybeibcaibxcnza7yovemtr2b24dr5fjjyowcj32vymu4ltnz7xut5xt4
- valory/termination_abci:0.1.0:bafybeie4zvjfxvdu7qrulmur3chpjz3kpj5m4bjsxvpk4gvj5zbyyayfaa
- valory/transaction_settlement_abci:0.1.0:bafybeiaefgqbs7zsn5xe5kdwrujj7ivygkn3ujpw6crnvi3knvxw75qmja
behaviours:
Expand Down Expand Up @@ -198,6 +198,9 @@ models:
use_termination: false
use_slashing: false
slash_cooldown_hours: 3
questions_to_close_batch_size: 1
xdai_threshold: 1000000000000000000
close_question_bond: 1000000000000000
slash_threshold_amount: 10000000000000000
light_slash_unit_amount: 5000000000000000
serious_slash_unit_amount: 8000000000000000
Expand Down

0 comments on commit 06967d6

Please sign in to comment.