Skip to content

Commit

Permalink
Merge pull request #8 from valory-xyz/fix/swap-profitability
Browse files Browse the repository at this point in the history
Fix/swap profitability
  • Loading branch information
Divya-Solulab authored Sep 27, 2024
2 parents 08c64b0 + c3e9ca2 commit 21d4bd1
Show file tree
Hide file tree
Showing 15 changed files with 1,186 additions and 842 deletions.
10 changes: 5 additions & 5 deletions packages/packages.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
"dev": {
"contract/valory/balancer_weighted_pool/0.1.0": "bafybeidyjlrlq3jrbackewedwt5irokhjupxgpqfgur2ri426cap2oqt7a",
"contract/valory/balancer_vault/0.1.0": "bafybeie6twptrkqddget7pjijzob2c4jqmrrtpkwombneh35xx56djz4ru",
"contract/valory/erc20/0.1.0": "bafybeibnsmxcllsvvoqc3pz4xzp3i3bzf5gahqm4er4ivkoyz77udnoxl4",
"contract/valory/erc20/0.1.0": "bafybeiav4gh7lxfnwp4f7oorkbvjxrdsgjgyhl43rgbblaugtl76zlx7vy",
"contract/valory/uniswap_v3_non_fungible_position_manager/0.1.0": "bafybeigadr3nyx6tkrual7oqn2qiup35addfevromxjzzlvkiukpyhtz6y",
"contract/valory/uniswap_v3_pool/0.1.0": "bafybeih64nqgwlverl2tubnkymtlvewngn2pthzzfjewvxpk7dt2im6gza",
"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": "bafybeihlimmcs6fv7capvbvfdaj4kou4tpknlvhnmwpra3brf66rerppcm",
"skill/valory/optimus_abci/0.1.0": "bafybeia6ucm5n7oeqrs6cotpivnnok5d2kb2kxiahmkwuw2jifb4if3fb4",
"agent/valory/optimus/0.1.0": "bafybeigawqflrr7moxgiivnwv6ts4mew7vn52mmdwueqwjrtajeeoj4rc4",
"service/valory/optimus/0.1.0": "bafybeiawewwnrbu4urr5nkyg2gd55gpcbrr4j4lreyzpkopftpmpf3jrry"
"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
16 changes: 14 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:bafybeihlimmcs6fv7capvbvfdaj4kou4tpknlvhnmwpra3brf66rerppcm
- valory/optimus_abci:0.1.0:bafybeia6ucm5n7oeqrs6cotpivnnok5d2kb2kxiahmkwuw2jifb4if3fb4
- 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 Expand Up @@ -130,6 +130,15 @@ models:
benchmark_tool:
args:
log_dir: ${str:/logs}
coingecko:
args:
token_price_endpoint: ${str:https://api.coingecko.com/api/v3/simple/token_price/{asset_platform_id}?contract_addresses={token_address}&vs_currencies=usd}
coin_price_endpoint: ${str:https://api.coingecko.com/api/v3/simple/price?ids={coin_id}&vs_currencies=usd}
api_key: ${str:null}
requests_per_minute: ${int:30}
credits: ${int:10000}
rate_limited_code: ${int:429}
chain_to_platform_id_mapping: ${str:{"optimism":"optimistic-ethereum","base":"base","ethereum":"ethereum"}}
params:
args:
cleanup_history_depth: 1
Expand Down Expand Up @@ -228,4 +237,7 @@ models:
store_path: ${str:/data/}
assets_info_filename: ${str:assets.json}
pool_info_filename: ${str:current_pool.json}
min_swap_amount_threshold: ${int:10}
max_fee_percentage: ${float:0.02}
max_gas_percentage: ${float:0.25}
balancer_graphql_endpoints: ${str:{"optimism":"https://api.studio.thegraph.com/query/75376/balancer-optimism-v2/version/latest","base":"https://api.studio.thegraph.com/query/24660/balancer-base-v2/version/latest"}}
235 changes: 123 additions & 112 deletions packages/valory/contracts/erc20/contract.py
Original file line number Diff line number Diff line change
@@ -1,112 +1,123 @@
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2023-2024 Valory AG
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ------------------------------------------------------------------------------

"""This module contains the class to connect to an ERC20 token contract."""

from typing import Dict

from aea.common import JSONLike
from aea.configurations.base import PublicId
from aea.contracts.base import Contract
from aea.crypto.base import LedgerApi
from aea_ledger_ethereum import EthereumApi


PUBLIC_ID = PublicId.from_str("valory/erc20:0.1.0")


class ERC20(Contract):
"""The ERC20 contract."""

contract_id = PUBLIC_ID

@classmethod
def check_balance(
cls,
ledger_api: EthereumApi,
contract_address: str,
account: str,
) -> JSONLike:
"""Check the balance of the given account."""
contract_instance = cls.get_instance(ledger_api, contract_address)
balance_of = getattr(contract_instance.functions, "balanceOf") # noqa
token_balance = balance_of(account).call()
wallet_balance = ledger_api.api.eth.get_balance(account)
return dict(token=token_balance, wallet=wallet_balance)

@classmethod
def get_allowance(
cls,
ledger_api: EthereumApi,
contract_address: str,
owner: str,
spender: str,
) -> JSONLike:
"""Check the balance of the given account."""
contract_instance = cls.get_instance(ledger_api, contract_address)
allowance = contract_instance.functions.allowance(owner, spender).call()
return dict(data=allowance)

@classmethod
def get_token_symbol(
cls,
ledger_api: EthereumApi,
contract_address: str,
) -> JSONLike:
"""Check the balance of the given account."""
contract_instance = cls.get_instance(ledger_api, contract_address)
symbol = contract_instance.functions.symbol().call()
return dict(data=symbol)

@classmethod
def build_deposit_tx(
cls,
ledger_api: EthereumApi,
contract_address: str,
) -> Dict[str, bytes]:
"""Build a deposit transaction."""
contract_instance = cls.get_instance(ledger_api, contract_address)
data = contract_instance.encodeABI("deposit")
return {"data": bytes.fromhex(data[2:])}

@classmethod
def build_withdraw_tx(
cls,
ledger_api: EthereumApi,
contract_address: str,
amount: int,
) -> Dict[str, bytes]:
"""Build a deposit transaction."""
contract_instance = cls.get_instance(ledger_api, contract_address)
data = contract_instance.encodeABI("withdraw", args=(amount,))
return {"data": bytes.fromhex(data[2:])}

@classmethod
def build_approval_tx(
cls,
ledger_api: LedgerApi,
contract_address: str,
spender: str,
amount: int,
) -> Dict[str, bytes]:
"""Build an ERC20 approval."""
contract_instance = cls.get_instance(ledger_api, contract_address)
checksumed_spender = ledger_api.api.to_checksum_address(spender)
data = contract_instance.encodeABI("approve", args=(checksumed_spender, amount))
return {"data": bytes.fromhex(data[2:])}
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2023-2024 Valory AG
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ------------------------------------------------------------------------------

"""This module contains the class to connect to an ERC20 token contract."""

from typing import Dict

from aea.common import JSONLike
from aea.configurations.base import PublicId
from aea.contracts.base import Contract
from aea.crypto.base import LedgerApi
from aea_ledger_ethereum import EthereumApi


PUBLIC_ID = PublicId.from_str("valory/erc20:0.1.0")


class ERC20(Contract):
"""The ERC20 contract."""

contract_id = PUBLIC_ID

@classmethod
def check_balance(
cls,
ledger_api: EthereumApi,
contract_address: str,
account: str,
) -> JSONLike:
"""Check the balance of the given account."""
contract_instance = cls.get_instance(ledger_api, contract_address)
balance_of = getattr(contract_instance.functions, "balanceOf") # noqa
token_balance = balance_of(account).call()
wallet_balance = ledger_api.api.eth.get_balance(account)
return dict(token=token_balance, wallet=wallet_balance)

@classmethod
def get_allowance(
cls,
ledger_api: EthereumApi,
contract_address: str,
owner: str,
spender: str,
) -> JSONLike:
"""Check the balance of the given account."""
contract_instance = cls.get_instance(ledger_api, contract_address)
allowance = contract_instance.functions.allowance(owner, spender).call()
return dict(data=allowance)

@classmethod
def get_token_symbol(
cls,
ledger_api: EthereumApi,
contract_address: str,
) -> JSONLike:
"""Check the balance of the given account."""
contract_instance = cls.get_instance(ledger_api, contract_address)
symbol = contract_instance.functions.symbol().call()
return dict(data=symbol)

@classmethod
def get_token_decimals(
cls,
ledger_api: EthereumApi,
contract_address: str,
) -> JSONLike:
"""Get the token decimals."""
contract_instance = cls.get_instance(ledger_api, contract_address)
decimals = contract_instance.functions.decimals().call()
return dict(data=decimals)

@classmethod
def build_deposit_tx(
cls,
ledger_api: EthereumApi,
contract_address: str,
) -> Dict[str, bytes]:
"""Build a deposit transaction."""
contract_instance = cls.get_instance(ledger_api, contract_address)
data = contract_instance.encodeABI("deposit")
return {"data": bytes.fromhex(data[2:])}

@classmethod
def build_withdraw_tx(
cls,
ledger_api: EthereumApi,
contract_address: str,
amount: int,
) -> Dict[str, bytes]:
"""Build a deposit transaction."""
contract_instance = cls.get_instance(ledger_api, contract_address)
data = contract_instance.encodeABI("withdraw", args=(amount,))
return {"data": bytes.fromhex(data[2:])}

@classmethod
def build_approval_tx(
cls,
ledger_api: LedgerApi,
contract_address: str,
spender: str,
amount: int,
) -> Dict[str, bytes]:
"""Build an ERC20 approval."""
contract_instance = cls.get_instance(ledger_api, contract_address)
checksumed_spender = ledger_api.api.to_checksum_address(spender)
data = contract_instance.encodeABI("approve", args=(checksumed_spender, amount))
return {"data": bytes.fromhex(data[2:])}
2 changes: 1 addition & 1 deletion packages/valory/contracts/erc20/contract.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fingerprint:
README.md: bafybeifmfma6rglvpa22odtozyosnp5mwljum64utxip2wgmezuhnjjjyi
__init__.py: bafybeif5vpc3dfrlxlch7brbhmdwksabyzddpfqgm56vdbbkek3t3br6ke
build/ERC20.json: bafybeiemn5b5nszuss7xj6lmvmjuendltp6wz7ubihdvd7c6wqw4bohbpa
contract.py: bafybeihekgdc4un2ees7bwgf5ofbbkjmzf4kk4i3363eclqu5uxfgnuxmm
contract.py: bafybeibbgvpfy6cg5sivhfutdskwhts35qf5xb6sggpamgzcabz3jvwn5a
fingerprint_ignore_patterns: []
contracts: []
class_name: ERC20
Expand Down
14 changes: 13 additions & 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:bafybeigawqflrr7moxgiivnwv6ts4mew7vn52mmdwueqwjrtajeeoj4rc4
agent: valory/optimus:0.1.0:bafybeif57cip6khg67sczuw5rb77uzmuwmxdsmrzn2m44up2phktrill54
number_of_agents: 1
deployment: {}
---
Expand Down Expand Up @@ -78,7 +78,19 @@ models:
assets_info_filename: ${ASSETS_INFO_FILENAME:str:assets.json}
pool_info_filename: ${POOL_INFO_FILENAME:str:current_pool.json}
merkl_fetch_campaigns_args: ${MERKL_FETCH_CAMPAIGNS_ARGS:str:{"url":"https://api.merkl.xyz/v3/campaigns","creator":"","live":"true"}}
min_swap_amount_threshold: ${MIN_SWAP_AMOUNT_THRESHOLD:int:10}
max_fee_percentage: ${MAX_FEE_PERCENTAGE:float:0.02}
max_gas_percentage: ${MAX_GAS_PERCENTAGE:float:0.25}
balancer_graphql_endpoints: ${BALANCER_GRAPHQL_ENDPOINTS:str:{"optimism":"https://api.studio.thegraph.com/query/75376/balancer-optimism-v2/version/latest","base":"https://api.studio.thegraph.com/query/24660/balancer-base-v2/version/latest"}}
coingecko:
args:
token_price_endpoint: ${COINGECKO_TOKEN_PRICE_ENDPOINT:str:https://api.coingecko.com/api/v3/simple/token_price/{asset_platform_id}?contract_addresses={token_address}&vs_currencies=usd}
coin_price_endpoint: ${COINGECKO_COIN_PRICE_ENDPOINT:str:https://api.coingecko.com/api/v3/simple/price?ids={coin_id}&vs_currencies=usd}
api_key: ${COINGECKO_API_KEY:str:null}
requests_per_minute: ${COINGECKO_REQUESTS_PER_MINUTE:int:30}
credits: ${COINGECKO_CREDITS:int:10000}
rate_limited_code: ${COINGECKO_RATE_LIMITED_CODE:int:429}
chain_to_platform_id_mapping: ${COINGECKO_CHAIN_TO_PLATFORM_ID_MAPPING:str:{"optimism":"optimistic-ethereum","base":"base","ethereum":"ethereum"}}
---
public_id: valory/ledger:0.19.0
type: connection
Expand Down
Loading

0 comments on commit 21d4bd1

Please sign in to comment.