-
Notifications
You must be signed in to change notification settings - Fork 55
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
Outside imports #24
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
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" | ||||||
) | ||||||
|
||||||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the purpose of the 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 |
||||||
|
||||||
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 { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
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) | ||||||
} |
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) | ||
} |
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 | ||
) |
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= |
Large diffs are not rendered by default.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?