#!/bin/bash
#
# This script upgrades the POW! contract on Sepolia
#

if [ "$FOC_ENV_FILE" ]; then
    source $FOC_ENV_FILE
fi

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
PROJECT_DIR=$SCRIPT_DIR/../..

# Load env variable from `.env` only if they're not already set
if [ -z "$STARKNET_KEYSTORE" ] || [ -z "$STARKNET_ACCOUNT" ]; then
  source $PROJECT_DIR/.env
fi

# Check if required env variables are set, if not exit
if [ -z "$STARKNET_KEYSTORE" ]; then
  echo "Error: STARKNET_KEYSTORE is not set."
  exit 1
elif [ -z "$STARKNET_ACCOUNT" ]; then
  echo "Error: STARKNET_ACCOUNT is not set."
  exit 1
fi

# Check if contract address was passed as first argument
CONTRACT_ADDRESS=$1
if [ -z "$CONTRACT_ADDRESS" ]; then
  echo "Error: Contract address must be provided as first argument"
  echo "Usage: $0 <contract_address> <new_class_hash>"
  exit 1
fi

# Check if new class hash was passed as second argument
NEW_CLASS_HASH=$2
if [ -z "$NEW_CLASS_HASH" ]; then
  echo "Error: New class hash must be provided as second argument"
  echo "Usage: $0 <contract_address> <new_class_hash>"
  exit 1
fi

# RPC_URL is loaded from .env file
if [ -z "$RPC_URL" ]; then
  echo "Error: RPC_URL is not set."
  echo "Please set RPC_URL in your .env.sep file or environment variables."
  echo "Example: RPC_URL=https://starknet-sepolia.infura.io/v3/YOUR_API_KEY"
  exit 1
fi

echo "Upgrading POW! contract on Sepolia..."
echo "Contract address: $CONTRACT_ADDRESS"
echo "New class hash: $NEW_CLASS_HASH"
echo
echo "======================================================================"
echo

# Invoke the upgrade method
echo "Invoking upgrade method..."
starkli invoke --rpc $RPC_URL --network sepolia --keystore-password "$STARKNET_KEYSTORE_PASSWORD" --keystore $STARKNET_KEYSTORE --account $STARKNET_ACCOUNT --watch $CONTRACT_ADDRESS upgrade $NEW_CLASS_HASH

echo
echo "Contract upgrade transaction submitted!"
echo "New class hash: $NEW_CLASS_HASH"