forked from gagliardetto/anchor-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
113 lines (98 loc) · 2.72 KB
/
utils.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
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
package main
import (
"os"
"path"
"strings"
. "github.com/dave/jennifer/jen"
"github.com/gagliardetto/anchor-go/sighash"
"github.com/gagliardetto/solana-go"
. "github.com/gagliardetto/utilz"
)
func ToPackageName(s string) string {
return sighash.ToRustSnakeCase(ToCamel(s))
}
func NewGoFile(programName string, includeBoilerplace bool) *File {
file := NewFile(ToPackageName(programName))
// Set a prefix to avoid collision between variable names and packages:
file.PackagePrefix = "ag"
// Add comment to file:
// file.HeaderComment("Code generated by https://github.com/gagliardetto. DO NOT EDIT.")
if includeBoilerplace {
{
// main function:
// file.Func().Id("main").Params().Block()
}
}
return file
}
// SaveGoFile encodes to a file the provided *jen.File.
func SaveGoFile(outDir string, assetFileName string, file *File) error {
// Save Go assets:
assetFilepath := path.Join(outDir, assetFileName)
// Create file Golang file:
goFile, err := os.Create(assetFilepath)
if err != nil {
panic(err)
}
defer goFile.Close()
// Write generated Golang to file:
Infof("Saving Golang assets to %q", MustAbs(assetFilepath))
return file.Render(goFile)
}
func DoGroup(f func(*Group)) *Statement {
g := &Group{}
g.CustomFunc(Options{
Multi: false,
}, f)
s := newStatement()
*s = append(*s, g)
return s
}
func DoGroupMultiline(f func(*Group)) *Statement {
g := &Group{}
g.CustomFunc(Options{
Multi: true,
}, f)
s := newStatement()
*s = append(*s, g)
return s
}
func newStatement() *Statement {
return &Statement{}
}
var sysVars = map[string]solana.PublicKey{
"SysVarClockPubkey": solana.SysVarClockPubkey,
"SysVarEpochSchedulePubkey": solana.SysVarEpochSchedulePubkey,
"SysVarFeesPubkey": solana.SysVarFeesPubkey,
"SysVarInstructionsPubkey": solana.SysVarInstructionsPubkey,
"SysVarRecentBlockHashesPubkey": solana.SysVarRecentBlockHashesPubkey,
"SysVarRentPubkey": solana.SysVarRentPubkey,
"SysVarSlotHashesPubkey": solana.SysVarSlotHashesPubkey,
"SysVarSlotHistoryPubkey": solana.SysVarSlotHistoryPubkey,
"SysVarStakeHistoryPubkey": solana.SysVarStakeHistoryPubkey,
"SysVarRewardsPubkey": solana.SysVarRewardsPubkey,
}
func isVar(name string) bool {
return strings.HasPrefix(name, "$(") && strings.HasSuffix(name, ")")
}
func getSysVarName(variable string) string {
variable = strings.TrimPrefix(variable, "$(")
variable = strings.TrimSuffix(variable, ")")
return variable
}
func isSysVar(name string) bool {
_, ok := sysVars[name]
return ok
}
func StringIf(condition bool, s string) string {
if condition {
return s
}
return ""
}
func CodeIf(condition bool, code Code) Code {
if condition {
return code
}
return Null()
}