Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Funding job to send amount upto the topup #637

Merged
merged 1 commit into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions operate/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

"""Constants."""

from operate.operate_types import Chain


OPERATE = ".operate"
CONFIG = "config.json"
SERVICES = "services"
Expand All @@ -40,3 +43,10 @@

TM_CONTROL_URL = "http://localhost:8080"
ZERO_ADDRESS = "0x0000000000000000000000000000000000000000"

WRAPPED_NATIVE_ASSET = {
Chain.GNOSIS.value: '0xe91D153E0b41518A2Ce8Dd3D7944Fa863463a97d',
Chain.OPTIMISTIC.value: '0x4200000000000000000000000000000000000006',
Chain.MODE.value: '0x4200000000000000000000000000000000000006',
Chain.BASE.value: '0x4200000000000000000000000000000000000006',
}
42 changes: 31 additions & 11 deletions operate/services/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from aea_ledger_ethereum import EthereumCrypto
from autonomy.chain.base import registry_contracts

from operate.constants import ZERO_ADDRESS
from operate.constants import WRAPPED_NATIVE_ASSET, ZERO_ADDRESS
from operate.keys import Key, KeysManager
from operate.ledger import PUBLIC_RPCS
from operate.ledger.profiles import CONTRACTS, OLAS, STAKING, WXDAI
Expand Down Expand Up @@ -1491,19 +1491,25 @@ def fund_service_single_chain( # pylint: disable=too-many-arguments,too-many-lo
address=key.address,
)
self.logger.info(
f"Agent {key.address} Asset: {asset_address} balance: {agent_balance}"
f"[FUNDING_JOB] Agent {key.address} Asset: {asset_address} balance: {agent_balance}"
)
if agent_fund_threshold > 0:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this check not performed in safe as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really see why we have this check. agent_fund_threshold will always be passed as positive without any programming error

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Me neither. Assuming the template has nonnegative values for the fund requirement, if we set, e.g. 0 fund requirements for USDC, then the condition agent_balance < agent_fund_threshold will not be triggered anyways...

self.logger.info(f"Required balance: {agent_fund_threshold}")
self.logger.info(f"[FUNDING_JOB] Required balance: {agent_fund_threshold}")
if agent_balance < agent_fund_threshold:
jmoreira-valory marked this conversation as resolved.
Show resolved Hide resolved
self.logger.info("Funding agents")
to_transfer = (
self.logger.info("[FUNDING_JOB] Funding agents")
target_balance = (
asset_funding_values["agent"]["topup"]
if asset_funding_values is not None
else fund_requirements.agent
)
transferable_balance = get_asset_balance(
ledger_api=ledger_api,
contract_address=asset_address,
address=wallet.safes[ledger_config.chain],
)
to_transfer = max(min(transferable_balance, target_balance - agent_balance), 0)
self.logger.info(
f"Transferring {to_transfer} asset ({asset_address}) to {key.address}"
f"[FUNDING_JOB] Transferring {to_transfer} asset ({asset_address}) to {key.address}"
)
wallet.transfer_asset(
asset=asset_address,
Expand All @@ -1519,24 +1525,38 @@ def fund_service_single_chain( # pylint: disable=too-many-arguments,too-many-lo
contract_address=asset_address,
address=chain_data.multisig,
)
if asset_address == ZERO_ADDRESS:
# also count the balance of the wrapped native asset
safe_balance += get_asset_balance(
ledger_api=ledger_api,
contract_address=WRAPPED_NATIVE_ASSET.get(chain, asset_address),
address=chain_data.multisig,
)
Comment on lines +1528 to +1534
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this approach not taken into account also for the agent?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because in the case of Agent EOA, the agent doesn't swap xDAI for wxDAI and we don't want to it to happen that someone send wxDAI to the Agent EOA and the funding job no long funds xDAI in it, failing the agent to make transactions anymore.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems quite custom logic, but ok.


safe_fund_treshold = (
asset_funding_values["safe"]["threshold"]
if asset_funding_values is not None
else fund_requirements.safe
)
self.logger.info(
f"Safe {chain_data.multisig} Asset: {asset_address} balance: {safe_balance}"
f"[FUNDING_JOB] Safe {chain_data.multisig} Asset: {asset_address} balance: {safe_balance}"
)
self.logger.info(f"Required balance: {safe_fund_treshold}")
self.logger.info(f"[FUNDING_JOB] Required balance: {safe_fund_treshold}")
if safe_balance < safe_fund_treshold:
self.logger.info("Funding safe")
to_transfer = (
self.logger.info("[FUNDING_JOB] Funding safe")
target_balance = (
asset_funding_values["safe"]["topup"]
if asset_funding_values is not None
else fund_requirements.safe
)
transferable_balance = get_asset_balance(
ledger_api=ledger_api,
contract_address=asset_address,
address=wallet.safes[ledger_config.chain],
)
to_transfer = max(min(transferable_balance, target_balance - safe_balance), 0)
self.logger.info(
f"Transferring {to_transfer} asset ({asset_address}) to {chain_data.multisig}"
f"[FUNDING_JOB] Transferring {to_transfer} asset ({asset_address}) to {chain_data.multisig}"
)
# TODO: This is a temporary fix
# we avoid the error here because there is a seperate prompt on the UI
Expand Down
Loading