Skip to content

Commit

Permalink
fix: autodeposit logic to use API directly and add custom version con…
Browse files Browse the repository at this point in the history
…trol
  • Loading branch information
Evan-Kim2028 committed Jan 17, 2025
1 parent 8991bbd commit dbfeca1
Showing 1 changed file with 66 additions and 65 deletions.
131 changes: 66 additions & 65 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

set -e

###############################################################################
# 1) Source .env if it exists
###############################################################################
load_env_file() {
local env_file="$1"
if [ -f "$env_file" ]; then
Expand All @@ -16,55 +13,51 @@ load_env_file() {
fi
}

# Load .env from same directory as the script
load_env_file "$(dirname "$(realpath "$0")")/.env"

###############################################################################
# 2) Set defaults
###############################################################################
APP_NAME="mev-commit"
ENVIRONMENT="${ENVIRONMENT:-production}"
MEV_COMMIT_VERSION="${MEV_COMMIT_VERSION:-unknown}"
MEV_COMMIT_VERSION="${MEV_COMMIT_VERSION:-latest}"
DOMAIN="${DOMAIN:-testnet.mev-commit.xyz}"
AUTO_DEPOSIT_VALUE="${AUTO_DEPOSIT_VALUE:-300000000000000000}" # Default 0.3 ETH
AUTO_DEPOSIT_VALUE="${AUTO_DEPOSIT_VALUE:-300000000000000000}"
BINARY_PATH="${BINARY_PATH:-/usr/local/bin/mev-commit}"
ARTIFACTS_BASE_URL="https://github.com/primev/mev-commit/releases"
RPC_URL="${RPC_URL:-wss://chainrpc-wss.${DOMAIN}}"
API_URL="http://127.0.0.1:13523"

# Logging
LOG_FMT="${LOG_FMT:-json}"
LOG_LEVEL="${LOG_LEVEL:-info}"

# For mev-commit >= v0.8.0, we must pass a file to --priv-key-file.

# We'll use an in-memory file descriptor so the key never touches disk.
PRIV_KEY_FD="/proc/self/fd/3"


echo "Using RPC_URL=${RPC_URL}"

###############################################################################
# 3) (Optional) Download mev-commit if needed
###############################################################################
INSTALLED_VERSION=""
if command -v "${BINARY_PATH}" &> /dev/null; then
INSTALLED_VERSION=$("${BINARY_PATH}" --version 2>/dev/null | awk '{print $3}')
echo "Installed mev-commit version: ${INSTALLED_VERSION}"
fi

if [ "${MEV_COMMIT_VERSION}" = "unknown" ]; then

if [ "${MEV_COMMIT_VERSION}" = "latest" ]; then
LATEST_VERSION=$(curl -sIL -o /dev/null -w %{url_effective} \
https://github.com/primev/mev-commit/releases/latest \
"${ARTIFACTS_BASE_URL}/latest" \
| sed 's:.*/::' | sed 's/^v//')
echo "Latest mev-commit version: ${LATEST_VERSION}"
else
LATEST_VERSION=${MEV_COMMIT_VERSION}

LATEST_VERSION="${MEV_COMMIT_VERSION}"
echo "Using mev-commit version: ${LATEST_VERSION}"
fi

# Download if not installed or if the version is out of date
if [ -z "${INSTALLED_VERSION}" ] || [ "${INSTALLED_VERSION}" != "${LATEST_VERSION}" ]; then
echo "Downloading mev-commit ${LATEST_VERSION}..."
FILE="mev-commit_${LATEST_VERSION}_Linux_x86_64.tar.gz"
DOWNLOAD_URL="${ARTIFACTS_BASE_URL}/download/v${LATEST_VERSION}/${FILE}"

TEMP_DIR=$(mktemp -d)

curl -sL "${DOWNLOAD_URL}" -o "${TEMP_DIR}/${FILE}"
Expand All @@ -81,11 +74,8 @@ else
echo "Using existing mev-commit binary found at ${BINARY_PATH}"
fi

###############################################################################
# 4) Print usage/info
###############################################################################
echo "**********************************************************************"
echo " Starting up the mev-commit Bidder Node (v0.8.0) "
echo " Starting up the mev-commit Bidder Node "
echo "**********************************************************************"
echo ""
echo "Configuration variables (current defaults):"
Expand All @@ -101,9 +91,6 @@ echo "You can set these in a .env file or pass CLI args. Example usage:"
echo " ./entrypoint.sh --private-key <HEXKEY> --auto-deposit 1000000000000000000"
echo ""

###############################################################################
# 5) Parse CLI arguments
###############################################################################
while [[ $# -gt 0 ]]; do
case "$1" in
--domain)
Expand Down Expand Up @@ -149,9 +136,6 @@ while [[ $# -gt 0 ]]; do
esac
done

###############################################################################
# 6) Ensure private key is set (prompt only if missing)
###############################################################################
if [ -z "${PRIVATE_KEY_BIDDER}" ]; then
echo "No PRIVATE_KEY_BIDDER environment variable found."
read -r -s -p "Enter your private key (hex, no 0x): " PRIVATE_KEY_BIDDER
Expand All @@ -163,28 +147,17 @@ if [ -z "${PRIVATE_KEY_BIDDER}" ]; then
exit 1
fi

###############################################################################
# 7) Decide on auto-deposit (skip prompt if AUTO_DEPOSIT_VALUE is already set)
###############################################################################
# If user didn't override AUTO_DEPOSIT_VALUE, ask if they want the default

if [ "${AUTO_DEPOSIT_VALUE}" = "300000000000000000" ]; then
# It's still the default. Confirm if user wants to override
echo "Current auto-deposit: ${AUTO_DEPOSIT_VALUE} (default 0.3 ETH)."
read -r -p "Use the default auto-deposit value? (y/n): " USE_DEFAULT_DEPOSIT
if [[ ! "${USE_DEFAULT_DEPOSIT,,}" =~ ^(y|yes)$ ]]; then
read -r -p "Enter custom auto-deposit value (in Wei): " AUTO_DEPOSIT_VALUE
fi
else
# If the user specified a custom value in ENV or CLI, skip the prompt
echo "Using auto-deposit value from environment: ${AUTO_DEPOSIT_VALUE}"
fi

# For a bidder node, we enable auto-deposit by default
AUTO_DEPOSIT_ENABLED="true"

###############################################################################
# 8) Fetch contract addresses from ${CONTRACTS_URL}
###############################################################################
BOOTNODE="/dnsaddr/bootnode.${DOMAIN}"
CONTRACTS_URL="https://contracts.${DOMAIN}"

Expand All @@ -199,51 +172,79 @@ PROVIDER_REGISTRY_ADDR=$(echo "${contracts_json}" | jq -r '.ProviderRegistry')
BLOCK_TRACKER_ADDR=$(echo "${contracts_json}" | jq -r '.BlockTracker')
PRECONF_ADDR=$(echo "${contracts_json}" | jq -r '.PreconfManager')

###############################################################################
# 9) Build CLI flags for mev-commit 0.8.0
###############################################################################


FLAGS=(
--peer-type "bidder"

# Settlement RPC (WebSocket)
--settlement-ws-rpc-endpoint "${RPC_URL}"

# Logging
--log-fmt "${LOG_FMT}"
--log-level "${LOG_LEVEL}"

# Bootnodes
--bootnodes "${BOOTNODE}"

# Key file is read from an in-memory FD
--priv-key-file "${PRIV_KEY_FD}"

# Bidder-specific
--bidder-bid-timeout "15s"

# Contracts
--bidder-registry-contract "${BIDDER_REGISTRY_ADDR}"
--provider-registry-contract "${PROVIDER_REGISTRY_ADDR}"
--block-tracker-contract "${BLOCK_TRACKER_ADDR}"
--preconf-contract "${PRECONF_ADDR}"

# Auto-deposit
--autodeposit-enabled="${AUTO_DEPOSIT_ENABLED}"
--autodeposit-amount "${AUTO_DEPOSIT_VALUE}"
)

###############################################################################
# 10) Start mev-commit without writing key to disk
###############################################################################

echo ""
echo "Starting mev-commit with the following flags:"
for f in "${FLAGS[@]}"; do
echo " $f"
done
echo ""

# We open a file descriptor (FD #3) containing the private key in memory.
# mev-commit reads that FD as if it were a file, but nothing is written to disk.
exec 3<<< "${PRIVATE_KEY_BIDDER}"

exec "${BINARY_PATH}" "${FLAGS[@]}"
"${BINARY_PATH}" "${FLAGS[@]}" &
MEV_COMMIT_PID=$!

wait_for_api() {
local url="$1"
local timeout=60
local start_time=$(date +%s)

echo "Waiting for mev-commit API to be ready at ${url}..."
while true; do
if curl -s -o /dev/null -w "%{http_code}" "${url}/health" | grep -q "200"; then
echo "mev-commit API is ready."
return 0
fi

local current_time=$(date +%s)
local elapsed_time=$((current_time - start_time))
if (( elapsed_time >= timeout )); then
echo "Timeout reached, mev-commit API did not become ready."
return 1
fi

sleep 1
done
}

if ! wait_for_api "${API_URL}"; then
echo "Failed to start mev-commit or API did not become ready."
kill "${MEV_COMMIT_PID}"
exit 1
fi

echo "Sending auto-deposit request..."
AUTO_DEPOSIT_RESPONSE=$(
curl \
--silent \
--show-error \
--output /dev/null \
--write-out "%{http_code}" \
--request POST "${API_URL}/v1/bidder/auto_deposit/${AUTO_DEPOSIT_VALUE}"
)

if [ "${AUTO_DEPOSIT_RESPONSE}" -ne 200 ]; then
echo "Failed to send auto-deposit request, status code: ${AUTO_DEPOSIT_RESPONSE}"
kill "${MEV_COMMIT_PID}"
exit 1
fi
echo "Auto-deposit request sent successfully."

wait "${MEV_COMMIT_PID}"

0 comments on commit dbfeca1

Please sign in to comment.