NFT Collections
Connect to your NFT Collection program by providing the on-chain address of the program:
- React
- Javascript
import { useProgram } from "@thirdweb-dev/react/solana"
export default function Component() {
const { program } = useProgram("{{program_address}}", "nft-collection")
...
}
import { ThirdwebSDK } from "@thirdweb-dev/sdk/solana";
const sdk = ThirdwebSDK.fromNetwork("devnet");
sdk.wallet.connect(signer);
// Get the interface for your NFT collection program
const program = await sdk.getProgram("{{program_address}}", "nft-collection");
Get an NFTs Metadata
- React
- Javascript
// Specify the mint address of the NFT to get the data of
const nftAddress = "...";
// And get the data for the NFT
const nft = await program.get(nftAddress);
console.log(nft.metadata.name);
console.log(nft.owner);
// Specify the mint address of the NFT to get the data of
const nftAddress = "...";
// And get the data for the NFT
const nft = await program.get(nftAddress);
console.log(nft.metadata.name);
console.log(nft.owner);
Get All NFTs
- React
- Javascript
import { useProgram, useNFTs } from "@thirdweb-dev/react/solana";
export default function Component() {
const { program } = useProgram("{{program_address}}");
const { data: metadata, isLoading } = useNFTs(program);
return (
<pre>{JSON.stringify(metadata)}</pre>
)
}
// Get all the NFTs that have been minted on this contract
const nfts = await program.getAll();
console.log(nfts[0].metadata.name);
console.log(nfts[0].owner);
Get NFT Balance
- React
- Javascript
// Specify the address of the wallet to get the balance of
const walletAddress = "..."
// Specify the mint address of the NFT to get the balance of
const nftAddress = "..."
const balance = await program.balanceOf(walletAddress, nftAddress);
// Specify the address of the wallet to get the balance of
const walletAddress = "..."
// Specify the mint address of the NFT to get the balance of
const nftAddress = "..."
const balance = await program.balanceOf(walletAddress, nftAddress);
Transfer NFTs
- React
- Javascript
import { useProgram, useTransferNFT } from "@thirdweb-dev/react/solana";
export default function Component() {
const { program } = useProgram("{{program_address}}");
const { mutateAsync: transfer, isLoading, error } = useTransferNFT(program);
return (
<button
onClick={() => transfer({
receiverAddress: "{{wallet_address}}",
tokenAddress: "..."
})}
>
Transfer
</button>
)
}
// The wallet address to transfer the NFTs to
const to = "...";
// The mint address of the NFT to transfer
const nftAddress = "...";
const tx = await program.transfer(to, nftAddress);
Mint an NFT
- React
- Javascript
import { useProgram, useMintNFT } from "@thirdweb-dev/react/solana";
export default function Component() {
const { program } = useProgram("{{program_address}}");
const { mutateAsync: mintNFT, isLoading, error } = useMintNFT(program);
return (
<button onClick={() => mintNFT({ metadata: { name: "First NFT" } })}>
Mint
</button>
)
}
// Specify who to mint the NFT to
const to = "...";
// Add the metadata of your NFT
const metadata = {
name: "NFT #1",
description: "My first NFT!",
image: readFileSync("files/image.jpg"),
properties: [
{
name: "coolness",
value: "very cool!"
}
]
}
// Then mint the new NFT and get its address
const address = await program.mintTo(to, metadata);
console.log(address);
Mint NFT Supply
- React
- Javascript
// Specify who to mint the additional NFT to
const to = "..."
// The address of the already minted NFT
const nftAddress = "..."
* // The amount of additional NFTs to mint
const amount = 1;
// Mint an additional NFT of the original NFT
const addresses = await program.mintAdditionalSupplyTo(to, nftAddress, amount);
// Specify who to mint the additional NFT to
const to = "..."
// The address of the already minted NFT
const nftAddress = "..."
* // The amount of additional NFTs to mint
const amount = 1;
// Mint an additional NFT of the original NFT
const addresses = await program.mintAdditionalSupplyTo(to, nftAddress, amount);
Burn an NFT
- React
- Javascript
// Specify the address of the NFT to burn
const nftAddress = "..."
// And send the actual burn transaction
const tx = await program.burn(nftAddress);
// Specify the address of the NFT to burn
const nftAddress = "..."
// And send the actual burn transaction
const tx = await program.burn(nftAddress);