Skip to content

Outside imports #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/go/contracts/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.PHONY: test
test:
go test ./...

.PHONY: generate
generate:
go generate

.PHONY: check-tidy
check-tidy: generate
go mod tidy
git diff --exit-code

.PHONY: ci
ci: check-tidy test
67 changes: 67 additions & 0 deletions lib/go/contracts/contracts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package contracts

//go:generate go run github.com/kevinburke/go-bindata/go-bindata -prefix ../../.. -o internal/assets/assets.go -pkg assets -nometadata -nomemcopy ../../../contracts ../../../transactions ../../../scripts

import (
"fmt"
"regexp"

"github.com/onflow/nft-storefront/lib/go/contracts/internal/assets"
)

const (
nftStorefrontFilename = "contracts/NFTStorefront.cdc"
SetupAccountTransaction = "transactions/setup_account.cdc"
SellItemTransaction = "transactions/sell_item.cdc"
BuyItemTransaction = "transactions/buy_item.cdc"
RemoveItemTransaction = "transactions/remove_item.cdc"
CleanupItemTransaction = "transactions/cleanup_item.cdc"
GetIDsScript = "scripts/read_storefront_ids.cdc"
GetListingDetailsScript = "scripts/read_listing_details.cdc"
Comment on lines +14 to +20
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These variables don't need to be exported from the package

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh @psiemens it's actually interesting here -- so my intention was to export these names, so that you can load up the scripts later with ReadWithAddresses! Then you can do something similar to what's currently done in the test package (set up accounts, etc).

What's the idiomatic Flow way of doing that?

)

var recognizedAddresses = map[string]bool{
"FungibleToken": true,
"FlowToken": true,
"NonFungibleToken": true,
"ExampleNFT": true,
"NFTStorefront": true,
}

// replaceAddresses replaces any ../*/(importFile).cdc with the address
func replaceAddresses(code string, addressMap map[string]string) string {
for importFile, address := range addressMap {
if !recognizedAddresses[importFile] {
fmt.Printf("Did you mispell anything? Replacing '%s'...\n", importFile)
}
Comment on lines +34 to +36
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose of the recognizedAddresses map? Does it just check that. addressMap doesn't contain an address that it doesn't recognize?

IMO you could simplify the code by removing this check -- this function is only called internally and I think it would actually be safer to assign "FungibleToken" and "NonFungibleToken" to constants that the top of this file to avoid spelling mistakes.


placeholder := regexp.MustCompile(fmt.Sprintf(`"[^"\s].*/%v.cdc"`, importFile))
code = placeholder.ReplaceAllString(code, withHexPrefix(address))
}
return code
}

// ReadWithAddresses loads a .cdc file with its addresses
func ReadWithAddresses(filename string, addressMap map[string]string) []byte {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func ReadWithAddresses(filename string, addressMap map[string]string) []byte {
func readWithAddresses(filename string, addressMap map[string]string) []byte {

I don't think this function needs to be exported

code := assets.MustAssetString(filename)
return []byte(replaceAddresses(code, addressMap))
}

func NFTStorefront(ftAddress, nftAddress string) []byte {
return ReadWithAddresses(nftStorefrontFilename, map[string]string{
"FungibleToken": ftAddress,
"NonFungibleToken": nftAddress,
})
}

func withHexPrefix(address string) string {
if address == "" {
return ""
}

if address[0:2] == "0x" {
return address
}

return fmt.Sprintf("0x%s", address)
}
38 changes: 38 additions & 0 deletions lib/go/contracts/contracts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package contracts_test

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/onflow/nft-storefront/lib/go/contracts"
)

const (
addressA = "0x0A"
addressB = "0x0B"
addressStorefront = "0x0EEE"
)

func TestNFTStorefrontContract(t *testing.T) {
contract := contracts.NFTStorefront(addrA, addrB)
assert.NotNil(t, contract)
assert.Contains(t, string(contract), addrA)
assert.Contains(t, string(contract), addrB)
}

func TestRemoveItemTransaction(t *testing.T) {
contract := contracts.ReadWithAddresses(contracts.RemoveItemTransaction, map[string]string{
"NFTStorefront": addrStorefront,
})
assert.NotNil(t, contract)
assert.Contains(t, string(contract), addrStorefront)
}

func TestGetListingDetailsScript(t *testing.T) {
contract := contracts.ReadWithAddresses(contracts.GetListingDetailsScript, map[string]string{
"NFTStorefront": addrStorefront,
})
assert.NotNil(t, contract)
assert.Contains(t, string(contract), addrStorefront)
}
11 changes: 11 additions & 0 deletions lib/go/contracts/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module github.com/onflow/nft-storefront/lib/go/contracts

go 1.17

require github.com/stretchr/testify v1.7.0

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
11 changes: 11 additions & 0 deletions lib/go/contracts/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
488 changes: 488 additions & 0 deletions lib/go/contracts/internal/assets/assets.go

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions lib/go/test/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (

const (
nftStorefrontNftStorefrontPath = "../../../contracts/NFTStorefront.cdc"
nftStorefrontRootPath = "../../../transactions"
nftStorefrontSetupAccountPath = nftStorefrontRootPath + "/setup_account.cdc"
nftStorefrontSellItemPath = nftStorefrontRootPath + "/sell_item.cdc"
nftStorefrontBuyItemPath = nftStorefrontRootPath + "/buy_item.cdc"
nftStorefrontRemoveItemPath = nftStorefrontRootPath + "/remove_item.cdc"
nftStorefrontRootPath = "../../.."
nftStorefrontSetupAccountPath = nftStorefrontRootPath + "/transactions/setup_account.cdc"
nftStorefrontSellItemPath = nftStorefrontRootPath + "/transactions/sell_item.cdc"
nftStorefrontBuyItemPath = nftStorefrontRootPath + "/transactions/buy_item.cdc"
nftStorefrontRemoveItemPath = nftStorefrontRootPath + "/transactions/remove_item.cdc"
nftStorefrontGetIDsPath = nftStorefrontRootPath + "/scripts/read_storefront_ids.cdc"
nftStorefrontGetListingDetailsPath = nftStorefrontRootPath + "/scripts/read_listing_details.cdc"
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import NFTStorefront from "../../contracts/NFTStorefront.cdc"
import NFTStorefront from "../contracts/NFTStorefront.cdc"

// This script returns an array of all the nft uuids for sale through a Storefront

Expand Down