Skip to content
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

Update ExampleNFT v2 with EVMBridgedMetadata view from master #215

Merged
merged 10 commits into from
May 2, 2024
45 changes: 43 additions & 2 deletions contracts/ExampleNFT.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ access(all) contract ExampleNFT: NonFungibleToken {
Type<MetadataViews.NFTCollectionData>(),
Type<MetadataViews.NFTCollectionDisplay>(),
Type<MetadataViews.Serial>(),
Type<MetadataViews.Traits>()
Type<MetadataViews.Traits>(),
Type<MetadataViews.EVMBridgedMetadata>()
]
}

Expand Down Expand Up @@ -123,6 +124,31 @@ access(all) contract ExampleNFT: NonFungibleToken {
traitsView.addTrait(fooTrait)

return traitsView
case Type<MetadataViews.EVMBridgedMetadata>():
// Implementing this view gives the project control over how the bridged NFT is represented as an
// ERC721 when bridged to EVM on Flow via the public infrastructure bridge.

// Get the contract-level name and symbol values
let contractLevel = ExampleNFT.resolveContractView(
resourceType: nil,
viewType: Type<MetadataViews.EVMBridgedMetadata>()
) as! MetadataViews.EVMBridgedMetadata?
?? panic("Could not resolve contract-level EVMBridgedMetadata")
// Compose the token-level URI based on a base URI and the token ID, pointing to a JSON file. This
// would be a file you've uploaded and are hosting somewhere - in this case HTTP, but this could be
// IPFS, S3, a data URL containing the JSON directly, etc.
let baseURI = "https://example-nft.onflow.org/token-metadata/"
let uriValue = self.id.toString().concat(".json")

return MetadataViews.EVMBridgedMetadata(
name: contractLevel.name,
symbol: contractLevel.symbol,
uri: MetadataViews.URI(
baseURI: baseURI, // defining baseURI results in a concatenation of baseURI and value
value: self.id.toString().concat(".json")
)
)

}
return nil
}
Expand Down Expand Up @@ -225,7 +251,8 @@ access(all) contract ExampleNFT: NonFungibleToken {
access(all) view fun getContractViews(resourceType: Type?): [Type] {
return [
Type<MetadataViews.NFTCollectionData>(),
Type<MetadataViews.NFTCollectionDisplay>()
Type<MetadataViews.NFTCollectionDisplay>(),
Type<MetadataViews.EVMBridgedMetadata>()
]
}

Expand Down Expand Up @@ -264,6 +291,20 @@ access(all) contract ExampleNFT: NonFungibleToken {
"twitter": MetadataViews.ExternalURL("https://twitter.com/flow_blockchain")
}
)
case Type<MetadataViews.EVMBridgedMetadata>():
// Implementing this view gives the project control over how the bridged NFT is represented as an ERC721
// when bridged to EVM on Flow via the public infrastructure bridge.

// Compose the contract-level URI. In this case, the contract metadata is located on some HTTP host,
// but it could be IPFS, S3, a data URL containing the JSON directly, etc.
return MetadataViews.EVMBridgedMetadata(
name: "ExampleNFT",
symbol: "XMPL",
uri: MetadataViews.URI(
baseURI: nil, // setting baseURI as nil sets the given value as the uri field value
value: "https://example-nft.onflow.org/contract-metadata.json"
)
)
}
return nil
}
Expand Down
9 changes: 9 additions & 0 deletions contracts/MetadataViews.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -767,4 +767,13 @@ access(all) contract MetadataViews {
}
}

access(all) fun getEVMBridgedMetadata(_ viewResolver: &{ViewResolver.Resolver}) : EVMBridgedMetadata? {
if let view = viewResolver.resolveView(Type<EVMBridgedMetadata>()) {
if let v = view as? EVMBridgedMetadata {
return v
}
}
return nil
}

}
12 changes: 6 additions & 6 deletions lib/go/contracts/internal/assets/assets.go

Large diffs are not rendered by default.

6 changes: 3 additions & 3 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.

6 changes: 4 additions & 2 deletions tests/example_nft_test.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,8 @@ fun testGetViews() {
Type<MetadataViews.NFTCollectionData>(),
Type<MetadataViews.NFTCollectionDisplay>(),
Type<MetadataViews.Serial>(),
Type<MetadataViews.Traits>()
Type<MetadataViews.Traits>(),
Type<MetadataViews.EVMBridgedMetadata>()
]
Test.assertEqual(expectedViews, supportedViews)
}
Expand All @@ -375,7 +376,8 @@ fun testGetExampleNFTViews() {
let supportedViews = scriptResult.returnValue! as! [Type]
let expectedViews = [
Type<MetadataViews.NFTCollectionData>(),
Type<MetadataViews.NFTCollectionDisplay>()
Type<MetadataViews.NFTCollectionDisplay>(),
Type<MetadataViews.EVMBridgedMetadata>()
]
Test.assertEqual(expectedViews, supportedViews)
}
Expand Down
22 changes: 18 additions & 4 deletions transactions/scripts/get_nft_metadata.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ access(all) struct NFT {
access(all) let traits: MetadataViews.Traits
access(all) let medias: MetadataViews.Medias?
access(all) let license: MetadataViews.License?
access(all) let bridgedName: String
access(all) let symbol: String
access(all) let tokenURI: String

init(
name: String,
Expand All @@ -49,8 +52,11 @@ access(all) struct NFT {
collectionSocials: {String: String},
edition: MetadataViews.Edition,
traits: MetadataViews.Traits,
medias: MetadataViews.Medias?,
license: MetadataViews.License?
medias:MetadataViews.Medias?,
license:MetadataViews.License?,
bridgedName: String,
symbol: String,
tokenURI: String
) {
self.name = name
self.description = description
Expand All @@ -74,6 +80,9 @@ access(all) struct NFT {
self.traits = traits
self.medias = medias
self.license = license
self.bridgedName = bridgedName
self.symbol = symbol
self.tokenURI = tokenURI
}
}

Expand Down Expand Up @@ -117,6 +126,8 @@ access(all) fun main(address: Address, id: UInt64): NFT {
let medias = MetadataViews.getMedias(nft)
let license = MetadataViews.getLicense(nft)

let bridgedMetadata = MetadataViews.getEVMBridgedMetadata(nft)!

return NFT(
name: display.name,
description: display.description,
Expand All @@ -138,7 +149,10 @@ access(all) fun main(address: Address, id: UInt64): NFT {
collectionSocials: collectionSocials,
edition: nftEditionView.infoList[0],
traits: traits,
medias:medias,
license:license
medias: medias,
license: license,
bridgedName: bridgedMetadata.name,
symbol: bridgedMetadata.symbol,
tokenURI: bridgedMetadata.uri.uri()
)
}
Loading