Skip to content

Commit

Permalink
fix: formatters
Browse files Browse the repository at this point in the history
  • Loading branch information
Divya-Solulab committed Sep 27, 2024
1 parent 7a30d33 commit c3e9ca2
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 62 deletions.
8 changes: 4 additions & 4 deletions packages/packages.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
"contract/valory/merkl_distributor/0.1.0": "bafybeihaqsvmncuzmwv2r6iuzc5t7ur6ugdhephz7ydftypksjidpsylbq",
"contract/valory/staking_token/0.1.0": "bafybeifrvtkofw5c26b3irm6izqfdpik6vpjhm6hqwcdzx333h6vhdanai",
"contract/valory/staking_activity_checker/0.1.0": "bafybeibjzsi2r5b6xd4iwl4wbwldptnynryzsdpifym4mkv32ynswx22ou",
"skill/valory/liquidity_trader_abci/0.1.0": "bafybeig5brc3h4mbquq6jvc6ygzydcuwk5aefxmgskytkzz6gxp2ukeigi",
"skill/valory/optimus_abci/0.1.0": "bafybeibrnhpcr6l4p5fmxspqkqkxg2pyxmbiwbwxb44qweucz73mmdwb34",
"agent/valory/optimus/0.1.0": "bafybeib3sapx5oxymxgcdkshct2qxwns3njl5zimo7swqrfgz6su6rxch4",
"service/valory/optimus/0.1.0": "bafybeidldvltwmftt6vl26nvqiolsv5pawgtyairiha4itbx3r7uayozey"
"skill/valory/liquidity_trader_abci/0.1.0": "bafybeibb37nibvq6ceseuptyqid6oevdr5taywmt5xrnyv4aom2yuboszy",
"skill/valory/optimus_abci/0.1.0": "bafybeiddcr6ynnhmpdxojtyf7inervcqjbar3wlujgauwvd27tuv2mctqe",
"agent/valory/optimus/0.1.0": "bafybeif57cip6khg67sczuw5rb77uzmuwmxdsmrzn2m44up2phktrill54",
"service/valory/optimus/0.1.0": "bafybeic2mihsbjf4cmgnztgcyvx3hluuicc6tgeja6dthflptgrur7b5iq"
},
"third_party": {
"protocol/open_aea/signing/1.0.0": "bafybeihv62fim3wl2bayavfcg3u5e5cxu3b7brtu4cn5xoxd6lqwachasi",
Expand Down
4 changes: 2 additions & 2 deletions packages/valory/agents/optimus/aea-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ protocols:
skills:
- valory/abstract_abci:0.1.0:bafybeidz54kvxhbdmpruzguuzzq7bjg4pekjb5amqobkxoy4oqknnobopu
- valory/abstract_round_abci:0.1.0:bafybeiajjzuh6vf23crp55humonknirvv2f4s3dmdlfzch6tc5ow52pcgm
- valory/liquidity_trader_abci:0.1.0:bafybeig5brc3h4mbquq6jvc6ygzydcuwk5aefxmgskytkzz6gxp2ukeigi
- valory/optimus_abci:0.1.0:bafybeibrnhpcr6l4p5fmxspqkqkxg2pyxmbiwbwxb44qweucz73mmdwb34
- valory/liquidity_trader_abci:0.1.0:bafybeibb37nibvq6ceseuptyqid6oevdr5taywmt5xrnyv4aom2yuboszy
- valory/optimus_abci:0.1.0:bafybeiddcr6ynnhmpdxojtyf7inervcqjbar3wlujgauwvd27tuv2mctqe
- valory/registration_abci:0.1.0:bafybeiffipsowrqrkhjoexem7ern5ob4fabgif7wa6gtlszcoaop2e3oey
- valory/reset_pause_abci:0.1.0:bafybeif4lgvbzsmzljesxbphycdv52ka7qnihyjrjpfaseclxadcmm6yiq
- valory/termination_abci:0.1.0:bafybeiekkpo5qef5zaeagm3si6v45qxcojvtjqe4a5ceccvk4q7k3xi3bi
Expand Down
2 changes: 1 addition & 1 deletion packages/valory/services/optimus/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ aea_version: '>=1.0.0, <2.0.0'
license: Apache-2.0
fingerprint: {}
fingerprint_ignore_patterns: []
agent: valory/optimus:0.1.0:bafybeib3sapx5oxymxgcdkshct2qxwns3njl5zimo7swqrfgz6su6rxch4
agent: valory/optimus:0.1.0:bafybeif57cip6khg67sczuw5rb77uzmuwmxdsmrzn2m44up2phktrill54
number_of_agents: 1
deployment: {}
---
Expand Down
130 changes: 86 additions & 44 deletions packages/valory/skills/liquidity_trader_abci/behaviours.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,18 @@
from abc import ABC
from collections import defaultdict
from enum import Enum
from typing import Any, Callable, Dict, Generator, List, Optional, Set, Tuple, Type, Union, cast
from typing import (
Any,
Callable,
Dict,
Generator,
List,
Optional,
Set,
Tuple,
Type,
cast,
)
from urllib.parse import urlencode

from aea.configurations.data_types import PublicId
Expand Down Expand Up @@ -103,7 +114,7 @@
HTTP_OK = [200, 201]
UTF8 = "utf-8"
STAKING_CHAIN = "optimism"
CAMPAIGN_TYPES = [1,2]
CAMPAIGN_TYPES = [1, 2]
INTEGRATOR = "valory"
WAITING_PERIOD_FOR_BALANCE_TO_REFLECT = 5
WaitableConditionType = Generator[None, None, Any]
Expand Down Expand Up @@ -515,9 +526,11 @@ def _is_staking_kpi_met(self) -> Generator[None, None, Optional[bool]]:
)
return None

