Skip to content

Commit

Permalink
Merge pull request #41 from ava-labs/abi-go-binding
Browse files Browse the repository at this point in the history
Create TeleporterMessenger ABI Go binding
  • Loading branch information
gwen917 authored Oct 4, 2023
2 parents 3de374d + aedfb7b commit 8e8f16b
Show file tree
Hide file tree
Showing 10 changed files with 5,705 additions and 0 deletions.
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:
- "*"

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
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.

967 changes: 967 additions & 0 deletions abis/BridgeToken/BridgeToken.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

TELEPORTER_PATH=$(
cd "$(dirname "${BASH_SOURCE[0]}")"
cd .. && pwd
)

DEFAULT_CONTRACT_LIST="TeleporterMessenger ERC20Bridge ExampleCrossChainMessenger BlockHashPublisher BlockHashReceiver BridgeToken"

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 $(convertToLower $contract_name) \
--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 convertToLower() {
if [ "$ARCH" = 'arm64' ]; then
echo $1 | perl -ne 'print lc'
else
echo $1 | sed -e 's/\(.*\)/\L\1/'
fi
}

0 comments on commit 8e8f16b

Please sign in to comment.