Skip to content

Commit

Permalink
adding vara-extrinsics code generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduard-Voiculescu committed Sep 10, 2024
1 parent d9d7109 commit 1208fec
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 74 deletions.
42 changes: 42 additions & 0 deletions vara-extrinsics/chain_configs.go
Original file line number Diff line number Diff line change
@@ -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
})
}
39 changes: 36 additions & 3 deletions vara-extrinsics/convo.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package soltransactions
package varaextrinsics

import (
"encoding/json"
Expand All @@ -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,
Expand Down Expand Up @@ -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{})
}
Expand Down Expand Up @@ -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").
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion vara-extrinsics/convo_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package soltransactions
package varaextrinsics

import (
"testing"
Expand Down
8 changes: 4 additions & 4 deletions vara-extrinsics/generate.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package soltransactions
package varaextrinsics

import (
"bytes"
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions vara-extrinsics/logging.go
Original file line number Diff line number Diff line change
@@ -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")
7 changes: 6 additions & 1 deletion vara-extrinsics/state.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package soltransactions
package varaextrinsics

import (
"strings"
Expand Down Expand Up @@ -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 }
30 changes: 30 additions & 0 deletions vara-extrinsics/templates/CONTRIBUTING.md.gotmpl
Original file line number Diff line number Diff line change
@@ -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.
25 changes: 25 additions & 0 deletions vara-extrinsics/templates/README.md
Original file line number Diff line number Diff line change
@@ -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).
55 changes: 0 additions & 55 deletions vara-extrinsics/templates/README.md.gotmpl

This file was deleted.

14 changes: 7 additions & 7 deletions vara-extrinsics/templates/substreams.yaml.gotmpl
Original file line number Diff line number Diff line change
@@ -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 }}
2 changes: 1 addition & 1 deletion vara-extrinsics/types.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package soltransactions
package varaextrinsics

import pbconvo "github.com/streamingfast/substreams-codegen/pb/sf/codegen/conversation/v1"

Expand Down

0 comments on commit 1208fec

Please sign in to comment.