To mint a new Carbon NFT, you first need to obtain both the Carbon token ID and the Carbon token series ID. These identifiers are returned from the token creation transaction and series creation transaction, respectively.
Minting a New Carbon NFT
The following code snippet demonstrates how to mint a new Carbon NFT.
Imports
These are the imports typically required for minting a Carbon NFT:
In the following examples, we will use the deployer’s PhantasmaKeys keypair and its Bytes32 public key.
Build NFT ROM
NftRomBuilder helps construct and serialize the NFT ROM.
Set NFT Minting Fees
MintNftFeeOptions is used to specify the fees for NFT minting. You can call the constructor without arguments to use default values.
Build and Sign the Transaction
MintNonFungibleTxHelper simplifies the process of building and signing the NFT minting transaction.
Broadcast the Transaction
Broadcast the transaction to the network.
Parse the Result
After the transaction is mined and its result becomes available, parse it to obtain the new Carbon NFT address. This address can later be used for infusion.
The method MintNonFungibleTxHelper.parseResult() returns an array of minted NFT addresses - in this case, it will contain a single element.
// Initialize PhantasmaKeys using WIF-encoded private key
const txSender = PhantasmaKeys.fromWIF("YOUR_WIF");
// Get the public key from the keypair
const senderPubKey = new Bytes32(txSender.PublicKey);
const tokenSchemas = TokenSchemasBuilder.prepareStandard(true);
const romSchema = tokenSchemas.rom; // use the schema from your token
// Include only fields defined in romSchema (plus optional "rom" bytes).
const metadata: MetadataField[] = [
{ name: "name", value: "NFT #1" }, // NFT name
{ name: "description", value: "Example mint" }, // NFT description
{ name: "imageURL", value: "https://example.com/nft.png" }, // NFT image URL (or IPFS URL)
{ name: "infoURL", value: "https://example.com/nft" }, // NFT information URL
{ name: "royalties", value: 10000000 }, // NFT royalties. 10000000 = 1%
];
// All ROM fields defined in the schema are required; missing or wrong-case names will throw.
// First we need to generate a new random Phantasma ID for the new NFT
const newPhantasmaNftId = await getRandomPhantasmaId();
const rom = NftRomBuilder.buildAndSerialize(
romSchema,
newPhantasmaNftId,
metadata,
);
// Optional Smart NFT ROM compatible with Phantasma VM and NFT's smart contract (leave empty if unsure)
const feeOptions = new MintNftFeeOptions(
10000, // Base fee
10000 // Fee multiplier
);
const carbonTokenId = 123n; // BigInt(CreateTokenTxHelper.parseResult(...))
const carbonSeriesId = 1; // CreateTokenSeriesTxHelper.parseResult(...)
const maxData = 100_000_000n;
const tx = MintNonFungibleTxHelper.buildTxAndSignHex(
carbonTokenId, // Carbon token ID to which the new NFT will be attached
carbonSeriesId, // Carbon token series ID to which the new NFT will be attached
txSender, // Keypair used to sign the transaction
senderPubKey, // Public key of new NFT owner
rom, // ROM generated by NftRomBuilder. It's mandatory
new Uint8Array(), // NFT's RAM, optional
feeOptions, // Fee options defined above
maxData, // Mint NFT max data (use this default value if unsure)
);
const rpc = new PhantasmaAPI("https://testnet.phantasma.info/rpc", undefined as any, "testnet");
// Use sendCarbonTransaction() to call Carbon methods
const txHash = await rpc.sendCarbonTransaction(tx);
// Wait for transaction confirmation...
const txInfo = await rpc.getTransaction(txHash);
if (txInfo.state === "Halt") {
const carbonNftAddresses = MintNonFungibleTxHelper.parseResult(
carbonTokenId,
txInfo.result,
);
console.log(
`Deployed NFT with phantasma ID ${newPhantasmaNftId} and carbon NFT address ${carbonNftAddresses[0]}`
);
} else {
console.log("Could not mint NFT");
}