multisig_nonces_since_last_cp = yield from self._get_multisig_nonces_since_last_cp(
multisig_nonces_since_last_cp = (
yield from self._get_multisig_nonces_since_last_cp(
chain=STAKING_CHAIN,
multisig=self.params.safe_contract_addresses.get(STAKING_CHAIN),
)
)
if multisig_nonces_since_last_cp is None:
return None
Expand Down Expand Up @@ -773,15 +786,18 @@ def async_act(self) -> Generator:
),
)
)
if multisig_nonces_since_last_cp is not None and min_num_of_safe_tx_required is not None:
if (
multisig_nonces_since_last_cp is not None
and min_num_of_safe_tx_required is not None
):
num_of_tx_left_to_meet_kpi = (
min_num_of_safe_tx_required - multisig_nonces_since_last_cp
)
if num_of_tx_left_to_meet_kpi > 0:
self.context.logger.info(
f"Number of tx left to meet KPI: {num_of_tx_left_to_meet_kpi}"
)
self.context.logger.info(f"Preparing vanity tx..")
self.context.logger.info("Preparing vanity tx..")
vanity_tx_hex = yield from self._prepare_vanity_tx(
chain=STAKING_CHAIN
)
Expand Down Expand Up @@ -1290,7 +1306,7 @@ def _get_top_tokens_by_value(self) -> Generator[None, None, Optional[List[Any]]]
"balance": balance,
}
)

# Fetch prices for tokens with balance greater than zero
token_prices = yield from self._fetch_token_prices(token_balances)

Expand All @@ -1309,13 +1325,15 @@ def _get_top_tokens_by_value(self) -> Generator[None, None, Optional[List[Any]]]

# Sort tokens by value in descending order and add the highest ones
token_balances.sort(key=lambda x: x["value"], reverse=True)

tokens = []
for token_data in token_balances:
if token_data["value"] > self.params.min_swap_amount_threshold:
tokens.append(token_data)
if len(tokens) == 2:
self.context.logger.info(f"Tokens selected for bridging/swapping: {tokens}")
self.context.logger.info(
f"Tokens selected for bridging/swapping: {tokens}"
)
break

