Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli tool for managing NTT deployments #467

Merged
merged 5 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/workflows/cli.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: CLI

on:
workflow_dispatch:
pull_request:
push:
branches:
- main

jobs:
test-evm:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: docker build -f Dockerfile.cli --target cli-local-test . --progress=plain

test-solana:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: docker build -f Dockerfile.cli --target cli-local-test-solana . --progress=plain
68 changes: 68 additions & 0 deletions Dockerfile.cli
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# NOTE: we use the backpackapp base image so we can use solana. You would think
# that the solana installer script just works, but you would be wrong. It seems
# to have been broken recently since the migration to the anza url.
# The old installer url returns a 403. So we instead rely on solana binaries cached on docker hub. Everything is fine.
FROM backpackapp/build:v0.30.1@sha256:c160fe32cba7c463981110b2aac2924de4833c06a0af6473a830ead880c4ef3b as base

RUN apt update

RUN apt install -y python3
RUN apt install -y build-essential
RUN apt install -y git
RUN apt install -y curl
RUN apt install -y unzip

RUN curl -fsSL https://bun.sh/install | bash

RUN curl -L https://foundry.paradigm.xyz | bash
RUN bash -ci "foundryup"

RUN apt install -y jq

FROM base as base-solana

RUN cargo install --git https://github.com/coral-xyz/anchor avm --locked --force
RUN avm install 0.29.0
RUN avm use 0.29.0

FROM base as cli-remote
# NOTE: when invoking the installer outside of the source tree, it clones the
# repo and installs that way.
# This build stage tests that path.
COPY cli/install.sh cli/install.sh
RUN bash -ci "./cli/install.sh"
RUN bash -ci "which ntt"

FROM base-solana as cli-local
# NOTE: when invoking the installer inside of the source tree, it installs from
# the local source tree.
# This build stage tests that path.
WORKDIR /app
COPY tsconfig.json tsconfig.json
COPY tsconfig.esm.json tsconfig.esm.json
COPY tsconfig.cjs.json tsconfig.cjs.json
COPY package.json package.json
COPY package-lock.json package-lock.json
COPY sdk sdk
COPY solana/package.json solana/package.json
COPY solana/ts solana/ts
COPY evm/ts evm/ts
COPY solana/tsconfig.*.json solana/
COPY cli/package.json cli/package.json
COPY cli/package-lock.json cli/package-lock.json
COPY cli/src cli/src
COPY cli/install.sh cli/install.sh
RUN bash -ci "./cli/install.sh"
RUN bash -ci "which ntt"

FROM cli-local as cli-local-test
COPY cli/test cli/test
COPY evm evm
RUN bash -ci "./cli/test/sepolia-bsc.sh"

FROM cli-local as cli-local-test-solana
COPY cli/test cli/test
# evm/script needed for the cli, at least for now
COPY evm/script evm/script
COPY solana solana
RUN bash -ci "./cli/test/solana.sh --use-tmp-dir"
13 changes: 13 additions & 0 deletions cli/example-overrides.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"chains": {
"Bsc": {
"rpc": "http://127.0.0.1:8545"
},
"Sepolia": {
"rpc": "http://127.0.0.1:8546"
},
"Solana": {
"rpc": "http://127.0.0.1:8899"
}
}
}
164 changes: 164 additions & 0 deletions cli/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
#!/usr/bin/env bash

set -euo pipefail

# check that 'bun' is installed

if ! command -v bun > /dev/null; then
echo "bun is not installed. Follow the instructions at https://bun.sh/docs/installation"
exit 1
fi

REPO="https://github.com/wormhole-foundation/example-native-token-transfers.git"

function main {
branch=""

while [[ $# -gt 0 ]]; do
key="$1"

case $key in
-b|--branch)
branch="$2"
shift
shift
;;
-r|--repo)
REPO="$2"
shift
shift
;;
*)
echo "Unknown option $key"
exit 1
;;
esac
done

path=""
mkdir -p "$HOME/.ntt-cli"

