forked from nxtmeta/governance-contract
-
Notifications
You must be signed in to change notification settings - Fork 6
/
solc.sh
188 lines (167 loc) · 3.94 KB
/
solc.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/bin/bash
[ "${SOL_GAS}" = "" ] && SOL_GAS="0x10000000"
SOLC=
solc --version > /dev/null 2>&1
if [ $? = 0 ]; then
SOLC=solc
else
# docker --version > /dev/null 2>&1 && SOLC="docker run -v $(pwd):/tmp --workdir /tmp --rm ethereum/solc:stable"
docker --version > /dev/null 2>&1 && SOLC="docker run -v $(pwd):/tmp --workdir /tmp --rm ethereum/solc:0.8.6"
fi
if [ "$SOLC" = "" ]; then
echo "Cannot find solc or docker."
exit 1
fi
function usage ()
{
echo "$(basename $0) [-f <format>] [-g gas] [-p gas-price] [-l <name>:<addr>]+
<sol-file> [<js-file> | <json-file>]
-f <format>: oupput format: \"js\" || \"json\", default is \"js\".
-g <gas>: gas amount to spend.
-p <gas-price>: gas price
-l <name>:<addr>: library name and address pair separated by ':'.
Multiple -l options can be used to specify multiple libraries.
-r <path>=<path>: remap paths, e.g. \"wherever=/foo/bar\"
Output Formats:
\"js\": creates 'remix'-generated .js style file that can be loaded to
geth/gmet console.
\"json\": creates 'truffle'-generated .json style file.
Environment Variables:
SOL_GAS for gas amount, equivalent to -g option.
SOL_LIBS for libaries, equivalent to -l option. Pairs should be separated by
space, e.g. \"name1:0x123..456 name2:abc..def\".
"
}
# int compile(string solFile, string jsFile)
function compile ()
{
${SOLC} ${PATH_REMAP} --optimize --abi --bin $1 | awk -v gas="$SOL_GAS" -v gas_price="$SOL_GASPRICE" -v libs="$SOL_LIBS" -v outfmt=$outfmt '
function flush2js() {
if (length(gas_price) != 0) {
gas_price_2 = ",\
gasPrice: \"" gas_price "\"";
}
if (length(code_name) > 0) {
printf "\n\
function %s_new() {\n\
return %s_contract.new(\n\
{\n\
from: web3.eth.accounts[0],\n\
data: %s_data,\n\
gas: \"%s\"%s\n\
}, function (e, contract) {\n\
if (typeof contract.address !== \"undefined\") {\n\
console.log(\"Contract mined! address: \" + contract.address + \" transactionHash: \" + contract.transactionHash);\n\
}\n\
});\n\
}\n\
\n\
function %s_load(addr) {\n\
return %s_contract.at(addr);\n\
}\n\
", code_name, code_name, code_name, gas, gas_price_2, code_name, code_name;
}
}
function flush2json() {
if (length(code_name) > 0) {
printf "{\n\
\"contractName\": \"%s\",\n\
\"abi\": %s,\n\
\"bytecode\": \"0x%s\"\n\
}\n\
", code_name, abi, code;
}
}
function flush2abi() {
if (length(code_name) > 0) {
printf "{\n\
\"contractName\": \"%s\",\n\
\"abi\": %s\n\
}\n\
", code_name, abi, code;
}
}
function flush() {
if (outfmt == "js")
flush2js();
else if (outfmt == "json")
flush2json();
else
flush2abi();
}
END {
flush()
}
/^$/ {
flush()
code_name = ""
}
/^=======/ {
code_name = $0
sub("^=.*:", "", code_name)
sub(" =======$", "", code_name)
}
# abi
/^\[/ {
if (length(code_name) > 0) {
abi = $0
if (outfmt == "js")
print "var " code_name "_contract = web3.eth.contract(" abi ");";
}
}
# binary: 60606040, 60806040 for contracts, 610eb861 for libraries
/^6[01]/ {
if (length(code_name) > 0) {
code = $0;
n = split(libs, alibs, " +");
for (i = 1; i <= n; i++) {
if (split(alibs[i], nv, ":") != 2)
continue;
sub("^0x", "", nv[2]);
gsub("_+[^_]*" nv[1] "_+", nv[2], code);
}
if (outfmt == "js")
print "var " code_name "_data = \"0x" code "\";";
}
}
' > $2;
}
args=`getopt f:g:l:p:r: $*`
if [ $? != 0 ]; then
usage;
exit 1;
fi
set -- $args
outfmt=js
for i; do
case "$i" in
-f)
outfmt=$2
shift;
shift;;
-g)
SOL_GAS=$2
shift;
shift;;
-p)
SOL_GASPRICE=$2
shift;
shift;;
-l)
[ "$SOL_LIBS" = "" ] || SOL_LIBS="$SOL_LIBS "
SOL_LIBS="${SOL_LIBS}$2";
shift;
shift;;
-r)
[ "$PATH_REMAP" = "" ] || PATH_REMAP="$PATH_REMAP "
PATH_REMAP="${PATH_REMAP}$2";
shift;
shift;;
esac
done
if [ $# != 3 -o "$outfmt" != "js" -a "$outfmt" != "json" -a "$outfmt" != "abi" ]; then
usage
exit 1
fi
compile "$2" "$3"
# EOF