if len(tokens) < 2:
Expand Down Expand Up @@ -1559,7 +1577,10 @@ def _build_bridge_swap_actions(

# If either of the token to swap and destination token match, we need to check which token don't match and build the bridge swap action based on this assessment.
if source_token0_chain == dest_chain or source_token1_chain == dest_chain:
if source_token0_address not in [dest_token0_address, dest_token1_address] or source_token0_chain != dest_chain:
if (
source_token0_address not in [dest_token0_address, dest_token1_address]
or source_token0_chain != dest_chain
):
# for example :- from_tokens = [usdc, xdai], to_tokens = [xdai, weth], then the pair to be created is [usdc, weth]
to_token = (
dest_token1_address
Expand All @@ -1583,7 +1604,10 @@ def _build_bridge_swap_actions(
}
bridge_swap_actions.append(bridge_swap_action)

if source_token1_address not in [dest_token0_address, dest_token1_address] or source_token1_chain != dest_chain:
if (
source_token1_address not in [dest_token0_address, dest_token1_address]
or source_token1_chain != dest_chain
):
to_token = (
dest_token0_address
if source_token0_address == dest_token1_address
Expand Down Expand Up @@ -1877,7 +1901,7 @@ def get_next_event(self) -> Generator[None, None, Tuple[str, Dict, Optional[Dict
self.current_pool = current_pool
self.store_current_pool()
self.context.logger.info("Exit was successful! Removing current pool")
#when we exit the pool, it may take time to reflect the balance of our assets
# when we exit the pool, it may take time to reflect the balance of our assets
yield from self.sleep(WAITING_PERIOD_FOR_BALANCE_TO_REFLECT)

# If last action was Claim Rewards and it was successful we update the list of assets and the last_reward_claimed_timestamp
Expand Down Expand Up @@ -2086,7 +2110,9 @@ def get_enter_pool_tx_hash(
self._get_balance(chain, assets[1], positions),
]
if any(amount == 0 or amount is None for amount in max_amounts_in):
self.context.logger.error(f"Insufficient balance for entering pool: {max_amounts_in}")
self.context.logger.error(
f"Insufficient balance for entering pool: {max_amounts_in}"
)
return None, None, None

tx_hash, contract_address = yield from pool.enter(
Expand Down Expand Up @@ -2316,28 +2342,34 @@ def get_exit_pool_tx_hash(
def get_transaction_data_for_route(
self, positions, action
) -> Generator[None, None, Dict]:
"""Prepares the bridge swap actions"""
bridge_and_swap_actions = {}
routes = yield from self._fetch_routes(positions, action)
if not routes:
return {}

for route in routes:
steps = route.get("steps", [])
all_steps_successful = True
bridge_and_swap_actions["actions"] = []
all_steps_successful = True
bridge_and_swap_actions["actions"] = []
step_transactions = yield from self._get_step_transactions_data(route)

total_gas_cost = 0
total_fee = 0
total_fee += sum(float(tx_info.get("fee",0)) for tx_info in step_transactions)
total_gas_cost += sum(float(tx_info.get("gas_cost", 0)) for tx_info in step_transactions)
from_amount_usd = float(route.get("fromAmountUSD",0))
to_amount_usd = float(route.get("toAmountUSD",0))
is_profitable = self._is_route_profitable(from_amount_usd, to_amount_usd, total_fee, total_gas_cost)
total_fee += sum(
float(tx_info.get("fee", 0)) for tx_info in step_transactions
)
total_gas_cost += sum(
float(tx_info.get("gas_cost", 0)) for tx_info in step_transactions
)
from_amount_usd = float(route.get("fromAmountUSD", 0))
to_amount_usd = float(route.get("toAmountUSD", 0))
is_profitable = self._is_route_profitable(
from_amount_usd, to_amount_usd, total_fee, total_gas_cost
)
if not is_profitable:
self.context.logger.info(f"Switching to next route.")
self.context.logger.info("Switching to next route.")
continue

for tx_info in step_transactions:
multisend_tx_hash = yield from self._build_multisend_tx(tx_info)
if not multisend_tx_hash:
Expand Down Expand Up @@ -2374,11 +2406,13 @@ def get_transaction_data_for_route(
f"Simulation successful for bridge/swap tx: {tx_info.get('source_token_symbol')}({tx_info.get('from_chain')}) --> {tx_info.get('target_token_symbol')}({tx_info.get('to_chain')}). Tool used: {tx_info.get('tool')}"
)

payload_string = yield from self._build_safe_tx(from_chain, multisend_tx_hash, multisend_address)
payload_string = yield from self._build_safe_tx(
from_chain, multisend_tx_hash, multisend_address
)
if not payload_string:
all_steps_successful = False
break

bridge_and_swap_actions["actions"].append(
{
"action": Action.BRIDGE_SWAP.value,
Expand All @@ -2389,7 +2423,9 @@ def get_transaction_data_for_route(
"to_token": tx_info.get("target_token"),
"to_token_symbol": tx_info.get("target_token_symbol"),
"payload": payload_string,
"safe_address": self.params.safe_contract_addresses.get(from_chain),
"safe_address": self.params.safe_contract_addresses.get(
from_chain
),
}
)

Expand All @@ -2402,7 +2438,9 @@ def get_transaction_data_for_route(
self.context.logger.error("NONE OF THE ROUTES WERE SUCCESSFUL!")
return {}

def _build_safe_tx(self, from_chain, multisend_tx_hash, multisend_address) -> Generator[None, None, Optional[str]]:
def _build_safe_tx(
self, from_chain, multisend_tx_hash, multisend_address
) -> Generator[None, None, Optional[str]]:
safe_address = self.params.safe_contract_addresses.get(from_chain)
safe_tx_hash = yield from self.contract_interact(
performative=ContractApiMessage.Performative.GET_RAW_TRANSACTION, # type: ignore
Expand Down Expand Up @@ -2478,7 +2516,9 @@ def _build_multisend_tx(self, tx_info) -> Generator[None, None, Optional[str]]:

return multisend_tx_hash

def _get_step_transactions_data(self, route: Dict[str, Any]) -> Generator[None, None, Optional[List[Any]]]:
def _get_step_transactions_data(
self, route: Dict[str, Any]
) -> Generator[None, None, Optional[List[Any]]]:
step_transactions = []
steps = route.get("steps", [])
for step in steps:
Expand Down Expand Up @@ -2507,9 +2547,9 @@ def _get_step_transactions_data(self, route: Dict[str, Any]) -> Generator[None,
step["action"]["toAddress"] = self.params.safe_contract_addresses.get(
to_chain
)
tx_info = yield from self._get_step_transaction(step)
tx_info = yield from self._get_step_transaction(step)
step_transactions.append(tx_info)

return step_transactions

def _get_step_transaction(
Expand Down Expand Up @@ -2578,14 +2618,8 @@ def _get_step_transaction(
gas_costs = estimate.get("gasCosts", [])
fee = 0
gas_cost = 0
fee += sum(
float(fee_cost.get("amountUSD", 0))
for fee_cost in fee_costs
)
gas_cost += sum(
float(gas_cost.get("amountUSD", 0))
for gas_cost in gas_costs
)
fee += sum(float(fee_cost.get("amountUSD", 0)) for fee_cost in fee_costs)
gas_cost += sum(float(gas_cost.get("amountUSD", 0)) for gas_cost in gas_costs)

return {
"source_token": source_token,
Expand All @@ -2599,11 +2633,13 @@ def _get_step_transaction(
"tool": tool,
"data": data,
"tx_hash": tx_hash,
"fee" : fee,
"gas_cost": gas_cost
"fee": fee,
"gas_cost": gas_cost,
}

def _fetch_routes(self, positions, action) -> Generator[None, None, Optional[Dict[str, Any]]]:

def _fetch_routes(
self, positions, action
) -> Generator[None, None, Optional[Dict[str, Any]]]:
"""Get transaction data for route from LiFi API"""
from_chain = action.get("from_chain")
to_chain = action.get("to_chain")
Expand Down Expand Up @@ -2676,7 +2712,13 @@ def _fetch_routes(self, positions, action) -> Generator[None, None, Optional[Dic

return routes

def _is_route_profitable(self, from_amount_usd: float, to_amount_usd: float, total_fee: float, total_gas_cost: float) -> bool:
def _is_route_profitable(
self,
from_amount_usd: float,
to_amount_usd: float,
total_fee: float,
total_gas_cost: float,
) -> bool:
"""Check if the route is profitable"""
allowed_fee_percentage = self.params.max_fee_percentage * 100
allowed_gas_percentage = self.params.max_gas_percentage * 100
Expand All @@ -2693,10 +2735,10 @@ def _is_route_profitable(self, from_amount_usd: float, to_amount_usd: float, tot
fee_percentage > allowed_fee_percentage
or gas_percentage > allowed_gas_percentage
):
self.context.logger.error(f"Route is not profitable!")
self.context.logger.error("Route is not profitable!")
return False

self.context.logger.info(f"Route is profitable!")
self.context.logger.info("Route is profitable!")
return True

def _simulate_execution_bundle(
Expand Down
2 changes: 1 addition & 1 deletion packages/valory/skills/liquidity_trader_abci/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from time import time
from typing import Any, Dict, List, Optional

from aea.skills.base import Model, SkillContext
from aea.skills.base import Model

from packages.valory.skills.abstract_round_abci.models import BaseParams
from packages.valory.skills.abstract_round_abci.models import (
Expand Down
5 changes: 1 addition & 4 deletions packages/valory/skills/liquidity_trader_abci/rounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,10 +416,7 @@ class FailedMultiplexerRound(DegenerateRound):


class LiquidityTraderAbciApp(AbciApp[Event]):
"""
LiquidityTraderAbciApp
"""
"""LiquidityTraderAbciApp"""

initial_round_cls: AppState = CallCheckpointRound
initial_states: Set[AppState] = {
Expand Down
6 changes: 3 additions & 3 deletions packages/valory/skills/liquidity_trader_abci/skill.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ license: Apache-2.0
aea_version: '>=1.0.0, <2.0.0'
fingerprint:
__init__.py: bafybeia7bn2ahqqwkf63ptje6rfnftuwrsp33sswgpcbh5osbesxxr6g4m
behaviours.py: bafybeigxzq7pbtmqwb37bz2cbndoeiehuodnbtx6dv34wrg26gnqnu23mu
behaviours.py: bafybeiaktfldwuypicsiqbyxjulesfxhfbbjzu6p7bwtpvfwx2cdyweqmq
dialogues.py: bafybeiay23otskx2go5xhtgdwfw2kd6rxd62sxxdu3njv7hageorl5zxzm
fsm_specification.yaml: bafybeiabbiulb7k6xkjysulmy6o4ugnhxlpp5jiaeextvwj65q4ttadoeq
handlers.py: bafybeidxw2lvgiifmo4siobpwuwbxscuifrdo3gnkjyn6bgexotj5f7zf4
models.py: bafybeiarqylk2bps7u35dfprufxalzcfjz2rdaxlmb5pxi5pmqdrejrs34
models.py: bafybeiahzdqz6megfdtiskfnh2hxukkhvp7lpjbif47druqsdnlpnzijnq
payloads.py: bafybeihi42vbsr2xyhzsp55tl6mqecqyy52jrhwjfw4trvrjlhfxuyaz3u
pool_behaviour.py: bafybeiaheuesscgqzwjbpyrezgwpdbdfurlmfwbc462qv6rblwwxlx5dpm
pools/balancer.py: bafybeigznhgv7ylo5dvlhxcqikhiuqlqtnx3ikv4tszyvkl2lpcuqgoa5u
pools/uniswap.py: bafybeigmqptgmjaxscszohfusgxsexqyx4awuyw7p4g5l7k2qpeyq7vdcu
rounds.py: bafybeiby4imfhqadx5pgrb34c3mluysqbboldpsppg5bsh7k7pqxbkqryq
rounds.py: bafybeidorqqosfidfxqwwegjjifzuqapl65ndpdnakolhlwawf57moopli
strategies/simple_strategy.py: bafybeiasu2nchowx6leksjllpuum4ckezxoj4o2m4sstavblplvvutmvzm
strategy_behaviour.py: bafybeidk6sorg47kuuubamcccksi65x3txldyo7y2hm5opbye2ghmz2ljy
fingerprint_ignore_patterns: []
Expand Down
2 changes: 1 addition & 1 deletion packages/valory/skills/optimus_abci/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
from packages.valory.skills.abstract_round_abci.tests.data.dummy_abci.models import (
RandomnessApi as BaseRandomnessApi,
)
from packages.valory.skills.liquidity_trader_abci.models import Coingecko
from packages.valory.skills.liquidity_trader_abci.models import (
Params as LiquidityTraderParams,
Coingecko
)
from packages.valory.skills.liquidity_trader_abci.models import (
SharedState as BaseSharedState,
Expand Down
Loading

0 comments on commit c3e9ca2

Please sign in to comment.