akash-network / console

Deploy docker containers on the Akash Network
https://console.akash.network
Apache License 2.0
84 stars 55 forks source link

Implement Auto Top Up Worker #395

Open baktun14 opened 1 month ago

baktun14 commented 1 month ago

Objective

Implement a worker CLI handler that automatically adds funds (top-ups) to Akash Network deployments when they are low on balance. This ensures deployments continue to run without requiring users to manually monitor and replenish funds, improving the user experience.

Scope

Details

General Workflow

  1. Target Owners Detection

    • For custodial wallets retrieve the deployment deposit grants given to the master wallet from the chain.
    • For managed wallets retrieve all the wallets from database.
    • For all of the above extract wallets addresses as deployments' owners.
  2. Draining Deployments Detection (per every owner)

    • Retrieve all deployments with grants that are about to drain (use calculations provided below).
  3. Top-up Action (per every deployment)

    • Calculate the required top-up amount (use calculations provided below).
    • For custodial wallets verify owner has sufficient balance to top up.
    • For managed wallets verify owner is granted sufficient funds to top up.
    • For custodial wallets send a transaction to the dedicate master wallet of the relevant denomination to top-up the deployment with the owner as the granter.
    • For managed wallets send a transaction to top-up the deployment to the same wallet with the master wallet as the granter.

Acceptance Criteria

Calculations

  // of the above is estimated block height of closure
  Math.ceil(deployment.lastWithdrawalHeight || lease.createHeight) + deployment.balance / blockRate

The above can be used to calculate the time when a deployment is expected to close. To achieve that average block time is needed which can be found in apps/api constants.ts or blocksService.ts.

Important to include job run interval into calculations to ensure not missing the time of closure.

To calculate amount to top up we need to get a number of blocks per time unit which is duration / average block time and multiply that by blockRate.

Design Chart For Reference

image

Link

Initial task version below

baktun14 commented 1 month ago

Need #398

ygrishajev commented 1 month ago

Started compiling a design chart

ygrishajev commented 1 month ago

Need #398

just for transparency, after a discussion we agreed there is a way to implement it without dependency on auth

ygrishajev commented 3 weeks ago

Right now there's a limitation of DepositDeploymentAuthorization that it only allows for one denom meaning we cant have same granter/grantee for both uakt and usdc grants. So there's an update to requirements:

Implement 2 new master wallets specifically for Auto Top Up. 1 wallet should serve for uakt top ups, and another for usdc grants.