valory-xyz / trader

Trader agent for AI prediction markets on Gnosis
Apache License 2.0
32 stars 16 forks source link

Bug when claiming bonds in the case of multiple answers on reality #310

Open kongzii opened 3 months ago

kongzii commented 3 months ago

According to Realitio docs, only the latest history hash is stored in the question struct:

Empty  Bytes32 + Answer + Bond + Answerer + False -> History Hash 1
History Hash 1 + Answer + Bond + Answerer + False -> History Hash 2
History Hash 2 + Answer + Bond + Answerer + False -> History Hash 3 <- stored in Questions struct

If multiple answers were submitted, and we query them on subgraph, we can see it:

Screenshot by Dropbox Capture

All the returned history hashes are the same. (it's this question)

And that's why the following code https://github.com/kongzii/trader/blob/main/packages/valory/skills/market_manager_abci/graph_tooling/requests.py#L297:

answers = [
            {
                "args": {
                    "answer": bytes.fromhex(answer["answer"][2:]),
                    "question_id": bytes.fromhex(answer["question"]["questionId"][2:]),
                    "history_hash": bytes.fromhex(
                        answer["question"]["historyHash"][2:]
                    ),
                    "user": Web3.to_checksum_address(answer["question"]["user"]),
                    "bond": int(answer["bondAggregate"]),
                    "timestamp": int(answer["timestamp"]),
                    "is_commitment": False,
                }
            }
            for answer in raw_answers
]

submits invalid hashes if there are multiple different answers and claiming of bonds fails.

gabrielfior commented 3 months ago

Here's what I know -> Subgraph only serves the latest history hash (not all previous history hashes) -> If there's a second answer, what happens? a. Second answer is correct - market creator will try to claim bond, unsuccessful (OK, as its answer was wrong) b. Second answer is incorrect - market creator will try to claim bond, unsucessful (NOT OK, because its answer was correct and claim should have been possible, but history hash will not match the one from the answer) In conclusion, fetching history_hash from the subgraph is not working for this. My understanding is that responses should be queried, since they contain the different history_hash corresponding to each new answer.

kongzii commented 2 months ago

We fixed it in our repo at https://github.com/gnosis/prediction-market-agent-tooling/pull/368