-
Notifications
You must be signed in to change notification settings - Fork 20
/
generate.go
60 lines (52 loc) · 1.46 KB
/
generate.go
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
package overflow
import (
"fmt"
"path/filepath"
"strings"
)
func (o *OverflowState) GenerateStub(network, filePath string, standalone bool) (string, error) {
solution, err := o.ParseAll()
if err != nil {
return "", err
}
interactionName := strings.TrimSuffix(filepath.Base(filePath), filepath.Ext(filePath))
var interaction *OverflowDeclarationInfo
var commandName string
if strings.HasPrefix(filePath, o.TransactionBasePath) || strings.HasPrefix("./"+filePath, o.TransactionBasePath) {
interaction = solution.Transactions[interactionName]
commandName = "Tx"
} else {
interaction = solution.Scripts[interactionName]
commandName = "Script"
}
if interaction == nil {
return "", fmt.Errorf("could not find interaction of type %s with name %s", commandName, interaction)
}
lines := []string{
fmt.Sprintf(` o.%s("%s",`, commandName, interactionName),
}
if commandName == "Tx" {
lines = append(lines, " WithSigner(\"<>\"),")
}
for name, value := range interaction.Parameters {
lines = append(lines, fmt.Sprintf(" WithArg(\"%s\", <>), //%s", name, value))
}
var stub string
if len(lines) > 1 {
lines = append(lines, " )")
stub = strings.Join(lines, "\n")
} else {
stub = strings.ReplaceAll(lines[0], ",", ")")
}
if !standalone {
return stub, nil
}
return fmt.Sprintf(`package main
import (
. "github.com/bjartek/overflow/v2"
)
func main() {
o := Overflow(WithNetwork("%s"), WithPrintResults())
%s
}`, network, stub), nil
}