-
Notifications
You must be signed in to change notification settings - Fork 6
/
stack
executable file
·408 lines (356 loc) · 11.6 KB
/
stack
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#!/usr/bin/env bash
############################################################################
############################################################################
# LOCAL DEV HELPERS
#
# You "can" use this script in production but it's not designed to be
# It's an opinionated script that helps with local development
############################################################################
############################################################################
set -euo pipefail
IFS=$'\n\t'
export DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# if we are running geth in docker (for testnets)
# then which folder do we mount the data folder to?
export GETH_DATA_DIRECTORY=${GETH_DATA_DIRECTORY:="/tmp/geth"}
# which docker network do we connect geth to?
export GETH_DOCKER_NETWORK=${GETH_DOCKER_NETWORK:="gdcn"}
# what is the docker image that we use for geth?
export GETH_DOCKER_IMAGE=${GETH_DOCKER_IMAGE:="ethereum/client-go:v1.13.5"}
# what is the docker image that we use for the faucet?
export FAUCET_DOCKER_IMAGE=${FAUCET_DOCKER_IMAGE:="gdcn-faucet"}
# what is the docker image that we use for solc compiler?
export SOLC_DOCKER_IMAGE=${SOLC_DOCKER_IMAGE:="gdcn-solc"}
# if "$DIR/.env" exists then source it
# this will contian the various addresses and private keys
if [[ -f "$DIR/.env" ]]; then
source "$DIR/.env"
fi
############################################################################
############################################################################
# setup
############################################################################
############################################################################
function install() {
build-faucet
go mod download
(cd hardhat && yarn install)
(cd frontend && yarn install)
(cd hardhat && npx hardhat compile)
print-env > .env
}
############################################################################
############################################################################
# geth
############################################################################
############################################################################
# run a local geth node in dev mode mounted against a local data directory
function geth() {
docker network ls | grep "$GETH_DOCKER_NETWORK" || docker network create "$GETH_DOCKER_NETWORK"
docker run -d --restart always \
--name geth \
--network "$GETH_DOCKER_NETWORK" \
-p 8545:8545 \
-p 8546:8546 \
-v ${GETH_DATA_DIRECTORY}:/data/geth \
$GETH_DOCKER_IMAGE \
--datadir /data/geth \
--dev \
--ws \
--ws.api web3,eth,net \
--ws.addr 0.0.0.0 \
--ws.port 8546 \
--ws.origins '*' \
--http \
--http.api web3,eth,net \
--http.addr 0.0.0.0 \
--http.corsdomain '*' \
--http.port 8545 \
--http.vhosts '*'
}
function geth-command() {
docker exec -i geth geth --exec "$@" attach /data/geth/geth.ipc
}
############################################################################
############################################################################
# faucet
############################################################################
############################################################################
function build-faucet() {
(
cd ../eth-faucet &&
docker build -t $FAUCET_DOCKER_IMAGE .
)
}
# run the faucet container
function faucet() {
eval $(./stack print-contract-env)
local privateKey=$(echo -n "$ADMIN_PRIVATE_KEY" | sed 's/^0x//')
local tokenAddress=$(echo -n "$WEB3_TOKEN_ADDRESS" | sed 's/^0x//')
docker run -d \
--name faucet \
-p 8085:8080 \
--network "$GETH_DOCKER_NETWORK" \
-e WEB3_PROVIDER=http://geth:8545 \
-e PRIVATE_KEY=$privateKey \
-e TOKEN_ADDRESS=$WEB3_TOKEN_ADDRESS \
$FAUCET_DOCKER_IMAGE \
--faucet.amount=100 \
--faucet.tokenamount=100 \
--faucet.minutes=1
}
function faucet-stop() {
docker rm -f faucet 2> /dev/null || true
}
############################################################################
############################################################################
# bacalhau
############################################################################
############################################################################
function bacalhau-serve() {
bacalhau serve \
--node-type compute,requester \
--peer none \
--private-internal-ipfs=false \
--job-selection-accept-networked
}
# move ALL the money apart from 1 eth to the given admin account
# this expects the .env file to already have been created
function fund-admin() {
if [[ -z "$ADMIN_ADDRESS" ]]; then
echo >&2 "ADMIN_ADDRESS must be set (source ${DIR}/.env)"
exit 1
fi
geth-command "eth.sendTransaction({from: eth.coinbase, to: \"${ADMIN_ADDRESS}\", value: new web3.BigNumber(eth.getBalance(eth.coinbase)).minus(web3.toWei(1, \"ether\")) })"
}
function geth-stop() {
docker rm -f geth 2> /dev/null || true
}
function clean-deploy() {
rm -rf ${DIR}/hardhat/artifacts
rm -rf ${DIR}/hardhat/cache
rm -rf ${DIR}/hardhat/deployments/geth
}
function clean() {
docker rm -f $(docker ps -aq) || true
sudo rm -rf ${GETH_DATA_DIRECTORY}
clean-deploy
}
function boot-message() {
echo ""
echo "############################################################################"
echo "# $@"
echo "############################################################################"
echo ""
}
function deploy() {
clean-deploy
boot-message "Compiling contracts"
compile-contracts
boot-message "Deploying contracts"
deploy-contracts
boot-message "Funding services with tokens"
fund-services-tokens
boot-message "Printing balances"
balances
}
function boot() {
clean
boot-message "Starting geth"
geth
sleep 5
boot-message "Funding admin account"
fund-admin
boot-message "Funding services with ether"
fund-services-ether
deploy
}
############################################################################
############################################################################
# hardhat
############################################################################
############################################################################
function go-binding() {
local name="$1"
local pkg="$2"
# compile the sol files into bytecode and ABI
docker run --rm \
-v $DIR/hardhat:/src \
-w /src \
--entrypoint solc \
$SOLC_DOCKER_IMAGE \
--base-path . \
--include-path node_modules \
--overwrite \
--abi --bin \
"contracts/$name.sol" \
-o artifacts
sudo chown -R $USER:$USER hardhat/artifacts
mkdir -p hardhat/artifacts/bindings/$pkg
# generate the go bindings
docker run --rm \
-v $DIR/hardhat:/src \
-w /src \
--entrypoint abigen \
$SOLC_DOCKER_IMAGE \
"--bin=artifacts/$name.bin" \
"--abi=artifacts/$name.abi" \
"--pkg=$pkg" "--out=artifacts/bindings/$pkg/$pkg.go"
sudo chown -R $USER:$USER hardhat/artifacts/bindings/$pkg
sudo chmod 0644 hardhat/artifacts/bindings/$pkg/$pkg.go
cp -r hardhat/artifacts/bindings/$pkg pkg/web3/bindings/$pkg
echo "Generated go binding hardhat/artifacts/bindings/$pkg/$pkg.go"
}
function go-bindings() {
# check if the $SOLC_DOCKER_IMAGE image exists
# and only build it if it doesn't
if [[ -z $(docker images -q $SOLC_DOCKER_IMAGE) ]]; then
docker build -t $SOLC_DOCKER_IMAGE hardhat/solc
fi
mkdir -p pkg/web3/bindings
go-binding LilypadToken token
go-binding LilypadPayments payments
go-binding LilypadStorage storage
go-binding LilypadUsers users
go-binding LilypadMediationRandom mediation
go-binding LilypadOnChainJobCreator jobcreator
go-binding LilypadController controller
echo "Generated all go bindings pkg/contract/bindings/"
}
function compile-contracts() {
(
set -euo pipefail
cd hardhat
npx hardhat compile
)
go-bindings
}
function deploy-contracts() {
(
set -euo pipefail
cd hardhat
npx hardhat deploy --network "$NETWORK"
)
}
function hardhat-script() {
(
set -euo pipefail
cd hardhat
npx hardhat run "$@"
)
}
# print the env settings for the various accounts and private keys
function print-env() {
hardhat-script scripts/print-env.ts | grep export
}
function print-contract-env() {
hardhat-script scripts/print-contract-env.ts | grep export
}
function generate-addresses() {
hardhat-script scripts/generate-addresses.ts | grep export
}
function print-local-dev-env() {
print-contract-env
}
function fund-services-ether() {
hardhat-script scripts/fund-services-ether.ts
}
function fund-services-tokens() {
hardhat-script scripts/fund-services-tokens.ts
}
function balances() {
hardhat-script scripts/balances.ts
}
function run-cowsay-onchain() {
export WEB3_PRIVATE_KEY=$JOB_CREATOR_PRIVATE_KEY
hardhat-script scripts/run-cowsay-onchain.ts
}
############################################################################
############################################################################
# services
############################################################################
############################################################################
function run() {
source .env
eval $(./stack print-local-dev-env)
export WEB3_PRIVATE_KEY=$JOB_CREATOR_PRIVATE_KEY
export SERVICE_SOLVER=$SOLVER_ADDRESS
export SERVICE_MEDIATORS=$MEDIATOR_ADDRESS
go run . run "$@"
}
function runsdxl() {
# Check for the number of arguments provided
if [ $# -lt 3 ]; then
echo "Usage: $0 runsdxl <model> PROMPT=\"<prompt>\""
exit 1
fi
source .env
eval $(./stack print-local-dev-env)
export WEB3_PRIVATE_KEY=$JOB_CREATOR_PRIVATE_KEY
export SERVICE_SOLVER=$SOLVER_ADDRESS
export SERVICE_MEDIATORS=$MEDIATOR_ADDRESS
local model="$1"
shift # Remove the first argument, so "$@" contains only the second one
local prompt="$@"
go run . run $model -i "PromptEnv=$prompt"
}
function solver() {
source .env
eval $(./stack print-local-dev-env)
export WEB3_PRIVATE_KEY=$SOLVER_PRIVATE_KEY
export JOB_CREATOR_ADDRESS=$JOB_CREATOR_ADDRESS
export SERVICE_MEDIATORS=$MEDIATOR_ADDRESS
export SERVER_PORT=8080
export SERVER_URL=http://localhost:8080
go run . solver "$@"
}
# we run the jobcreator as the solver
function jobcreator() {
source .env
eval $(./stack print-local-dev-env)
export WEB3_PRIVATE_KEY=$SOLVER_PRIVATE_KEY
export SERVICE_SOLVER=$SOLVER_ADDRESS
export SERVICE_MEDIATORS=$MEDIATOR_ADDRESS
go run . jobcreator "$@"
}
function resource-provider() {
source .env
eval $(./stack print-local-dev-env)
export WEB3_PRIVATE_KEY=$RESOURCE_PROVIDER_PRIVATE_KEY
export SERVICE_SOLVER=$SOLVER_ADDRESS
export SERVICE_MEDIATORS=$MEDIATOR_ADDRESS
go run . resource-provider "$@"
}
function mediator() {
source .env
eval $(./stack print-local-dev-env)
export WEB3_PRIVATE_KEY=$MEDIATOR_PRIVATE_KEY
export SERVICE_SOLVER=$SOLVER_ADDRESS
go run . mediator "$@"
}
############################################################################
############################################################################
# tests
############################################################################
############################################################################
function unit-tests() {
(
set -euo pipefail
cd hardhat
npx hardhat test --network hardhat
)
}
# this assumes boot having been run already
function integration-tests() {
source .env
eval $(./stack print-local-dev-env)
export WEB3_SOLVER_ADDRESS=$SOLVER_ADDRESS
export SERVICE_SOLVER=$SOLVER_ADDRESS
export SERVICE_MEDIATORS=$MEDIATOR_ADDRESS
(
set -euo pipefail
cd test
go test -v -count 1 .
)
}
eval "$@"