Skip to content

Commit

Permalink
Merge pull request #125 from valory-xyz/fix/funding
Browse files Browse the repository at this point in the history
Fix service funding top up value calculations
  • Loading branch information
angrybayblade authored May 24, 2024
2 parents e94b992 + dd0296c commit 1aaba6a
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 57 deletions.
2 changes: 1 addition & 1 deletion electron/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const process = require('process');
const { spawnSync } = require('child_process');
const Docker = require('dockerode');

const Version = '0.1.0rc21';
const Version = '0.1.0rc22';
const OperateDirectory = `${os.homedir()}/.operate`;
const VenvDir = `${OperateDirectory}/venv`;
const VersionFile = `${OperateDirectory}/version.txt`;
Expand Down
45 changes: 24 additions & 21 deletions operate/services/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -767,59 +767,60 @@ def unstake_service_on_chain_from_safe(self, hash: str) -> None:
service.chain_data.staked = True
service.store()

def fund_service(
def fund_service( # pylint: disable=too-many-arguments
self,
hash: str,
rpc: t.Optional[str] = None,
min_agent_fund_requirement: t.Optional[float] = None,
min_safe_fund_requirement: t.Optional[float] = 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,
) -> None:
"""Fund service if required."""
service = self.create_or_load(hash=hash)
wallet = self.wallet_manager.load(ledger_type=service.ledger_config.type)
ledger_api = wallet.ledger_api(chain_type=service.ledger_config.chain, rpc=rpc)
min_agent_fund_requirement = (
min_agent_fund_requirement
agent_fund_threshold = (
agent_fund_threshold
or service.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: {min_agent_fund_requirement}")
if agent_balance < min_agent_fund_requirement:
self.logger.info(f"Required balance: {agent_fund_threshold}")
if agent_balance < agent_fund_threshold:
self.logger.info("Funding agents")
to_transfer = (
service.chain_data.user_params.fund_requirements.agent
- agent_balance
agent_topup
or service.chain_data.user_params.fund_requirements.agent
)
self.logger.info(f"Transferring {to_transfer} units to {key.address}")
wallet.transfer(
to=key.address,
amount=to_transfer,
amount=int(to_transfer),
chain_type=service.ledger_config.chain,
from_safe=from_safe,
)

safe_balanace = ledger_api.get_balance(service.chain_data.multisig)
min_safe_fund_requirement = (
min_safe_fund_requirement
or service.chain_data.user_params.fund_requirements.safe
safe_fund_treshold = (
safe_fund_treshold or service.chain_data.user_params.fund_requirements.safe
)
self.logger.info(f"Safe {service.chain_data.multisig} balance: {safe_balanace}")
self.logger.info(f"Required balance: {min_safe_fund_requirement}")
if safe_balanace < min_safe_fund_requirement:
self.logger.info(f"Required balance: {safe_fund_treshold}")
if safe_balanace < safe_fund_treshold:
self.logger.info("Funding safe")
to_transfer = (
service.chain_data.user_params.fund_requirements.safe - safe_balanace
safe_topup or service.chain_data.user_params.fund_requirements.safe
)
self.logger.info(
f"Transferring {to_transfer} units to {service.chain_data.multisig}"
)
wallet.transfer(
to=t.cast(str, service.chain_data.multisig),
amount=to_transfer,
amount=int(to_transfer),
chain_type=service.ledger_config.chain,
)

Expand All @@ -838,10 +839,12 @@ async def funding_job(
await loop.run_in_executor(
executor,
self.fund_service,
hash,
PUBLIC_RPCS[service.ledger_config.chain],
100000000000000000,
500000000000000000,
hash, # Service hash
PUBLIC_RPCS[service.ledger_config.chain], # RPC
100000000000000000, # agent_topup
2000000000000000000, # safe_topup
50000000000000000, # agent_fund_threshold
500000000000000000, # safe_fund_treshold
from_safe,
)
except Exception: # pylint: disable=broad-except
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,5 @@
"start": "electron .",
"build": "rm -rf dist/ && electron-builder build"
},
"version": "0.1.0-rc21"
"version": "0.1.0-rc22"
}
69 changes: 36 additions & 33 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "olas-operate-middleware"
version = "0.1.0-rc21"
version = "0.1.0-rc22"
description = ""
authors = ["David Vilela <[email protected]>", "Viraj Patel <[email protected]>"]
readme = "README.md"
Expand Down

0 comments on commit 1aaba6a

Please sign in to comment.