From d961244c3768d88b364cbef60ba74097b4cf45fd Mon Sep 17 00:00:00 2001 From: Supreme2580 Date: Mon, 4 Nov 2024 03:57:15 +0100 Subject: [PATCH] dynamic routing for nft metadata --- backend/routes/indexer/nft.go | 11 ++----- backend/routes/routes.go | 30 +++++++++++++++++++ frontend/src/tabs/nfts/NFTs.js | 7 ++++- onchain/src/nfts/canvas_nft.cairo | 4 +-- tests/integration/docker/deploy.sh | 3 +- tests/integration/local/deploy.sh | 3 +- .../integration/mainnet/deploy-canvas-nft.sh | 4 ++- .../integration/sepolia/deploy-canvas-nft.sh | 4 ++- 8 files changed, 50 insertions(+), 16 deletions(-) diff --git a/backend/routes/indexer/nft.go b/backend/routes/indexer/nft.go index 7a912899..738efe2b 100644 --- a/backend/routes/indexer/nft.go +++ b/backend/routes/indexer/nft.go @@ -173,6 +173,7 @@ func processNFTMintedEvent(event IndexerEvent) { "nfts", fmt.Sprintf("nfts/%s", roundDir), fmt.Sprintf("nfts/%s/images", roundDir), + fmt.Sprintf("nfts/%s/metadata", roundDir), } for _, dir := range dirs { @@ -185,14 +186,6 @@ func processNFTMintedEvent(event IndexerEvent) { } } - if _, err := os.Stat("nfts/meta"); os.IsNotExist(err) { - err = os.MkdirAll("nfts/meta", os.ModePerm) - if err != nil { - PrintIndexerError("processNFTMintedEvent", "Error creating nfts/meta directory", tokenIdLowHex, positionHex, widthHex, heightHex, nameHex, imageHashHex, blockNumberHex, minter) - return - } - } - // Save image to disk filename := fmt.Sprintf("nfts/%s/images/nft-%d.png", roundDir, tokenId) file, err := os.Create(filename) @@ -249,7 +242,7 @@ func processNFTMintedEvent(event IndexerEvent) { return } - metadataFilename := fmt.Sprintf("nfts/meta/nft-%d.json", tokenId) + metadataFilename := fmt.Sprintf("nfts/%s/metadata/nft-%d.json", roundDir, tokenId) err = os.WriteFile(metadataFilename, metadataFile, 0644) if err != nil { PrintIndexerError("processNFTMintedEvent", "Error writing NFT metadata file", tokenIdLowHex, positionHex, widthHex, heightHex, nameHex, imageHashHex, blockNumberHex, minter) diff --git a/backend/routes/routes.go b/backend/routes/routes.go index 1bcbfbf7..2f9dcfd2 100644 --- a/backend/routes/routes.go +++ b/backend/routes/routes.go @@ -55,6 +55,36 @@ func InitBaseRoutes() { // Serve the file http.ServeFile(w, r, filepath) }) + + http.HandleFunc(fmt.Sprintf("/round-%s/metadata/", roundNumber), func(w http.ResponseWriter, r *http.Request) { + fmt.Printf("Received request for: %s\n", r.URL.Path) + routeutils.SetupHeaders(w) + + filename := strings.TrimPrefix(r.URL.Path, fmt.Sprintf("/round-%s/metadata/", roundNumber)) + fmt.Printf("Filename: %s\n", filename) + + workDir, err := os.Getwd() + if err != nil { + fmt.Printf("Error getting working directory: %v\n", err) + http.Error(w, "Server error", http.StatusInternalServerError) + return + } + fmt.Printf("Working directory: %s\n", workDir) + + filepath := fmt.Sprintf("%s/nfts/round-%s/metadata/%s", workDir, roundNumber, filename) + fmt.Printf("Looking for file at: %s\n", filepath) + + if _, err := os.Stat(filepath); os.IsNotExist(err) { + fmt.Printf("File not found: %s\n", filepath) + http.Error(w, fmt.Sprintf("Metadata not found at %s", filepath), http.StatusNotFound) + return + } + + w.Header().Set("Content-Type", "application/json") + + fmt.Printf("Serving file: %s\n", filepath) + http.ServeFile(w, r, filepath) + }) } func InitRoutes() { diff --git a/frontend/src/tabs/nfts/NFTs.js b/frontend/src/tabs/nfts/NFTs.js index b910e42f..3f0d3852 100644 --- a/frontend/src/tabs/nfts/NFTs.js +++ b/frontend/src/tabs/nfts/NFTs.js @@ -16,6 +16,7 @@ import { PaginationView } from '../../ui/pagination.js'; const NFTsMainSection = (props) => { const roundNumber = process.env.REACT_APP_ROUND_NUMBER || '3'; const imageURL = `${nftUrl}/round-${roundNumber}/images/`; + const metadataURL = `${nftUrl}/round-${roundNumber}/metadata/`; return (
{ tokenId={nft.tokenId} position={nft.position} image={imageURL + 'nft-' + nft.tokenId + '.png'} + metadata={metadataURL + 'nft-' + nft.tokenId + '.json'} width={nft.width} height={nft.height} name={nft.name} @@ -87,7 +89,9 @@ const NFTsMainSection = (props) => { }; const NFTsExpandedSection = (props) => { - const imageURL = nftUrl + '/nft-images/'; + const roundNumber = process.env.REACT_APP_ROUND_NUMBER || '3'; + const imageURL = `${nftUrl}/round-${roundNumber}/images/`; + const metadataURL = `${nftUrl}/round-${roundNumber}/metadata/`; return (
@@ -122,6 +126,7 @@ const NFTsExpandedSection = (props) => { tokenId={nft.tokenId} position={nft.position} image={imageURL + 'nft-' + nft.tokenId + '.png'} + metadata={metadataURL + 'nft-' + nft.tokenId + '.json'} width={nft.width} height={nft.height} name={nft.name} diff --git a/onchain/src/nfts/canvas_nft.cairo b/onchain/src/nfts/canvas_nft.cairo index 2bdb1f51..c733130b 100644 --- a/onchain/src/nfts/canvas_nft.cairo +++ b/onchain/src/nfts/canvas_nft.cairo @@ -70,8 +70,8 @@ mod CanvasNFT { #[constructor] - fn constructor(ref self: ContractState, name: ByteArray, symbol: ByteArray) { - let base_uri = "https://api.art-peace.net/nft-meta/nft-"; + fn constructor(ref self: ContractState, name: ByteArray, symbol: ByteArray, round_number: felt252) { + let base_uri = format!("https://api.art-peace.net/round-{}/metadata/", round_number); self.erc721.initializer(name, symbol, base_uri); } diff --git a/tests/integration/docker/deploy.sh b/tests/integration/docker/deploy.sh index 2eab87f2..fc99d764 100755 --- a/tests/integration/docker/deploy.sh +++ b/tests/integration/docker/deploy.sh @@ -74,7 +74,8 @@ echo "Declared class \"$NFT_CLASS_NAME\" with hash $NFT_CLASS_HASH" NFT_NAME="0 318195848183955342120051 10" NFT_SYMBOL="0 4271952 3" -CALLDATA=$(echo -n $NFT_NAME $NFT_SYMBOL) +ROUND_NUMBER=3 +CALLDATA=$(echo -n $NFT_NAME $NFT_SYMBOL $ROUND_NUMBER) echo "Deploying contract \"$NFT_CLASS_NAME\"..." echo "/root/.local/bin/sncast --url $RPC_URL --accounts-file $ACCOUNT_FILE --account $ACCOUNT_NAME --wait --json deploy --class-hash $NFT_CLASS_HASH --constructor-calldata $CALLDATA" diff --git a/tests/integration/local/deploy.sh b/tests/integration/local/deploy.sh index e2fd77df..b7855341 100755 --- a/tests/integration/local/deploy.sh +++ b/tests/integration/local/deploy.sh @@ -69,7 +69,8 @@ echo "Deployed contract \"$ART_PEACE_CLASS_NAME\" with address $ART_PEACE_CONTRA NFT_NAME="0 318195848183955342120051 10" NFT_SYMBOL="0 4271952 3" -CALLDATA=$(echo -n $NFT_NAME $NFT_SYMBOL) +ROUND_NUMBER=3 +CALLDATA=$(echo -n $NFT_NAME $NFT_SYMBOL $ROUND_NUMBER) echo "Deploying contract \"$NFT_CLASS_NAME\"..." echo "sncast --url $RPC_URL --accounts-file $ACCOUNT_FILE --account $ACCOUNT_NAME --wait --json deploy --class-hash $NFT_CLASS_HASH --constructor-calldata $CALLDATA" diff --git a/tests/integration/mainnet/deploy-canvas-nft.sh b/tests/integration/mainnet/deploy-canvas-nft.sh index ff2fcf8e..2bf41e8a 100755 --- a/tests/integration/mainnet/deploy-canvas-nft.sh +++ b/tests/integration/mainnet/deploy-canvas-nft.sh @@ -77,8 +77,10 @@ CANVAS_NFT_DECLARE_OUTPUT=$(starkli declare --network mainnet --keystore $STARKN CANVAS_NFT_CONTRACT_CLASSHASH=$(echo $CANVAS_NFT_DECLARE_OUTPUT | tail -n 1 | awk '{print $NF}') echo "Contract class hash: $CANVAS_NFT_CONTRACT_CLASSHASH" +ROUND_NUMBER=3 + # Deploying the contract -CALLDATA=$(echo 0 81458137135092567582733106 11 0 4271952 3) +CALLDATA=$(echo 0 81458137135092567582733106 11 0 4271952 $ROUND_NUMBER) echo "Deploying the contract..." echo "starkli deploy --network mainnet --keystore $STARKNET_KEYSTORE --account $STARKNET_ACCOUNT --watch $CANVAS_NFT_CONTRACT_CLASSHASH $CALLDATA" starkli deploy --network mainnet --keystore $STARKNET_KEYSTORE --account $STARKNET_ACCOUNT --watch $CANVAS_NFT_CONTRACT_CLASSHASH $CALLDATA diff --git a/tests/integration/sepolia/deploy-canvas-nft.sh b/tests/integration/sepolia/deploy-canvas-nft.sh index 1d5262ef..f70febd3 100755 --- a/tests/integration/sepolia/deploy-canvas-nft.sh +++ b/tests/integration/sepolia/deploy-canvas-nft.sh @@ -77,8 +77,10 @@ CANVAS_NFT_DECLARE_OUTPUT=$(starkli declare --network sepolia --keystore $STARKN CANVAS_NFT_CONTRACT_CLASSHASH=$(echo $CANVAS_NFT_DECLARE_OUTPUT | tail -n 1 | awk '{print $NF}') echo "Contract class hash: $CANVAS_NFT_CONTRACT_CLASSHASH" +ROUND_NUMBER=3 + # Deploying the contract -CALLDATA=$(echo 0 318195848183955342120051 10 0 4271952 3) +CALLDATA=$(echo 0 318195848183955342120051 10 0 4271952 $ROUND_NUMBER) echo "Deploying the contract..." echo "starkli deploy --network sepolia --keystore $STARKNET_KEYSTORE --account $STARKNET_ACCOUNT --watch $CANVAS_NFT_CONTRACT_CLASSHASH $CALLDATA" starkli deploy --network sepolia --keystore $STARKNET_KEYSTORE --account $STARKNET_ACCOUNT --watch $CANVAS_NFT_CONTRACT_CLASSHASH $CALLDATA