#!/bin/bash

die() {
    printf "ERROR: %s\n" "$*"
    exit 1
}

dieOnError() {
    if [ $? -ne 0 ]; then
        die $1
    fi
}

NETWORK=${NETWORK:-localhost}

#    --currency-address 0xf8acfb5d9a57216974819f8b2da74e1fb18902cc \
export DEPLOY_AMOUNT_DECIMALS=6

rm /tmp/create-pool-output.txt
npx hardhat --network $NETWORK deploy $VERIFY \
    --currency-address 0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174 \
    --nft-name "Ensuro Policies NFT" --nft-symbol "EPOL" \
    --treasury-address 0xbBFFca2C1fa80B5051ea832dCe55B57e51A13CEe | tee /tmp/create-pool-output.txt
dieOnError "Error deploying pool"

POOL=`egrep -o  "^PolicyPool deployed to: (https?://.*/)?0x[0-9a-fA-F]+" /tmp/create-pool-output.txt |
    sed -r 's|PolicyPool deployed to: (https?://.*/)?(0x[0-9a-fA-F]+)|\2|g'`
POOL_CONFIG=`egrep -o  "^PolicyPoolConfig deployed to: (https?://.*/)?0x[0-9a-fA-F]+" /tmp/create-pool-output.txt |
    sed -r 's|PolicyPoolConfig deployed to: (https?://.*/)?(0x[0-9a-fA-F]+)|\2|g'`

echo "PolicyPool = $POOL"

read
# KOALA_WALLET="0xF9ED268a71875043623eb4A6170F20F05Ac607d1"  # TODO: later ask for production one
KOALA_WALLET="0xbBFFca2C1fa80B5051ea832dCe55B57e51A13CEe"  # using our treasury, later we have to change it
KOALA_WALLET="0xD01587EcD64504851E181b36153ef4d93c2bf939"  # Provided by Dimrok 2021-12-21

npx hardhat --network $NETWORK deploy:eToken $VERIFY --pool-address $POOL \
    --etk-symbol eUSD1YEAR --etk-name "eToken USD 1year" --expiration-period 365

read

SCR_PERCENTAGE="0.2448"
ENSURO_FEE="0.0321"
SCR_INTEREST_RATE="0.0729"
MOC="1.285"
MAX_SCR_PER_POLICY="500"
SCR_LIMIT="250000"

npx hardhat --network $NETWORK deploy:riskModule $VERIFY --pool-address $POOL \
    --rm-name "Koala Partner A" --max-scr-per-policy $MAX_SCR_PER_POLICY --scr-limit $SCR_LIMIT \
    --scr-percentage $SCR_PERCENTAGE \
    --ensuro-fee $ENSURO_FEE \
    --moc $MOC \
    --scr-interest-rate $SCR_INTEREST_RATE \
    --wallet $KOALA_WALLET
dieOnError "Error deploying Trustfull RM"

# npx hardhat --network $NETWORK deploy:riskModule $VERIFY --pool-address $POOL \
#     --rm-name "Koala Partner B" --max-scr-per-policy $MAX_SCR_PER_POLICY --scr-limit $SCR_LIMIT \
#     --scr-percentage $SCR_PERCENTAGE \
#     --ensuro-fee $ENSURO_FEE \
#     --moc $MOC \
#     --scr-interest-rate $SCR_INTEREST_RATE \
#     --wallet $KOALA_WALLET
# dieOnError "Error deploying Trustfull RM"

# npx hardhat --network $NETWORK deploy:riskModule $VERIFY --pool-address $POOL \
#    --rm-name "Koala Partner C" --max-scr-per-policy $MAX_SCR_PER_POLICY --scr-limit $SCR_LIMIT \
#    --scr-percentage $SCR_PERCENTAGE \
#    --ensuro-fee $ENSURO_FEE \
#    --moc $MOC \
#    --scr-interest-rate $SCR_INTEREST_RATE \
#    --wallet $KOALA_WALLET
# dieOnError "Error deploying Trustfull RM"

read

AAVE_ADDR_PROV=0xd05e3E715d945B59290df0ae8eF85c1BdB684744  # Polygon
SWAP_ROUTER=0x1b02dA8Cb0d097eB8D57A175b88c7D8b47997506  # Polygon

# AaveAssetManager
npx hardhat --network $NETWORK deploy:aaveAssetManager $VERIFY \
   --pool-address $POOL \
   --liquidity-min 1000 \
   --liquidity-middle 2000 \
   --liquidity-max 3000 \
   --claim-rewards-min 80 \
   --reinvest-rewards-min 100 \
   --aave-addr-prov $AAVE_ADDR_PROV \
   --swap-router $SWAP_ROUTER || die "Error deploying AaveAssetManager"

# Whitelist
npx hardhat --network $NETWORK deploy:whitelist $VERIFY \
    --pool-address $POOL
dieOnError "Error deploying Whitelist"

# npx hardhat --network $NETWORK ens:grantRole --contract-address $POOL_CONFIG \
#     --role LP_WHITELIST_ROLE \
#     --account 0x539fe6aD90931e02F902618d0e33D326F670B89C
#
# # Whitelist LP
# # Approve from LP to Pool
# # Deposit from LP to Pool
# # Grant Role to Koala Relay
#
# RM_ADDRESSES="0xD6F5494e724bAeD8ABD48D296Ac06FbF98f4566e 0xD9F2DDF2B48be00b309650C77908763b66dDF41B 0xad1889A60946123B60053B9C1f988E4Ed7DbB77C"
# KOALA_DEFENDER=0x2417dee3bd29b6b63f6ab954cd9e4e7e0552bcfd
#
# for RM in $RM_ADDRESSES; do
#   npx hardhat --network $NETWORK ens:grantRole --contract-address $RM \
#       --role PRICER_ROLE \
#       --account $KOALA_DEFENDER
#   npx hardhat --network $NETWORK ens:grantRole --contract-address $RM \
#       --role RESOLVER_ROLE \
#       --account $KOALA_DEFENDER
# done
