Skip to content

Commit

Permalink
remove serialization in ExampleNFT
Browse files Browse the repository at this point in the history
  • Loading branch information
sisyphusSmiling authored and joshuahannan committed Feb 12, 2025
1 parent 84b0d34 commit bffcd56
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 45 deletions.
11 changes: 6 additions & 5 deletions contracts/ExampleNFT.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import "NonFungibleToken"
import "ViewResolver"
import "MetadataViews"
import "CrossVMMetadataViews"
import "SerializeMetadata"
import "EVM"

access(all) contract ExampleNFT: NonFungibleToken {
Expand Down Expand Up @@ -174,11 +173,13 @@ access(all) contract ExampleNFT: NonFungibleToken {
// bridged which can be useful for Cadence NFTs with dynamic metadata values.
// See FLIP-318 for more information about cross-VM NFTs: https://github.com/onflow/flips/issues/318
// Here we serialize the NFT's metadata and encode the string as EVM bytes, but you could pass any
// Here we encoded the EVMBridgedMetadata URI and encode the string as EVM bytes, but you could pass any
// Cadence values that can be abi encoded and decode them in your EVM contract as you wish. Within
// your EVM contract, you can abi decode the bytes and update metadata as you see fit.
let serializedAsDataURI = SerializeMetadata.serializeNFTMetadataAsURI(&self as &{NonFungibleToken.NFT})
let encodedURI = EVM.encodeABI([serializedAsDataURI])
// your EVM contract, you can abi decode the bytes and update metadata in your ERC721 contract as
// you see fit.
let bridgedMetadata = (self.resolveView(Type<MetadataViews.EVMBridgedMetadata>()) as! MetadataViews.EVMBridgedMetadata?)!
let uri = bridgedMetadata.uri.uri()
let encodedURI = EVM.encodeABI([uri])
let evmBytes = EVM.EVMBytes(value: encodedURI)
return CrossVMMetadataViews.EVMBytesMetadata(bytes: evmBytes)
}
Expand Down
22 changes: 1 addition & 21 deletions flow.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,24 +95,6 @@
"mainnet": "f233dcee88fe0abe",
"testnet": "9a0766d93b6608b7"
}
},
"Serialize": {
"source": "mainnet://1e4aa0b87d10b141.Serialize",
"hash": "50bf2599bac68e3fb0e426a262e7db2eed91b90c0a5ad57e70688cbf93282b4f",
"aliases": {
"mainnet": "1e4aa0b87d10b141",
"testing": "0000000000000007",
"testnet": "dfc20aee650fcbdf"
}
},
"SerializeMetadata": {
"source": "mainnet://1e4aa0b87d10b141.SerializeMetadata",
"hash": "7be42ac4e42fd3019ab6771f205abeb80ded5a461649a010b1a0668533909012",
"aliases": {
"mainnet": "1e4aa0b87d10b141",
"testing": "0000000000000007",
"testnet": "dfc20aee650fcbdf"
}
}
},
"networks": {
Expand Down Expand Up @@ -144,9 +126,7 @@
"MetadataViews",
"ExampleNFT",
"NFTForwarding",
"CrossVMMetadataViews",
"SerializeMetadata",
"Serialize"
"CrossVMMetadataViews"
]
},
"mainnet": {
Expand Down
13 changes: 9 additions & 4 deletions lib/go/contracts/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ var (
placeholderUniversalCollection = regexp.MustCompile(`"UniversalCollection"`)
universalCollectionImport = "UniversalCollection from "
placeholderCrossVMMetadataViews = regexp.MustCompile(`"CrossVMMetadataViews"`)
crossVMMetadataViewsImport = "CrossVMMetadataViews from "
crossVMMetadataImport = "CrossVMMetadataViews from "
placeholderEVM = regexp.MustCompile(`"EVM"`)
evmImport = "EVM from "
)

const (
Expand Down Expand Up @@ -62,12 +64,14 @@ func NonFungibleToken(resolverAddress string) []byte {
// ExampleNFT returns the ExampleNFT contract.
//
// The returned contract will import the NonFungibleToken contract from the specified address.
func ExampleNFT(nftAddress, metadataAddress, resolverAddress flow.Address) []byte {
func ExampleNFT(nftAddress, metadataAddress, resolverAddress, evmAddress, crossVMMetadataAddress flow.Address) []byte {
code := assets.MustAssetString(filenameExampleNFT)

code = placeholderNonFungibleToken.ReplaceAllString(code, nonFungibleTokenImport+withHexPrefix(nftAddress.String()))
code = placeholderMetadataViews.ReplaceAllString(code, metadataViewsImport+withHexPrefix(metadataAddress.String()))
code = placeholderResolver.ReplaceAllString(code, viewResolverImport+withHexPrefix(resolverAddress.String()))
code = placeholderEVM.ReplaceAllString(code, evmImport+withHexPrefix(evmAddress.String()))
code = placeholderCrossVMMetadataViews.ReplaceAllString(code, crossVMMetadataImport+withHexPrefix(crossVMMetadataAddress.String()))

return []byte(code)
}
Expand All @@ -87,10 +91,11 @@ func ViewResolver() []byte {
return []byte(code)
}

func CrossVMMetadataViews(evmAddress string) []byte {
func CrossVMMetadataViews(resolverAddress, evmAddress string) []byte {
code := assets.MustAssetString(filenameCrossVMMetadataViews)

code = placeholderFungibleToken.ReplaceAllString(code, fungibleTokenImport+withHexPrefix(evmAddress))
code = placeholderFungibleToken.ReplaceAllString(code, viewResolverImport+withHexPrefix(resolverAddress))
code = placeholderFungibleToken.ReplaceAllString(code, evmImport+withHexPrefix(evmAddress))

return []byte(code)
}
Expand Down
16 changes: 14 additions & 2 deletions lib/go/contracts/contracts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ func TestExampleNFTContract(t *testing.T) {
addressA := addresses.New()
addressB := addresses.New()
addressC := addresses.New()
addressD := addresses.New()
addressE := addresses.New()

contract := contracts.ExampleNFT(addressA, addressB, addressC)
contract := contracts.ExampleNFT(addressA, addressB, addressC, addressD, addressE)
assert.NotNil(t, contract)

assert.Contains(t, string(contract), addressA.String())
Expand All @@ -37,6 +39,16 @@ func TestMetadataViewsContract(t *testing.T) {
}

func TestCrossVMMetadataViewsContract(t *testing.T) {
contract := contracts.CrossVMMetadataViews(addrA)
addresses := test.AddressGenerator()
addressA := addresses.New()
addressB := addresses.New()

contract := contracts.CrossVMMetadataViews(addressA.String(), addressB.String())
assert.NotNil(t, contract)
}

/**
have ("github.com/onflow/flow-go-sdk".Address, "github.com/onflow/flow-go-sdk".Address, "github.com/onflow/flow-go-sdk".Address)
want ("github.com/onflow/flow-go-sdk".Address, "github.com/onflow/flow-go-sdk".Address, "github.com/onflow/flow-go-sdk".Address, "github.com/onflow/flow-go-sdk".Address, "github.com/onflow/flow-go-sdk".Address, "github.com/onflow/flow-go-sdk".Address
*/
12 changes: 6 additions & 6 deletions lib/go/contracts/internal/assets/assets.go

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions lib/go/templates/internal/assets/assets.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion lib/go/test/nft_test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,13 @@ func deployNFTContracts(

metadataAddress := deploy(t, b, adapter, "MetadataViews", contracts.MetadataViews(emulatorFTAddress, nftAddress.String(), resolverAddress.String()), nftAccountKey)

evmAddress := flow.HexToAddress(emulatorEVMAddress)
crossVMMetadataAddress := deploy(t, b, adapter, "CrossVMMetadataViews", contracts.CrossVMMetadataViews(resolverAddress.String(), evmAddress.String()), nftAccountKey)

exampleNFTAddress := deploy(
t, b, adapter,
"ExampleNFT",
contracts.ExampleNFT(nftAddress, metadataAddress, resolverAddress),
contracts.ExampleNFT(nftAddress, metadataAddress, resolverAddress, evmAddress, crossVMMetadataAddress),
exampleNFTAccountKey,
)

Expand Down
1 change: 1 addition & 0 deletions lib/go/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var _ = chainhash.Hash{}

const (
emulatorFTAddress = "ee82856bf20e2aa6"
emulatorEVMAddress = "f8d6e0586b0a20c7"
)

// Sets up testing and emulator objects and initialize the emulator default addresses
Expand Down
Loading

0 comments on commit bffcd56

Please sign in to comment.