-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmd.go
1309 lines (1091 loc) · 39.6 KB
/
cmd.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
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"bufio"
"context"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"math/big"
"os"
"strconv"
"time"
"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/starknet.go/rpc"
"github.com/consensys/gnark-crypto/ecc/stark-curve/fp"
"github.com/spf13/cobra"
)
func CreateRootCommand() *cobra.Command {
// rootCmd represents the base command when called without any subcommands
rootCmd := &cobra.Command{
Use: "influence-eth",
Short: "Influence.eth leaderboards by Moonstream",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}
completionCmd := CreateCompletionCommand(rootCmd)
versionCmd := CreateVersionCommand()
blockNumberCmd := CreateBlockNumberCommand()
doEverythingCmd := CreateDoEverythingCommand()
eventsCmd := CreateEventsCommand()
findDeploymentBlockCmd := CreateFindDeploymentCmd()
parseCmd := CreateParseCommand()
leaderboardCmd := CreateLeaderboardCommand()
leaderboardsCmd := CreateLeaderboardsCommand()
rootCmd.AddCommand(completionCmd, versionCmd, doEverythingCmd, blockNumberCmd, eventsCmd, findDeploymentBlockCmd, parseCmd, leaderboardCmd, leaderboardsCmd)
// By default, cobra Command objects write to stderr. We have to forcibly set them to output to
// stdout.
rootCmd.SetOut(os.Stdout)
return rootCmd
}
func CreateCompletionCommand(rootCmd *cobra.Command) *cobra.Command {
completionCmd := &cobra.Command{
Use: "completion",
Short: "Generate shell completion scripts for influence-eth",
Long: `Generate shell completion scripts for influence-eth.
The command for each shell will print a completion script to stdout. You can source this script to get
completions in your current shell session. You can add this script to the completion directory for your
shell to get completions for all future sessions.
For example, to activate bash completions in your current shell:
$ . <(influence-eth completion bash)
To add influence-eth completions for all bash sessions:
$ influence-eth completion bash > /etc/bash_completion.d/influence-eth_completions`,
}
bashCompletionCmd := &cobra.Command{
Use: "bash",
Short: "bash completions for influence-eth",
Run: func(cmd *cobra.Command, args []string) {
rootCmd.GenBashCompletion(cmd.OutOrStdout())
},
}
zshCompletionCmd := &cobra.Command{
Use: "zsh",
Short: "zsh completions for influence-eth",
Run: func(cmd *cobra.Command, args []string) {
rootCmd.GenZshCompletion(cmd.OutOrStdout())
},
}
fishCompletionCmd := &cobra.Command{
Use: "fish",
Short: "fish completions for influence-eth",
Run: func(cmd *cobra.Command, args []string) {
rootCmd.GenFishCompletion(cmd.OutOrStdout(), true)
},
}
powershellCompletionCmd := &cobra.Command{
Use: "powershell",
Short: "powershell completions for influence-eth",
Run: func(cmd *cobra.Command, args []string) {
rootCmd.GenPowerShellCompletion(cmd.OutOrStdout())
},
}
completionCmd.AddCommand(bashCompletionCmd, zshCompletionCmd, fishCompletionCmd, powershellCompletionCmd)
return completionCmd
}
func CreateVersionCommand() *cobra.Command {
versionCmd := &cobra.Command{
Use: "version",
Short: "Print the version of influence-eth that you are currently using",
Run: func(cmd *cobra.Command, args []string) {
cmd.Println(Version)
},
}
return versionCmd
}
func CreateBlockNumberCommand() *cobra.Command {
var providerURL string
var timeout uint64
blockNumberCmd := &cobra.Command{
Use: "block-number",
Short: "Get the current block number on your Starknet RPC provider",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if providerURL == "" {
providerURLFromEnv := os.Getenv("STARKNET_RPC_URL")
if providerURLFromEnv == "" {
return errors.New("you must provide a provider URL using -p/--provider or set the STARKNET_RPC_URL environment variable")
}
providerURL = providerURLFromEnv
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
client, clientErr := rpc.NewClient(providerURL)
if clientErr != nil {
return clientErr
}
provider := rpc.NewProvider(client)
ctx := context.Background()
if timeout > 0 {
ctx, _ = context.WithDeadline(ctx, time.Now().Add(time.Duration(timeout)*time.Second))
}
blockNumber, err := provider.BlockNumber(ctx)
if err != nil {
return err
}
cmd.Println(blockNumber)
return nil
},
}
blockNumberCmd.Flags().StringVarP(&providerURL, "provider", "p", "", "The URL of your Starknet RPC provider (defaults to value of STARKNET_RPC_URL environment variable)")
blockNumberCmd.Flags().Uint64VarP(&timeout, "timeout", "t", 0, "The timeout for requests to your Starknet RPC provider")
return blockNumberCmd
}
func CreateEventsCommand() *cobra.Command {
var providerURL, contractAddress string
var timeout, fromBlock, toBlock uint64
var batchSize, coldInterval, hotInterval, hotThreshold, confirmations int
eventsCmd := &cobra.Command{
Use: "events",
Short: "Crawl events from your Starknet RPC provider",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if providerURL == "" {
providerURLFromEnv := os.Getenv("STARKNET_RPC_URL")
if providerURLFromEnv == "" {
return errors.New("you must provide a provider URL using -p/--provider or set the STARKNET_RPC_URL environment variable")
}
providerURL = providerURLFromEnv
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
client, clientErr := rpc.NewClient(providerURL)
if clientErr != nil {
return clientErr
}
provider := rpc.NewProvider(client)
ctx := context.Background()
eventsChan := make(chan RawEvent)
// If "fromBlock" is not specified, find the block at which the contract was deployed and
// use that instead.
if fromBlock == 0 {
addressFelt, parseAddressErr := FeltFromHexString(contractAddress)
if parseAddressErr != nil {
return parseAddressErr
}
deploymentBlock, fromBlockErr := DeploymentBlock(ctx, provider, addressFelt)
if fromBlockErr != nil {
return fromBlockErr
}
fromBlock = deploymentBlock
}
go ContractEvents(ctx, provider, contractAddress, eventsChan, hotThreshold, time.Duration(hotInterval)*time.Millisecond, time.Duration(coldInterval)*time.Millisecond, fromBlock, toBlock, confirmations, batchSize)
for event := range eventsChan {
unparsedEvent := ParsedEvent{Name: EVENT_UNKNOWN, Event: event}
serializedEvent, marshalErr := json.Marshal(unparsedEvent)
if marshalErr != nil {
cmd.ErrOrStderr().Write([]byte(marshalErr.Error()))
}
cmd.Println(string(serializedEvent))
}
return nil
},
}
eventsCmd.PersistentFlags().StringVarP(&providerURL, "provider", "p", "", "The URL of your Starknet RPC provider (defaults to value of STARKNET_RPC_URL environment variable)")
eventsCmd.PersistentFlags().Uint64VarP(&timeout, "timeout", "t", 0, "The timeout for requests to your Starknet RPC provider")
eventsCmd.Flags().StringVarP(&contractAddress, "contract", "c", "", "The address of the contract from which to crawl events (if not provided, no contract constraint will be specified)")
eventsCmd.Flags().IntVarP(&batchSize, "batch-size", "N", 100, "The number of events to fetch per batch (defaults to 100)")
eventsCmd.Flags().IntVar(&hotThreshold, "hot-threshold", 2, "Number of successive iterations which must return events before we consider the crawler hot")
eventsCmd.Flags().IntVar(&hotInterval, "hot-interval", 100, "Milliseconds at which to poll the provider for updates on the contract while the crawl is hot")
eventsCmd.Flags().IntVar(&coldInterval, "cold-interval", 10000, "Milliseconds at which to poll the provider for updates on the contract while the crawl is cold")
eventsCmd.Flags().IntVar(&confirmations, "confirmations", 5, "Number of confirmations to wait for before considering a block canonical")
eventsCmd.Flags().Uint64Var(&fromBlock, "from", 0, "The block number from which to start crawling")
eventsCmd.Flags().Uint64Var(&toBlock, "to", 0, "The block number to which to crawl (set to 0 for continuous crawl)")
return eventsCmd
}
func CreateFindDeploymentCmd() *cobra.Command {
var providerURL, contractAddress string
findDeploymentCmd := &cobra.Command{
Use: "find-deployment-block",
Short: "Discover the block number in which a contract was deployed",
PreRunE: func(cmd *cobra.Command, args []string) error {
if providerURL == "" {
providerURLFromEnv := os.Getenv("STARKNET_RPC_URL")
if providerURLFromEnv == "" {
return errors.New("you must provide a provider URL using -p/--provider or set the STARKNET_RPC_URL environment variable")
}
providerURL = providerURLFromEnv
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
client, clientErr := rpc.NewClient(providerURL)
if clientErr != nil {
return clientErr
}
provider := rpc.NewProvider(client)
ctx := context.Background()
if contractAddress == "" {
return errors.New("you must provide a contract address using -c/--contract")
}
fieldAdditiveIdentity := fp.NewElement(0)
if contractAddress[:2] == "0x" {
contractAddress = contractAddress[2:]
}
decodedAddress, decodeErr := hex.DecodeString(contractAddress)
if decodeErr != nil {
return decodeErr
}
address := felt.NewFelt(&fieldAdditiveIdentity)
address.SetBytes(decodedAddress)
deploymentBlock, err := DeploymentBlock(ctx, provider, address)
if err != nil {
return err
}
cmd.Println(deploymentBlock)
return nil
},
}
findDeploymentCmd.Flags().StringVarP(&providerURL, "provider", "p", "", "The URL of your Starknet RPC provider (defaults to value of STARKNET_RPC_URL environment variable)")
findDeploymentCmd.Flags().StringVarP(&contractAddress, "contract", "c", "", "The address of the smart contract to find the deployment block for")
return findDeploymentCmd
}
func CreateParseCommand() *cobra.Command {
var infile, outfile string
parseCmd := &cobra.Command{
Use: "parse",
Short: "Parse a file (as produced by the \"stark events\" command) to process previously unknown events",
RunE: func(cmd *cobra.Command, args []string) error {
ifp := os.Stdin
var infileErr error
if infile != "" && infile != "-" {
ifp, infileErr = os.Open(infile)
if infileErr != nil {
return infileErr
}
defer ifp.Close()
}
ofp := os.Stdout
var outfileErr error
if outfile != "" {
ofp, outfileErr = os.Create(outfile)
if outfileErr != nil {
return outfileErr
}
defer ofp.Close()
}
parser, newParserErr := NewEventParser()
if newParserErr != nil {
return newParserErr
}
newline := []byte("\n")
scanner := bufio.NewScanner(ifp)
for scanner.Scan() {
var partialEvent PartialEvent
line := scanner.Text()
json.Unmarshal([]byte(line), &partialEvent)
passThrough := true
if partialEvent.Name == EVENT_UNKNOWN {
var event RawEvent
json.Unmarshal(partialEvent.Event, &event)
parsedEvent, parseErr := parser.Parse(event)
if parseErr == nil {
passThrough = false
parsedEventBytes, marshalErr := json.Marshal(parsedEvent)
if marshalErr != nil {
return marshalErr
}
_, writeErr := ofp.Write(parsedEventBytes)
if writeErr != nil {
return writeErr
}
_, writeErr = ofp.Write(newline)
if writeErr != nil {
return writeErr
}
}
}
if passThrough {
partialEventBytes, marshalErr := json.Marshal(partialEvent)
if marshalErr != nil {
return marshalErr
}
_, writeErr := ofp.Write(partialEventBytes)
if writeErr != nil {
return writeErr
}
_, writeErr = ofp.Write(newline)
if writeErr != nil {
return writeErr
}
}
}
return nil
},
}
parseCmd.Flags().StringVarP(&infile, "infile", "i", "", "File containing crawled events from which to build the leaderboard (as produced by the \"influence-eth stark events\" command, defaults to stdin)")
parseCmd.Flags().StringVarP(&outfile, "outfile", "o", "", "File to write reparsed events to (defaults to stdout)")
return parseCmd
}
func CreateDoEverythingCommand() *cobra.Command {
var providerURL, contractAddress, outfile, fromBlockFilePath string
var batchSize, coldInterval, hotInterval, hotThreshold, confirmations int
doEverythingCmd := &cobra.Command{
Use: "do-everything",
Short: "Just do everything with events",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if providerURL == "" {
providerURLFromEnv := os.Getenv("STARKNET_RPC_URL")
if providerURLFromEnv == "" {
return errors.New("you must provide a provider URL using -p/--provider or set the STARKNET_RPC_URL environment variable")
}
providerURL = providerURLFromEnv
}
if fromBlockFilePath == "" {
return errors.New("flag --from-block-file should be set")
}
if outfile == "" {
return errors.New("flag -o/--outfile should be set")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
client, clientErr := rpc.NewClient(providerURL)
if clientErr != nil {
return clientErr
}
provider := rpc.NewProvider(client)
ctx := context.Background()
eventsChan := make(chan RawEvent)
var fromBlock uint64
fromBlockFile, err := os.Open(fromBlockFilePath)
if err != nil {
return err
}
defer fromBlockFile.Close()
scanner := bufio.NewScanner(fromBlockFile)
if scanner.Scan() {
blockNumberStr := scanner.Text()
fromBlock, err = strconv.ParseUint(blockNumberStr, 10, 64)
if err != nil {
return err
}
}
if fromBlock == 0 {
fieldAdditiveIdentity := fp.NewElement(0)
if contractAddress[:2] == "0x" {
contractAddress = contractAddress[2:]
}
decodedAddress, decodeErr := hex.DecodeString(contractAddress)
if decodeErr != nil {
return decodeErr
}
address := felt.NewFelt(&fieldAdditiveIdentity)
address.SetBytes(decodedAddress)
fromBlock, err = DeploymentBlock(ctx, provider, address)
if err != nil {
return err
}
}
latestBlock, err := provider.BlockNumber(ctx)
if err != nil {
return err
}
if fromBlock > latestBlock {
log.Printf("fromBlock %d can not be less then latest block %d", fromBlock, latestBlock)
return nil
}
ofp, err := os.OpenFile(outfile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer ofp.Close()
fmt.Printf("Starting processing events from block %d to block %d\n", fromBlock, latestBlock)
go ContractEvents(ctx, provider, contractAddress, eventsChan, hotThreshold, time.Duration(hotInterval)*time.Millisecond, time.Duration(coldInterval)*time.Millisecond, fromBlock, latestBlock, confirmations, batchSize)
parser, newParserErr := NewEventParser()
if newParserErr != nil {
return newParserErr
}
newline := []byte("\n")
batchCounter := 0
eventsCounter := big.NewInt(0)
for event := range eventsChan {
if batchCounter >= 1000 {
fmt.Printf("Processed another 1000 events with total %s, working block number %d\n", eventsCounter.String(), event.BlockNumber)
batchCounter = 0
}
batchCounter++
eventsCounter.Add(eventsCounter, big.NewInt(1))
unparsedEvent := ParsedEvent{Name: EVENT_UNKNOWN, Event: event}
passThrough := true
parsedEvent, parseErr := parser.Parse(event)
if parseErr == nil {
passThrough = false
parsedEventBytes, marshalErr := json.Marshal(parsedEvent)
if marshalErr != nil {
return marshalErr
}
if _, writeErr := ofp.Write(parsedEventBytes); writeErr != nil {
fmt.Printf("Error writing to file: %v\n", writeErr)
continue
}
if _, writeErr := ofp.Write(newline); writeErr != nil {
fmt.Printf("Error writing newline to file: %v\n", writeErr)
continue
}
}
if passThrough {
serializedEvent, marshalErr := json.Marshal(unparsedEvent)
if marshalErr != nil {
return marshalErr
}
if _, writeErr := ofp.Write(serializedEvent); writeErr != nil {
fmt.Printf("Error writing to file: %v\n", writeErr)
continue
}
if _, writeErr := ofp.Write(newline); writeErr != nil {
fmt.Printf("Error writing newline to file: %v\n", writeErr)
continue
}
}
}
fmt.Printf("Processed %s events from block %d to block %d\n", eventsCounter.String(), fromBlock, latestBlock)
recordedBlock := latestBlock + 1
writeBlockErr := os.WriteFile(fromBlockFilePath, []byte(fmt.Sprintf("%d", recordedBlock)), 0644)
if writeBlockErr != nil {
return writeBlockErr
}
fmt.Printf("Updated old block number %d to %d in file %s\n", fromBlock, recordedBlock, fromBlockFilePath)
return nil
},
}
doEverythingCmd.Flags().StringVarP(&providerURL, "provider", "p", "", "The URL of your Starknet RPC provider (defaults to value of STARKNET_RPC_URL environment variable)")
doEverythingCmd.Flags().StringVarP(&contractAddress, "contract", "c", "", "The address of the contract from which to crawl events (if not provided, no contract constraint will be specified)")
doEverythingCmd.Flags().IntVarP(&batchSize, "batch-size", "N", 100, "The number of events to fetch per batch (defaults to 100)")
doEverythingCmd.Flags().IntVar(&hotThreshold, "hot-threshold", 2, "Number of successive iterations which must return events before we consider the crawler hot")
doEverythingCmd.Flags().IntVar(&hotInterval, "hot-interval", 100, "Milliseconds at which to poll the provider for updates on the contract while the crawl is hot")
doEverythingCmd.Flags().IntVar(&coldInterval, "cold-interval", 10000, "Milliseconds at which to poll the provider for updates on the contract while the crawl is cold")
doEverythingCmd.Flags().IntVar(&confirmations, "confirmations", 5, "Number of confirmations to wait for before considering a block canonical")
doEverythingCmd.Flags().StringVarP(&fromBlockFilePath, "from-block-file", "f", "", "File contains the block number from which to start crawling")
doEverythingCmd.Flags().StringVarP(&outfile, "outfile", "o", "", "File to write reparsed events to")
return doEverythingCmd
}
type LeaderboardCommandCreator func(infile, outfile, accessToken, leaderboardId *string) error
type LeaderboardCommandFunc struct {
Name string
Description string
Func LeaderboardCommandCreator
}
var LEADERBOARD_MISSIONS = []LeaderboardCommandFunc{
{
Name: "c-1-base-camp",
Description: "Prepare community leaderboard",
Func: CL1BaseCamp,
},
{
Name: "c-2-romulus-remus-and-the-rest",
Description: "Prepare community leaderboard",
Func: CL2RomulusRemusAndTheRest,
},
{
Name: "c-3-learn-by-doing",
Description: "Prepare community leaderboard",
Func: CL3LearnByDoing,
},
{
Name: "c-4-four-pillars",
Description: "Prepare community leaderboard",
Func: CL4FourPillars,
},
{
Name: "c-5-together-we-can-rise",
Description: "Prepare community leaderboard",
Func: CL5TogetherWeCanRise,
},
{
Name: "c-6-the-fleet",
Description: "Prepare community leaderboard",
Func: CL6TheFleet,
},
{
Name: "c-7-rock-breaker",
Description: "Prepare community leaderboard",
Func: CL7RockBreaker,
},
{
Name: "c-8-good-news-everyone",
Description: "Prepare community leaderboard",
Func: CL8GoodNewsEveryone,
},
{
Name: "c-9-prospecting-pays-off",
Description: "Prepare community leaderboard",
Func: CL9ProspectingPaysOff,
},
{
Name: "c-10-potluck",
Description: "Prepare community leaderboard",
Func: CL10Potluck,
},
{
Name: "1-new-recruits-r1",
Description: "Prepare leaderboard",
Func: L1NewRecruitsR1,
},
{
Name: "1-new-recruits-r2",
Description: "Prepare leaderboard",
Func: L1NewRecruitsR2,
},
{
Name: "2-buried-treasure-r1",
Description: "Prepare leaderboard",
Func: L2BuriedTreasureR1,
},
{
Name: "2-buried-treasure-r2",
Description: "Prepare leaderboard",
Func: L2BuriedTreasureR2,
},
{
Name: "3-market-maker-r1",
Description: "Prepare leaderboard",
Func: L3MarketMakerR1,
},
{
Name: "3-market-maker-r2",
Description: "Prepare leaderboard",
Func: L3MarketMakerR2,
},
{
Name: "4-breaking-ground-r1",
Description: "Prepare leaderboard",
Func: L4BreakingGroundR1,
},
{
Name: "4-breaking-ground-r2",
Description: "Prepare leaderboard",
Func: L4BreakingGroundR2,
},
{
Name: "5-city-builder",
Description: "Prepare leaderboard",
Func: L5CityBuilder,
},
{
Name: "6-explore-the-stars-r1",
Description: "Prepare leaderboard",
Func: L6ExploreTheStarsR1,
},
{
Name: "6-explore-the-stars-r2",
Description: "Prepare leaderboard",
Func: L6ExploreTheStarsR2,
},
{
Name: "7-expand-the-colony",
Description: "Prepare leaderboard",
Func: L7ExpandTheColony,
},
{
Name: "8-special-delivery",
Description: "Prepare leaderboard",
Func: L8SpecialDelivery,
},
{
Name: "9-dinner-is-served",
Description: "Prepare leaderboard",
Func: L9DinnerIsServed,
},
}
type LeaderboardsMap struct {
Name string `json:"name"`
LeaderboardId string `json:"leaderboard_id"`
}
func CreateLeaderboardsCommand() *cobra.Command {
var infile, accessToken, leaderboardsMapFilePath string
leaderboardsCmd := &cobra.Command{
Use: "leaderboards",
Short: "Prepare all Moonstream.to leaderboards",
RunE: func(cmd *cobra.Command, args []string) error {
var inputFile *os.File
var readErr error
if leaderboardsMapFilePath != "" {
inputFile, readErr = os.Open(leaderboardsMapFilePath)
if readErr != nil {
log.Fatalf("Unable to read file %s, err: %v", leaderboardsMapFilePath, readErr)
}
} else {
log.Fatalf("Please specify file with events with --input flag")
}
defer inputFile.Close()
byteValue, err := ioutil.ReadAll(inputFile)
if err != nil {
log.Fatalf("Error reading file, err: %v", err)
}
leaderboardsMap := make(map[string]string)
err = json.Unmarshal(byteValue, &leaderboardsMap)
if err != nil {
log.Fatalf("Error unmarshalling JSON, err: %v", err)
}
for _, lm := range LEADERBOARD_MISSIONS {
lId, ok := leaderboardsMap[lm.Name]
if !ok {
log.Printf("Passed %s leaderboard, not ID passed in config file", lm.Name)
continue
}
emptyOutput := ""
err := lm.Func(&infile, &emptyOutput, &accessToken, &lId)
if err != nil {
log.Printf("Failed %s leaderboard", lm.Name)
continue
}
log.Printf("Updated %s leaderboard known as %s", lId, lm.Name)
time.Sleep(500 * time.Millisecond)
}
return nil
},
}
leaderboardsCmd.PersistentFlags().StringVarP(&infile, "infile", "i", "", "File containing crawled events from which to build the leaderboard (as produced by the \"influence-eth stark events\" command, defaults to stdin)")
leaderboardsCmd.PersistentFlags().StringVarP(&accessToken, "token", "t", "", "Moonstream user access token (could be set with MOONSTREAM_ACCESS_TOKEN environment variable)")
leaderboardsCmd.PersistentFlags().StringVarP(&leaderboardsMapFilePath, "leaderboards-map", "m", "", "Pass to leaderboards map JSON file")
return leaderboardsCmd
}
func CreateLeaderboardCommand() *cobra.Command {
var infile, outfile, accessToken, leaderboardId string
leaderboardCmd := &cobra.Command{
Use: "leaderboard",
Short: "Prepare Moonstream.to leaderboard",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}
leaderboardCmd.PersistentFlags().StringVarP(&infile, "infile", "i", "", "File containing crawled events from which to build the leaderboard (as produced by the \"influence-eth stark events\" command, defaults to stdin)")
leaderboardCmd.PersistentFlags().StringVarP(&outfile, "outfile", "o", "", "File to write reparsed events to (defaults to stdout)")
leaderboardCmd.PersistentFlags().StringVarP(&accessToken, "token", "t", "", "Moonstream user access token (could be set with MOONSTREAM_ACCESS_TOKEN environment variable)")
leaderboardCmd.PersistentFlags().StringVarP(&leaderboardId, "leaderboard-id", "l", "", "Leaderboard ID to update data for at Moonstream.to portal")
for _, lm := range LEADERBOARD_MISSIONS {
lm := lm // Create a local copy of lm for closure to capture
newCmd := &cobra.Command{
Use: lm.Name,
Short: lm.Description,
RunE: func(cmd *cobra.Command, args []string) error {
err := lm.Func(&infile, &outfile, &accessToken, &leaderboardId)
return err
},
}
leaderboardCmd.AddCommand(newCmd)
}
lCrewOwnersCmd := CreateLCrewOwnersCommand(&infile, &outfile, &accessToken, &leaderboardId)
lCrewsCmd := CreateLCrewsCommand(&infile, &outfile, &accessToken, &leaderboardId)
leaderboardCmd.AddCommand(lCrewOwnersCmd, lCrewsCmd)
return leaderboardCmd
}
func CL1BaseCamp(infile, outfile, accessToken, leaderboardId *string) error {
events, parseEventsErr := ParseEventFromFile[TransitFinished](*infile, "TransitFinished")
if parseEventsErr != nil {
return parseEventsErr
}
scores := GenerateC1BaseCampToScores(events)
outErr := PrepareLeaderboardOutput(scores, *outfile, *accessToken, *leaderboardId)
if outErr != nil {
return outErr
}
return nil
}
func CL2RomulusRemusAndTheRest(infile, outfile, accessToken, leaderboardId *string) error {
conPlanEvents, parseEventsErr := ParseEventFromFile[ConstructionPlanned](*infile, "ConstructionPlanned")
if parseEventsErr != nil {
return parseEventsErr
}
conFinEvents, parseEventsErr := ParseEventFromFile[ConstructionFinished](*infile, "ConstructionFinished")
if parseEventsErr != nil {
return parseEventsErr
}
asteroids := map[uint64]bool{
1: true, // AP
}
scores := GenerateCommunityConstructionsToScores(conPlanEvents, conFinEvents, nil, asteroids, 5000, 15000)
outErr := PrepareLeaderboardOutput(scores, *outfile, *accessToken, *leaderboardId)
if outErr != nil {
return outErr
}
return nil
}
func CL3LearnByDoing(infile, outfile, accessToken, leaderboardId *string) error {
conPlanEvents, parseEventsErr := ParseEventFromFile[ConstructionPlanned](*infile, "ConstructionPlanned")
if parseEventsErr != nil {
return parseEventsErr
}
conFinEvents, parseEventsErr := ParseEventFromFile[ConstructionFinished](*infile, "ConstructionFinished")
if parseEventsErr != nil {
return parseEventsErr
}
buildingTypes := map[uint64]bool{
1: true, // Warehouse
2: true, // Extractor
}
scores := GenerateCommunityConstructionsToScores(conPlanEvents, conFinEvents, buildingTypes, nil, 4000, 10000)
outErr := PrepareLeaderboardOutput(scores, *outfile, *accessToken, *leaderboardId)
if outErr != nil {
return outErr
}
return nil
}
func CL4FourPillars(infile, outfile, accessToken, leaderboardId *string) error {
conPlanEvents, parseEventsErr := ParseEventFromFile[ConstructionPlanned](*infile, "ConstructionPlanned")
if parseEventsErr != nil {
return parseEventsErr
}
conFinEvents, parseEventsErr := ParseEventFromFile[ConstructionFinished](*infile, "ConstructionFinished")
if parseEventsErr != nil {
return parseEventsErr
}
buildingTypes := map[uint64]bool{
3: true, // Refinery
4: true, // Bioreactor
5: true, // Factory
6: true, // Shipyard
}
scores := GenerateCommunityConstructionsToScores(conPlanEvents, conFinEvents, buildingTypes, nil, 2000, 5000)
outErr := PrepareLeaderboardOutput(scores, *outfile, *accessToken, *leaderboardId)
if outErr != nil {
return outErr
}
return nil
}
func CL5TogetherWeCanRise(infile, outfile, accessToken, leaderboardId *string) error {
conPlanEvents, parseEventsErr := ParseEventFromFile[ConstructionPlanned](*infile, "ConstructionPlanned")
if parseEventsErr != nil {
return parseEventsErr
}
conFinEvents, parseEventsErr := ParseEventFromFile[ConstructionFinished](*infile, "ConstructionFinished")
if parseEventsErr != nil {
return parseEventsErr
}
buildingTypes := map[uint64]bool{
7: true, // Spaceport
8: true, // Marketplace
9: true, // Habitat
}
scores := GenerateCommunityConstructionsToScores(conPlanEvents, conFinEvents, buildingTypes, nil, 300, 1000)
outErr := PrepareLeaderboardOutput(scores, *outfile, *accessToken, *leaderboardId)
if outErr != nil {
return outErr
}
return nil
}
func CL6TheFleet(infile, outfile, accessToken, leaderboardId *string) error {
events, parseEventsErr := ParseEventFromFile[ShipAssemblyFinished](*infile, "ShipAssemblyFinished")
if parseEventsErr != nil {
return parseEventsErr
}
scores := GenerateC6TheFleet(events)
outErr := PrepareLeaderboardOutput(scores, *outfile, *accessToken, *leaderboardId)
if outErr != nil {
return outErr
}
return nil
}
func CL7RockBreaker(infile, outfile, accessToken, leaderboardId *string) error {
events, parseEventsErr := ParseEventFromFile[ResourceExtractionFinished](*infile, "ResourceExtractionFinished")
if parseEventsErr != nil {
return parseEventsErr
}
scores := GenerateC7RockBreaker(events)
outErr := PrepareLeaderboardOutput(scores, *outfile, *accessToken, *leaderboardId)
if outErr != nil {
return outErr
}
return nil
}
func CL8GoodNewsEveryone(infile, outfile, accessToken, leaderboardId *string) error {
unknownEvents, parseEventsErr := ParseEventFromFile[RawEvent](*infile, "UNKNOWN")
if parseEventsErr != nil {
return parseEventsErr
}
trFinEvents, parseEventsErr := ParseEventFromFile[TransitFinished](*infile, "TransitFinished")
if parseEventsErr != nil {
return parseEventsErr
}
scores := GenerateC8GoodNewsEveryoneToScores(trFinEvents, unknownEvents)
outErr := PrepareLeaderboardOutput(scores, *outfile, *accessToken, *leaderboardId)
if outErr != nil {
return outErr
}
return nil
}
func CL9ProspectingPaysOff(infile, outfile, accessToken, leaderboardId *string) error {
events, parseEventsErr := ParseEventFromFile[SamplingDepositFinished](*infile, "SamplingDepositFinished")
if parseEventsErr != nil {
return parseEventsErr
}
scores := GenerateC9ProspectingPaysOff(events)
outErr := PrepareLeaderboardOutput(scores, *outfile, *accessToken, *leaderboardId)
if outErr != nil {
return outErr
}
return nil
}
func CL10Potluck(infile, outfile, accessToken, leaderboardId *string) error {
stEventsV1, parseEventsErr := ParseEventFromFile[MaterialProcessingStartedV1](*infile, "MaterialProcessingStartedV1")
if parseEventsErr != nil {
return parseEventsErr
}
finEvents, parseEventsErr := ParseEventFromFile[MaterialProcessingFinished](*infile, "MaterialProcessingFinished")
if parseEventsErr != nil {
return parseEventsErr
}
scores := GenerateC10Potluck(stEventsV1, finEvents)
outErr := PrepareLeaderboardOutput(scores, *outfile, *accessToken, *leaderboardId)
if outErr != nil {
return outErr
}
return nil
}
func CreateLCrewOwnersCommand(infile, outfile, accessToken, leaderboardId *string) *cobra.Command {
leaderboardCrewOwnersCmd := &cobra.Command{
Use: "crew-owners",
Short: "Prepare leaderboard with crews",
RunE: func(cmd *cobra.Command, args []string) error {
events, parseEventsErr := ParseEventFromFile[Influence_Contracts_Crew_Crew_Transfer](*infile, "influence::contracts::crew::Crew::Transfer")
if parseEventsErr != nil {
return parseEventsErr
}
scores := GenerateCrewOwnersToScores(events)
outErr := PrepareLeaderboardOutput(scores, *outfile, *accessToken, *leaderboardId)