# check if there's a package.json in the parent directory, with "name": "@wormhole-foundation/ntt-cli"
if [ -f "$(dirname $0)/package.json" ] && grep -q '"name": "@wormhole-foundation/ntt-cli"' "$(dirname $0)/package.json"; then
path="$(dirname $0)/.."
version=$(git -C "$path" rev-parse HEAD 2>/dev/null || echo "unknown")
dirty=$(git -C "$path" diff --quiet 2>/dev/null || echo "-dirty")
echo "$version$dirty" > "$HOME/.ntt-cli/version"
else
check_commit_included_in_main="false"
# if branch is set, use it. otherwise use the latest tag of the form "vX.Y.Z+cli"
if [ -z "$branch" ]; then
branch="$(select_branch)"
# if the branch was not set, we want to check that the default is included
# in the main branch, i.e. it has been reviewed
check_commit_included_in_main="true"
else
branch="origin/$branch"
fi

# clone to $HOME/.ntt-cli if it doesn't exist, otherwise update it
echo "Cloning $REPO $branch"

path="$HOME/.ntt-cli/.checkout"

if [ ! -d "$path" ]; then
git clone "$REPO" "$path"
fi
pushd "$path"
# update origin url to REPO
git remote set-url origin "$REPO"
git fetch origin
if [ "$check_commit_included_in_main" = "true" ]; then
# check that the commit is included in the main branch
if ! git merge-base --is-ancestor "$branch" "origin/main"; then
echo "ref '$branch' is not included in the main branch"
exit 1
fi
fi
# reset hard
git reset --hard "$branch"
version=$(git rev-parse HEAD)
dirty=$(git diff --quiet || echo "-dirty")
echo "$version$dirty" > "$HOME/.ntt-cli/version"
popd
fi

absolute_path="$(cd $path && pwd)"
echo $absolute_path >> "$HOME/.ntt-cli/version"

# jq would be nicer but it's not portable
# here we make the assumption that the file uses 2 spaces for indentation.
# this is a bit fragile, but we don't want to catch further nested objects
# (there might be a "version" in the scripts section, for example)
version=$(cat "$path/cli/package.json" | grep '^ "version":' | cut -d '"' -f 4)
echo "$version" >> "$HOME/.ntt-cli/version"

remote_url=$(git -C "$path" remote get-url origin 2>/dev/null || echo "unknown")
echo "$remote_url" >> "$HOME/.ntt-cli/version"

echo "Installing ntt CLI version $version"
install_cli "$path"
}

# function that determines which branch/tag to clone
function select_branch {
# if the repo has a tag of the form "vX.Y.Z+cli", use that (the latest one)
branch=""
regex="refs/tags/v[0-9]*\.[0-9]*\.[0-9]*+cli"
if git ls-remote --tags "$REPO" | grep -q "$regex"; then
branch="$(git ls-remote --tags "$REPO" | grep "$regex" | sort -V | tail -n 1 | awk '{print $2}')"
else
# otherwise error
echo "No tag of the form vX.Y.Z+cli found" >&2
exit 1
fi

echo "$branch"
}

function install_cli {
cd "$1"

# if 'ntt' is already installed, uninstall it
# just check with 'which'
if which ntt > /dev/null; then
echo "Removing existing ntt CLI"
rm $(which ntt)
fi

# swallow the output of the first install
# TODO: figure out why it fails the first time.
bun install > /dev/null 2>&1 || true
bun install

# make a temporary directory

tmpdir="$(mktemp -d)"

# create a temporary symlink 'npm' to 'bun'

ln -s "$(command -v bun)" "$tmpdir/npm"

# add the temporary directory to the PATH

export PATH="$tmpdir:$PATH"

# swallow the output of the first build
# TODO: figure out why it fails the first time.
bun --bun run --filter '*' build > /dev/null 2>&1 || true
bun --bun run --filter '*' build

# remove the temporary directory

rm -r "$tmpdir"

# now link the CLI

cd cli

bun link

bun link @wormhole-foundation/ntt-cli
}

main "$@"
9 changes: 5 additions & 4 deletions cli/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "cli",
"name": "@wormhole-foundation/ntt-cli",
"version": "1.0.0-beta",
"module": "src/index.ts",
"type": "module",
"devDependencies": {
Expand All @@ -13,7 +14,7 @@
"ntt": "src/index.ts"
},
"dependencies": {
"chalk": "^5.3.0",
"yargs": "^17.7.2"
},
"version": "0.2.0"
}
}
}
Loading
Loading