Skip to content

Commit

Permalink
dynamic routing for nft metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
supreme2580 committed Nov 4, 2024
1 parent 8df8b97 commit d961244
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 16 deletions.
11 changes: 2 additions & 9 deletions backend/routes/indexer/nft.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
30 changes: 30 additions & 0 deletions backend/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
7 changes: 6 additions & 1 deletion frontend/src/tabs/nfts/NFTs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<div
className={`${props.expanded ? 'NFTs__main__expanded' : 'NFTs__main'}`}
Expand Down Expand Up @@ -64,6 +65,7 @@ const NFTsMainSection = (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}
Expand All @@ -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 (
<div className='NFTs__all'>
Expand Down Expand Up @@ -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}
Expand Down
4 changes: 2 additions & 2 deletions onchain/src/nfts/canvas_nft.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
3 changes: 2 additions & 1 deletion tests/integration/docker/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion tests/integration/local/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion tests/integration/mainnet/deploy-canvas-nft.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 3 additions & 1 deletion tests/integration/sepolia/deploy-canvas-nft.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d961244

Please sign in to comment.