#!/usr/bin/env bash
set -ex

export ETH_GAS=${ETH_GAS:-"7000000"}

export CONFIG_STEP=${CONFIG_STEP:-"step-7"}
CONFIG_FILE="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )/$CONFIG_STEP.json"

# shellcheck source=/dev/null
./step-4-deploy

# shellcheck source=/dev/null
. load-addresses

tokens=$(jq -r ".tokens | keys_unsorted[]" "$CONFIG_FILE")
for token in $tokens; do
    type=$(jq -r ".tokens.${token} | .pip | .type" "$CONFIG_FILE")
    if [ "${type}" = "value" ]; then # Takes the first token which its pip is a value (not a median, so can be manipulated easily)
        break
    fi
done

ilk=$(jq -r ".tokens.${token}.ilks | keys_unsorted[0]" "$CONFIG_FILE")

seth send "$CDP_MANAGER" 'open(bytes32)' "$(seth --to-bytes32 "$(seth --from-ascii "${token}-${ilk}")")"
ID=$(seth call "$CDP_MANAGER" 'cdpi()(uint256)')
URN=$(seth call "$CDP_MANAGER" 'getUrn(uint256)(bytes32)' "$ID")

if [ "${token}" = "ETH" ]; then
    seth send "$ETH" 'deposit()' --value "$(seth --to-wei 5 ETH)"
fi

seth send "$(eval echo "\$$token")" "approve(address,uint256)" "$(eval echo "\$MCD_JOIN_${token}_${ilk}")" "$(seth --to-uint256 "$(seth --to-wei 5 ETH)")"

seth send "$(eval echo "\$MCD_JOIN_${token}_${ilk}")" 'join(bytes32,uint256)' "$URN" "$(seth --to-uint256 "$(seth --to-wei 5 ETH)")"

dink=$(seth --to-uint256 "$(mcd --to-hex "$(seth --to-wei 5 eth)")")
mat=$(jq -r ".tokens.${token}.ilks.${ilk} | .mat" "$CONFIG_FILE")
price=$(jq -r ".tokens.${token} | .pip | .price" "$CONFIG_FILE")
dart=$(seth --to-uint256 "$(mcd --to-hex "$(seth --to-wei "$(echo "5 * $price * 100 / $mat" | bc -l)" eth)")") # Max debt amount

./set-price "$token" "$price"

seth send "$MCD_SPOT" 'poke(bytes32)' "$(seth --to-bytes32 "$(seth --from-ascii "${token}-${ilk}")")"

seth send "$CDP_MANAGER" 'frob(address,uint256,int256,int256)' "$MCD_VAT" "$ID" "$dink" "$dart"

./set-price "$token" "$(echo "$price - 1" | bc -l)"
seth send "$MCD_SPOT" 'poke(bytes32)' "$(seth --to-bytes32 "$(seth --from-ascii "${token}-${ilk}")")"

# Bite CDP
seth send "$MCD_CAT" 'bite(bytes32,bytes32)' "$(seth --to-bytes32 "$(seth --from-ascii "${token}-${ilk}")")" "$URN"

echo "STEP 7 COMPLETED SUCCESSFULLY"
