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

test -z "$1" && exit 1
if ! [[ $1 =~ ^[0-9]+$ ]]
    then
        exit 1
fi

export SOLC_FLAGS=${SOLC_FLAGS:-"--optimize"}
export ETH_GAS=${ETH_GAS:-"4000000"}
export ETH_FROM=${ETH_FROM:-"$(seth rpc eth_coinbase)"}
NETWORK=$(seth chain)

dustLimits=
# shellcheck source=/dev/null
. "./bin/${NETWORK}-dustlimits"

test -z "$NETWORK" && exit 1

dapp --use solc:0.4.25 build

ETH_NONCE=$(seth nonce "$ETH_FROM")

OTC=$(ETH_NONCE=$ETH_NONCE dapp create MatchingMarket "$1")

export SETH_ASYNC=yes

i=0
for dustLimit in $dustLimits; do
	i=$((i+1))
	token=$(echo "$dustLimit" | cut -f1 -d-)
	limit=$(echo "$dustLimit" | cut -f2 -d-)
	ETH_NONCE=$((ETH_NONCE + i)) seth send "$OTC" "setMinSell(address, uint)" "$token" "$limit"
done

export OTC=$OTC

echo OTC DEPLOYED AT: "$OTC"
