#!/bin/bash

set -e -x

# Pass API keys by environment variables
# Fail if the variable is not set
set -u # or set -o nounset
: "$CLOUDFLARE_ETHEREUM_MAINNET_API_KEY"
: "$ANKR_OPTIMISM_MAINNET_API_KEY"
: "$ANKR_BASE_MAINNET_API_KEY"
: "$ANKR_ETHEREUM_MAINNET_API_KEY"
: "$ANKR_ARBITRUM_ONE_API_KEY"
: "$BLOCK_PI_OPTIMISM_MAINNET_API_KEY"
: "$BLOCK_PI_BASE_MAINNET_API_KEY"
: "$BLOCK_PI_ETHEREUM_MAINNET_API_KEY"
: "$BLOCK_PI_ARBITRUM_ONE_API_KEY"
: "$ALCHEMY_OPTIMISM_MAINNET_API_KEY"
: "$ALCHEMY_BASE_MAINNET_API_KEY"
: "$ALCHEMY_ETHEREUM_MAINNET_API_KEY"
: "$ALCHEMY_ARBITRUM_ONE_API_KEY"

NETWORK="ic"
IDENTITY="hsm"
WALLET=$(dfx identity get-wallet --network=$NETWORK --identity=$IDENTITY)
CANISTER="evm_rpc"
FLAGS="--network=$NETWORK --identity=$IDENTITY --wallet=$WALLET"

CLOUDFLARE_ETHEREUM_MAINNET_PROVIDER_ID="0";

ANKR_OPTIMISM_MAINNET_PROVIDER_ID="18";
ANKR_BASE_MAINNET_PROVIDER_ID="14";
ANKR_ETHEREUM_MAINNET_PROVIDER_ID="1";
ANKR_ARBITRUM_ONE_PROVIDER_ID="10";

BLOCK_PI_OPTIMISM_MAINNET_PROVIDER_ID="20";
BLOCK_PI_BASE_MAINNET_PROVIDER_ID="16";
BLOCK_PI_ETHEREUM_MAINNET_PROVIDER_ID="3";
BLOCK_PI_ARBITRUM_ONE_PROVIDER_ID="12";

ALCHEMY_OPTIMISM_MAINNET_PROVIDER_ID="19";
ALCHEMY_BASE_MAINNET_PROVIDER_ID="15";
ALCHEMY_ETHEREUM_MAINNET_PROVIDER_ID="8";
ALCHEMY_ARBITRUM_ONE_PROVIDER_ID="11";

dfx canister call ${CANISTER} updateApiKeys "(vec {
  record { ${CLOUDFLARE_ETHEREUM_MAINNET_PROVIDER_ID} : nat64; opt \"${CLOUDFLARE_ETHEREUM_MAINNET_API_KEY}\" };
  record { ${ANKR_OPTIMISM_MAINNET_PROVIDER_ID} : nat64; opt \"${ANKR_OPTIMISM_MAINNET_API_KEY}\" };
  record { ${ANKR_BASE_MAINNET_PROVIDER_ID} : nat64; opt \"${ANKR_BASE_MAINNET_API_KEY}\" };
  record { ${ANKR_ETHEREUM_MAINNET_PROVIDER_ID} : nat64; opt \"${ANKR_ETHEREUM_MAINNET_API_KEY}\" };
  record { ${ANKR_ARBITRUM_ONE_PROVIDER_ID} : nat64; opt \"${ANKR_ARBITRUM_ONE_API_KEY}\" };
  record { ${BLOCK_PI_OPTIMISM_MAINNET_PROVIDER_ID} : nat64; opt \"${BLOCK_PI_OPTIMISM_MAINNET_API_KEY}\" };
  record { ${BLOCK_PI_BASE_MAINNET_PROVIDER_ID} : nat64; opt \"${BLOCK_PI_BASE_MAINNET_API_KEY}\" };
  record { ${BLOCK_PI_ETHEREUM_MAINNET_PROVIDER_ID} : nat64; opt \"${BLOCK_PI_ETHEREUM_MAINNET_API_KEY}\" };
  record { ${BLOCK_PI_ARBITRUM_ONE_PROVIDER_ID} : nat64; opt \"${BLOCK_PI_ARBITRUM_ONE_API_KEY}\" };
  record { ${ALCHEMY_OPTIMISM_MAINNET_PROVIDER_ID} : nat64; opt \"${ALCHEMY_OPTIMISM_MAINNET_API_KEY}\" };
  record { ${ALCHEMY_BASE_MAINNET_PROVIDER_ID} : nat64; opt \"${ALCHEMY_BASE_MAINNET_API_KEY}\" };
  record { ${ALCHEMY_ETHEREUM_MAINNET_PROVIDER_ID} : nat64; opt \"${ALCHEMY_ETHEREUM_MAINNET_API_KEY}\" };
  record { ${ALCHEMY_ARBITRUM_ONE_PROVIDER_ID} : nat64; opt \"${ALCHEMY_ARBITRUM_ONE_API_KEY}\" };
})" ${FLAGS}

