|
| 1 | +package nearhelloworld |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "regexp" |
| 7 | + |
| 8 | + registry "github.com/pinax-network/graph-networks-libs/packages/golang/lib" |
| 9 | + networks "github.com/streamingfast/firehose-networks" |
| 10 | + codegen "github.com/streamingfast/substreams-codegen" |
| 11 | + "github.com/streamingfast/substreams-codegen/base" |
| 12 | + "github.com/streamingfast/substreams-codegen/loop" |
| 13 | +) |
| 14 | + |
| 15 | +type Convo struct { |
| 16 | + *codegen.Conversation[*Project] |
| 17 | +} |
| 18 | + |
| 19 | +func New() codegen.Converser { |
| 20 | + return &Convo{&codegen.Conversation[*Project]{ |
| 21 | + State: &Project{ |
| 22 | + ConversationState: base.ConversationState{ |
| 23 | + ChainName: "near", |
| 24 | + }, |
| 25 | + }, |
| 26 | + }} |
| 27 | +} |
| 28 | + |
| 29 | +func init() { |
| 30 | + codegen.RegisterConversation( |
| 31 | + "near-hello-world", |
| 32 | + "Example Substreams that reads NEAR blocks and extract data from transactions and receipts.", |
| 33 | + "Use this example as a starting point to create your own custom Substreams, which indexes the data you need from the NEAR blockchain.", |
| 34 | + New, |
| 35 | + 60, |
| 36 | + "NEAR", |
| 37 | + ) |
| 38 | +} |
| 39 | + |
| 40 | +func (c *Convo) NextStep() loop.Cmd { |
| 41 | + p := c.State |
| 42 | + if p.Name == "" { |
| 43 | + return cmd(codegen.AskProjectName{}) |
| 44 | + } |
| 45 | + |
| 46 | + return cmd(codegen.RunGenerate{}) |
| 47 | +} |
| 48 | + |
| 49 | +func (c *Convo) Update(msg loop.Msg) loop.Cmd { |
| 50 | + switch msg := msg.(type) { |
| 51 | + case codegen.MsgStart: |
| 52 | + c.SetClientVersion(msg.Version) |
| 53 | + var msgCmd loop.Cmd |
| 54 | + if msg.Hydrate != nil { |
| 55 | + if err := json.Unmarshal([]byte(msg.Hydrate.SavedState), &c.State); err != nil { |
| 56 | + return loop.Quit(fmt.Errorf(`something went wrong, here's an error message to share with our devs (%s); we've notified them already`, err)) |
| 57 | + } |
| 58 | + |
| 59 | + msgCmd = c.Msg().Message("Ok, I reloaded your state.").Cmd() |
| 60 | + } else { |
| 61 | + msgCmd = c.Msg().Message("Ok, let's start a new package.").Cmd() |
| 62 | + } |
| 63 | + return loop.Seq(msgCmd, c.NextStep()) |
| 64 | + |
| 65 | + case codegen.InputSubstreamsConsumptionChoice: |
| 66 | + return c.HandleSubstreamsConsumptionChoice(msg.Value) |
| 67 | + |
| 68 | + case codegen.InputSourceDownloaded: |
| 69 | + return c.HandleDownloaded(msg.Value) |
| 70 | + |
| 71 | + case codegen.AskProjectName: |
| 72 | + return c.CmdAskProjectName() |
| 73 | + |
| 74 | + case codegen.InputProjectName: |
| 75 | + c.State.Name = msg.Value |
| 76 | + return c.NextStep() |
| 77 | + |
| 78 | + case codegen.RunGenerate: |
| 79 | + return c.CmdGenerate(c.State.Generate) |
| 80 | + |
| 81 | + case codegen.ReturnGenerate: |
| 82 | + return c.CmdDownloadFiles(msg) |
| 83 | + } |
| 84 | + |
| 85 | + return loop.Quit(fmt.Errorf("invalid loop message: %T", msg)) |
| 86 | +} |
| 87 | + |
| 88 | +var cmd = codegen.Cmd |
| 89 | + |
| 90 | +var nearNetworkRegexp = regexp.MustCompile(`^near`) |
| 91 | + |
| 92 | +func nearNetworks() []*registry.Network { |
| 93 | + return networks.GetSubstreamsRegistry().Search(nearNetworkRegexp) |
| 94 | +} |
| 95 | + |
0 commit comments