module Main
import App from "deployed.app.aqua"

export App, node_report

data Result:
  stderr: string
  stdout: string

data VAResponse:
    amount: string
    locked: string
    code_hash: string
    storage_usage: u64
    storage_paid_at: u64
    block_height: u64
    block_hash: string

data VAResult:
    stderr: string
    stdout: VAResponse

service NearRpcServices:
  gas_price(network_id: string, block_ref: string) -> Result
  node_status(network_id: string) -> Result
  tx_status(network_id: string, tx_id: string, account_id: string, receipt: bool) -> Result
  view_account(network_id: string, account_id: string) -> Result
  view_account_structured(network_id: string, account_id: string) -> VAResult

func node_report(network_id: string) -> Result:
    services <- App.services()
    on services.nearAdapter.default!.peerId:
        NearRpcServices services.nearAdapter.default!.serviceId
        res <- NearRpcServices.node_status(network_id)
    <- res

func node_status(network_id: string, node: string, service_id: string) -> Result: 
    on node:
        NearRpcServices service_id
        res <- NearRpcServices.node_status(network_id)
    <- res

func view_account(network_id: string, account_id: string, node: string, service_id: string) -> Result:
  on node:
    NearRpcServices service_id
        res <- NearRpcServices.view_account(network_id, account_id)
    <- res

func view_account_report(network_id: string, account_id: string) -> Result:
    services <- App.services()
    on services.nearAdapter.default!.peerId:
        NearRpcServices services.nearAdapter.default!.serviceId
        res <- NearRpcServices.view_account(network_id, account_id)
    <- res

func view_account_structured(network_id: string, account_id: string, node: string, service_id: string) -> VAResult:
  on node:
    NearRpcServices service_id
        res <- NearRpcServices.view_account_structured(network_id, account_id)
    <- res

func view_account_structured_report(network_id: string, account_id: string) -> VAResult:
    services <- App.services()
    on services.nearAdapter.default!.peerId:
        NearRpcServices services.nearAdapter.default!.serviceId
        res <- NearRpcServices.view_account_structured(network_id, account_id)
    <- res

func account_balance(network_id: string, account_id: string, node: string, service_id: string) -> string:
  on node:
    NearRpcServices service_id
        res <- NearRpcServices.view_account_structured(network_id, account_id)
    <- res.stdout.amount

func account_balance_report(network_id: string, account_id: string) -> string:
    services <- App.services()
    on services.nearAdapter.default!.peerId:
        NearRpcServices services.nearAdapter.default!.serviceId
        res <- NearRpcServices.view_account_structured(network_id, account_id)
    <- res.stdout.amount

func tx_status(network_id: string, tx_id: string, account_id: string, receipt: bool, node: string, service_id: string) -> Result:
  on node:
    NearRpcServices service_id
    res <- NearRpcServices.tx_status(network_id, tx_id, account_id, receipt)
  <- res

func tx_status_report(network_id: string, tx_id: string, account_id: string, receipt: bool) -> Result:
    services <- App.services()
    on services.nearAdapter.default!.peerId:
        NearRpcServices services.nearAdapter.default!.serviceId
        res <- NearRpcServices.tx_status(network_id, tx_id, account_id, receipt)
    <- res