-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_go_pkg.sh
116 lines (97 loc) · 3.7 KB
/
generate_go_pkg.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/bin/bash
if [ "$#" -ne 4 ]; then
# print help
echo "==========================================================="
echo "GO source code generator"
echo "Usage: $0 <contract_path> <contract_name> <go_pkg> <go_path>"
echo ""
echo "Example: "
echo ""
echo " $0 contracts/V2 NonCompoundingRewardsPool staker staker"
echo ""
echo "will create: ./go-staker/staker/NonCompoundingRewardsPool.go"
echo "based on: ./contracts/V2/NonCompoundingRewardsPool.sol"
echo ""
echo " $0 contracts StakeLock lock staker/lock"
echo ""
echo "will create: ./go-staker/staker/lock/StakeLock.go"
echo "based on: ./contracts/StakeLock.sol"
echo "==========================================================="
exit 1
fi
CONTRACT_PATH=$1
CONTRACT_NAME=$2
GO_PKG=$3
# we can choose to place the files in a deeper hierachy inside the module. normally the last segment should match the package name
GO_PATH=$4
CURRENT_USER=$(id -u):$(id -g)
GO_MOD_BASENAME=go-staker
SOLIDITY_VERSION=$(grep -E "pragma solidity [^;]+;" ${CONTRACT_PATH}/${CONTRACT_NAME}.sol | cut -d' ' -f3 | tr -d ';')
NODE_VERSION=10.17
echo "==========================================================="
echo "Generate GO source code in package: ${GO_PKG}, at path: ${GO_PATH}"
echo "For contract: ${CONTRACT_PATH}/${CONTRACT_NAME}"
echo "Node: ${NODE_VERSION}"
echo "Solidity: ${SOLIDITY_VERSION}"
echo "==========================================================="
echo "Temporary volume for build files:"
docker volume create build-contracts
echo
echo "Prepare a custom image for Node (${NODE_VERSION})"
# Necessary since I am running the actual build in the container,
# using a user different than root
# commands in the format $(...) run on the host and docker build runs with their results
docker build -q -t node:${NODE_VERSION}-custom - << EOF
FROM node:${NODE_VERSION}
WORKDIR /.npm
RUN chown -R ${CURRENT_USER} /.npm
WORKDIR /.config
RUN chown -R ${CURRENT_USER} /.config
EOF
echo
# Build the current project with the required Node version (useful when Node is not available at the exact version)
docker run --rm --user ${CURRENT_USER} -v $(pwd):/src --workdir /src node:${NODE_VERSION}-custom npm install
echo "Prepare a custom image for Solc (${SOLIDITY_VERSION})"
# Solc (Solidity compiler).
# The build stage is there only to copy the solc binary. building it from source would take longer
docker build -q -t solc:${SOLIDITY_VERSION}-vol - << EOF
FROM ethereum/solc:${SOLIDITY_VERSION} as build
FROM alpine:latest
COPY --from=build /usr/bin/solc /usr/bin/
RUN mkdir -p /build && chmod -R a+w /build
VOLUME ["/build"]
ENTRYPOINT ["/usr/bin/solc"]
EOF
echo
echo "Compile contract: ${CONTRACT_PATH}/${CONTRACT_NAME}.sol"
docker run --rm \
--user ${CURRENT_USER} \
-v $(pwd):/root \
-v build-contracts:/build \
solc:${SOLIDITY_VERSION}-vol \
@openzeppelin/=/root/node_modules/@openzeppelin/ --abi /root/${CONTRACT_PATH}/${CONTRACT_NAME}.sol --allow-paths '' -o /build
echo
echo "Prepare a custom image for Abigen (latest)"
# abigen needs solc, hence the build stage
docker build -q -t abigen:custom - << EOF
FROM ethereum/solc:${SOLIDITY_VERSION} as build
FROM golang:latest
COPY --from=build /usr/bin/solc /usr/bin/
RUN git clone https://github.com/ethereum/go-ethereum
RUN cd ./go-ethereum && make && make devtools
ENTRYPOINT ["abigen"]
EOF
echo
echo "Generate GO source code"
docker run \
--rm \
--user ${CURRENT_USER} \
-v build-contracts:/build \
-v $(pwd)/${GO_MOD_BASENAME}/${GO_PATH}:/out \
abigen:custom --abi=/build/${CONTRACT_NAME}.abi --pkg=${GO_PKG} --out=/out/${CONTRACT_NAME}.go
echo
echo Cleanup
docker volume rm build-contracts
docker image rm abigen:custom
docker image rm solc:${SOLIDITY_VERSION}-vol
docker image rm node:${NODE_VERSION}-custom