Skip to content

Commit

Permalink
implement EVMBridgedMetadata in ExampleNFT
Browse files Browse the repository at this point in the history
  • Loading branch information
sisyphusSmiling committed Apr 26, 2024
1 parent 694a05c commit 5bb1075
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 10 deletions.
43 changes: 41 additions & 2 deletions contracts/ExampleNFT.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ pub contract ExampleNFT: NonFungibleToken, ViewResolver {
Type<MetadataViews.NFTCollectionData>(),
Type<MetadataViews.NFTCollectionDisplay>(),
Type<MetadataViews.Serial>(),
Type<MetadataViews.Traits>()
Type<MetadataViews.Traits>(),
Type<MetadataViews.EVMBridgedMetadata>()
]
}

Expand Down Expand Up @@ -159,6 +160,29 @@ pub contract ExampleNFT: NonFungibleToken, ViewResolver {
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.resolveView(
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.name,
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 @@ -365,6 +389,20 @@ pub contract ExampleNFT: NonFungibleToken, ViewResolver {
"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 All @@ -377,7 +415,8 @@ pub contract ExampleNFT: NonFungibleToken, ViewResolver {
pub fun getViews(): [Type] {
return [
Type<MetadataViews.NFTCollectionData>(),
Type<MetadataViews.NFTCollectionDisplay>()
Type<MetadataViews.NFTCollectionDisplay>(),
Type<MetadataViews.EVMBridgedMetadata>()
]
}

Expand Down
9 changes: 9 additions & 0 deletions contracts/MetadataViews.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -803,4 +803,13 @@ pub contract MetadataViews {
}
}

pub fun getEVMBridgedMetadata(_ 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.

9 changes: 9 additions & 0 deletions lib/go/test/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,15 @@ func TestGetNFTMetadata(t *testing.T) {
max, _ := cadence.NewUFix64("100.0")
assert.Equal(t, max, fooRarityMax)
assert.Equal(t, fooRarity.Fields[2], cadence.NewOptional(rarityDescription))

const (
bridgedName = "ExampleNFT"
symbol = "XMPL"
tokenURI = "https://example-nft.onflow.org/token-metadata/0"
)
assert.Equal(t, cadence.String(bridgedName), nftResult.Fields[24])
assert.Equal(t, cadence.String(symbol), nftResult.Fields[25])
assert.Equal(t, cadence.String(tokenURI), nftResult.Fields[26])
})
}

Expand Down
21 changes: 19 additions & 2 deletions tests/scripts/get_nft_metadata.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ pub struct NFT {
pub let traits: MetadataViews.Traits
pub let medias: MetadataViews.Medias?
pub let license: MetadataViews.License?
pub let bridgedName: String
pub let symbol: String
pub let tokenURI: String

init(
name: String,
Expand All @@ -54,7 +57,10 @@ pub struct NFT {
edition: MetadataViews.Edition,
traits: MetadataViews.Traits,
medias: MetadataViews.Medias?,
license: MetadataViews.License?
license: MetadataViews.License?,
bridgedName: String,
symbol: String,
tokenURI: String
) {
self.name = name
self.description = description
Expand All @@ -80,6 +86,9 @@ pub struct NFT {
self.traits = traits
self.medias = medias
self.license = license
self.bridgedName = bridgedName
self.symbol = symbol
self.tokenURI = tokenURI
}
}

Expand Down Expand Up @@ -120,6 +129,8 @@ pub fun main(address: Address, id: UInt64): Bool {
let medias = MetadataViews.getMedias(nft)
let license = MetadataViews.getLicense(nft)

let bridgedMetadata = MetadataViews.getEVMBridgedMetadata(nft)!

let nftMetadata = NFT(
name: display.name,
description: display.description,
Expand All @@ -144,7 +155,10 @@ pub fun main(address: Address, id: UInt64): Bool {
edition: nftEditionView.infoList[0],
traits: traits,
medias: medias,
license: license
license: license,
bridgedName: bridgedMetadata.name,
symbol: bridgedMetadata.symbol,
tokenURI: bridgedMetadata.uri.uri()
)

assert("NFT Name" == nftMetadata.name)
Expand Down Expand Up @@ -177,6 +191,9 @@ pub fun main(address: Address, id: UInt64): Bool {
assert(100.0 == nftMetadata.traits.traits[3]!.rarity!.max)
assert(nil == nftMetadata.medias)
assert(nil == nftMetadata.license)
assert("ExampleNFT" == nftMetadata.bridgedName)
assert("XMPL" == nftMetadata.symbol)
assert("https://example-nft.onflow.org/token-metadata/".concat(id.toString()).concat(".json") == nftMetadata.tokenURI)

let coll <- nftCollectionView.createEmptyCollection()
assert(0 == coll.getIDs().length)
Expand Down

0 comments on commit 5bb1075

Please sign in to comment.