Skip to content

Commit

Permalink
Merge pull request #337 from valory-xyz/hotfix/liquidity_benchmarking
Browse files Browse the repository at this point in the history
Fixed bug in benchmarking with liquidity amounts
  • Loading branch information
Adamantios authored Oct 23, 2024
2 parents 58621fa + 2dc13ae commit 6df2b61
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 54 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ packages/valory/skills/termination_abci/
packages/valory/contracts/erc20/
packages/valory/contracts/mech/
packages/valory/contracts/gnosis_safe_proxy_factory/
packages/valory/contracts/mech_marketplace/
packages/valory/contracts/multisend/
packages/valory/contracts/service_registry/
packages/valory/contracts/gnosis_safe/
Expand Down
12 changes: 6 additions & 6 deletions packages/packages.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
"contract/valory/staking_token/0.1.0": "bafybeigwsffbzx62d3ikz5kyjsewcvknrrnq3bcqgktnbrcpz4g6lyu3eq",
"contract/valory/relayer/0.1.0": "bafybeicawmds6czx7db2lcktvexwrp245jpekgulndtos5s5zdid3ilvq4",
"skill/valory/market_manager_abci/0.1.0": "bafybeidmipituxl4xva6h3aeorhqstketwxd7ka7z5fog3jprfr6qx2d3i",
"skill/valory/decision_maker_abci/0.1.0": "bafybeihc6vzoyh3ovhmqiwhm4hen3bbybfskut4ef5i3il36kxqlwt66yq",
"skill/valory/trader_abci/0.1.0": "bafybeib2k6pr7c4wic2uj5ebnqvpbuwikrpzormg7agikipefvdybul2om",
"skill/valory/tx_settlement_multiplexer_abci/0.1.0": "bafybeih6uiqec5buugsxh2zi4geadrqlc6l6q5czltzxoexpthxgylc764",
"skill/valory/decision_maker_abci/0.1.0": "bafybeie6ddcrsjyf5lswx62mmul2pw7mmfu6tlvf72b2pvinu5uzufxztu",
"skill/valory/trader_abci/0.1.0": "bafybeiez4yehbfouw4awpujvv7qjaig6jdn5v7i3hjkmrxdgfdar2cp2fq",
"skill/valory/tx_settlement_multiplexer_abci/0.1.0": "bafybeihlstlv6gvdxgastki5hsm3wtbnj5qjrhdcl3mltju2vt37qudgei",
"skill/valory/staking_abci/0.1.0": "bafybeifyqszbiccxwwfldrl6lz5dspuaxbqd5qnniyecusddwvxpfwiyr4",
"skill/valory/check_stop_trading_abci/0.1.0": "bafybeicsrrxjs6bup267jiek3r7eutd42ssfyjthyvxfku6qozw3xebxta",
"agent/valory/trader/0.1.0": "bafybeib26xend6jcwxzedk3qce2ckblluohdyjsqsxpvtgrwmtphhok4r4",
"service/valory/trader/0.1.0": "bafybeifht3nlrj477qsloisydwtr6w6e7vgtz2wgmr5jtudfp6rxos7sra",
"service/valory/trader_pearl/0.1.0": "bafybeidts6y6wytfcwlhnvnacf3vhm6lkiocoe22kkmxxx2onzguez3y3q"
"agent/valory/trader/0.1.0": "bafybeibx6x4sbhxv2o2e3k52nuk3dsscx76vgsl2bovuaflbh7qcb2osay",
"service/valory/trader/0.1.0": "bafybeifhtiogupfjfs2roy24rh3qzvx7yfj7apan3mrbugryz5mznoget4",
"service/valory/trader_pearl/0.1.0": "bafybeicd7egqjr2p5nd6ntzhxa45wugsed4cxleznnuuwom6sgvbd3twvq"
},
"third_party": {
"protocol/open_aea/signing/1.0.0": "bafybeihv62fim3wl2bayavfcg3u5e5cxu3b7brtu4cn5xoxd6lqwachasi",
Expand Down
6 changes: 3 additions & 3 deletions packages/valory/agents/trader/aea-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ skills:
- valory/reset_pause_abci:0.1.0:bafybeigrdlxed3xlsnxtjhnsbl3cojruihxcqx4jxhgivkd5i2fkjncgba
- valory/termination_abci:0.1.0:bafybeib5l7jhew5ic6iq24dd23nidcoimzqkrk556gqywhoziatj33zvwm
- valory/transaction_settlement_abci:0.1.0:bafybeic7q7recyka272udwcupblwbkc3jkodgp74fvcdxb7urametg5dae
- valory/tx_settlement_multiplexer_abci:0.1.0:bafybeih6uiqec5buugsxh2zi4geadrqlc6l6q5czltzxoexpthxgylc764
- valory/tx_settlement_multiplexer_abci:0.1.0:bafybeihlstlv6gvdxgastki5hsm3wtbnj5qjrhdcl3mltju2vt37qudgei
- valory/market_manager_abci:0.1.0:bafybeidmipituxl4xva6h3aeorhqstketwxd7ka7z5fog3jprfr6qx2d3i
- valory/decision_maker_abci:0.1.0:bafybeihc6vzoyh3ovhmqiwhm4hen3bbybfskut4ef5i3il36kxqlwt66yq
- valory/trader_abci:0.1.0:bafybeib2k6pr7c4wic2uj5ebnqvpbuwikrpzormg7agikipefvdybul2om
- valory/decision_maker_abci:0.1.0:bafybeie6ddcrsjyf5lswx62mmul2pw7mmfu6tlvf72b2pvinu5uzufxztu
- valory/trader_abci:0.1.0:bafybeiez4yehbfouw4awpujvv7qjaig6jdn5v7i3hjkmrxdgfdar2cp2fq
- valory/staking_abci:0.1.0:bafybeifyqszbiccxwwfldrl6lz5dspuaxbqd5qnniyecusddwvxpfwiyr4
- valory/check_stop_trading_abci:0.1.0:bafybeicsrrxjs6bup267jiek3r7eutd42ssfyjthyvxfku6qozw3xebxta
- valory/mech_interact_abci:0.1.0:bafybeid6m3i5ofq7vuogqapdnoshhq7mswmudhvfcr2craw25fdwtoe3lm
Expand Down
2 changes: 1 addition & 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:bafybeib26xend6jcwxzedk3qce2ckblluohdyjsqsxpvtgrwmtphhok4r4
agent: valory/trader:0.1.0:bafybeibx6x4sbhxv2o2e3k52nuk3dsscx76vgsl2bovuaflbh7qcb2osay
number_of_agents: 4
deployment:
agent:
Expand Down
2 changes: 1 addition & 1 deletion packages/valory/services/trader_pearl/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license: Apache-2.0
fingerprint:
README.md: bafybeibg7bdqpioh4lmvknw3ygnllfku32oca4eq5pqtvdrdsgw6buko7e
fingerprint_ignore_patterns: []
agent: valory/trader:0.1.0:bafybeib26xend6jcwxzedk3qce2ckblluohdyjsqsxpvtgrwmtphhok4r4
agent: valory/trader:0.1.0:bafybeibx6x4sbhxv2o2e3k52nuk3dsscx76vgsl2bovuaflbh7qcb2osay
number_of_agents: 1
deployment:
agent:
Expand Down
10 changes: 5 additions & 5 deletions packages/valory/skills/decision_maker_abci/behaviours/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ def _write_benchmark_results(
self,
prediction_response: PredictionResponse,
bet_amount: Optional[float] = None,
liquidity_info: LiquidityInfo = INIT_LIQUIDITY_INFO,
liquidity_info: Optional[LiquidityInfo] = INIT_LIQUIDITY_INFO,
) -> None:
"""Write the results to the benchmarking file."""
add_headers = False
Expand Down Expand Up @@ -675,10 +675,10 @@ def _write_benchmark_results(
prediction_response.p_no,
prediction_response.confidence,
bet_amount,
liquidity_info.l0_start,
liquidity_info.l1_start,
liquidity_info.l0_end,
liquidity_info.l1_end,
liquidity_info.l0_start if liquidity_info is not None else 0,
liquidity_info.l1_start if liquidity_info is not None else 0,
liquidity_info.l0_end if liquidity_info is not None else 0,
liquidity_info.l1_end if liquidity_info is not None else 0,
)
results_text = tuple(str(res) for res in results)
row = ",".join(results_text) + NEW_LINE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import json
from copy import deepcopy
from math import prod
from typing import Any, Dict, Generator, Optional, Tuple, Union
from typing import Any, Dict, Generator, List, Optional, Tuple, Union

from packages.valory.skills.decision_maker_abci.behaviours.base import (
DecisionMakerBaseBehaviour,
Expand Down Expand Up @@ -204,13 +204,13 @@ def _get_bet_sample_info(bet: Bet, vote: int) -> Tuple[int, int]:

return selected_type_tokens_in_pool, other_tokens_in_pool

def _calc_binary_shares(
self, bet: Bet, net_bet_amount: int, vote: int
) -> Tuple[int, int]:
"""Calculate the claimed shares. This calculation only works for binary markets."""
# calculate the pool's k (x*y=k)
token_amounts = bet.outcomeTokenAmounts
self.context.logger.info(f"Token amounts: {[x for x in token_amounts]}")
def _compute_new_tokens_distribution(
self,
token_amounts: List[int],
prices: List[float],
net_bet_amount: int,
vote: int,
) -> Tuple[int, int, int, int, int]:
k = prod(token_amounts)
self.context.logger.info(f"k: {k}")

Expand All @@ -219,12 +219,6 @@ def _calc_binary_shares(
bet_per_token = net_bet_amount / BINARY_N_SLOTS
self.context.logger.info(f"Bet per token: {bet_per_token}")

# calculate the number of the traded tokens
prices = bet.outcomeTokenMarginalPrices
self.context.logger.info(f"Prices: {prices}")

if prices is None:
return 0, 0
tokens_traded = [int(bet_per_token / prices[i]) for i in range(BINARY_N_SLOTS)]
self.context.logger.info(f"Tokens traded: {[x for x in tokens_traded]}")

Expand Down Expand Up @@ -268,18 +262,46 @@ def _calc_binary_shares(
available_shares = int(selected_type_tokens_in_pool * price)
self.context.logger.info(f"Available shares: {available_shares}")

return (
selected_type_tokens_in_pool,
other_tokens_in_pool,
other_shares,
num_shares,
available_shares,
)

def _calc_binary_shares(
self, bet: Bet, net_bet_amount: int, vote: int
) -> Tuple[int, int]:
"""Calculate the claimed shares. This calculation only works for binary markets."""
# calculate the pool's k (x*y=k)
token_amounts = bet.outcomeTokenAmounts
self.context.logger.info(f"Token amounts: {[x for x in token_amounts]}")

# calculate the number of the traded tokens
prices = bet.outcomeTokenMarginalPrices
self.context.logger.info(f"Prices: {prices}")

if prices is None:
return 0, 0

_, _, _, num_shares, available_shares = self._compute_new_tokens_distribution(
token_amounts, prices, net_bet_amount, vote
)

return num_shares, available_shares

def _get_mocked_bet(self) -> Bet:
"""Prepare the mocked bet based on the stored liquidity info."""
shared_state = self.shared_state
question_id = shared_state.mock_question_id
benchmarking_mode = self.benchmarking_mode
outcome_token_amounts = shared_state.liquidity_amounts.setdefault(
question_id, benchmarking_mode.outcome_token_amounts
current_liquidity_dictionary = shared_state.liquidity_amounts
outcome_token_amounts = current_liquidity_dictionary.setdefault(
question_id, benchmarking_mode.outcome_token_amounts.copy()
)
outcome_token_marginal_prices = shared_state.liquidity_prices.setdefault(
question_id, benchmarking_mode.outcome_token_marginal_prices
question_id, benchmarking_mode.outcome_token_marginal_prices.copy()
)
return Bet(
id="",
Expand All @@ -296,14 +318,28 @@ def _get_mocked_bet(self) -> Bet:
scaledLiquidityMeasure=10,
)

def _calculate_new_liquidity(self, bet_amount: int, vote: int) -> LiquidityInfo:
def _calculate_new_liquidity(
self, net_bet_amount: int, vote: int
) -> Optional[LiquidityInfo]:
"""Calculate and return the new liquidity information."""
liquidity_amounts = self.shared_state.current_liquidity_amounts
selected_type_tokens_in_pool = liquidity_amounts[vote]
opposite_vote = vote ^ 1
other_tokens_in_pool = liquidity_amounts[opposite_vote]
new_selected = selected_type_tokens_in_pool + bet_amount
new_other = other_tokens_in_pool * selected_type_tokens_in_pool / new_selected
token_amounts = self.shared_state.current_liquidity_amounts
k = prod(token_amounts)
prices = self.shared_state.current_liquidity_prices
if prices is None:
return None

(
selected_type_tokens_in_pool,
other_tokens_in_pool,
other_shares,
_,
_,
) = self._compute_new_tokens_distribution(
token_amounts, prices, net_bet_amount, vote
)

new_other = other_tokens_in_pool + other_shares
new_selected = int(k / new_other)
if vote == 0:
return LiquidityInfo(
selected_type_tokens_in_pool,
Expand All @@ -318,10 +354,28 @@ def _calculate_new_liquidity(self, bet_amount: int, vote: int) -> LiquidityInfo:
new_selected,
)

def _update_liquidity_info(self, bet_amount: int, vote: int) -> LiquidityInfo:
def _update_liquidity_info(
self, net_bet_amount: int, vote: int
) -> Optional[LiquidityInfo]:
"""Update the liquidity information and the prices after placing a bet for a market."""
liquidity_info = self._calculate_new_liquidity(bet_amount, vote)
self.shared_state.current_liquidity_prices = liquidity_info.get_new_prices()
liquidity_info = self._calculate_new_liquidity(net_bet_amount, vote)
if liquidity_info is None:
return None

if liquidity_info.l0_start is None or liquidity_info.l1_start is None:
return None

# to compute the new price we need the previous constants
prices = self.shared_state.current_liquidity_prices

liquidity_constants = [
liquidity_info.l0_start * prices[0],
liquidity_info.l1_start * prices[1],
]

self.shared_state.current_liquidity_prices = liquidity_info.get_new_prices(
liquidity_constants
)
self.shared_state.current_liquidity_amounts = liquidity_info.get_end_liquidity()
return liquidity_info

Expand Down
9 changes: 4 additions & 5 deletions packages/valory/skills/decision_maker_abci/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,11 @@ def validate_end_information(self) -> Tuple[int, int]:
# return the values for type checking purposes (`mypy` would complain that they might be `None` otherwise)
return self.l0_end, self.l1_end

def get_new_prices(self) -> List[float]:
"""Calculate and return the new prices based on the end liquidity."""
def get_new_prices(self, liquidity_constants: List[float]) -> List[float]:
"""Calculate and return the new prices based on the end liquidity and the liquidity constants of the market."""
l0_end, l1_end = self.validate_end_information()
total_end_liquidity = l0_end + l1_end
new_p0 = l0_end / total_end_liquidity
new_p1 = l1_end / total_end_liquidity
new_p0 = liquidity_constants[0] / l0_end
new_p1 = liquidity_constants[1] / l1_end
return [new_p0, new_p1]

def get_end_liquidity(self) -> List[int]:
Expand Down
6 changes: 3 additions & 3 deletions packages/valory/skills/decision_maker_abci/skill.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ fingerprint:
README.md: bafybeia367zzdwndvlhw27rvnwodytjo3ms7gbc3q7mhrrjqjgfasnk47i
__init__.py: bafybeih563ujnigeci2ldzh7hakbau6a222vsed7leg3b7lq32vcn3nm4a
behaviours/__init__.py: bafybeih6ddz2ocvm6x6ytvlbcz6oi4snb5ee5xh5h65nq4w2qf7fd7zfky
behaviours/base.py: bafybeidjmsljdaw7y5chzzkub63mzf5ydpauch5ugojds5jwpeyvovkq3y
behaviours/base.py: bafybeidc4vhgh5pghlv3s4ftgmdcfuvkrs7ri4kxwxkwdtorusunchgtja
behaviours/bet_placement.py: bafybeihmia64t2payxfqcnfdqg675ui2yp3hnyfwb2xhj2hn7wl237b4re
behaviours/blacklisting.py: bafybeicqwj7o4l7qcym5xjqwq45jngqkhyf44mn6qise2ysyfnlnwz7pdy
behaviours/check_benchmarking.py: bafybeiao2lyj7apezkqrpgsyzb3dwvrdgsrgtprf6iuhsmlsufvxfl5bci
behaviours/claim_subscription.py: bafybeigbqkhc6mb73rbwaks32tfiqx6u2xza43uiy6rvbtrnqd6m4fru3e
behaviours/decision_receive.py: bafybeih4bggraucst2vajweswgr34s2yhbo7hnictqorechs53e7amb7wa
behaviours/decision_receive.py: bafybeibip6km45cpw6fntih6zxznjcwbc5otj4hknurvh3mdge5hbffane
behaviours/decision_request.py: bafybeiabjzzcwcfbfmtoftjhewmkgbhxfnigbc5cwmmxl6cob5gv64jwwa
behaviours/handle_failed_tx.py: bafybeidxpc6u575ymct5tdwutvzov6zqfdoio5irgldn3fw7q3lg36mmxm
behaviours/order_subscription.py: bafybeicrmdvhci5prfldvuf3bclbbqi6j7lpv6hmphw3qwgmkmwat3od44
Expand All @@ -32,7 +32,7 @@ fingerprint:
handlers.py: bafybeigod6gbjrxy4mbmulbzsbokeaoycoqys64vqtxnumishfukzf73za
io_/__init__.py: bafybeifxgmmwjqzezzn3e6keh2bfo4cyo7y5dq2ept3stfmgglbrzfl5rq
io_/loader.py: bafybeih3sdsx5dhe4kzhtoafexjgkutsujwqy3zcdrlrkhtdks45bc7exa
models.py: bafybeie6z3vlckwrsindzwdsb2anwcpw6crygydrkjylwrvxkcpwrdc5su
models.py: bafybeiakchouc2lgpdcu62ymspvfxj4ptn5uk4jjittsa255twt5egfioy
payloads.py: bafybeicloiy4ax7dlipwp4czlueflgjgtlev4a6vhn2m7ztoehnemiiko4
policy.py: bafybeihcltocxg7zsmvjtx44ilal4zir4fz2rupgmhacktzv5neb2lmas4
redeem_info.py: bafybeifiiix4gihfo4avraxt34sfw35v6dqq45do2drrssei2shbps63mm
Expand Down
4 changes: 2 additions & 2 deletions packages/valory/skills/trader_abci/skill.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ skills:
- valory/transaction_settlement_abci:0.1.0:bafybeic7q7recyka272udwcupblwbkc3jkodgp74fvcdxb7urametg5dae
- valory/termination_abci:0.1.0:bafybeib5l7jhew5ic6iq24dd23nidcoimzqkrk556gqywhoziatj33zvwm
- valory/market_manager_abci:0.1.0:bafybeidmipituxl4xva6h3aeorhqstketwxd7ka7z5fog3jprfr6qx2d3i
- valory/decision_maker_abci:0.1.0:bafybeihc6vzoyh3ovhmqiwhm4hen3bbybfskut4ef5i3il36kxqlwt66yq
- valory/tx_settlement_multiplexer_abci:0.1.0:bafybeih6uiqec5buugsxh2zi4geadrqlc6l6q5czltzxoexpthxgylc764
- valory/decision_maker_abci:0.1.0:bafybeie6ddcrsjyf5lswx62mmul2pw7mmfu6tlvf72b2pvinu5uzufxztu
- valory/tx_settlement_multiplexer_abci:0.1.0:bafybeihlstlv6gvdxgastki5hsm3wtbnj5qjrhdcl3mltju2vt37qudgei
- valory/staking_abci:0.1.0:bafybeifyqszbiccxwwfldrl6lz5dspuaxbqd5qnniyecusddwvxpfwiyr4
- valory/check_stop_trading_abci:0.1.0:bafybeicsrrxjs6bup267jiek3r7eutd42ssfyjthyvxfku6qozw3xebxta
- valory/mech_interact_abci:0.1.0:bafybeid6m3i5ofq7vuogqapdnoshhq7mswmudhvfcr2craw25fdwtoe3lm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protocols:
- valory/ledger_api:1.0.0:bafybeihdk6psr4guxmbcrc26jr2cbgzpd5aljkqvpwo64bvaz7tdti2oni
skills:
- valory/abstract_round_abci:0.1.0:bafybeib733xfbndtpvkf44mtk7oyodnficgloo6xhn7xmqxxeos33es65u
- valory/decision_maker_abci:0.1.0:bafybeihc6vzoyh3ovhmqiwhm4hen3bbybfskut4ef5i3il36kxqlwt66yq
- valory/decision_maker_abci:0.1.0:bafybeie6ddcrsjyf5lswx62mmul2pw7mmfu6tlvf72b2pvinu5uzufxztu
- valory/staking_abci:0.1.0:bafybeifyqszbiccxwwfldrl6lz5dspuaxbqd5qnniyecusddwvxpfwiyr4
- valory/mech_interact_abci:0.1.0:bafybeid6m3i5ofq7vuogqapdnoshhq7mswmudhvfcr2craw25fdwtoe3lm
behaviours:
Expand Down

0 comments on commit 6df2b61

Please sign in to comment.