Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 17 additions & 37 deletions evm-hello-world/convo.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ethhelloworld

import (
"encoding/json"
"fmt"
"strconv"
"strings"
Expand All @@ -10,6 +9,11 @@ import (
"github.com/streamingfast/substreams-codegen/loop"
)

var sharedFlowConfig = codegen.SharedFlowConfig{
// For now, we ask in this specific conversation for the chain's name, to be refactored at some point
ValidChains: nil,
}

func init() {
supportedChains := make([]string, 0, len(ChainConfigs))
for _, conf := range ChainConfigs {
Expand Down Expand Up @@ -40,11 +44,12 @@ func New() codegen.Converser {
}

func (c *Convo) NextStep() (out loop.Cmd) {
if !c.IsPreSharedFlowDone(sharedFlowConfig) {
return c.NextPreSharedFlowStep(sharedFlowConfig)
}

p := c.State

if p.Name == "" {
return cmd(codegen.AskProjectName{})
}
if p.ChainName == "" {
return cmd(codegen.AskChainName{})
}
Expand All @@ -57,36 +62,23 @@ func (c *Convo) NextStep() (out loop.Cmd) {
return cmd(codegen.AskInitialStartBlockType{})
}

return cmd(codegen.RunGenerate{})
return c.NextPostSharedFlowStep(sharedFlowConfig)
}

func isValidChainName(input string) bool {
return ChainConfigByID[input] != nil
}

func (c *Convo) Update(msg loop.Msg) loop.Cmd {
switch msg := msg.(type) {
case codegen.MsgStart:
c.SetClientVersion(msg.Version)
var msgCmd loop.Cmd
if msg.Hydrate != nil {
if err := json.Unmarshal([]byte(msg.Hydrate.SavedState), &c.State); err != nil {
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))
}

msgCmd = c.Msg().Message("Ok, I reloaded your state.").Cmd()
} else {
msgCmd = c.Msg().Message("Ok, let's start a new package.").Cmd()
}
return loop.Seq(msgCmd, c.NextStep())

case codegen.AskProjectName:
return c.CmdAskProjectName()
if c.IsPreSharedFlowMsg(msg, sharedFlowConfig) {
return c.UpdatePreSharedFlowMsg(msg, sharedFlowConfig, c.NextStep)
}

case codegen.InputProjectName:
c.State.Name = msg.Value
return c.NextStep()
if c.IsPostSharedFlowMsg(msg, sharedFlowConfig) {
return c.UpdatePostSharedFlowMsg(msg, sharedFlowConfig)
}

switch msg := msg.(type) {
case codegen.AskChainName:
var labels, values []string
for _, conf := range ChainConfigs {
Expand All @@ -103,12 +95,6 @@ func (c *Convo) Update(msg loop.Msg) loop.Cmd {
Messagef(`Hmm, %q seems like an invalid chain name. Maybe it was supported and is not anymore?`, c.State.ChainName).
Cmd()

case codegen.InputSubstreamsConsumptionChoice:
return c.HandleSubstreamsConsumptionChoice(msg.Value)

case codegen.InputSourceDownloaded:
return c.HandleSourceDownloaded(msg.Value)

case codegen.InputChainName:
c.State.ChainName = msg.Value
if isValidChainName(msg.Value) {
Expand Down Expand Up @@ -137,12 +123,6 @@ func (c *Convo) Update(msg loop.Msg) loop.Cmd {
c.State.InitialBlock = initialBlock
c.State.InitialBlockSet = true
return c.NextStep()

case codegen.RunGenerate:
return c.HandleRunGenerate(c.State.Generate)

case codegen.ReturnGenerate:
return c.HandleReturnGenerate(msg)
}

return loop.Quit(fmt.Errorf("invalid loop message: %T", msg))
Expand Down
10 changes: 10 additions & 0 deletions evm-hello-world/convo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ func TestConvoNextStep(t *testing.T) {
assert.Equal(t, codegen.AskChainName{}, next())
p.ChainName = "arbitrum"

assert.Equal(t, codegen.AskInitialStartBlockType{}, next())
p.InitialBlock = 0
p.InitialBlockSet = true

assert.Equal(t, codegen.AskSubstreamsConsumptionChoice{}, next())
sinkChoice := codegen.SubstreamsSinkChoiceSourceOnly
p.SubstreamsSinkChoice = &sinkChoice

assert.Equal(t, codegen.RunGenerate{}, next())

res := p.Generate()
assert.NoError(t, res.Err)
assert.NotEmpty(t, res.ProjectFiles)
Expand Down
Loading