-
Notifications
You must be signed in to change notification settings - Fork 1
/
autogenerate_bot.go
277 lines (247 loc) · 8.52 KB
/
autogenerate_bot.go
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
package backend
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
"github.com/stellar/go/clients/horizonclient"
"github.com/stellar/go/keypair"
"github.com/stellar/go/network"
hProtocol "github.com/stellar/go/protocols/horizon"
"github.com/stellar/go/txnbuild"
"github.com/stellar/kelp/gui/model2"
"github.com/stellar/kelp/plugins"
"github.com/stellar/kelp/support/kelpos"
"github.com/stellar/kelp/support/networking"
"github.com/stellar/kelp/support/toml"
"github.com/stellar/kelp/trader"
)
const issuerSeed = "SANPCJHHXCPRN6IIZRBEQXS5M3L2LY7EYQLAVTYD56KL3V7ABO4I3ISZ"
var centralizedPricePrecisionOverride = int8(6)
var centralizedVolumePrecisionOverride = int8(1)
var centralizedMinBaseVolumeOverride = float64(30.0)
var centralizedMinQuoteVolumeOverride = float64(10.0)
func (s *APIServer) autogenerateBot(w http.ResponseWriter, r *http.Request) {
kp, e := keypair.Random()
if e != nil {
s.writeError(w, fmt.Sprintf("error generating keypair: %s\n", e))
return
}
// make and register bot, which places it in the initial bot state
bot := model2.MakeAutogeneratedBot()
e = s.kos.RegisterBot(bot)
if e != nil {
s.writeError(w, fmt.Sprintf("error registering bot: %s\n", e))
return
}
e = s.setupOpsDirectory()
if e != nil {
s.writeError(w, fmt.Sprintf("error setting up ops directory: %s\n", e))
return
}
filenamePair := bot.Filenames()
sampleTrader := s.makeSampleTrader(kp.Seed())
traderFilePath := s.botConfigsPath.Join(filenamePair.Trader)
log.Printf("writing autogenerated bot config to file: %s\n", traderFilePath.AsString())
e = toml.WriteFile(traderFilePath.Native(), sampleTrader)
if e != nil {
s.writeError(w, fmt.Sprintf("error writing trader toml file: %s\n", e))
return
}
sampleBuysell := makeSampleBuysell()
strategyFilePath := s.botConfigsPath.Join(filenamePair.Strategy)
log.Printf("writing autogenerated strategy config to file: %s\n", strategyFilePath.AsString())
e = toml.WriteFile(strategyFilePath.Native(), sampleBuysell)
if e != nil {
s.writeError(w, fmt.Sprintf("error writing strategy toml file: %s\n", e))
return
}
// we only want to start initializing bot once it has been created, so we only advance state if everything is completed
go func() {
e := s.setupTestnetAccount(kp.Address(), kp.Seed(), bot.Name)
if e != nil {
log.Printf("error setting up account for bot '%s': %s\n", bot.Name, e)
return
}
e = s.kos.AdvanceBotState(bot.Name, kelpos.InitState())
if e != nil {
log.Printf("error advancing bot state after setting up account for bot '%s': %s\n", bot.Name, e)
return
}
}()
botJson, e := json.Marshal(*bot)
if e != nil {
s.writeError(w, fmt.Sprintf("unable to serialize bot: %s\n", e))
return
}
w.WriteHeader(http.StatusOK)
w.Write(botJson)
}
func (s *APIServer) setupTestnetAccount(address string, signer string, botName string) error {
// this function runs in testnet mode only
client := s.apiTestNet
fundedAccount, e := s.checkFundAccount(client, address, botName)
if e != nil {
return fmt.Errorf("error checking and funding account: %s", e)
}
var txOps []txnbuild.Operation
trustOp := txnbuild.ChangeTrust{
Line: txnbuild.CreditAsset{
Code: "COUPON",
Issuer: "GBMMZMK2DC4FFP4CAI6KCVNCQ7WLO5A7DQU7EC7WGHRDQBZB763X4OQI",
},
}
txOps = append(txOps, &trustOp)
paymentOp := txnbuild.Payment{
Destination: address,
Amount: "1000.0",
Asset: txnbuild.CreditAsset{
Code: "COUPON",
Issuer: "GBMMZMK2DC4FFP4CAI6KCVNCQ7WLO5A7DQU7EC7WGHRDQBZB763X4OQI",
},
SourceAccount: &txnbuild.SimpleAccount{AccountID: "GBMMZMK2DC4FFP4CAI6KCVNCQ7WLO5A7DQU7EC7WGHRDQBZB763X4OQI"},
}
txOps = append(txOps, &paymentOp)
tx, e := txnbuild.NewTransaction(
txnbuild.TransactionParams{
SourceAccount: fundedAccount,
Operations: txOps,
Timebounds: txnbuild.NewInfiniteTimeout(),
BaseFee: 100,
// If IncrementSequenceNum is true, NewTransaction() will call `sourceAccount.IncrementSequenceNumber()`
// to obtain the sequence number for the transaction.
// If IncrementSequenceNum is false, NewTransaction() will call `sourceAccount.GetSequenceNumber()`
// to obtain the sequence number for the transaction.
// leaving as true since that's what it was in the old sdk so we want to maintain backward compatibility and we
// need to increment the seq number on the account somewhere to use the next seq num
IncrementSequenceNum: true,
},
)
if e != nil {
return fmt.Errorf("cannot make transaction to create trustline transaction for account %s for bot '%s': %s", address, botName, e)
}
for _, s := range []string{signer, issuerSeed} {
kp, e := keypair.Parse(s)
if e != nil {
return fmt.Errorf("cannot parse seed %s required for signing: %s", s, e)
}
tx, e = tx.Sign(network.TestNetworkPassphrase, kp.(*keypair.Full))
if e != nil {
return fmt.Errorf("cannot sign trustline transaction for account %s for bot '%s': %s", address, botName, e)
}
}
txn64, e := tx.Base64()
if e != nil {
return fmt.Errorf("cannot convert trustline transaction to base64 for account %s for bot '%s': %s", address, botName, e)
}
resp, e := client.SubmitTransactionXDR(txn64)
if e != nil {
return fmt.Errorf("error submitting change trust transaction for address %s for bot '%s': %s", address, botName, e)
}
log.Printf("successfully added trustline for address %s for bot '%s': %v\n", address, botName, resp)
return nil
}
func (s *APIServer) checkFundAccount(client *horizonclient.Client, address string, botName string) (*hProtocol.Account, error) {
account, e := client.AccountDetail(horizonclient.AccountRequest{AccountID: address})
if e == nil {
log.Printf("account already exists %s for bot '%s', no need to fund via friendbot\n", address, botName)
return &account, nil
} else if e != nil {
var herr *horizonclient.Error
switch t := e.(type) {
case *horizonclient.Error:
herr = t
case horizonclient.Error:
herr = &t
default:
return nil, fmt.Errorf("unexpected error when checking for existence of account %s for bot '%s': %s", address, botName, e)
}
if herr.Problem.Status != 404 {
return nil, fmt.Errorf("unexpected horizon error code when checking for existence of account %s for bot '%s': %d (%v)", address, botName, herr.Problem.Status, *herr)
}
}
if !strings.Contains(client.HorizonURL, "test") {
log.Printf("not attempting to create mainnet account %s for bot '%s' since mainnet account does not exist\n", address, botName)
}
// since it's a 404 we want to continue funding below
var fundResponse interface{}
e = networking.JSONRequest(http.DefaultClient, "GET", "https://friendbot.stellar.org/?addr="+address, "", nil, &fundResponse, "")
if e != nil {
return nil, fmt.Errorf("error funding address %s for bot '%s': %s", address, botName, e)
}
log.Printf("successfully funded account %s for bot '%s': %s\n", address, botName, fundResponse)
// refetch account to confirm
account, e = client.AccountDetail(horizonclient.AccountRequest{AccountID: address})
if e != nil {
var herr *horizonclient.Error
switch t := e.(type) {
case *horizonclient.Error:
herr = t
case horizonclient.Error:
herr = &t
default:
return nil, fmt.Errorf("unexpected error when checking for existence of account %s for bot '%s': %s", address, botName, e)
}
return nil, fmt.Errorf("horizon error when checking for existence of account %s for bot '%s': %d (%v) -- could this be caused because horizon has not ingested this data yet? (programmer: maybe create hProtocol.Account instance manually instead of fetching)", address, botName, herr.Problem.Status, *herr)
}
return &account, nil
}
func (s *APIServer) makeSampleTrader(seed string) *trader.BotConfig {
return trader.MakeBotConfig(
"",
seed,
"XLM",
"",
"COUPON",
"GBMMZMK2DC4FFP4CAI6KCVNCQ7WLO5A7DQU7EC7WGHRDQBZB763X4OQI",
60,
15000,
5,
"both",
0,
0,
s.horizonTestnetURI,
nil,
"",
"",
&trader.FeeConfig{
CapacityTrigger: 0.8,
Percentile: 90,
MaxOpFeeStroops: 5000,
},
¢ralizedPricePrecisionOverride,
¢ralizedVolumePrecisionOverride,
¢ralizedMinBaseVolumeOverride,
¢ralizedMinQuoteVolumeOverride,
)
}
func makeSampleBuysell() *plugins.BuySellConfig {
return plugins.MakeBuysellConfig(
0.001,
0.001,
0.0,
0.0,
true,
10.0,
"exchange",
"kraken/XXLM/ZUSD",
"fixed",
"1.0",
[]plugins.StaticLevel{
plugins.StaticLevel{
SPREAD: 0.0010,
AMOUNT: 100.0,
}, plugins.StaticLevel{
SPREAD: 0.0015,
AMOUNT: 100.0,
}, plugins.StaticLevel{
SPREAD: 0.0020,
AMOUNT: 100.0,
}, plugins.StaticLevel{
SPREAD: 0.0025,
AMOUNT: 100.0,
},
},
)
}