# Compose v2 - Chain Gateway

x-common-env: &common-env
  env_file:
    - ../env/.env.runtime
    - ../env/.env.version

volumes:
  chain-gateway-vault-run:
    driver: local
    driver_opts:
      type: tmpfs
      device: tmpfs
      o: size=8m,uid=0,gid=0,mode=0700

networks:
  sendico-net:
    external: true
    name: sendico-net

services:
  sendico_chain_gateway:
    <<: *common-env
    container_name: sendico-chain-gateway
    restart: unless-stopped
    image: ${REGISTRY_URL}/chain/gateway:${APP_V}
    pull_policy: always
    environment:
      CHAIN_GATEWAY_MONGO_HOST: ${CHAIN_GATEWAY_MONGO_HOST}
      CHAIN_GATEWAY_MONGO_PORT: ${CHAIN_GATEWAY_MONGO_PORT}
      CHAIN_GATEWAY_MONGO_DATABASE: ${CHAIN_GATEWAY_MONGO_DATABASE}
      CHAIN_GATEWAY_MONGO_USER: ${CHAIN_GATEWAY_MONGO_USER}
      CHAIN_GATEWAY_MONGO_PASSWORD: ${CHAIN_GATEWAY_MONGO_PASSWORD}
      CHAIN_GATEWAY_MONGO_AUTH_SOURCE: ${CHAIN_GATEWAY_MONGO_AUTH_SOURCE}
      CHAIN_GATEWAY_MONGO_REPLICA_SET: ${CHAIN_GATEWAY_MONGO_REPLICA_SET}
      MONGO_HOSTS_0: ${MONGO_HOSTS_0}
      MONGO_PORTS_0: ${MONGO_PORTS_0}
      MONGO_HOSTS_1: ${MONGO_HOSTS_1}
      MONGO_PORTS_1: ${MONGO_PORTS_1}
      MONGO_HOSTS_2: ${MONGO_HOSTS_2}
      MONGO_PORTS_2: ${MONGO_PORTS_2}
      NATS_URL: ${NATS_URL}
      NATS_HOST: ${NATS_HOST}
      NATS_PORT: ${NATS_PORT}
      NATS_USER: ${NATS_USER}
      NATS_PASSWORD: ${NATS_PASSWORD}
      CHAIN_GATEWAY_ARBITRUM_RPC_URL: ${CHAIN_GATEWAY_ARBITRUM_RPC_URL}
      CHAIN_GATEWAY_SERVICE_WALLET_KEY: ${CHAIN_GATEWAY_SERVICE_WALLET_KEY}
      CHAIN_GATEWAY_SERVICE_WALLET_ADDRESS: ${CHAIN_GATEWAY_SERVICE_WALLET_ADDRESS}
      VAULT_TOKEN_FILE: /run/vault/token
    command: ["--config.file", "/app/config.yml"]
    ports:
      - "0.0.0.0:${CHAIN_GATEWAY_GRPC_PORT}:50070"
      - "0.0.0.0:${CHAIN_GATEWAY_METRICS_PORT}:9403"
    volumes:
      - chain-gateway-vault-run:/run/vault:ro
    depends_on:
      sendico_chain_gateway_vault_agent:
        condition: service_healthy
    healthcheck:
      test: ["CMD-SHELL","wget -qO- http://localhost:9403/health | grep -q '\"status\":\"ok\"'"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s
    networks:
      - sendico-net

  sendico_chain_gateway_vault_agent:
    <<: *common-env
    container_name: sendico-chain-gateway-vault-agent
    restart: unless-stopped
    image: hashicorp/vault:latest
    pull_policy: always
    cap_add: ["IPC_LOCK"]
    environment:
      VAULT_ADDR: ${VAULT_ADDR}
      CHAIN_GATEWAY_VAULT_ROLE_ID: ${CHAIN_GATEWAY_VAULT_ROLE_ID}
      CHAIN_GATEWAY_VAULT_SECRET_ID: ${CHAIN_GATEWAY_VAULT_SECRET_ID}
    command: >
      sh -lc 'set -euo pipefail; umask 077;
              : "${CHAIN_GATEWAY_VAULT_ROLE_ID:?}"; : "${CHAIN_GATEWAY_VAULT_SECRET_ID:?}";
              printf "%s" "$CHAIN_GATEWAY_VAULT_ROLE_ID"   > /run/vault/role_id;
              printf "%s" "$CHAIN_GATEWAY_VAULT_SECRET_ID" > /run/vault/secret_id;
              unset CHAIN_GATEWAY_VAULT_ROLE_ID CHAIN_GATEWAY_VAULT_SECRET_ID;
              exec vault agent -config=/etc/vault/agent/chain-gateway.hcl'
    volumes:
      - ./vault-agent/chain-gateway.hcl:/etc/vault/agent/chain-gateway.hcl:ro
      - chain-gateway-vault-run:/run/vault
    healthcheck:
      test: ["CMD","test","-s","/run/vault/token"]
      interval: 10s
      timeout: 5s
      retries: 6
    networks:
      - sendico-net
