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

Create TeleporterMessenger ABI Go binding #41

Merged
merged 29 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from 27 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
39 changes: 39 additions & 0 deletions .github/workflows/abi_go_bindings_checker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: ABI Go Bindings Checker

on:
pull_request:
branches:
- "*"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need need to add on push to main?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

based on Cam's comments, we may not want to run this on main in automated fashion


env:
GO_VERSION: "1.20.8"

jobs:
abi_binding:
name: abi_binding
runs-on: ubuntu-20.04

steps:
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: ${{ env.GO_VERSION }}

- name: Checkout Teleporter repository
uses: actions/checkout@v4
with:
submodules: recursive

- name: Install forge and generate ABI Go bindings
run: |
BASE_DIR=${XDG_CONFIG_HOME:-$HOME}
curl -L https://foundry.paradigm.xyz | bash
source $HOME/.bashrc
$BASE_DIR/.foundry/bin/foundryup
export GOPATH=$HOME/go
export PATH="$PATH:$BASE_DIR/.foundry/bin"
export PATH="$PATH:$GOPATH/bin"
./scripts/abi_go_bindings.sh

- name: Check for clean branch
run: .github/workflows/check_clean_branch.sh
12 changes: 12 additions & 0 deletions .github/workflows/check_clean_branch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash
# Exits if any uncommitted changes are found.

set -o errexit
gwen917 marked this conversation as resolved.
Show resolved Hide resolved
set -o nounset
set -o pipefail

# Modifies the index based on the current index.
# Checks to see if merges or updates are needed by checking stat() information.
git update-index --really-refresh > /dev/null
# Checks to see if there are any differences from the index to the current HEAD commit
git diff-index --quiet HEAD
427 changes: 427 additions & 0 deletions abis/BlockHashPublisher/BlockHashPublisher.go

Large diffs are not rendered by default.

565 changes: 565 additions & 0 deletions abis/BlockHashReceiver/BlockHashReceiver.go

Large diffs are not rendered by default.

1,284 changes: 1,284 additions & 0 deletions abis/ERC20Bridge/ERC20Bridge.go

Large diffs are not rendered by default.

610 changes: 610 additions & 0 deletions abis/ExampleCrossChainMessenger/ExampleCrossChainMessenger.go

Large diffs are not rendered by default.

1,716 changes: 1,716 additions & 0 deletions abis/TeleporterMessenger/TeleporterMessenger.go

Large diffs are not rendered by default.

77 changes: 77 additions & 0 deletions scripts/abi_go_bindings.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/usr/bin/env bash
# Copyright (C) 2023, Ava Labs, Inc. All rights reserved.
# See the file LICENSE for licensing terms.

set -e

source ./scripts/utils.sh

setARCH

gwen917 marked this conversation as resolved.
Show resolved Hide resolved
TELEPORTER_PATH=$(
cd "$(dirname "${BASH_SOURCE[0]}")"
cd .. && pwd
)

DEFAULT_CONTRACT_LIST="TeleporterMessenger ERC20Bridge ExampleCrossChainMessenger BlockHashPublisher BlockHashReceiver"
gwen917 marked this conversation as resolved.
Show resolved Hide resolved

CONTRACT_LIST=
HELP=
while [ $# -gt 0 ]; do
case "$1" in
-c | --contract) CONTRACT_LIST=$2 ;;
-h | --help) HELP=true ;;
esac
shift
done

if [ "$HELP" = true ]; then
echo "Usage: ./scripts/abi_go_bindings.sh [OPTIONS]"
echo "Build contracts and generate Go bindings"
echo ""
echo "Options:"
echo " -c, --contract <contract_name> Generate Go bindings for the contract. If empty, generate Go bindings for a default list of contracts"
echo " -c, --contract "contract1 contract2" Generate Go bindings for multiple contracts"
echo " -h, --help Print this help message"
exit 0
fi

if ! command -v forge &> /dev/null; then
echo "forge not found, installing"
curl -L https://foundry.paradigm.xyz | bash
source $HOME/.bashrc
foundryup
fi

echo "Building subnet-evm abigen"
go install $TELEPORTER_PATH/subnet-evm/cmd/abigen

echo "Building Contracts"
cd $TELEPORTER_PATH/contracts
forge build --extra-output-files abi

contract_names=($CONTRACT_LIST)

# If CONTRACT_LIST is empty, use DEFAULT_CONTRACT_LIST
if [[ -z "${CONTRACT_LIST}" ]]; then
contract_names=($DEFAULT_CONTRACT_LIST)
fi

cd $TELEPORTER_PATH
for contract_name in "${contract_names[@]}"
do
abi_file=$TELEPORTER_PATH/contracts/out/$contract_name.sol/$contract_name.abi.json
if ! [ -f $abi_file ]; then
echo "Error: Contract $contract_name abi file not found"
exit 1
fi

echo "Generating Go bindings for $contract_name..."
mkdir -p $TELEPORTER_PATH/abis/$contract_name
$GOPATH/bin/abigen --abi $abi_file \
--pkg $(convertToSnakeCase $contract_name) \
gwen917 marked this conversation as resolved.
Show resolved Hide resolved
--out $TELEPORTER_PATH/abis/$contract_name/$contract_name.go
echo "Done generating Go bindings for $contract_name."
done

exit 0
8 changes: 8 additions & 0 deletions scripts/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,12 @@ function setARCH() {
export ARCH=$(uname -m)
[ $ARCH = x86_64 ] && ARCH=amd64
echo "ARCH set to $ARCH"
}

function convertToSnakeCase() {
if [ "$ARCH" = 'arm64' ]; then
echo $1 | perl -pe 's/([A-Z])/_\L\1/g' | sed 's/^_//'
else
echo $1 | sed -r 's/([A-Z])/_\L\1/g' | sed 's/^_//'
fi
}
Loading