Skip to content

C++ SDK Carbon

A structured transaction carries a typed message, a Blockchain::TxMsg, that the chain executes without a VM script. Transfers, mints and burns of tokens and NFTs, and calls into the token module such as a token creation, use it. The types and builders live in the namespace phantasma::carbon, under include/Carbon/.

A message goes through four steps: build, plan the fee, sign, send. SendTransaction in Carbon/Send.h runs the last three.

Workflow Guides

TaskPage
Create a token and its seriesToken Deployment
Mint NFTsNFT Minting
Choose schemas, build metadataSchemas And Metadata
Plan the fee, sign, sendFees And Broadcasting
Call a module method, read a message backCarbon Operations

Headers

HeaderContents
Carbon/Carbon.h, Carbon/Types.hSerialization primitives, Bytes32, SmallString, intx.
Carbon/DataBlockchain.hTxMsg, the message bodies, SignedTxMsg, TxMsgSigner, ParseTx, ParseSignedTx.
Carbon/Tx.hTxLimits, NativeTxHelper, the token module builders, TxEnvelope, TokenHelper, the result parsers.
Carbon/Contracts/Token.h, Carbon/Contracts/TokenSchemas.hToken, series and NFT structures, and their builders.
Carbon/PhantasmaAddress.hFromPhantasmaAddress, from a Phantasma address to a Bytes32.
Carbon/FeePlan.h, Carbon/FeePlanSummary.h, Carbon/ChainFeePlan.h, Carbon/FeeInfusions.hFee planning.
Carbon/Preflight.h, Carbon/Send.hThe symbol check of a token creation, and the one-call send.

The Message

TxMsg fieldMeaning
typeThe message type, a TxTypes value.
expiryThe last moment the chain accepts the message, in milliseconds since 1970.
maxGasThe gas offer in KCAL atoms. A builder leaves it at 0, which marks the message as not planned; planning sets it.
maxDataThe storage deposit ceiling in SOUL atoms. Planning sets it.
gasFromThe 32-byte public key of the account that pays the gas.
payloadA text the transaction carries, a SmallString of at most 255 bytes.
The bodyOne member per type, for example transferFt, burnFungible or call.

Accounts are 32-byte public keys: carbon::Bytes32(keys.GetPublicKey()) for a key pair, carbon::FromPhantasmaAddress(address) for a parsed address.

Native Builders

NativeTxHelper builds the native types. Each builder takes the parties, the token and the amount or instances, and optional TxLimits:

BuilderParametersMessage types
TransferFungible(parties, to, tokenId, amount)amount in atoms, a uint64_tTransferFungible, TransferFungible_GasPayer
TransferNonFungible(parties, to, tokenId, instanceIds, count, out, outError)Answers false for an empty listTransferNonFungible_Single for one instance, TransferNonFungible_Multi for several, and their _GasPayer forms
MintFungible(owner, to, tokenId, amount)amount an intxMintFungible
BurnFungible(parties, tokenId, amount)amount an intxBurnFungible, BurnFungible_GasPayer
BurnNonFungible(parties, tokenId, instanceId)BurnNonFungible, BurnNonFungible_GasPayer

parties is { from }, or { from, &gasPayer } for a second account that pays the gas and signs as well. The account in from or owner signs. Any holder can burn its own tokens; only the token owner can mint. The instanceIds array of TransferNonFungible must outlive the message, which points at it.

A transfer amount is a uint64_t, also for a big-fungible token. Only an amount above that range needs a module call or a script transfer, whose amounts are big integers. Mints and burns carry an intx because they also serve big-fungible tokens.

TxLimits holds maxGas, maxData and expiry. Without expiry a builder stamps DefaultExpiryMs (45 seconds) from now; ExpiryWithin(windowMs) gives the latest expiry the chain accepts.

Example: Send NFTs With A Gas Payer

using namespace phantasma::carbon;

const Bytes32 from(ownerKeys.GetPublicKey());
const Bytes32 payer(gasPayerKeys.GetPublicKey());
const uint64_t instanceIds[] = { 1001, 1002 };

// Two NFT instances of token 77 move to the recipient. The payer pays the gas.
Blockchain::TxMsg send;
std::string error;
if( !NativeTxHelper::TransferNonFungible({ from, &payer }, recipient, 77, instanceIds, 2, send, error) )
    return; // error says why

String hash;
if( !SendTransaction(api, send, { &gasPayerKeys, &ownerKeys }, config, SendOptions{}, hash, error) )
    return;

SendTransaction plans the message against config, the chain’s gas config, signs it with the given keys and sends it. A _GasPayer message is signed by the gas payer first, then by the owner. config comes from ToGasConfig(api.GetGasConfig().gasConfig); it changes only through a governance resolution, so read it once and keep it.

Token Module Builders

BuilderCallResult parserPage
CreateTokenTxHelper::BuildTx(tokenInfo, creator, limits)Token.CreateTokenCreateTokenTxHelper::ParseResult: the token idToken Deployment
CreateTokenSeriesTxHelper::BuildTx(tokenId, seriesInfo, creator, limits)Token.CreateTokenSeriesCreateTokenSeriesTxHelper::ParseResult: the Carbon series idToken Deployment
MintPhantasmaNonFungibleTxHelper::BuildTx(tokenId, sender, receiver, tokens, out, outError, limits)Token.MintPhantasmaNonFungibleMintPhantasmaNonFungibleTxHelper::ParseResult: the NFT ids and instance idsNFT Minting
MintNonFungibleTxHelper::BuildTx(...)A direct MintNonFungibleMintNonFungibleTxHelper::ParseResult: NFT addressesRefused by the chain while caller-chosen NFT ids are disabled, as on mainnet

These builders return a TxEnvelope: the message in msg and, in buffers, the bytes the message points into. Move an envelope; do not copy it, because a copy’s message still points into the original’s buffers.

Signing

FunctionDoes
SignAndSerialize(env, keys)Signs an envelope with one key and returns the signed bytes.
Blockchain::TxMsgSigner::SignAndSerialize(msg, keys)The same for a message.
Blockchain::TxMsgSigner::SignAndSerialize(msg, signers, out, outError)Signs with several keys. It answers false when the message has no gas offer, when the count is not the one the type fixes, or when a key is not the account the message names.

The signing functions refuse a message whose maxGas is 0, a message that was never planned: the one-key forms answer an empty result, or throw with PHANTASMA_EXCEPTION_ENABLE, and the several-key form answers false. A Call, CallMulti or Trade takes its witnesses in the order given, and the gas payer must be one of them.

Base16::Encode(signedBytes) gives the hex that api.SendCarbonTransaction takes.

NFT Addresses And Instance Ids

FunctionMeaning
TokenHelper::GetNftAddress(tokenId, instanceId)The address of one NFT instance. Assets sent to it belong to the NFT.
TokenHelper::IsNftAddress(address)true for the form of an NFT address: fifteen zero bytes, a 0x01 byte, a non-zero token id and a non-zero instance id.

An instance id holds the Carbon series id in its low 32 bits and the mint number in its high 32 bits. A transfer into an NFT address needs the signature of the NFT’s owner.

Related Pages