Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store redeeming progress #121

Merged
merged 16 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions packages/packages.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"dev": {
"skill/valory/market_manager_abci/0.1.0": "bafybeidropskas24c4lcekzdbwoflhnzaglmeqxvpyuoa7cf7q2rf2yex4",
"skill/valory/decision_maker_abci/0.1.0": "bafybeihcwzgpl5cgxmbi2ttqum5khnryxnp3cdrxm64q6dgerjr7weqk2m",
"skill/valory/trader_abci/0.1.0": "bafybeic7ztyme3wlzdykvnnpxvaxxpy4byqjxt6uunoalbz7etzo6uhfza",
"skill/valory/decision_maker_abci/0.1.0": "bafybeigwmf5lrwsq4rvwmcmvdejbwcwb6zkndx5tcpljf4vvvb7fvz7nwe",
"skill/valory/trader_abci/0.1.0": "bafybeidw6qlawxhfjgvoul2lmgxx6c5hx6ehkxa6isy5fjtif4akkrskta",
"contract/valory/market_maker/0.1.0": "bafybeif4mm2s3gxtvp227yypkcnna5ftec7vajcftvtbdmqddh7nprah5m",
"agent/valory/trader/0.1.0": "bafybeid2e66zg5gx33xskdxmfcw7vtluvy4bwnoda3ouchu6rsmhtlkgsi",
"service/valory/trader/0.1.0": "bafybeicdmgkvyefq4fn4amoqhtfi4h5bgbidwt57fwerlpziaon3rsvmzi",
"agent/valory/trader/0.1.0": "bafybeibq5he4r5okaleh7ysnoeusbmaxu7obcxiwb4obaoks6s3ai6jaea",
"service/valory/trader/0.1.0": "bafybeifgp6ml5xsjzyeehi6737fiulcupkbyjimlyfmnfg6s6nsrljol64",
"contract/valory/erc20/0.1.0": "bafybeibysifjwhlcn7nntsjkowdmja53ed6urfzypi6sxfc7chtheecvpi",
"skill/valory/tx_settlement_multiplexer_abci/0.1.0": "bafybeif22r6jg3lptvm2jwyxpozr4wdk63j52rpnmkq7fepst4s4dt4auq",
"skill/valory/tx_settlement_multiplexer_abci/0.1.0": "bafybeica2fsr66v7q4emhfrjsy5olplnb6asty6y4wzq76jgsvjqjii7fe",
"contract/valory/mech/0.1.0": "bafybeifaavuqr2bz7nypapjwkufc5c6vwxm5ifucmtd3w7sh42tne35nga",
"contract/valory/realitio/0.1.0": "bafybeic3wihpzz4deguslpzlnsgrlrabclq3njwuagyaiiv6rzqujfrs6e",
"contract/valory/realitio/0.1.0": "bafybeid6kh4tiqswpeufkr7eowmq7seoyhkssnedgzw6pe4h7wswui6dlm",
"contract/valory/realitio_proxy/0.1.0": "bafybeidx37xzjjmapwacedgzhum6grfzhp5vhouz4zu3pvpgdy5pgb2fr4",
"contract/valory/conditional_tokens/0.1.0": "bafybeicbkivacygycd26mqwvsp4c7velo2lpbxzwmrsgxg65prnvri6yoi",
"contract/valory/conditional_tokens/0.1.0": "bafybeifov35k2ifwagzyfvl7km6je6tmyvcqgrccldob3zmbx2af2wzque",
"contract/valory/agent_registry/0.1.0": "bafybeifwdtwxdc2jdlhzdyxctqdmoz6zroxf5o4nhuok5l4luvnofqavty"
},
"third_party": {
Expand Down
12 changes: 7 additions & 5 deletions packages/valory/agents/trader/aea-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ contracts:
- valory/erc20:0.1.0:bafybeibysifjwhlcn7nntsjkowdmja53ed6urfzypi6sxfc7chtheecvpi
- valory/multisend:0.1.0:bafybeig5byt5urg2d2bsecufxe5ql7f4mezg3mekfleeh32nmuusx66p4y
- valory/mech:0.1.0:bafybeifaavuqr2bz7nypapjwkufc5c6vwxm5ifucmtd3w7sh42tne35nga
- valory/conditional_tokens:0.1.0:bafybeicbkivacygycd26mqwvsp4c7velo2lpbxzwmrsgxg65prnvri6yoi
- valory/realitio:0.1.0:bafybeic3wihpzz4deguslpzlnsgrlrabclq3njwuagyaiiv6rzqujfrs6e
- valory/conditional_tokens:0.1.0:bafybeifov35k2ifwagzyfvl7km6je6tmyvcqgrccldob3zmbx2af2wzque
- valory/realitio:0.1.0:bafybeid6kh4tiqswpeufkr7eowmq7seoyhkssnedgzw6pe4h7wswui6dlm
- valory/realitio_proxy:0.1.0:bafybeidx37xzjjmapwacedgzhum6grfzhp5vhouz4zu3pvpgdy5pgb2fr4
- valory/agent_registry:0.1.0:bafybeifwdtwxdc2jdlhzdyxctqdmoz6zroxf5o4nhuok5l4luvnofqavty
protocols:
- open_aea/signing:1.0.0:bafybeie7xyems76v5b4wc2lmaidcujizpxfzjnnwdeokmhje53g7ym25ii
- valory/abci:0.1.0:bafybeihmzlmmb4pdo3zkhg6ehuyaa4lhw7bfpclln2o2z7v3o6fcep26iu
Expand All @@ -41,10 +42,10 @@ skills:
- valory/reset_pause_abci:0.1.0:bafybeicpxn2khtaesuf4cq6ypwdmdmonlqroj2q2i6cxvpizc2y4cw66pe
- valory/termination_abci:0.1.0:bafybeieqm46zuccaagnko3qlw6p3nvoohdrfgvpmw467r5lyil2dqrzjsy
- valory/transaction_settlement_abci:0.1.0:bafybeia6cdxdlqrcwk2maw25fo7dafzd2p3rs7syropvufophk2pitzbwy
- valory/tx_settlement_multiplexer_abci:0.1.0:bafybeif22r6jg3lptvm2jwyxpozr4wdk63j52rpnmkq7fepst4s4dt4auq
- valory/tx_settlement_multiplexer_abci:0.1.0:bafybeica2fsr66v7q4emhfrjsy5olplnb6asty6y4wzq76jgsvjqjii7fe
- valory/market_manager_abci:0.1.0:bafybeidropskas24c4lcekzdbwoflhnzaglmeqxvpyuoa7cf7q2rf2yex4
- valory/decision_maker_abci:0.1.0:bafybeihcwzgpl5cgxmbi2ttqum5khnryxnp3cdrxm64q6dgerjr7weqk2m
- valory/trader_abci:0.1.0:bafybeic7ztyme3wlzdykvnnpxvaxxpy4byqjxt6uunoalbz7etzo6uhfza
- valory/decision_maker_abci:0.1.0:bafybeigwmf5lrwsq4rvwmcmvdejbwcwb6zkndx5tcpljf4vvvb7fvz7nwe
- valory/trader_abci:0.1.0:bafybeidw6qlawxhfjgvoul2lmgxx6c5hx6ehkxa6isy5fjtif4akkrskta
default_ledger: ethereum
required_ledgers:
- ethereum
Expand Down Expand Up @@ -184,6 +185,7 @@ models:
conditional_tokens_address: ${str:0xCeAfDD6bc0bEF976fdCd1112955828E00543c0Ce}
realitio_proxy_address: ${str:0xAB16D643bA051C11962DA645f74632d3130c81E2}
realitio_address: ${str:0x79e32aE03fb27B07C89c0c568F80287C01ca2E57}
event_filtering_batch_size: ${int:5000}
redeeming_batch_size: ${int:5}
slippage: ${float:0.01}
policy_epsilon: ${float:0.1}
Expand Down
49 changes: 14 additions & 35 deletions packages/valory/contracts/conditional_tokens/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

"""This module contains the conditional tokens contract definition."""

from typing import List, Dict
from typing import List

from requests.exceptions import ReadTimeout as RequestsReadTimeoutError
from urllib3.exceptions import ReadTimeoutError as Urllib3ReadTimeoutError
Expand All @@ -28,11 +28,6 @@
from aea.contracts.base import Contract
from aea.crypto.base import LedgerApi
from hexbytes import HexBytes
from web3.types import BlockIdentifier


DEFAULT_FROM_BLOCK = "earliest"
DEFAULT_TO_BLOCK = "latest"


class ConditionalTokensContract(Contract):
Expand All @@ -46,51 +41,35 @@ def check_redeemed(
ledger_api: LedgerApi,
contract_address: str,
redeemer: str,
from_block: int,
to_block: int,
collateral_tokens: List[str],
parent_collection_ids: List[bytes],
condition_ids: List[HexBytes],
index_sets: List[List[int]],
from_block_numbers: Dict[HexBytes, BlockIdentifier],
chunk_size: int = 5_000,
) -> JSONLike:
"""Filter to find out whether a position has already been redeemed."""
earliest_block = DEFAULT_FROM_BLOCK

for condition_id, from_block in from_block_numbers.items():
if (
isinstance(earliest_block, int)
and earliest_block > from_block
or earliest_block == DEFAULT_FROM_BLOCK
):
earliest_block = from_block

contract_instance = cls.get_instance(ledger_api, contract_address)
to_checksum = ledger_api.api.to_checksum_address
redeemer_checksummed = to_checksum(redeemer)
collateral_tokens_checksummed = [
to_checksum(token) for token in collateral_tokens
]
latest_block = ledger_api.api.eth.block_number
try:
redeemed = []
for from_block in range(earliest_block, latest_block, chunk_size):
to_block = min(from_block + chunk_size, latest_block)
payout_filter = contract_instance.events.PayoutRedemption.build_filter()
payout_filter.args.redeemer.match_single(redeemer_checksummed)
payout_filter.args.collateralToken.match_any(*collateral_tokens_checksummed)
payout_filter.args.parentCollectionId.match_any(*parent_collection_ids)
payout_filter.args.conditionId.match_any(*condition_ids)
payout_filter.args.indexSets.match_any(*index_sets)
payout_filter.fromBlock = from_block
payout_filter.toBlock = to_block
redeemed_chunk = list(payout_filter.deploy(ledger_api.api).get_all_entries())
redeemed.extend(redeemed_chunk)
payout_filter = contract_instance.events.PayoutRedemption.build_filter()
payout_filter.args.redeemer.match_single(redeemer_checksummed)
payout_filter.args.collateralToken.match_any(*collateral_tokens_checksummed)
payout_filter.args.parentCollectionId.match_any(*parent_collection_ids)
payout_filter.args.conditionId.match_any(*condition_ids)
payout_filter.args.indexSets.match_any(*index_sets)
payout_filter.fromBlock = from_block
payout_filter.toBlock = to_block
redeemed = list(payout_filter.deploy(ledger_api.api).get_all_entries())
except (Urllib3ReadTimeoutError, RequestsReadTimeoutError):
msg = (
"The RPC timed out! This usually happens if the filtering is too wide. "
f"The service tried to filter from block {earliest_block} to latest, with a step {chunk_size},"
f"as the earliest market creation transaction took place at block {earliest_block}. "
f"If this issue persists, please try lowering the step size!"
f"The service tried to filter from block {from_block} to {to_block}."
f"If this issue persists, please try lowering the `EVENT_FILTERING_BATCH_SIZE`!"
)
return dict(error=msg)

Expand Down
2 changes: 1 addition & 1 deletion packages/valory/contracts/conditional_tokens/contract.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ aea_version: '>=1.0.0, <2.0.0'
fingerprint:
__init__.py: bafybeidhdxio3oq5gqdnxmngumvt3fcd6zyiyrpk5f2k4dwhflbg4e5iky
build/ConditionalTokens.json: bafybeia2ahis7zx2yhhf23kpkcxu56hto6fwg6ptjg5ld46lp4dgz7cz3e
contract.py: bafybeie73lgxfuhqhksqqm6mthwqyw3rau2h2eul6mp7vyswpv37vgyz2a
contract.py: bafybeigzlqbxka2fxjt2ho26ppryvf2lcxecorwtcmfybu4bhqnmpfgqri
fingerprint_ignore_patterns: []
class_name: ConditionalTokensContract
contract_interface_paths:
Expand Down
84 changes: 27 additions & 57 deletions packages/valory/contracts/realitio/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,19 @@

"""This module contains the Realitio_v2_1 contract definition."""

from typing import List, Tuple, Union
from typing import List, Tuple, Union, Dict

from requests.exceptions import ReadTimeout as RequestsReadTimeoutError
from urllib3.exceptions import ReadTimeoutError as Urllib3ReadTimeoutError
from aea.common import JSONLike
from aea.configurations.base import PublicId
from aea.contracts.base import Contract
from aea.crypto.base import LedgerApi
from eth_typing import ChecksumAddress
from web3.constants import HASH_ZERO
from web3.types import BlockIdentifier
from requests.exceptions import ReadTimeout as RequestsReadTimeoutError
from urllib3.exceptions import ReadTimeoutError as Urllib3ReadTimeoutError

ZERO_HEX = HASH_ZERO[2:]
ZERO_BYTES = bytes.fromhex(ZERO_HEX)
ClaimParamsType = Tuple[
List[bytes], List[ChecksumAddress], List[int], List[bytes]
]


class RealitioContract(Contract):
Expand All @@ -53,82 +52,53 @@ def check_finalized(
return dict(finalized=is_finalized)

@classmethod
def _get_claim_params(
def get_claim_params(
cls,
ledger_api: LedgerApi,
contract_address: str,
from_block: BlockIdentifier,
from_block: int,
to_block: int,
question_id: bytes,
chunk_size: int = 5_000,
) -> Union[str, Tuple[bytes, List[bytes], List[ChecksumAddress], List[int], List[bytes]]]:
) -> Dict[str, Union[str, list]]:
"""Filters the `LogNewAnswer` event by question id to calculate the history hashes."""
contract_instance = cls.get_instance(ledger_api, contract_address)
to_block = ledger_api.api.eth.block_number

try:
answered = []
for chunk in range(from_block, to_block, chunk_size):
answer_filter = contract_instance.events.LogNewAnswer.build_filter()
answer_filter.fromBlock = chunk
answer_filter.toBlock = min(chunk + chunk_size, to_block)
answer_filter.args.question_id.match_single(question_id)
answered.extend(list(answer_filter.deploy(ledger_api.api).get_all_entries()))
answer_filter = contract_instance.events.LogNewAnswer.build_filter()
answer_filter.fromBlock = from_block
answer_filter.toBlock = to_block
answer_filter.args.question_id.match_single(question_id)
answered = list(answer_filter.deploy(ledger_api.api).get_all_entries())
except (Urllib3ReadTimeoutError, RequestsReadTimeoutError):
msg = (
"The RPC timed out! This usually happens if the filtering is too wide. "
f"The service tried to filter from block {from_block} to latest, "
"as the market was created at this time. Did the market get created too long in the past?\n"
"Please consider manually redeeming for the market with question id "
f"{question_id!r} if this issue persists."
f"The service tried to filter from block {from_block} to {to_block}."
f"If this issue persists, please try lowering the `EVENT_FILTERING_BATCH_SIZE`!"
)
return msg
else:
n_answered = len(answered)

if n_answered == 0:
msg = f"No answers have been given for question with id {question_id.hex()}!"
return msg
return dict(error=msg)

history_hashes = []
addresses = []
bonds = []
answers = []
for i, answer in enumerate(reversed(answered)):
# history_hashes second-last-to-first, the hash of each history entry, calculated as described here:
# https://realitio.github.io/docs/html/contract_explanation.html#answer-history-entries.
if i == n_answered - 1:
history_hashes.append(ZERO_BYTES)
else:
history_hashes.append(answered[i + 1]["args"]["history_hash"])

# last-to-first, the address of each answerer or commitment sender
addresses.append(answer["args"]["user"])
# last-to-first, the bond supplied with each answer or commitment
bonds.append(answer["args"]["bond"])
# last-to-first, each answer supplied, or commitment ID if the answer was supplied with commit->reveal
answers.append(answer["args"]["answer"])
if len(answered) == 0:
msg = (
f"No answers have been given for question with id {question_id.hex()} "
f"between blocks {from_block} and {to_block}."
)
return dict(info=msg)

return question_id, history_hashes, addresses, bonds, answers
return dict(answered=answered)

@classmethod
def build_claim_winnings(
cls,
ledger_api: LedgerApi,
contract_address: str,
from_block: BlockIdentifier,
question_id: bytes,
claim_params: ClaimParamsType,
) -> JSONLike:
"""Build `claimWinnings` transaction."""
contract = cls.get_instance(ledger_api, contract_address)
claim_params = cls._get_claim_params(
ledger_api, contract_address, from_block, question_id
)
if isinstance(claim_params, str):
return dict(error=claim_params)

data = contract.encodeABI(
fn_name="claimWinnings",
args=claim_params,
args=(question_id, *claim_params),
)
return dict(data=data)

Expand Down
2 changes: 1 addition & 1 deletion packages/valory/contracts/realitio/contract.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ aea_version: '>=1.0.0, <2.0.0'
fingerprint:
__init__.py: bafybeictahkgfmlqv5kksvj6klmxtmjdpeq4sp3x7dp2yr5x4kmzbcihse
build/Realitio.json: bafybeiagi7zoeoy5s7duhg4oeuekj2s6z5mad2z6g2pn3n5elsvze25qiu
contract.py: bafybeibkmmgybvqu7tf6angouhrqmqzf6cu5piwu7jt5l4ddlren7fhkdy
contract.py: bafybeiawjv5qeouf6wac5adqzeudl5loaya3mszhc65whb22shsvivjwxm
fingerprint_ignore_patterns: []
class_name: RealitioContract
contract_interface_paths:
Expand Down
6 changes: 5 additions & 1 deletion packages/valory/services/trader/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: bafybeigtuothskwyvrhfosps2bu6suauycolj67dpuxqvnicdrdu7yhtvq
fingerprint_ignore_patterns: []
agent: valory/trader:0.1.0:bafybeid2e66zg5gx33xskdxmfcw7vtluvy4bwnoda3ouchu6rsmhtlkgsi
agent: valory/trader:0.1.0:bafybeibq5he4r5okaleh7ysnoeusbmaxu7obcxiwb4obaoks6s3ai6jaea
number_of_agents: 4
deployment: {}
---
Expand Down Expand Up @@ -101,6 +101,7 @@ type: skill
conditional_tokens_address: ${CONDITIONAL_TOKENS_ADDRESS:str:0xCeAfDD6bc0bEF976fdCd1112955828E00543c0Ce}
realitio_proxy_address: ${REALITIO_PROXY_ADDRESS:str:0xAB16D643bA051C11962DA645f74632d3130c81E2}
realitio_address: ${REALITIO_ADDRESS:str:0x79e32aE03fb27B07C89c0c568F80287C01ca2E57}
event_filtering_batch_size: ${EVENT_FILTERING_BATCH_SIZE:int:5000}
redeeming_batch_size: ${REDEEMING_BATCH_SIZE:int:1}
slippage: ${SLIPPAGE:float:0.01}
policy_epsilon: ${POLICY_EPSILON:float:0.1}
Expand Down Expand Up @@ -170,6 +171,7 @@ type: skill
conditional_tokens_address: ${CONDITIONAL_TOKENS_ADDRESS:str:0xCeAfDD6bc0bEF976fdCd1112955828E00543c0Ce}
realitio_proxy_address: ${REALITIO_PROXY_ADDRESS:str:0xAB16D643bA051C11962DA645f74632d3130c81E2}
realitio_address: ${REALITIO_ADDRESS:str:0x79e32aE03fb27B07C89c0c568F80287C01ca2E57}
event_filtering_batch_size: ${EVENT_FILTERING_BATCH_SIZE:int:5000}
redeeming_batch_size: ${REDEEMING_BATCH_SIZE:int:1}
slippage: ${SLIPPAGE:float:0.01}
policy_epsilon: ${POLICY_EPSILON:float:0.1}
Expand Down Expand Up @@ -237,6 +239,7 @@ type: skill
conditional_tokens_address: ${CONDITIONAL_TOKENS_ADDRESS:str:0xCeAfDD6bc0bEF976fdCd1112955828E00543c0Ce}
realitio_proxy_address: ${REALITIO_PROXY_ADDRESS:str:0xAB16D643bA051C11962DA645f74632d3130c81E2}
realitio_address: ${REALITIO_ADDRESS:str:0x79e32aE03fb27B07C89c0c568F80287C01ca2E57}
event_filtering_batch_size: ${EVENT_FILTERING_BATCH_SIZE:int:5000}
redeeming_batch_size: ${REDEEMING_BATCH_SIZE:int:1}
slippage: ${SLIPPAGE:float:0.01}
policy_epsilon: ${POLICY_EPSILON:float:0.1}
Expand Down Expand Up @@ -304,6 +307,7 @@ type: skill
conditional_tokens_address: ${CONDITIONAL_TOKENS_ADDRESS:str:0xCeAfDD6bc0bEF976fdCd1112955828E00543c0Ce}
realitio_proxy_address: ${REALITIO_PROXY_ADDRESS:str:0xAB16D643bA051C11962DA645f74632d3130c81E2}
realitio_address: ${REALITIO_ADDRESS:str:0x79e32aE03fb27B07C89c0c568F80287C01ca2E57}
event_filtering_batch_size: ${EVENT_FILTERING_BATCH_SIZE:int:5000}
redeeming_batch_size: ${REDEEMING_BATCH_SIZE:int:1}
slippage: ${SLIPPAGE:float:0.01}
policy_epsilon: ${POLICY_EPSILON:float:0.1}
Expand Down
6 changes: 6 additions & 0 deletions packages/valory/skills/decision_maker_abci/behaviours/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
from packages.valory.skills.decision_maker_abci.models import (
DecisionMakerParams,
MultisendBatch,
SharedState,
)
from packages.valory.skills.decision_maker_abci.policy import EGreedyPolicy
from packages.valory.skills.decision_maker_abci.states.base import SynchronizedData
Expand Down Expand Up @@ -88,6 +89,11 @@ def params(self) -> DecisionMakerParams:
"""Return the params."""
return cast(DecisionMakerParams, 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."""
Expand Down
Loading