-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathctl.sh
executable file
·199 lines (185 loc) · 4.79 KB
/
ctl.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
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env bash
LAMPORTS_PER_SOL=1000000000
API_ENDPOINT=https://api.mainnet-beta.solana.com/
source .env
##
## Usage: ./ctl.sh COMMAND SUBCOMMAND
##
## ~> build
## all Build all images
## keeper Build bot image
## tracker Build wallet-tracker image
## autoswap Build auto-swap image
##
## ~> push
## all Push all images to Docker registry
## keeper Push bot image to Docker registry
## tracker Push tracker image to Docker registry
## autoswap Push auto-swaü image to Docker registry
##
## ~> run
## all Run the complete stack locally
## autoswap Run Auto-Swap locally
##
## ~> infra
## plan Plan infrastructure change
## provision Provision infrastructure
## hosts Show list of servers
## connect Connect to a server
## playbook Run a maintenance playbook
##
## ~> balance
## sol Show SOL balance
## usdc Show USDC balance
##
RED="31"
GREEN="32"
GREENBLD="\e[1;${GREEN}m"
REDBOLD="\e[1;${RED}m"
REDITALIC="\e[3;${RED}m"
EC="\e[0m"
function info {
printf "\n${GREENBLD}Wallet Address:\t$WALLET_ADDRESS${EC}\n"
printf "${GREENBLD}Environment:\t$ENV${EC}\n"
sed -n 's/^##//p' ctl.sh
}
function build {
function all {
build keeper
build tracker
}
function keeper {
mkdir -p .build
git clone https://github.com/drift-labs/keeper-bots-v2 -b mainnet-beta .build/keeper-bots-v2
#pushd .build/keeper-bots-v2
# git checkout 21fd791d142490fe033b5e25719927c106a0aaf2
#popd
docker build -f Dockerfile -t ${DOCKER_IMAGE} .build/keeper-bots-v2
rm -rf .build
}
function tracker {
pushd wallet-tracker
docker build -t ${DOCKER_IMAGE_WALLET_TRACKER} .
popd
}
function autoswap {
pushd auto-swap
docker build -t ${DOCKER_IMAGE_AUTO_SWAP} .
popd
}
function metrics {
pushd user-metrics
docker build -t ${DOCKER_IMAGE_USER_METRICS} .
popd
}
${@:-}
}
function push {
function all {
push keeper
push tracker
}
function keeper {
docker push ${DOCKER_IMAGE}
}
function tracker {
docker push ${DOCKER_IMAGE_WALLET_TRACKER}
}
function autoswap {
docker push ${DOCKER_IMAGE_AUTO_SWAP}
}
function metrics {
docker push ${DOCKER_IMAGE_USER_METRICS}
}
${@:-}
}
function run {
function all {
docker compose up
}
function autoswap {
pushd auto-swap
npm start
popd
}
function metircs {
pushd user-metrics
npm start
popd
}
${@:-}
}
function infra {
function plan {
terraform init
terraform plan
}
function provision {
terraform init
terraform apply
echo "[bots]" > inventory.cfg
terraform output -json | jq --raw-output ' .instances.value | to_entries[] | .value' >> inventory.cfg
}
function hosts {
terraform output -json | jq --raw-output '.instances.value | to_entries[] | [.key, .value] | @tsv'
}
function connect {
infra hosts \
| fzf --height=~50 \
| awk '{print $2}' \
| xargs -o ssh -l root $@
}
function playbook {
pushd ansible
ansible-playbook --ssh-common-args='-o StrictHostKeyChecking=accept-new' -i ../inventory.cfg $(fzf --height=~10)
popd
}
${@:-}
}
function balance {
function sol {
local addr=${1:-$WALLET_ADDRESS}
local balance=$(curl $API_ENDPOINT -s -X POST -H "Content-Type: application/json" -d '
{
"jsonrpc": "2.0", "id": 1,
"method": "getBalance",
"params": [
"'${addr}'"
]
}
' | jq .result.value)
echo "$(jq -n $balance/$LAMPORTS_PER_SOL)"
}
function usdc {
local addr=${1:-$WALLET_ADDRESS}
local balance=$(
curl $API_ENDPOINT -s -X POST -H "Content-Type: application/json" -d '
{
"jsonrpc": "2.0", "id": 1,
"method": "getTokenAccountsByOwner",
"params": [
"'${addr}'",
{
"mint": "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"
},{
"encoding": "jsonParsed"
}]
}' | jq .result.value[0].account.data.parsed.info.tokenAmount.uiAmount)
echo ${balance}
}
${@:-}
}
function repl {
clear
cat motd
info
echo -e "\n${REDBOLD}Enter command...${EC}"
read -p '~> ';
clear
cat motd
./ctl.sh ${REPLY}
printf "\n"
read -p "Press any key to continue."
repl
}
${@:-info}