From 1208fec6ad015c440819d541ccbad2ba42afea50 Mon Sep 17 00:00:00 2001 From: Eduard Voiculescu Date: Tue, 10 Sep 2024 10:51:42 -0400 Subject: [PATCH] adding vara-extrinsics code generation --- vara-extrinsics/chain_configs.go | 42 ++++++++++++++ vara-extrinsics/convo.go | 39 ++++++++++++- vara-extrinsics/convo_test.go | 2 +- vara-extrinsics/generate.go | 8 +-- vara-extrinsics/logging.go | 4 +- vara-extrinsics/state.go | 7 ++- .../templates/CONTRIBUTING.md.gotmpl | 30 ++++++++++ vara-extrinsics/templates/README.md | 25 +++++++++ vara-extrinsics/templates/README.md.gotmpl | 55 ------------------- .../templates/substreams.yaml.gotmpl | 14 ++--- vara-extrinsics/types.go | 2 +- 11 files changed, 154 insertions(+), 74 deletions(-) create mode 100644 vara-extrinsics/chain_configs.go create mode 100644 vara-extrinsics/templates/CONTRIBUTING.md.gotmpl create mode 100644 vara-extrinsics/templates/README.md delete mode 100644 vara-extrinsics/templates/README.md.gotmpl diff --git a/vara-extrinsics/chain_configs.go b/vara-extrinsics/chain_configs.go new file mode 100644 index 0000000..b72f012 --- /dev/null +++ b/vara-extrinsics/chain_configs.go @@ -0,0 +1,42 @@ +package varaextrinsics + +import "sort" + +type ChainConfig struct { + ID string // Public + DisplayName string // Public + ExplorerLink string + FirehoseEndpoint string + Network string + + initialBlockCache map[string]uint64 +} + +var ChainConfigs []*ChainConfig + +var ChainConfigByID = map[string]*ChainConfig{ + "vara-mainnet": { + DisplayName: "Vara Mainnet", + ExplorerLink: "https://vara.subscan.io/", + FirehoseEndpoint: "mainnet.vara.streamingfast.io:443", + Network: "vara-mainnet", + initialBlockCache: make(map[string]uint64), + }, + "vara-testnet": { + DisplayName: "Vara Testnet", + ExplorerLink: "", + FirehoseEndpoint: "testnet.vara.streamingfast.io:443", + Network: "vara-testnet", + initialBlockCache: make(map[string]uint64), + }, +} + +func init() { + for k, v := range ChainConfigByID { + v.ID = k + ChainConfigs = append(ChainConfigs, v) + } + sort.Slice(ChainConfigs, func(i, j int) bool { + return ChainConfigs[i].DisplayName < ChainConfigs[j].DisplayName + }) +} diff --git a/vara-extrinsics/convo.go b/vara-extrinsics/convo.go index eb5e21a..9b7edda 100644 --- a/vara-extrinsics/convo.go +++ b/vara-extrinsics/convo.go @@ -1,4 +1,4 @@ -package soltransactions +package varaextrinsics import ( "encoding/json" @@ -19,7 +19,7 @@ type Convo struct { func init() { codegen.RegisterConversation( "vara-extrinsics", - "Stream Vara Extrinsics", + "Get Vara transactions filtered by specifics Extrinsics", "Allows you to specified a regex containing the Extrinsics used to filter Vara transactions", codegen.ConversationFactory(New), 40, @@ -65,6 +65,14 @@ func (p *Project) NextStep() (out loop.Cmd) { return cmd(codegen.AskProjectName{}) } + if p.ChainName == "" { + return cmd(codegen.AskChainName{}) + } + + if !p.IsValidChainName(p.ChainName) { + return loop.Seq(cmd(codegen.MsgInvalidChainName{}), cmd(codegen.AskChainName{})) + } + if !p.InitialBlockSet { return cmd(codegen.AskInitialStartBlockType{}) } @@ -111,6 +119,32 @@ func (c *Convo) Update(msg loop.Msg) loop.Cmd { c.state.Name = msg.Value return c.NextStep() + case codegen.AskChainName: + var labels, values []string + for _, conf := range ChainConfigs { + labels = append(labels, conf.DisplayName) + values = append(values, conf.ID) + } + return c.action(codegen.InputChainName{}).ListSelect("Please select the chain"). + Labels(labels...). + Values(values...). + Cmd() + + case codegen.MsgInvalidChainName: + return c.msg(). + Messagef(`Hmm, %q seems like an invalid chain name. Maybe it was supported and is not anymore?`, c.state.ChainName). + Cmd() + + case codegen.InputChainName: + c.state.ChainName = msg.Value + if c.state.IsValidChainName(msg.Value) { + return loop.Seq( + c.msg().Messagef("Got it, will be using chain %q", c.state.ChainConfig().DisplayName).Cmd(), + c.NextStep(), + ) + } + return c.NextStep() + case codegen.AskInitialStartBlockType: return c.action(codegen.InputAskInitialStartBlockType{}). TextInput(codegen.InputAskInitialStartBlockTypeTextInput(), "Submit"). @@ -136,7 +170,6 @@ func (c *Convo) Update(msg loop.Msg) loop.Cmd { case InputExtrinsicId: c.state.ExtrinsicId = msg.Value - fmt.Printf("%s", msg.Value) return c.NextStep() case codegen.RunGenerate: diff --git a/vara-extrinsics/convo_test.go b/vara-extrinsics/convo_test.go index a994dc0..b889da5 100644 --- a/vara-extrinsics/convo_test.go +++ b/vara-extrinsics/convo_test.go @@ -1,4 +1,4 @@ -package soltransactions +package varaextrinsics import ( "testing" diff --git a/vara-extrinsics/generate.go b/vara-extrinsics/generate.go index 44972e3..f9f0447 100644 --- a/vara-extrinsics/generate.go +++ b/vara-extrinsics/generate.go @@ -1,4 +1,4 @@ -package soltransactions +package varaextrinsics import ( "bytes" @@ -194,9 +194,9 @@ func (p *Project) Render() (projectFiles map[string][]byte, err error) { } templateFiles := map[string]string{ - "substreams.yaml.gotmpl": "substreams.yaml", - "README.md.gotmpl": "README.md", - // "CONTRIBUTING.md": "CONTRIBUTING.md", + "substreams.yaml.gotmpl": "substreams.yaml", + "README.md": "README.md", + "CONTRIBUTING.md.gotmpl": "CONTRIBUTING.md", } for templateFile, finalFileName := range templateFiles { diff --git a/vara-extrinsics/logging.go b/vara-extrinsics/logging.go index 99455b4..9464820 100644 --- a/vara-extrinsics/logging.go +++ b/vara-extrinsics/logging.go @@ -1,7 +1,7 @@ -package soltransactions +package varaextrinsics import ( "github.com/streamingfast/logging" ) -var zlog, tracer = logging.PackageLogger("vara-extrinsics", "github.com/streamingfast/substreams-codegen/codegen/vara-extrinsics") +var zlog, _ = logging.PackageLogger("vara-extrinsics", "github.com/streamingfast/substreams-codegen/codegen/vara-extrinsics") diff --git a/vara-extrinsics/state.go b/vara-extrinsics/state.go index 270fa5c..2f80f0b 100644 --- a/vara-extrinsics/state.go +++ b/vara-extrinsics/state.go @@ -1,4 +1,4 @@ -package soltransactions +package varaextrinsics import ( "strings" @@ -27,3 +27,8 @@ type Project struct { func (p *Project) ModuleName() string { return strings.ReplaceAll(p.Name, "-", "_") } func (p *Project) KebabName() string { return strings.ReplaceAll(p.Name, "_", "-") } + +func (p *Project) ChainConfig() *ChainConfig { return ChainConfigByID[p.ChainName] } +func (p *Project) ChainEndpoint() string { return ChainConfigByID[p.ChainName].FirehoseEndpoint } +func (p *Project) ChainNetwork() string { return ChainConfigByID[p.ChainName].Network } +func (p *Project) IsValidChainName(input string) bool { return ChainConfigByID[input] != nil } diff --git a/vara-extrinsics/templates/CONTRIBUTING.md.gotmpl b/vara-extrinsics/templates/CONTRIBUTING.md.gotmpl new file mode 100644 index 0000000..a79003d --- /dev/null +++ b/vara-extrinsics/templates/CONTRIBUTING.md.gotmpl @@ -0,0 +1,30 @@ +# Contributing + +## Understand the Generated Project + +Only a `substreams.yaml` file has been generated. This file declares a Substreams module, `filtered_extrinsics`, which uses a Vara Foundational Module (a module built by the StreamingFast team). + +```yaml +specVersion: v0.1.0 +package: + name: {{ .Name }} + version: v0.1.0 + +imports: + vara: https://github.com/streamingfast/substreams-foundational-modules/releases/download/vara-foundational-v0.1.4/foundational-modules-vara-common-v0.1.4.spkg # 1 + +modules: + - name: map_filtered_extrinsics # 2 + use: vara:filtered_extrinsics # 3 + +network: vara-mainnet + +params: + map_filtered_extrinsics: {{ .ExtrinsicId }} # 4 +``` + +1. Import the Vara Foundational Modules. +2. Declare the `map_filtered_extrinsics` module, which you will run later. +3. Use the `filtered_extrinsics` module from the Vara Foundational Modules. + Essentially, you are _using_ the Vara Foundational Module, which is pre-built for you. +4. Pass the regular expression to filter the transactions based on the specified Extrinsic IDs. diff --git a/vara-extrinsics/templates/README.md b/vara-extrinsics/templates/README.md new file mode 100644 index 0000000..f8265eb --- /dev/null +++ b/vara-extrinsics/templates/README.md @@ -0,0 +1,25 @@ +# Vara Extrinsic + +This Substreams project allows you to retrieve Vara transactions filtered by one or several Extrincis (i.e. you will only receive transactions containing the specified Extrinsics). + +## Get Started + +### Build the Substreams + +```bash +substreams build +``` + +### Authenticate + +```bash +substreams auth +``` + +### Run your Substreams + +```bash +substreams gui +``` + +For information on contributing, please refer to [CONTRIBUTING.md](CONTRIBUTING.md). diff --git a/vara-extrinsics/templates/README.md.gotmpl b/vara-extrinsics/templates/README.md.gotmpl deleted file mode 100644 index dc34069..0000000 --- a/vara-extrinsics/templates/README.md.gotmpl +++ /dev/null @@ -1,55 +0,0 @@ -# Vara Extrinsic - -This Substreams project allows you to retrieve Solana transactions filtered by one or several Program IDs (i.e. you will only receive transactions containing the specified Program IDs). -**NOTE:** Transactions containing voting instructions will NOT be present. - -## Get Started - - -### Build the Substreams - -```bash -substreams build -``` - -### Authenticate - -To run your Substreams you will need to [authenticate](https://substreams.streamingfast.io/documentation/consume/authentication) yourself. - -```bash -substreams auth -``` - -### Run your Substreams - -```bash -substreams gui -``` - -## Understand the Generated Project - -Only a `substreams.yaml` file has been generated. This file declares a Substreams module, `map_filtered_transactions`, which uses a Solana Foundational Module (a module built by the team). - -```yaml -specVersion: v0.1.0 -package: - name: my_project_sol - version: v0.1.0 - -imports: - solana: https://spkg.io/streamingfast/solana-common-v0.2.0.spkg // 1. - -modules: - - name: map_filtered_transactions // 2. - use: solana:filtered_transactions_without_votes // 3. - -network: solana - -params: - map_filtered_transactions: {{ .ProgramId }} // 4. -``` -1. Import the Solana Foundational Modules. -2. Declare the `map_filtered_transactions` module, which you will run later. -3. Use the `filtered_transactions_without_votes` module from the Solana Foundational Modules. -Essentially, you are _using_ the Solana Foundational Module, which is pre-built for you. -4. Pass the regular expression to filter the transactions based on the specified Program IDs. diff --git a/vara-extrinsics/templates/substreams.yaml.gotmpl b/vara-extrinsics/templates/substreams.yaml.gotmpl index 6a37d94..2c3229d 100644 --- a/vara-extrinsics/templates/substreams.yaml.gotmpl +++ b/vara-extrinsics/templates/substreams.yaml.gotmpl @@ -1,16 +1,16 @@ specVersion: v0.1.0 package: - name: my_project_sol + name: {{ .Name }} version: v0.1.0 imports: - solana: https://spkg.io/streamingfast/solana-common-v0.2.0.spkg + vara: https://github.com/streamingfast/substreams-foundational-modules/releases/download/vara-foundational-v0.1.4/foundational-modules-vara-common-v0.1.4.spkg modules: - - name: map_filtered_transactions - use: solana:filtered_transactions_without_votes - -network: solana + - name: map_filtered_extrinsics + use: vara:filtered_extrinsics params: - map_filtered_transactions: {{ .ProgramId }} + map_filtered_extrinsics: {{ .ExtrinsicId }} + +network: {{ .ChainNetwork }} diff --git a/vara-extrinsics/types.go b/vara-extrinsics/types.go index f37616d..8ef1dff 100644 --- a/vara-extrinsics/types.go +++ b/vara-extrinsics/types.go @@ -1,4 +1,4 @@ -package soltransactions +package varaextrinsics import pbconvo "github.com/streamingfast/substreams-codegen/pb/sf/codegen/conversation/v1"