Skip to content

Commit

Permalink
Merge pull request #11 from valory-xyz/fix/fund-ops
Browse files Browse the repository at this point in the history
Fix/fund ops
  • Loading branch information
0xArdi authored Sep 18, 2024
2 parents 1bf2a6b + 7ae87ef commit 8b1120b
Show file tree
Hide file tree
Showing 12 changed files with 4,534 additions and 4,062 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ Once the command has completed, i.e. the service is running, you can see the liv
```bash
docker logs optimus_abci_0 --follow
```
Execute the report command to view a summary of the service status:

```bash
poetry run python report.py
```
To inspect the tree state transition of the current run of the agent run:
```bash
poetry run autonomy analyse logs --from-dir .optimus/services/[service-hash]/deployment/persistent_data/logs/ --agent aea_0 --fsm --reset-db
Expand Down
70 changes: 69 additions & 1 deletion operate/services/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from aea.helpers.base import IPFSHash
from aea.helpers.logging import setup_logger
from autonomy.chain.base import registry_contracts
from pycparser.ply.yacc import token

from operate.keys import Key, KeysManager
from operate.ledger import PUBLIC_RPCS
Expand Down Expand Up @@ -475,7 +476,7 @@ def deploy_service_onchain_from_safe_single_chain( # pylint: disable=too-many-s
is_update = (
(not is_first_mint)
and (on_chain_hash is not None)
and (on_chain_hash != service.hash or current_agent_id != agent_id)
and (current_agent_id != agent_id)
)

if is_update:
Expand Down Expand Up @@ -1102,6 +1103,73 @@ def fund_service( # pylint: disable=too-many-arguments
rpc=rpc or ledger_config.rpc,
)

def fund_service_erc20( # pylint: disable=too-many-arguments
self,
hash: str,
token: str,
rpc: t.Optional[str] = None,
agent_topup: t.Optional[float] = None,
safe_topup: t.Optional[float] = None,
agent_fund_threshold: t.Optional[float] = None,
safe_fund_treshold: t.Optional[float] = None,
from_safe: bool = True,
chain_id: str = "10",
) -> None:
"""Fund service if required."""
service = self.load_or_create(hash=hash)
chain_config = service.chain_configs[chain_id]
ledger_config = chain_config.ledger_config
chain_data = chain_config.chain_data
wallet = self.wallet_manager.load(ledger_config.type)
ledger_api = wallet.ledger_api(chain_type=ledger_config.chain, rpc=rpc or ledger_config.rpc)
agent_fund_threshold = (
agent_fund_threshold
or chain_data.user_params.fund_requirements.agent
)

for key in service.keys:
agent_balance = ledger_api.get_balance(address=key.address)
self.logger.info(f"Agent {key.address} balance: {agent_balance}")
self.logger.info(f"Required balance: {agent_fund_threshold}")
if agent_balance < agent_fund_threshold:
self.logger.info("Funding agents")
to_transfer = (
agent_topup
or chain_data.user_params.fund_requirements.agent
)
self.logger.info(f"Transferring {to_transfer} units to {key.address}")
wallet.transfer_erc20(
token=token,
to=key.address,
amount=int(to_transfer),
chain_type=ledger_config.chain,
from_safe=from_safe,
rpc=rpc or ledger_config.rpc,
)

safe_balance = registry_contracts.erc20.get_instance(ledger_api, token).functions.balanceOf(chain_data.multisig).call()
safe_fund_treshold = (
safe_fund_treshold or chain_data.user_params.fund_requirements.safe
)
self.logger.info(f"Safe {chain_data.multisig} balance: {safe_balance}")
self.logger.info(f"Required balance: {safe_fund_treshold}")
if safe_balance < safe_fund_treshold:
self.logger.info("Funding safe")
to_transfer = (
safe_topup or chain_data.user_params.fund_requirements.safe
)
self.logger.info(
f"Transferring {to_transfer} units to {chain_data.multisig}"
)
wallet.transfer_erc20(
token=token,
to=t.cast(str, chain_data.multisig),
amount=int(to_transfer),
chain_type=ledger_config.chain,
rpc=rpc or ledger_config.rpc,
)


async def funding_job(
self,
hash: str,
Expand Down
Loading

0 comments on commit 8b1120b

Please sign in to comment.