-
Notifications
You must be signed in to change notification settings - Fork 179
/
dsl.go
102 lines (78 loc) · 1.89 KB
/
dsl.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
package dsl
import (
"encoding/hex"
"fmt"
"strings"
sdk "github.com/onflow/flow-go-sdk"
)
type CadenceCode interface {
ToCadence() string
}
type Transaction struct {
Import Import
Content CadenceCode
}
func (t Transaction) ToCadence() string {
return fmt.Sprintf("%s \n transaction { %s }", t.Import.ToCadence(), t.Content.ToCadence())
}
type Prepare struct {
Content CadenceCode
}
func (p Prepare) ToCadence() string {
return fmt.Sprintf("prepare(signer: AuthAccount) { %s }", p.Content.ToCadence())
}
type Contract struct {
Name string
Members []CadenceCode
}
func (c Contract) ToCadence() string {
memberStrings := make([]string, len(c.Members))
for i, member := range c.Members {
memberStrings[i] = member.ToCadence()
}
return fmt.Sprintf("access(all) contract %s { %s }", c.Name, strings.Join(memberStrings, "\n"))
}
type Resource struct {
Name string
Code string
}
func (r Resource) ToCadence() string {
return fmt.Sprintf("access(all) resource %s { %s }", r.Name, r.Code)
}
type Import struct {
Names []string
Address sdk.Address
}
func (i Import) ToCadence() string {
if i.Address != sdk.EmptyAddress {
if len(i.Names) > 0 {
return fmt.Sprintf("import %s from 0x%s\n", strings.Join(i.Names, ", "), i.Address)
}
return fmt.Sprintf("import 0x%s\n", i.Address)
}
return ""
}
type UpdateAccountCode struct {
Code string
Name string
}
func (u UpdateAccountCode) ToCadence() string {
bytes := []byte(u.Code)
hexCode := hex.EncodeToString(bytes)
return fmt.Sprintf(`
let code = "%s"
signer.contracts.add(name: "%s", code: code.decodeHex())
`, hexCode, u.Name)
}
type Main struct {
Import Import
ReturnType string
Code string
}
func (m Main) ToCadence() string {
return fmt.Sprintf("%s \npub fun main(): %s { %s }", m.Import.ToCadence(), m.ReturnType, m.Code)
}
type Code string
func (c Code) ToCadence() string {
return string(c)
}