# Ensure private information is already in the shell environment
# RINKEBY_DEPLOYER_PRIVATE_KEY
# RINKEBY_DR_RESOLVER_ADDRESS
# RINKEBY_RPC_URL
# ETHERSCAN_API_KEY

# ------------------------------------------------------------------------
# set some environment variables

RINKEBY_DEPLOYER_ADDRESS="0x0000000000000000000000000000000000000000" # set this
GNOSIS_SAFE_ADDRESS="0x7E16c4Fa845f8d05Bc50Cb9Ad6c875F6178Ce5AA" # set this
PROXY_ADMIN_TIMELOCK_DELAY="60" # set this

# ------------------------------------------------------------------------
# Deploy the contracts from hardhat

# RESET_FLAG="--reset"
RESET_FLAG=""
RINKEBY_DEPLOYER_PRIVATE_KEY=${RINKEBY_DEPLOYER_PRIVATE_KEY} \\
RINKEBY_TREASURY_PRIVATE_KEY=${RINKEBY_DEPLOYER_PRIVATE_KEY} \\
RINKEBY_DR_RESOLVER_ADDRESS=${GNOSIS_SAFE_ADDRESS} \\
RINKEBY_RPC_URL=${RINKEBY_RPC_URL} \\
PROXY_ADMIN_OWNER=${GNOSIS_SAFE_ADDRESS} \\
PROXY_ADMIN_TIMELOCK_DELAY="${PROXY_ADMIN_TIMELOCK_DELAY}" \\
  npx hardhat deploy --network rinkeby \\
  ${RESET_FLAG}

# set these deployed contract address env variables from the values obtained above
TESTERC20_ADDRESS="0x" # set this
PROXYADMIN_ADDRESS="0x" # set this
DAOTREASURY_ADDRESS="0x" # set this
JOB_IMPLEMENTATION_ADDRESS="0x" # set this
JOB_PROXY_ADDRESS="0x" # set this

# ------------------------------------------------------------------------
# Verify all implementations on etherscan

# Use hardhat verify on each of the contracts

# verify TestERC20
ETHERSCAN_API_KEY=${ETHERSCAN_API_KEY} \\
RINKEBY_RPC_URL=${RINKEBY_RPC_URL} \\
  npx hardhat verify --network rinkeby "${TESTERC20_ADDRESS}"

# verify DaoTreasury
ETHERSCAN_API_KEY=${ETHERSCAN_API_KEY} \\
RINKEBY_RPC_URL=${RINKEBY_RPC_URL} \\
  npx hardhat verify --network rinkeby "${DAOTREASURY_ADDRESS}" "0x0000000000000000000000000000000000000000"

# verify Job (Implementation)
ETHERSCAN_API_KEY=${ETHERSCAN_API_KEY} \\
RINKEBY_RPC_URL=${RINKEBY_RPC_URL} \\
  npx hardhat verify --network rinkeby "${JOB_IMPLEMENTATION_ADDRESS}"

# verify ProxyAdmin
tee tmp-arguments.js > /dev/null <<__EOT__
module.exports = [
  ${PROXY_ADMIN_TIMELOCK_DELAY},
  ["${GNOSIS_SAFE_ADDRESS}"],
  ["${GNOSIS_SAFE_ADDRESS}"]
];
__EOT__

ETHERSCAN_API_KEY=${ETHERSCAN_API_KEY} \\
RINKEBY_RPC_URL=${RINKEBY_RPC_URL} \\
  npx hardhat verify --network rinkeby \\
  --contract contracts/ProxyAdmin.sol:ProxyAdmin \\
  --constructor-args tmp-arguments.js \\
  "${PROXYADMIN_ADDRESS}"

rm tmp-arguments.js

# Job Proxy should already be verified - just update etherscan

# ------------------------------------------------------------------------
# Verify ownership of the job proxy is the ProxyAdmin

# verify owner of the job proxy is the ProxyAdmin
echo "Expect owner to be ${PROXYADMIN_ADDRESS}" && echo
RINKEBY_RPC_URL=${RINKEBY_RPC_URL} \\
  npx hardhat proxy-info \\
    --network rinkeby \\
    --proxycontract "${JOB_PROXY_ADDRESS}"

# ------------------------------------------------------------------------
# Change the owner of the DAO treasury to the Gnosis safe

RINKEBY_RPC_URL=${RINKEBY_RPC_URL} \\
  npx hardhat transfer-ownership \\
    --network rinkeby \\
    --privatekey "${RINKEBY_DEPLOYER_PRIVATE_KEY}" \\
    --contractaddress "${DAOTREASURY_ADDRESS}" \\
    --newowner "${GNOSIS_SAFE_ADDRESS}"

# ------------------------------------------------------------------------
# renounce the TIMELOCK_ADMIN_ROLE from the deployer

# check role
RINKEBY_RPC_URL=${RINKEBY_RPC_URL} \\
  npx hardhat --network rinkeby \\
    proxy-admin-check-role \\
    --admincontract "${PROXYADMIN_ADDRESS}" \\
    --address "${RINKEBY_DEPLOYER_ADDRESS}" \\
    --role "TIMELOCK_ADMIN_ROLE"

# renounce the role
RINKEBY_RPC_URL=${RINKEBY_RPC_URL} \\
  npx hardhat --network rinkeby \\
    proxy-admin-renounce-admin-role \\
    --privatekey "${RINKEBY_DEPLOYER_PRIVATE_KEY}" \\
    --admincontract "${PROXYADMIN_ADDRESS}" \\
    --role "TIMELOCK_ADMIN_ROLE"

# check role again
RINKEBY_RPC_URL=${RINKEBY_RPC_URL} \\
  npx hardhat --network rinkeby \\
    proxy-admin-check-role \\
    --admincontract "${PROXYADMIN_ADDRESS}" \\
    --address "${RINKEBY_DEPLOYER_ADDRESS}" \\
    --role "TIMELOCK_ADMIN_ROLE"