-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.go
775 lines (669 loc) · 25.5 KB
/
api.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
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
// Package mango provides a client that can be used to make calls
// to the Manifold Markets API.
//
// Currently, it provides wrapper functions for every documented
// API call that Manifold offers. It also offers data types representing
// each data structure that can be returned by the API.
//
// See [the Manifold API docs] for more details.
//
// [the Manifold API docs]: https://docs.manifold.markets/api
package mango
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"strconv"
"strings"
)
// TODO: POST doc comments need a note that they won't work without an API key
// GetAuthenticatedUser returns the [User] associated with the current API key.
// If the key is invalid or not present, returns nil and an error.
//
// See [the Manifold API docs for GET /v0/me] for more details.
//
// [the Manifold API docs for GET /v0/me]: https://docs.manifold.markets/api#get-v0me
func (mc *Client) GetAuthenticatedUser() (*User, error) {
req, err := http.NewRequest(http.MethodGet, requestURL(mc.url, getMe, "", ""), nil)
if err != nil {
return nil, fmt.Errorf("error creating http request: %v", err)
}
resp, err := mc.postRequest(req)
if err != nil {
return nil, fmt.Errorf("error making http request: %v", err)
}
return parseResponse(resp, User{})
}
// GetBets returns a slice of [Bet] and an error. It takes a [GetBetsRequest] which has the following
// optional parameters:
// - [GetBetsRequest.UserId]
// - [GetBetsRequest.Username]
// - [GetBetsRequest.ContractId]
// - [GetBetsRequest.ContractSlug]
// - [GetBetsRequest.Before] - the ID of the bet before which the list will start.
// - [GetBetsRequest.Limit] - the maximum and default limit is 1000.
//
// If there is an error making the request, then nil and an error
// will be returned.
//
// See [the Manifold API docs for GET /v0/bets] for more details.
//
// [the Manifold API docs for GET /v0/bets]: https://docs.manifold.markets/api#get-v0bets
func (mc *Client) GetBets(gbr GetBetsRequest) (*[]Bet, error) {
if gbr.Limit == 0 {
gbr.Limit = defaultLimit
}
resp, err := http.Get(requestURL(
mc.url,
getBets, "", "",
"userId", gbr.UserId,
"username", gbr.Username,
"contractId", gbr.ContractId,
"contractSlug", gbr.ContractSlug,
"before", gbr.Before,
"limit", strconv.FormatInt(gbr.Limit, 10),
))
if err != nil {
return nil, fmt.Errorf("error making http request: %v", err)
}
return parseResponse(resp, []Bet{})
}
// GetComments returns a slice of [Comment] and an error. It takes a [GetCommentsRequest] which has the following
// optional parameters:
// - [GetCommentsRequest.ContractId]
// - [GetCommentsRequest.ContractSlug]
//
// If there is an error making the request, then nil and an error
// will be returned.
//
// See [the Manifold API docs for GET /v0/comments] for more details.
//
// [the Manifold API docs for GET /v0/comments]: https://docs.manifold.markets/api#get-v0comments
func (mc *Client) GetComments(gcr GetCommentsRequest) (*[]Comment, error) {
if gcr.ContractId == "" && gcr.ContractSlug == "" {
return nil, fmt.Errorf("either contractID or contractSlug must be specified")
}
resp, err := http.Get(requestURL(mc.url, getComments, "", "",
"contractId", gcr.ContractId,
"contractSlug", gcr.ContractSlug,
))
if err != nil {
return nil, fmt.Errorf("error making http request: %v", err)
}
return parseResponse(resp, []Comment{})
}
// GetGroupById returns a [Group] by its unique id.
//
// If there is an error making the request, then nil and an error
// will be returned.
//
// See [the Manifold API docs for GET /v0/group/by-id/id] for more details.
//
// [the Manifold API docs for GET /v0/group/by-id/id]: https://docs.manifold.markets/api#get-v0groupby-idid
func (mc *Client) GetGroupById(id string) (*Group, error) {
resp, err := http.Get(requestURL(mc.url, getGroupByID, id, ""))
if err != nil {
return nil, fmt.Errorf("error making http request: %v", err)
}
return parseResponse(resp, Group{})
}
// GetGroupBySlug returns a [Group] by its slug.
//
// If there is an error making the request, then nil and an error
// will be returned.
//
// See [the Manifold API docs for GET /v0/group/slug] for more details.
//
// [the Manifold API docs for GET /v0/group/slug]: https://docs.manifold.markets/api#get-v0groupslug
func (mc *Client) GetGroupBySlug(slug string) (*Group, error) {
resp, err := http.Get(requestURL(mc.url, getGroupBySlug, slug, ""))
if err != nil {
return nil, fmt.Errorf("error making http request: %v", err)
}
return parseResponse(resp, Group{})
}
// GetGroups returns a slice of [Group]. Optionally a userId can be passed:
// in this case, only groups available to the given user will be returned.
// Results are unordered.
//
// If there is an error making the request, then nil and an error
// will be returned.
//
// See [the Manifold API docs for GET /v0/groups] for more details.
//
// [the Manifold API docs for GET /v0/groups]: https://docs.manifold.markets/api#get-v0groups
func (mc *Client) GetGroups(userId *string) (*[]Group, error) {
uid := ""
if userId != nil {
uid = *userId
}
resp, err := http.Get(requestURL(
mc.url, getGroups, "", "",
"availableToUserId", uid,
))
if err != nil {
return nil, fmt.Errorf("error making http request: %v", err)
}
return parseResponse(resp, []Group{})
}
// GetMarketByID returns a [FullMarket] by its unique id.
//
// If there is an error making the request, then nil and an error
// will be returned.
//
// See [the Manifold API docs for GET /v0/market/marketId] for more details.
//
// [the Manifold API docs for GET /v0/market/marketId]: https://docs.manifold.markets/api#get-v0marketmarketid
func (mc *Client) GetMarketByID(id string) (*FullMarket, error) {
resp, err := http.Get(requestURL(mc.url, getMarketByID, id, ""))
if err != nil {
return nil, fmt.Errorf("error making http request: %v", err)
}
return parseResponse(resp, FullMarket{})
}
// GetMarketBySlug returns a [FullMarket] by its unique slug.
//
// If there is an error making the request, then nil and an error
// will be returned.
//
// See [the Manifold API docs for GET /v0/slug/marketSlug] for more details.
//
// [the Manifold API docs for GET /v0/slug/marketSlug]: https://docs.manifold.markets/api#get-v0slugmarketslug
func (mc *Client) GetMarketBySlug(slug string) (*FullMarket, error) {
resp, err := http.Get(requestURL(mc.url, getMarketBySlug, slug, ""))
if err != nil {
return nil, fmt.Errorf("error making http request: %v", err)
}
return parseResponse(resp, FullMarket{})
}
// GetMarkets returns a slice of [LiteMarket] and an error. It takes a [GetMarketsRequest] which has the following
// optional parameters:
// - [GetMarketsRequest.Before] - the ID of the market before which the list will start.
// - [GetMarketsRequest.Limit] - the maximum and default limit is 1000.
//
// If there is an error making the request, then nil and an error
// will be returned.
//
// See [the Manifold API docs for GET /v0/markets] for more details.
//
// [the Manifold API docs for GET /v0/markets]: https://docs.manifold.markets/api#get-v0markets
func (mc *Client) GetMarkets(gmr GetMarketsRequest) (*[]LiteMarket, error) {
if gmr.Limit == 0 {
gmr.Limit = defaultLimit
}
resp, err := http.Get(requestURL(
mc.url, getMarkets,
"",
"",
"limit", strconv.FormatInt(gmr.Limit, 10), "before", gmr.Before,
))
if err != nil {
return nil, fmt.Errorf("error making http request: %v", err)
}
return parseResponse(resp, []LiteMarket{})
}
// GetMarketsForGroup returns a slice of [LiteMarket] and an error. It takes a group ID to retrieve the markets for.
//
// If there is an error making the request, then nil and an error
// will be returned.
//
// See [the Manifold API docs for GET /v0/group/by-id/id/markets] for more details
//
// [the Manifold API docs for GET /v0/group/by-id/id/markets]: https://docs.manifold.markets/api#get-v0groupby-ididmarkets
func (mc *Client) GetMarketsForGroup(id string) (*[]LiteMarket, error) {
resp, err := http.Get(requestURL(mc.url, getGroupByID, id, marketsSuffix))
if err != nil {
log.Printf("error making http request: %v", err)
}
return parseResponse(resp, []LiteMarket{})
}
// GetMarketPositions returns positions for a given [Market] ID. It takes a [GetMarketPositionsRequest]
// which has the following parameters:
// - [GetMarketPositionsRequest.MarketId] - Required.
// - [GetMarketPositionsRequest.Order] - Optional. The field to order results by. Default: "profit". Options: "shares" or "profit". // TODO: make this an enum
// - [GetMarketPositionsRequest.Top] - Optional. The number of top positions (ordered by order) to return. Default: null.
// - [GetMarketPositionsRequest.Bottom] - Optional. The number of bottom positions (ordered by order) to return. Default: null.
// - [GetMarketPositionsRequest.UserId] - Optional. The user ID to query by. Default: null. If provided, only the position for this user will be returned.
//
// If there is an error making the request, then nil and an error
// will be returned.
//
// See [the Manifold API docs for GET /v0/market/marketId/positions] for more details.
//
// [the Manifold API docs for GET /v0/market/marketId/positions]: https://docs.manifold.markets/api#get-v0marketmarketidpositions
func (mc *Client) GetMarketPositions(gmpr GetMarketPositionsRequest) (*[]ContractMetric, error) {
if gmpr.MarketId == "" {
return nil, fmt.Errorf("no market ID provided")
}
var t, b string
if gmpr.Top != 0 {
t = fmt.Sprintf("%d", gmpr.Top)
} else {
t = "null"
}
if gmpr.Bottom != 0 {
b = fmt.Sprintf("%d", gmpr.Bottom)
} else {
b = "null"
}
resp, err := http.Get(requestURL(mc.url, getMarketByID, gmpr.MarketId, positionsSuffix,
"order", gmpr.Order,
"top", t,
"bottom", b,
"userId", gmpr.UserId,
))
if err != nil {
return nil, fmt.Errorf("error making http request: %v", err)
}
return parseResponse(resp, []ContractMetric{})
}
// SearchMarkets returns a slice of [FullMarket] that are the results of searching for provided terms.
// It can take any number of strings. Each string should be an individual search term.
//
// If there is an error making the request, then nil and an error
// will be returned.
//
// See [the Manifold API docs for GET /v0/search-markets] for more details.
//
// [the Manifold API docs for GET /v0/search-markets]: https://docs.manifold.markets/api#get-v0search-markets
func (mc *Client) SearchMarkets(terms ...string) (*[]FullMarket, error) {
ts := ""
for i, t := range terms {
ts += strings.TrimSpace(t)
if i+1 < len(terms) {
ts += " "
}
}
resp, err := http.Get(requestURL(mc.url, getSearchMarkets, "", "",
"terms", ts,
))
if err != nil {
return nil, fmt.Errorf("error making http request: %v", err)
}
return parseResponse(resp, []FullMarket{})
}
// GetUserByID returns a [User] by user id.
//
// If there is an error making the request, then nil and an error
// will be returned.
//
// See [the Manifold API docs for GET /v0/user/by-id/id] for more details.
//
// [the Manifold API docs for GET /v0/user/by-id/id]: https://docs.manifold.markets/api#get-v0userby-idid
func (mc *Client) GetUserByID(id string) (*User, error) {
resp, err := http.Get(requestURL(mc.url, getUserByID, id, ""))
if err != nil {
return nil, fmt.Errorf("error making http request: %v", err)
}
return parseResponse(resp, User{})
}
// GetUserByUsername returns a [User] by username
//
// If there is an error making the request, then nil and an error
// will be returned.
//
// See [the Manifold API docs for GET /v0/user/username] for more details.
//
// [the Manifold API docs for GET /v0/user/username]: https://docs.manifold.markets/api#get-v0userusername
func (mc *Client) GetUserByUsername(un string) (*User, error) {
resp, err := http.Get(requestURL(mc.url, getUserByUsername, un, ""))
if err != nil {
return nil, fmt.Errorf("error making http request: %v", err)
}
return parseResponse(resp, User{})
}
// GetUsers returns a slice of [User] and an error. It takes a [GetUsersRequest] which has the following parameters:
// - [GetUsersRequest.Before] - Optional. The ID of the user before which the list will start.
// - [GetUsersRequest.Limit] - Optional. The default and maximum limit is 1000.
//
// If there is an error making the request, then nil and an error
// will be returned.
//
// See [the Manifold API docs for GET /v0/markets] for more details.
//
// [the Manifold API docs for GET /v0/markets]: https://docs.manifold.markets/api#get-v0markets
func (mc *Client) GetUsers(gur GetUsersRequest) (*[]User, error) {
if gur.Limit == 0 {
gur.Limit = defaultLimit
}
resp, err := http.Get(requestURL(
mc.url, getUsers,
"",
"",
"limit", strconv.FormatInt(gur.Limit, 10), "before", gur.Before,
))
if err != nil {
return nil, fmt.Errorf("error making http request: %v", err)
}
return parseResponse(resp, []User{})
}
// PostBet makes a new bet on a market. It takes a [PostBetRequest] which has the following parameters:
// - [PostBetRequest.Amount] - Required.
// - [PostBetRequest.ContractId] - Required.
// - [PostBetRequest.Outcome] - Required.
// - [PostBetRequest.LimitProb] - Optional. A number between 0.001 and 0.999 inclusive representing the limit probability for your bet
//
// If there is an error making the request, then an error will be returned.
//
// See [the Manifold API docs for POST /v0/bet] for more details.
//
// [the Manifold API docs for POST /v0/bet]: https://docs.manifold.markets/api#post-v0bet
func (mc *Client) PostBet(pbr PostBetRequest) error {
jsonBody, err := json.Marshal(pbr)
if err != nil {
return fmt.Errorf("error making http request: %v", err)
}
bodyReader := bytes.NewReader(jsonBody)
req, err := http.NewRequest(http.MethodPost, requestURL(
mc.url, postBet,
"",
""), bodyReader)
if err != nil {
return fmt.Errorf("error creating http request: %v", err)
}
resp, err := mc.postRequest(req)
if err != nil {
return fmt.Errorf("client: error making http request: %v", err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("bet placement failed with status %d", resp.StatusCode)
}
return nil
}
// CancelBet cancels an existing limit order for the given betId.
//
// If there is an error making the request, then an error will be returned.
//
// See [the Manifold API docs for POST /v0/bet/cancel/id] for more details.
//
// [the Manifold API docs for POST /v0/bet/cancel/id]: https://docs.manifold.markets/api#post-v0betcancelid
func (mc *Client) CancelBet(betId string) error {
req, err := http.NewRequest(http.MethodPost, requestURL(
mc.url, postCancellation,
betId,
""), nil)
if err != nil {
return fmt.Errorf("error creating http request: %v", err)
}
resp, err := mc.postRequest(req)
if err != nil {
return fmt.Errorf("client: error making http request: %v", err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("bet cancellation failed with status %d", resp.StatusCode)
}
return nil
}
// CreateMarket creates a new market. It takes a [PostMarketRequest] which has the following parameters:
// - [PostMarketRequest.OutcomeType] - Required. One of [OutcomeType]
// - [PostMarketRequest.Question] - Required.
// - [PostMarketRequest.Description] - Optional. Non-rich text description.
// - [PostMarketRequest.DescriptionHtml] - Optional.
// - [PostMarketRequest.DescriptionMarkdown] - Optional.
// - [PostMarketRequest.CloseTime] - Optional. In epoch time. Default 7 days from time of creation.
// - [PostMarketRequest.Visibility] - Optional. One of "public" or "unlisted" TODO: make this an enum
// - [PostMarketRequest.GroupId] - Optional. A group to show the market under.
// - [PostMarketRequest.InitialProb] - Required for binary markets. Must be between 1 and 99.
// - [PostMarketRequest.Min] - Required for numeric markets. The minimum value that the market may resolve to.
// - [PostMarketRequest.Max] - Required for numeric markets. The maximum value that the market may resolve to.
// - [PostMarketRequest.IsLogScale] - Required for numeric markets. If true, your numeric market will increase exponentially from min to max.
// - [PostMarketRequest.InitialVal] - Required for numeric markets. An initial value for the market, between min and max, exclusive.
// - [PostMarketRequest.Answers] - Required for multiple choice markets. An array of strings, each of which will be a valid answer for the market.
//
// If there is an error making the request, then an error will be returned.
//
// See [the Manifold API docs for POST /v0/market] for more details.
//
// [the Manifold API docs for POST /v0/market]: https://docs.manifold.markets/api#post-v0market
func (mc *Client) CreateMarket(pmr PostMarketRequest) (*string, error) {
// TODO: add input validation
jsonBody, err := json.Marshal(pmr)
if err != nil {
return nil, fmt.Errorf("error making http request: %v", err)
}
bodyReader := bytes.NewReader(jsonBody)
req, err := http.NewRequest(http.MethodPost, requestURL(
mc.url, postMarket,
"",
""), bodyReader)
if err != nil {
return nil, fmt.Errorf("error creating http request: %v", err)
}
resp, err := mc.postRequest(req)
if err != nil {
return nil, fmt.Errorf("client: error making http request: %v", err)
}
if resp.StatusCode != http.StatusOK {
// TODO: print the response body here too
return nil, fmt.Errorf("market creation failed with status %d", resp.StatusCode)
}
mir, err := parseResponse(resp, marketIdResponse{})
if err != nil {
return nil, fmt.Errorf("error reading response body: %v", err)
}
return &mir.Id, nil
}
// AddLiquidity adds a given amount of liquidity to a given market.
//
// If there is an error making the request, then an error will be returned.
//
// See [the Manifold API docs for POST /v0/market/marketId/add-liquidity] for more details.
//
// [the Manifold API docs for POST /v0/market/marketId/add-liquidity]: https://docs.manifold.markets/api#post-v0marketmarketidadd-liquidity
func (mc *Client) AddLiquidity(marketId string, amount int64) error {
amt := struct {
Amount int64 `json:"amount"`
}{amount}
jsonBody, err := json.Marshal(amt)
if err != nil {
return fmt.Errorf("error making http request: %v", err)
}
bodyReader := bytes.NewReader(jsonBody)
req, err := http.NewRequest(http.MethodPost, requestURL(
mc.url, postMarket,
marketId,
liquiditySuffix), bodyReader)
if err != nil {
return fmt.Errorf("error creating http request: %v", err)
}
resp, err := mc.postRequest(req)
if err != nil {
return fmt.Errorf("client: error making http request: %v", err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("liquidity addition failed with status %d", resp.StatusCode)
}
return nil
}
// CloseMarket updates the closing time of a given market to be the given epoch timestamp,
// or closes it immediately if no new time is provided.
//
// If there is an error making the request, then an error will be returned.
//
// See [the Manifold API docs for POST /v0/market/marketId/close] for more details.
//
// [the Manifold API docs for POST /v0/market/marketId/close]: https://docs.manifold.markets/api#post-v0marketmarketidclose
func (mc *Client) CloseMarket(marketId string, ct *int64) error {
if ct == nil {
ct = new(int64)
}
c := struct {
closeTime int64 `json:"closeTime,omitempty"`
}{*ct}
jsonBody, err := json.Marshal(c)
if err != nil {
return fmt.Errorf("error making http request: %v", err)
}
bodyReader := bytes.NewReader(jsonBody)
req, err := http.NewRequest(http.MethodPost, requestURL(
mc.url, postMarket,
marketId,
closureSuffix), bodyReader)
if err != nil {
return fmt.Errorf("error creating http request: %v", err)
}
resp, err := mc.postRequest(req)
if err != nil {
return fmt.Errorf("client: error making http request: %v", err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("market closure failed with status %d", resp.StatusCode)
}
return nil
}
// AddMarketToGroup adds a given market to a given group.
//
// If there is an error making the request, then an error will be returned.
//
// See [the Manifold API docs for POST /v0/market/marketId/group] for more details.
//
// [the Manifold API docs for POST /v0/market/marketId/group]: https://docs.manifold.markets/api#post-v0marketmarketidgroup
func (mc *Client) AddMarketToGroup(marketId, gi string) error {
g := struct {
groupId string `json:"groupId,omitempty"`
}{gi}
jsonBody, err := json.Marshal(g)
if err != nil {
return fmt.Errorf("error making http request: %v", err)
}
bodyReader := bytes.NewReader(jsonBody)
req, err := http.NewRequest(http.MethodPost, requestURL(
mc.url, postMarket,
marketId,
groupSuffix), bodyReader)
if err != nil {
return fmt.Errorf("error creating http request: %v", err)
}
resp, err := mc.postRequest(req)
if err != nil {
return fmt.Errorf("client: error making http request: %v", err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("adding market to group failed with status %d", resp.StatusCode)
}
return nil
}
// TODO: add more information about input params to this comment
// ResolveMarket creates a new market. It takes a [ResolveMarketRequest] which has the following parameters:
// - [ResolveMarketRequest.Outcome] - Required. One of "YES", "NO", "MKT", "CANCEL" or a number depending on market type. TODO: make this an enum
// - [ResolveMarketRequest.Resolutions] - Required for free response or multiple choice markets.
// - [ResolveMarketRequest.ProbabilityInt] - Required if value is present.
// - [ResolveMarketRequest.Value] - Optional, only relevant to numeric markets.
//
// If there is an error making the request, then an error will be returned.
//
// See [the Manifold API docs for POST /v0/market/marketId/resolve] for more details.
//
// [the Manifold API docs for POST /v0/market/marketId/resolve]: https://docs.manifold.markets/api#post-v0marketmarketidresolve
func (mc *Client) ResolveMarket(marketId string, rmr ResolveMarketRequest) error {
jsonBody, err := json.Marshal(rmr)
if err != nil {
return fmt.Errorf("error making http request: %v", err)
}
bodyReader := bytes.NewReader(jsonBody)
req, err := http.NewRequest(http.MethodPost, requestURL(
mc.url, postMarket,
marketId,
resolutionSuffix), bodyReader)
if err != nil {
return fmt.Errorf("error creating http request: %v", err)
}
resp, err := mc.postRequest(req)
if err != nil {
return fmt.Errorf("client: error making http request: %v", err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("market resolution failed with status %d", resp.StatusCode)
}
return nil
}
// SellShares creates a new market. It takes a [SellSharesRequest] which has the following parameters:
// - [SellSharesRequest.Outcome] - Optional. One of "YES" or "NO". If omitted and only one kind of shares are held, sells those. TODO: make this an enum
// - [SellSharesRequest.Shares] - Optional. If omitted, all shares held will be sold.
//
// If there is an error making the request, then an error will be returned.
//
// See [the Manifold API docs for POST /v0/market/marketId/sell] for more details.
//
// [the Manifold API docs for POST /v0/market/marketId/sell]: https://docs.manifold.markets/api#post-v0marketmarketidsell
func (mc *Client) SellShares(marketId string, ssr SellSharesRequest) error {
jsonBody, err := json.Marshal(ssr)
if err != nil {
return fmt.Errorf("error making http request: %v", err)
}
bodyReader := bytes.NewReader(jsonBody)
req, err := http.NewRequest(http.MethodPost, requestURL(
mc.url, postMarket,
marketId,
sellSuffix), bodyReader)
if err != nil {
return fmt.Errorf("error creating http request: %v", err)
}
resp, err := mc.postRequest(req)
if err != nil {
return fmt.Errorf("client: error making http request: %v", err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("selling shares failed with status %d", resp.StatusCode)
}
return nil
}
// PostComment makes a new bet on a market. It takes a [PostCommentRequest] which has the following parameters:
// - [PostCommentRequest.ContractId] - Required.
// - [PostCommentRequest.Content] - Optional. A plaintext string.
// - [PostCommentRequest.Html] - Optional.
// - [PostCommentRequest.Markdown] - Optional.
//
// If there is an error making the request, then an error will be returned.
//
// See [the Manifold API docs for POST /v0/comment] for more details.
//
// [the Manifold API docs for POST /v0/comment]: https://docs.manifold.markets/api#post-v0comment
func (mc *Client) PostComment(marketId string, pcr PostCommentRequest) error {
jsonBody, err := json.Marshal(pcr)
if err != nil {
return fmt.Errorf("error making http request: %v", err)
}
bodyReader := bytes.NewReader(jsonBody)
req, err := http.NewRequest(http.MethodPost, requestURL(
mc.url, postComment,
marketId,
""), bodyReader)
if err != nil {
return fmt.Errorf("error creating http request: %v", err)
}
resp, err := mc.postRequest(req)
if err != nil {
return fmt.Errorf("client: error making http request: %v", err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("posting comment failed with status %d", resp.StatusCode)
}
return nil
}
// parseResponse takes an HTTP response and a type and attempts to unmarshal
// the body from the response into the given type.
func parseResponse[S any](r *http.Response, s S) (*S, error) {
body, err := io.ReadAll(r.Body)
if err != nil {
return nil, fmt.Errorf("error reading response body: %v", err)
}
if r.StatusCode != http.StatusOK {
return nil, fmt.Errorf("non-200 status code found: %v, message: %v", r.StatusCode, string(body))
}
if err = json.Unmarshal(body, &s); err != nil {
return nil, fmt.Errorf("error unmarshalling JSON: %v", err)
}
return &s, nil
}
func (mc *Client) postRequest(req *http.Request) (*http.Response, error) {
if mc.key == "" {
return nil, fmt.Errorf("no API key found")
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Key %v", mc.key))
return mc.client.Do(req)
}