github.com/koko1123/flow-go-1@v0.29.6/utils/dsl/dsl.go (about) 1 package dsl 2 3 import ( 4 "encoding/hex" 5 "fmt" 6 "strings" 7 8 sdk "github.com/onflow/flow-go-sdk" 9 ) 10 11 type CadenceCode interface { 12 ToCadence() string 13 } 14 15 type Transaction struct { 16 Import Import 17 Content CadenceCode 18 } 19 20 func (t Transaction) ToCadence() string { 21 return fmt.Sprintf("%s \n transaction { %s }", t.Import.ToCadence(), t.Content.ToCadence()) 22 } 23 24 type Prepare struct { 25 Content CadenceCode 26 } 27 28 func (p Prepare) ToCadence() string { 29 return fmt.Sprintf("prepare(signer: AuthAccount) { %s }", p.Content.ToCadence()) 30 } 31 32 type Contract struct { 33 Name string 34 Members []CadenceCode 35 } 36 37 func (c Contract) ToCadence() string { 38 39 memberStrings := make([]string, len(c.Members)) 40 for i, member := range c.Members { 41 memberStrings[i] = member.ToCadence() 42 } 43 44 return fmt.Sprintf("access(all) contract %s { %s }", c.Name, strings.Join(memberStrings, "\n")) 45 } 46 47 type Resource struct { 48 Name string 49 Code string 50 } 51 52 func (r Resource) ToCadence() string { 53 return fmt.Sprintf("access(all) resource %s { %s }", r.Name, r.Code) 54 } 55 56 type Import struct { 57 Names []string 58 Address sdk.Address 59 } 60 61 func (i Import) ToCadence() string { 62 if i.Address != sdk.EmptyAddress { 63 if len(i.Names) > 0 { 64 return fmt.Sprintf("import %s from 0x%s\n", strings.Join(i.Names, ", "), i.Address) 65 } 66 return fmt.Sprintf("import 0x%s\n", i.Address) 67 } 68 return "" 69 } 70 71 type UpdateAccountCode struct { 72 Code string 73 Name string 74 } 75 76 func (u UpdateAccountCode) ToCadence() string { 77 78 bytes := []byte(u.Code) 79 80 hexCode := hex.EncodeToString(bytes) 81 82 return fmt.Sprintf(` 83 let code = "%s" 84 signer.contracts.add(name: "%s", code: code.decodeHex()) 85 `, hexCode, u.Name) 86 } 87 88 type Main struct { 89 Import Import 90 ReturnType string 91 Code string 92 } 93 94 func (m Main) ToCadence() string { 95 return fmt.Sprintf("%s \npub fun main(): %s { %s }", m.Import.ToCadence(), m.ReturnType, m.Code) 96 } 97 98 type Code string 99 100 func (c Code) ToCadence() string { 101 return string(c) 102 }