Skip to content

Commit

Permalink
Merge pull request #116 from valory-xyz/feat/remove-redeeming-limit
Browse files Browse the repository at this point in the history
Remove redeeming limit
  • Loading branch information
Adamantios authored Nov 1, 2023
2 parents 48aefd3 + 6de1a29 commit ab76053
Show file tree
Hide file tree
Showing 18 changed files with 345 additions and 221 deletions.
16 changes: 8 additions & 8 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": "bafybeigmy3g2klkzdhttti2ouxf575ua5y7ajmh4a3yfpukeukqxjky47m",
"skill/valory/decision_maker_abci/0.1.0": "bafybeidzc26z7vudomj3gh7jyiain3erca2nejxxhl5v37rp22zfj5xwki",
"skill/valory/trader_abci/0.1.0": "bafybeieqfrs4qsazroxjeysbisvbdipnedkgt6ahgatq42rwnvadv74xku",
"skill/valory/market_manager_abci/0.1.0": "bafybeidropskas24c4lcekzdbwoflhnzaglmeqxvpyuoa7cf7q2rf2yex4",
"skill/valory/decision_maker_abci/0.1.0": "bafybeia6qyuwmuhadhqls4vzk4iesipybit43fmdgm6qh4drnzd3vulcqm",
"skill/valory/trader_abci/0.1.0": "bafybeigv4bvygaxcazvwn6yll25oh6gnriertx6ydzvya5a4rl7el3lutu",
"contract/valory/market_maker/0.1.0": "bafybeif4mm2s3gxtvp227yypkcnna5ftec7vajcftvtbdmqddh7nprah5m",
"agent/valory/trader/0.1.0": "bafybeie2li4kyjchnbgdkntwhkfhvwllzp5h6kp5hsqzkumfykfhthbqui",
"service/valory/trader/0.1.0": "bafybeiakeiq4d7afhiaky2brwyum4i74e67ytvypcvlr4ij77f2teh7rwm",
"agent/valory/trader/0.1.0": "bafybeibo3hlq3spwgnnclhe4tetcztyz5zsn2fk5mpbaj6wajdedoeb4sy",
"service/valory/trader/0.1.0": "bafybeiaybavgtl7qscnnini5ny43ykh4cwdb5gfepoj6nv5k5zknaujvby",
"contract/valory/erc20/0.1.0": "bafybeieqj7dea4tcv6z2yqkgtmhd23vbiycsr5trhwxdvlbwulpl6vhmam",
"skill/valory/tx_settlement_multiplexer_abci/0.1.0": "bafybeicoe4vpnminp2nwuyxbwcs2mnantvsgxrjfd7ogbgjkumuozotndy",
"skill/valory/tx_settlement_multiplexer_abci/0.1.0": "bafybeif42r2cks5fqdwuuf6u3tdn4drugnyepd4kng6il6scejegi6h6sq",
"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": "bafybeif5jwc6owfe6brmqq5sfnhrwo2ajdtt4glrmyhlqxcvtkyofahwsa",
"contract/valory/conditional_tokens/0.1.0": "bafybeifov35k2ifwagzyfvl7km6je6tmyvcqgrccldob3zmbx2af2wzque",
"contract/valory/agent_registry/0.1.0": "bafybeifwdtwxdc2jdlhzdyxctqdmoz6zroxf5o4nhuok5l4luvnofqavty",
"contract/valory/service_staking_token/0.1.0": "bafybeibifxmoeaiptovouispl5jdgm4lduzcfgsx723zlw45cfpllecuwy"
},
Expand Down
15 changes: 8 additions & 7 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:bafybeieqj7dea4tcv6z2yqkgtmhd23vbiycsr5trhwxdvlbwulpl6vhmam
- valory/multisend:0.1.0:bafybeig5byt5urg2d2bsecufxe5ql7f4mezg3mekfleeh32nmuusx66p4y
- valory/mech:0.1.0:bafybeifaavuqr2bz7nypapjwkufc5c6vwxm5ifucmtd3w7sh42tne35nga
- valory/conditional_tokens:0.1.0:bafybeif5jwc6owfe6brmqq5sfnhrwo2ajdtt4glrmyhlqxcvtkyofahwsa
- 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:bafybeicoe4vpnminp2nwuyxbwcs2mnantvsgxrjfd7ogbgjkumuozotndy
- valory/market_manager_abci:0.1.0:bafybeigmy3g2klkzdhttti2ouxf575ua5y7ajmh4a3yfpukeukqxjky47m
- valory/decision_maker_abci:0.1.0:bafybeidzc26z7vudomj3gh7jyiain3erca2nejxxhl5v37rp22zfj5xwki
- valory/trader_abci:0.1.0:bafybeieqfrs4qsazroxjeysbisvbdipnedkgt6ahgatq42rwnvadv74xku
- valory/tx_settlement_multiplexer_abci:0.1.0:bafybeif42r2cks5fqdwuuf6u3tdn4drugnyepd4kng6il6scejegi6h6sq
- valory/market_manager_abci:0.1.0:bafybeidropskas24c4lcekzdbwoflhnzaglmeqxvpyuoa7cf7q2rf2yex4
- valory/decision_maker_abci:0.1.0:bafybeia6qyuwmuhadhqls4vzk4iesipybit43fmdgm6qh4drnzd3vulcqm
- valory/trader_abci:0.1.0:bafybeigv4bvygaxcazvwn6yll25oh6gnriertx6ydzvya5a4rl7el3lutu
default_ledger: ethereum
required_ledgers:
- ethereum
Expand Down Expand Up @@ -184,9 +185,9 @@ 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}
redeem_margin_days: ${int:15}
policy_epsilon: ${float:0.1}
policy_store_path: ${str:/data/}
irrelevant_tools: ${list:["openai-text-davinci-002", "openai-text-davinci-003",
Expand Down
55 changes: 14 additions & 41 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,57 +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
earliest_condition_id = HexBytes("")

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
earliest_condition_id = condition_id

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, "
f"as the earliest market creation transaction took place at block {earliest_block}. "
f"Did the creation happen too long in the past?\n"
f"The market with condition id {earliest_condition_id!r} "
f"is the oldest one and the block filtering was set based on it."
"If the issue persists, try to decrease the value of `redeem_margin_days` "
f"in the agent's configuration, based on the date corresponding to the block number {earliest_block}."
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: bafybeigzkxofon5npsqksdnfxeq4azsbeost2mgqqdlyvyvaom7wtsf7bi
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
10 changes: 5 additions & 5 deletions 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:bafybeie2li4kyjchnbgdkntwhkfhvwllzp5h6kp5hsqzkumfykfhthbqui
agent: valory/trader:0.1.0:bafybeibo3hlq3spwgnnclhe4tetcztyz5zsn2fk5mpbaj6wajdedoeb4sy
number_of_agents: 4
deployment: {}
---
Expand Down Expand Up @@ -101,9 +101,9 @@ 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}
redeem_margin_days: ${REDEEM_MARGIN_DAYS:int:15}
policy_epsilon: ${POLICY_EPSILON:float:0.1}
policy_store_path: ${POLICY_STORE_PATH:str:/data/}
irrelevant_tools: ${IRRELEVANT_TOOLS:list:["openai-text-davinci-002", "openai-text-davinci-003",
Expand Down Expand Up @@ -171,9 +171,9 @@ 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}
redeem_margin_days: ${REDEEM_MARGIN_DAYS:int:15}
policy_epsilon: ${POLICY_EPSILON:float:0.1}
policy_store_path: ${POLICY_STORE_PATH:str:/data/}
irrelevant_tools: ${IRRELEVANT_TOOLS:list:["openai-text-davinci-002", "openai-text-davinci-003",
Expand Down Expand Up @@ -239,9 +239,9 @@ 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}
redeem_margin_days: ${REDEEM_MARGIN_DAYS:int:15}
policy_epsilon: ${POLICY_EPSILON:float:0.1}
policy_store_path: ${POLICY_STORE_PATH:str:/data/}
irrelevant_tools: ${IRRELEVANT_TOOLS:list:["openai-text-davinci-002", "openai-text-davinci-003",
Expand Down Expand Up @@ -307,9 +307,9 @@ 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}
redeem_margin_days: ${REDEEM_MARGIN_DAYS:int:15}
policy_epsilon: ${POLICY_EPSILON:float:0.1}
policy_store_path: ${POLICY_STORE_PATH:str:/data/}
irrelevant_tools: ${IRRELEVANT_TOOLS:list:["openai-text-davinci-002", "openai-text-davinci-003",
Expand Down
Loading

0 comments on commit ab76053

Please sign in to comment.