Creating a mosaic

After creating a namespace, follow this guide to create a mosaic .

Background

Mosaics can be used to represent any asset in the blockchain such as objects, tickets, coupons, stock share representation, and even your cryptocurrency.

A mosaic is like a file hosted on a domain and it represents an asset. Like a website and directory, a mosaic can have the same name as other files on other domains. However, a namespace + mosaic is always unique.

Prerequisites

Let’s get into some code

The first step is to choose a name for your mosaic. The name of the mosaic, up to a size limit of 64 characters, must be unique under the domain name.

Our mosaic will be called token, and its parent namespace will be foo.

const transactionHttp = new TransactionHttp('http://localhost:3000');

const privateKey = process.env.PRIVATE_KEY as string;
const account = Account.createFromPrivateKey(privateKey, NetworkType.MIJIN_TEST);

// Replace with namespace name and mosaic name
const namespaceName = 'foo';
const mosaicName = 'token';
        // Replace with private key
        final String privateKey = "";

        final Account account = Account.createFromPrivateKey(privateKey, NetworkType.MIJIN_TEST);

        // Replace with namespace name and mosaic name
        final String namespaceName = "foo";
        final String mosaicName = "token";
const transactionHttp = new TransactionHttp('http://localhost:3000');

const privateKey = process.env.PRIVATE_KEY;
const account = Account.createFromPrivateKey(privateKey, NetworkType.MIJIN_TEST);

// Replace with namespace name and mosaic name
const namespaceName = 'foo';
const mosaicName = 'token';

It is necessary to announce two transactions when creating a mosaic:

  1. A mosaic definition transaction, to create the mosaic.

Under mosaic properties, we define a mosaic with supplyMutable, transferable among accounts other than the creator and registered for 1000 blocks. foo:token won’t be divisible.

const mosaicDefinitionTransaction = MosaicDefinitionTransaction.create(
    Deadline.create(),
    mosaicName,
    namespaceName,
    MosaicProperties.create({
        supplyMutable: true,
        transferable: true,
        levyMutable: false,
        divisibility: 0,
        duration: UInt64.fromUint(1000)
    }),
    NetworkType.MIJIN_TEST);
        MosaicDefinitionTransaction mosaicDefinitionTransaction = MosaicDefinitionTransaction.create(
                new Deadline(2, ChronoUnit.HOURS),
                mosaicName,
                namespaceName,
                new MosaicProperties(true, true, false, 0, BigInteger.valueOf(1000)),
                NetworkType.MIJIN_TEST
        );
const mosaicDefinitionTransaction = MosaicDefinitionTransaction.create(
    Deadline.create(),
    mosaicName,
    namespaceName,
    MosaicProperties.create({
        supplyMutable: true,
        transferable: true,
        levyMutable: false,
        divisibility: 0,
        duration: UInt64.fromUint(1000)
    }),
    NetworkType.MIJIN_TEST);
  1. A mosaic supply change transaction, to set the supply. foo:token initial supply is 1.000.000

Note

Once you announce a MosaicSupplyChangeTransaction, you cannot change mosaic properties for this mosaic.

const mosaicSupplyChangeTransaction = MosaicSupplyChangeTransaction.create(
    Deadline.create(),
    mosaicDefinitionTransaction.mosaicId,
    MosaicSupplyType.Increase,
    UInt64.fromUint(1000000),
    NetworkType.MIJIN_TEST);
        MosaicSupplyChangeTransaction mosaicSupplyChangeTransaction = MosaicSupplyChangeTransaction.create(
                new Deadline(2, ChronoUnit.HOURS),
                mosaicDefinitionTransaction.getMosaicId(),
                MosaicSupplyType.INCREASE,
                BigInteger.valueOf(1000000),
                NetworkType.MIJIN_TEST
        );
const mosaicSupplyChangeTransaction = MosaicSupplyChangeTransaction.create(
    Deadline.create(),
    mosaicDefinitionTransaction.mosaicId,
    MosaicSupplyType.Increase,
    UInt64.fromUint(1000000),
    NetworkType.MIJIN_TEST);
  1. Both transactions can be announced together using an aggregate transaction.
const aggregateTransaction = AggregateTransaction.createComplete(
    Deadline.create(),
    [
        mosaicDefinitionTransaction.toAggregate(account.publicAccount),
        mosaicSupplyChangeTransaction.toAggregate(account.publicAccount)
    ],
    NetworkType.MIJIN_TEST,
    []);

const signedTransaction = account.sign(aggregateTransaction);

transactionHttp
    .announce(signedTransaction)
    .subscribe(x=> console.log(x),err => console.error(err));
        AggregateTransaction aggregateTransaction = AggregateTransaction.createComplete(
                new Deadline(2, ChronoUnit.HOURS),
                Arrays.asList(
                        mosaicDefinitionTransaction.toAggregate(account.getPublicAccount()),
                        mosaicSupplyChangeTransaction.toAggregate(account.getPublicAccount())
                ),
                NetworkType.MIJIN_TEST
        );

        final SignedTransaction signedTransaction = account.sign(aggregateTransaction);

        final TransactionHttp transactionHttp = new TransactionHttp("http://localhost:3000");

        transactionHttp.announce(signedTransaction).toFuture().get();
const aggregateTransaction = AggregateTransaction.createComplete(
    Deadline.create(),
    [
        mosaicDefinitionTransaction.toAggregate(account.publicAccount),
        mosaicSupplyChangeTransaction.toAggregate(account.publicAccount)
    ],
    NetworkType.MIJIN_TEST,
    []);

const signedTransaction = account.sign(aggregateTransaction);

transactionHttp
    .announce(signedTransaction)
    .subscribe(x=> console.log(x),err => console.error(err));
nem2-cli transaction mosaic --mosaicname token --namespacename foo --amount 1000000 --transferable --supplymutable --divisibility 0 --duration 1000

What’s next?

Transfer one mosaic created to another account or modify its properties following the next guide.