github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/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: auth(Storage, Capabilities, Contracts) &Account) { %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 SetAccountCode struct {
    72  	Code   string
    73  	Name   string
    74  	Update bool
    75  }
    76  
    77  func (u SetAccountCode) ToCadence() string {
    78  
    79  	bytes := []byte(u.Code)
    80  
    81  	hexCode := hex.EncodeToString(bytes)
    82  
    83  	if u.Update {
    84  		return fmt.Sprintf(`
    85  		let code = "%s"
    86          signer.contracts.update(name: "%s", code: code.decodeHex())
    87      `, hexCode, u.Name)
    88  	}
    89  
    90  	return fmt.Sprintf(`
    91  		let code = "%s"
    92          signer.contracts.add(name: "%s", code: code.decodeHex())
    93      `, hexCode, u.Name)
    94  }
    95  
    96  type Main struct {
    97  	Import     Import
    98  	ReturnType string
    99  	Code       string
   100  }
   101  
   102  func (m Main) ToCadence() string {
   103  	return fmt.Sprintf("%s \naccess(all) fun main(): %s { %s }", m.Import.ToCadence(), m.ReturnType, m.Code)
   104  }
   105  
   106  type Code string
   107  
   108  func (c Code) ToCadence() string {
   109  	return string(c)
   110  }