-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbase_state.go
More file actions
148 lines (120 loc) · 4.88 KB
/
base_state.go
File metadata and controls
148 lines (120 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package codegen
import (
"strings"
registry "github.com/pinax-network/graph-networks-libs/packages/golang/lib"
networks "github.com/streamingfast/firehose-networks"
)
var _ ConversationState = (*BaseConversationState)(nil)
// ConversationState implements common fields to implement the ConversationState interface
type BaseConversationState struct {
Name string `json:"name"`
// ChainName while named with `name` does contain actually the network registry chain ID.
ChainName string `json:"chainName"`
// SubstreamsSinkChoice holds the user's choice of Substreams sink.
SubstreamsSinkChoice *SubstreamsSinkChoice `json:"substreamsSinkChoice,omitempty"`
}
func (p *BaseConversationState) GetSubstreamsSinkChoice() *SubstreamsSinkChoice {
return p.SubstreamsSinkChoice
}
func (p *BaseConversationState) SetSubstreamsSinkChoice(choice SubstreamsSinkChoice) {
p.SubstreamsSinkChoice = &choice
}
// GetChainName implements codegen.ConversationState.
func (p *BaseConversationState) GetChainName() string {
return p.ChainName
}
// SetChainName implements codegen.ConversationState.
func (p *BaseConversationState) SetChainName(name string) {
p.ChainName = name
}
// SetProjectName implements ConversationState.
func (p *BaseConversationState) SetProjectName(name string) {
p.Name = name
}
// GetModuleName implements codegen.ConversationState.
func (p *BaseConversationState) GetModuleName() string {
return strings.ReplaceAll(p.Name, "-", "_")
}
// Generate implements codegen.ConversationState, generate nothing by defaults for now.
func (p *BaseConversationState) Generate() ReturnGenerate {
return ReturnGenerate{
Err: nil,
ProjectFiles: make(map[string][]byte, 0),
}
}
// GetChainNetwork is a legacy name used in templates that returns the Substreams
// value that should be put in the `network` field of the `substreams.yaml` file.
// It uses the ChainName field to lookup the network in the registry and return
// its FullName. If ChainName is empty or the config cannot be found, an empty
// string is returned.
func (p *BaseConversationState) GetChainNetwork() string {
if network := p.FindNetworkFromChainName(); network != nil {
return network.ID
}
return ""
}
// GetChainNetwork returns the chain config from the registry based on the ChainName field.
// If ChainName is empty, nil is returned right away, otherwise it attempts to find the config
// in the registry by doing `networks.GetSubstreamsRegistry().Find(p.ChainName)`.
func (p *BaseConversationState) FindNetworkFromChainName() *registry.Network {
if p.ChainName == "" {
return nil
}
return networks.GetSubstreamsRegistry().Find(p.ChainName)
}
func (p *BaseConversationState) GetChainDisplayName() string {
return p.ChainDisplayName()
}
// ChainDisplayName returns the display name of the chain from the registry based on the ChainName field.
// If ChainName is empty or the config cannot be found, an empty string is returned.
func (p *BaseConversationState) ChainDisplayName() string {
if network := p.FindNetworkFromChainName(); network != nil {
return network.FullName
}
return ""
}
// ChainExplorerLink returns the explorer link of the chain from the registry based on the ChainName field.
// If ChainName is empty or the config cannot be found, an empty string is returned.
func (p *BaseConversationState) ChainExplorerLink() string {
if network := p.FindNetworkFromChainName(); network != nil && len(network.ExplorerUrls) > 0 {
return network.ExplorerUrls[0]
}
return ""
}
// IsChainTestnet returns true if the chain is a testnet according to the registry information.
func (p *BaseConversationState) IsChainTestnet() bool {
if network := p.FindNetworkFromChainName(); network != nil {
return network.NetworkType == registry.Testnet
}
return false
}
func (c *BaseConversationState) IsValidChainInput(input string) bool {
return networks.GetSubstreamsRegistry().Find(input) != nil
}
// GetPackageURL generates a probable GitHub URL for the project based on the project name and chain.
// Returns a URL in the format: https://github.com/username/{Name}-{ChainName}
// The 'username' is a placeholder that users should update with their actual GitHub username or organization.
func (p *BaseConversationState) GetPackageURL() string {
if p.Name == "" {
return ""
}
chainSuffix := ""
if p.ChainName != "" {
chainSuffix = "-" + p.ChainName
}
return "https://github.com/username/" + p.Name + chainSuffix
}
// GetPackageDescription generates a description for the package based on the project name and chain.
// Returns a description using the Name field and ChainDisplayName() method result.
// Example: "Substreams module for my-project on Ethereum Mainnet"
func (p *BaseConversationState) GetPackageDescription() string {
if p.Name == "" {
return "Substreams module"
}
desc := "Substreams module for " + p.Name
chainDisplay := p.ChainDisplayName()
if chainDisplay != "" {
desc += " on " + chainDisplay
}
return desc
}