From 24a9ab3a914eaa0a76cdcbb6d3da4ca667c7f4e9 Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Wed, 18 Oct 2023 17:21:07 +0530 Subject: [PATCH] Add a container for ERC20 contract txs in the payments stack --- app/data/compose/docker-compose-ponder.yml | 17 ++++++ .../config/ponder/deploy-erc20-contract.sh | 54 +++++++++++++++++++ app/data/config/ponder/ponder-start.sh | 16 ++++++ .../config/ponder/ponder.indexer.config.ts | 2 +- .../config/ponder/ponder.watcher.config.ts | 2 +- .../cerc-watcher-erc20/Dockerfile | 7 +-- .../cerc-watcher-erc20/build.sh | 2 +- app/data/stacks/fixturenet-payments/stack.yml | 2 + 8 files changed, 94 insertions(+), 8 deletions(-) create mode 100755 app/data/config/ponder/deploy-erc20-contract.sh diff --git a/app/data/compose/docker-compose-ponder.yml b/app/data/compose/docker-compose-ponder.yml index 517e26ab..a18f4ec6 100644 --- a/app/data/compose/docker-compose-ponder.yml +++ b/app/data/compose/docker-compose-ponder.yml @@ -1,6 +1,20 @@ version: '3.7' services: + ponder-er20-contracts: + image: cerc/watcher-erc20:local + restart: on-failure + environment: + CERC_SCRIPT_DEBUG: ${CERC_SCRIPT_DEBUG} + CERC_ETH_RPC_ENDPOINT: ${CERC_ETH_RPC_ENDPOINT:-http://fixturenet-eth-geth-1:8545} + CERC_PRIVATE_KEY_DEPLOYER: ${CERC_PRIVATE_KEY_DEPLOYER:-0x888814df89c4358d7ddb3fa4b0213e7331239a80e1f013eaa7b2deca2a41a218} + volumes: + - ../config/ponder/deploy-erc20-contract.sh:/app/deploy-erc20-contract.sh + - erc20_deployment:/app/deployment + command: ["bash", "-c", "/app/deploy-erc20-contract.sh"] + extra_hosts: + - "host.docker.internal:host-gateway" + ponder-app-indexer: hostname: ponder-app-indexer restart: unless-stopped @@ -23,6 +37,7 @@ services: - ../config/ponder/base-rates-config.json:/app/examples/token-erc20/base-rates-config.json - peers_ids:/peers - nitro_deployment:/nitro + - erc20_deployment:/erc20 - ponder_indexer_nitro_data:/app/examples/token-erc20/.ponder/nitro-db ports: - "42070" @@ -52,6 +67,7 @@ services: - ../config/ponder/base-rates-config.json:/app/examples/token-erc20/base-rates-config.json - peers_ids:/peers - nitro_deployment:/nitro + - erc20_deployment:/erc20 - ponder_watcher_nitro_data:/app/examples/token-erc20/.ponder/nitro-db ports: - "42069" @@ -61,5 +77,6 @@ services: volumes: peers_ids: nitro_deployment: + erc20_deployment: ponder_indexer_nitro_data: ponder_watcher_nitro_data: diff --git a/app/data/config/ponder/deploy-erc20-contract.sh b/app/data/config/ponder/deploy-erc20-contract.sh new file mode 100755 index 00000000..4387bc19 --- /dev/null +++ b/app/data/config/ponder/deploy-erc20-contract.sh @@ -0,0 +1,54 @@ +#!/bin/bash + +set -e +if [ -n "$CERC_SCRIPT_DEBUG" ]; then + set -x +fi + +erc20_address_file="/app/deployment/erc20-address.json" + +# Check and exit if a deployment already exists (on restarts) +if [ -f ${erc20_address_file} ]; then + echo "${erc20_address_file} already exists, skipping ERC20 contract deployment" + cat ${erc20_address_file} + exit +fi + +wait_for_chain_endpoint() { + # Wait till ETH RPC endpoint is available with block number > 1 + retry_interval=5 + while true; do + block_number_hex=$(curl -s -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}' ${CERC_ETH_RPC_ENDPOINT} | jq -r '.result') + + # Check if the request call was successful + if [ $? -ne 0 ]; then + echo "RPC endpoint ${CERC_ETH_RPC_ENDPOINT} not yet available, retrying in $retry_interval seconds..." + sleep $retry_interval + continue + fi + + # Convert hex to decimal + block_number_dec=$(printf %u ${block_number_hex}) + + # Check if block number is > 1 to avoid failures in the deployment + if [ "$block_number_dec" -ge 1 ]; then + echo "RPC endpoint ${CERC_ETH_RPC_ENDPOINT} is up" + break + else + echo "RPC endpoint ${CERC_ETH_RPC_ENDPOINT} not yet available, retrying in $retry_interval seconds..." + sleep $retry_interval + continue + fi + done +} + +wait_for_chain_endpoint + +echo "Using CERC_PRIVATE_KEY_DEPLOYER from env" + +echo ETH_RPC_URL=${CERC_ETH_RPC_ENDPOINT} > .env +echo ACCOUNT_PRIVATE_KEY=${CERC_PRIVATE_KEY_DEPLOYER} >> .env +yarn token:deploy:docker --file ${erc20_address_file} + +# Keep the container running +tail -f diff --git a/app/data/config/ponder/ponder-start.sh b/app/data/config/ponder/ponder-start.sh index 4fec4120..1c2bb4a2 100755 --- a/app/data/config/ponder/ponder-start.sh +++ b/app/data/config/ponder/ponder-start.sh @@ -31,6 +31,21 @@ else exit 1 fi +# Read ERC20 address from a file +# Keep retrying until found +erc20_address_file="/erc20/erc20-address.json" +echo "Reading ERC20 address from ${erc20_address_file}" +retry_interval=5 +while true; do + if [[ -e "$erc20_address_file" ]]; then + ERC20_CONTRACT=$(jq -r '.address' ${erc20_address_file}) + break + else + echo "File not yet available, retrying in $retry_interval seconds..." + sleep $retry_interval + fi +done + echo "Using CERC_PONDER_NITRO_PK from env for Nitro account" echo "Using CERC_PONDER_NITRO_CHAIN_PK (account with funds) from env for sending Nitro txs" echo "Using ${CERC_PONDER_NITRO_CHAIN_URL} as the RPC endpoint for Nitro txs" @@ -56,6 +71,7 @@ echo "UPSTREAM_NITRO_PAY_AMOUNT=\"$CERC_UPSTREAM_NITRO_PAY_AMOUNT\"" >> "$env_fi echo "INDEXER_GQL_ENDPOINT=\"$CERC_INDEXER_GQL_ENDPOINT\"" >> "$env_file" echo "INDEXER_NITRO_ADDRESS=\"$CERC_INDEXER_NITRO_ADDRESS\"" >> "$env_file" echo "INDEXER_NITRO_PAY_AMOUNT=\"$CERC_INDEXER_NITRO_PAY_AMOUNT\"" >> "$env_file" +echo "ERC20_CONTRACT=\"$ERC20_CONTRACT\"" >> "$env_file" cat "$env_file" diff --git a/app/data/config/ponder/ponder.indexer.config.ts b/app/data/config/ponder/ponder.indexer.config.ts index 218417c0..dac02724 100644 --- a/app/data/config/ponder/ponder.indexer.config.ts +++ b/app/data/config/ponder/ponder.indexer.config.ts @@ -34,7 +34,7 @@ export const config: Config = { name: "AdventureGold", network: "fixturenet", abi: "./abis/AdventureGold.json", - address: "0x32353A6C91143bfd6C7d363B546e62a9A2489A20", + address: process.env.ERC20_CONTRACT, startBlock: 5, maxBlockRange: 100, }, diff --git a/app/data/config/ponder/ponder.watcher.config.ts b/app/data/config/ponder/ponder.watcher.config.ts index a5615f86..ab62f246 100644 --- a/app/data/config/ponder/ponder.watcher.config.ts +++ b/app/data/config/ponder/ponder.watcher.config.ts @@ -14,7 +14,7 @@ export const config: Config = { name: "AdventureGold", network: "fixturenet", abi: "./abis/AdventureGold.json", - address: "0x32353A6C91143bfd6C7d363B546e62a9A2489A20", + address: process.env.ERC20_CONTRACT, startBlock: 5, maxBlockRange: 100, }, diff --git a/app/data/container-build/cerc-watcher-erc20/Dockerfile b/app/data/container-build/cerc-watcher-erc20/Dockerfile index 5ec94987..19be25ee 100644 --- a/app/data/container-build/cerc-watcher-erc20/Dockerfile +++ b/app/data/container-build/cerc-watcher-erc20/Dockerfile @@ -1,13 +1,10 @@ FROM node:16.17.1-alpine3.16 -RUN apk --update --no-cache add git python3 alpine-sdk +RUN apk --update --no-cache add git python3 alpine-sdk bash curl jq WORKDIR /app COPY . . -RUN echo "Building watcher-ts" && \ - git checkout v0.2.19 && \ +RUN echo "Building erc20-watcher-ts" && \ yarn && yarn build - -WORKDIR /app/packages/erc20-watcher diff --git a/app/data/container-build/cerc-watcher-erc20/build.sh b/app/data/container-build/cerc-watcher-erc20/build.sh index 67ac0d24..6817968b 100755 --- a/app/data/container-build/cerc-watcher-erc20/build.sh +++ b/app/data/container-build/cerc-watcher-erc20/build.sh @@ -6,4 +6,4 @@ source ${CERC_CONTAINER_BASE_DIR}/build-base.sh # See: https://stackoverflow.com/a/246128/1701505 SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) -docker build -t cerc/watcher-erc20:local -f ${SCRIPT_DIR}/Dockerfile ${build_command_args} ${CERC_REPO_BASE_DIR}/watcher-ts +docker build -t cerc/watcher-erc20:local -f ${SCRIPT_DIR}/Dockerfile ${build_command_args} ${CERC_REPO_BASE_DIR}/erc20-watcher-ts diff --git a/app/data/stacks/fixturenet-payments/stack.yml b/app/data/stacks/fixturenet-payments/stack.yml index f18b0064..7421851a 100644 --- a/app/data/stacks/fixturenet-payments/stack.yml +++ b/app/data/stacks/fixturenet-payments/stack.yml @@ -20,6 +20,7 @@ repos: - github.com/cerc-io/mobymask-ui@v0.2.2 # ponder repo - github.com/cerc-io/ponder@laconic-esm + - github.com/cerc-io/erc20-watcher-ts containers: # fixturenet images - cerc/go-ethereum @@ -42,6 +43,7 @@ containers: - cerc/mobymask-ui # ponder image - cerc/ponder + - cerc/watcher-erc20 pods: - fixturenet-eth - ipld-eth-server-payments