diff --git a/evm-hello-world/convo.go b/evm-hello-world/convo.go index 1e9b0a2..1df82ec 100644 --- a/evm-hello-world/convo.go +++ b/evm-hello-world/convo.go @@ -1,7 +1,6 @@ package ethhelloworld import ( - "encoding/json" "fmt" "strconv" "strings" @@ -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 { @@ -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{}) } @@ -57,7 +62,7 @@ func (c *Convo) NextStep() (out loop.Cmd) { return cmd(codegen.AskInitialStartBlockType{}) } - return cmd(codegen.RunGenerate{}) + return c.NextPostSharedFlowStep(sharedFlowConfig) } func isValidChainName(input string) bool { @@ -65,28 +70,15 @@ func isValidChainName(input string) bool { } 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 { @@ -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) { @@ -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)) diff --git a/evm-hello-world/convo_test.go b/evm-hello-world/convo_test.go index 0797fcd..67623b8 100644 --- a/evm-hello-world/convo_test.go +++ b/evm-hello-world/convo_test.go @@ -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)