Skip to content

Commit af239c6

Browse files
authored
fix parseCadenceTemplate (#87)
1 parent 3f6bba9 commit af239c6

2 files changed

Lines changed: 13 additions & 59 deletions

File tree

atlas/transactions/user/buy_packs_primary_sale.cdc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import FungibleToken from 0x{{.FungibleTokenContractAddress}}
22
import NonFungibleToken from 0x{{.NonFungibleTokenContractAddress}}
33
import DapperUtilityCoin from 0x{{.DapperUtilityCoinContractAddress}}
4-
import {{.NFTProductName}}, AllDay from 0x{{.NFTContractAddress}}
4+
import PackNFT from 0x{{.NFTContractAddress}}
55
import NFTStorefront from 0x{{.NFTStorefrontV1ContractAddress}}
66

77
/// Transaction facilitates the purchase of listed NFT.
@@ -14,7 +14,7 @@ import NFTStorefront from 0x{{.NFTStorefrontV1ContractAddress}}
1414

1515
transaction() {
1616
let paymentVault: @{FungibleToken.Vault}
17-
let {{.NFTProductName}}Collection: &{NonFungibleToken.CollectionPublic}
17+
let PackNFTCollection: &{NonFungibleToken.CollectionPublic}
1818
let storefront: &NFTStorefront.Storefront
1919
let listings: [&{NFTStorefront.ListingPublic}]
2020

@@ -51,7 +51,7 @@ transaction() {
5151
self.paymentVault <- provider.withdraw(amount: salePrice)
5252

5353
// Access the buyer's NFT collection to store the purchased NFT.
54-
self.{{.NFTProductName}}Collection = buyer.capabilities.borrow<&{NonFungibleToken.CollectionPublic}>({{.NFTProductName}}.CollectionPublicPath)
54+
self.PackNFTCollection = buyer.capabilities.borrow<&{NonFungibleToken.CollectionPublic}>(PackNFT.CollectionPublicPath)
5555
?? panic("Could not borrow Storefront from provided address")
5656

5757
}
@@ -65,7 +65,7 @@ transaction() {
6565
payment: <-listingPaymentVault
6666
)
6767

68-
self.{{.NFTProductName}}Collection.deposit(token: <-item)
68+
self.PackNFTCollection.deposit(token: <-item)
6969
}
7070
destroy self.paymentVault
7171
}

utils/parse_cadence_template.go

Lines changed: 9 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,22 @@
11
package utils
22

33
import (
4-
"fmt"
5-
"reflect"
6-
"regexp"
4+
"bytes"
5+
"text/template"
76
)
87

9-
func getQuotedAddressRegexExpressionReplacer(contractName string) string {
10-
const regexPattern = `"(?:(?:\./|(?:\.\./)+)?(?:[a-zA-Z0-9_\-]+/)*%s(\.cdc)?)"`
11-
return fmt.Sprintf(regexPattern, contractName)
12-
}
13-
14-
func getRegexExpressionReplacer(contractName string) string {
15-
const regexPattern = `%s`
16-
return fmt.Sprintf(regexPattern, contractName)
17-
}
18-
198
// ParseCadenceTemplate parses the Cadence template and replaces placeholders
20-
func ParseCadenceTemplate(template []byte, data ...interface{}) ([]byte, error) {
21-
if err := validateStruct(data); err != nil {
22-
return nil, err
23-
}
24-
updatedTemplate, err := replacePlaceholders(string(template), data)
9+
func ParseCadenceTemplate(tmp []byte, data interface{}) ([]byte, error) {
10+
tmpl, err := template.New("Template").Parse(string(tmp))
2511
if err != nil {
2612
return nil, err
2713
}
2814

29-
return []byte(updatedTemplate), nil
30-
}
31-
32-
// validateStruct ensures the provided data is a struct
33-
func validateStruct(data []interface{}) error {
34-
for _, item := range data {
35-
if item == nil {
36-
continue
37-
}
38-
if reflect.ValueOf(item).Kind() != reflect.Struct {
39-
return fmt.Errorf("data must be a struct")
40-
}
41-
}
42-
return nil
43-
}
44-
45-
// replacePlaceholders replaces the placeholders in the template with actual values
46-
func replacePlaceholders(template string, data []interface{}) (string, error) {
47-
for index, item := range data {
48-
if item == nil {
49-
continue
50-
}
51-
val := reflect.ValueOf(item)
52-
for i := 0; i < val.NumField(); i++ {
53-
field := val.Type().Field(i)
54-
fieldName := field.Name
55-
fieldValue := val.Field(i).String()
56-
if index == 0 {
57-
replacer := regexp.MustCompile(getQuotedAddressRegexExpressionReplacer(fieldName))
58-
template = replacer.ReplaceAllString(template, "0x"+fieldValue)
59-
} else {
60-
replacer := regexp.MustCompile(getRegexExpressionReplacer(fieldName))
61-
template = replacer.ReplaceAllString(template, fieldValue)
62-
}
63-
}
64-
15+
buf := &bytes.Buffer{}
16+
err = tmpl.Execute(buf, data)
17+
if err != nil {
18+
return nil, err
6519
}
6620

67-
return template, nil
21+
return buf.Bytes(), nil
6822
}

0 commit comments

Comments
 (0)