Interacting with mosaics

Creating a mosaic

Once you own a namespace, it is the time 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 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, as the root namespace was unique even if the rest of it is not.

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.

// Replace with a private key
const privateKey = process.env.PRIVATE_KEY as string;

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

// Replace with namespace name and mosaic name
const namespace = 'foo';
const mosaic = '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";
// Replace with a private key
const privateKey = process.env.PRIVATE_KEY;

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

// Replace with namespace name and mosaic name
const namespace = 'foo';
const mosaic = '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(),
    mosaic,
    namespace,
    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(),
    mosaic,
    namespace,
    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,
);

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);

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

transactionHttp.announce(signedTransaction).subscribe(x=> console.log(x));
        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);

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

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

What’s next?

Now that you have your mosaic, try to transfer it or modify its properties following next guide.

Modifying mosaic supply

Did you register a mosaics with supplyMutable option set to true?

In that case, this guide will walk you through increasing or decreasing your mosaic available supply.

Prerequisites

  • Finish creating a mosaic guide
  • NEM2-SDK or CLI
  • A text editor or IDE
  • An account with XEM
  • Have registered a supply mutable mosaic

Let’s get into some code

If you have followed the previous guide, right now you should own a supply mutable mosaic.

Let’s increase 2.000.000 the initial supply. Just sign and announce a mosaic supply change transaction.

// Replace with a private key
const privateKey = process.env.PRIVATE_KEY as string;

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

// Replace with mosaic id
const mosaicID = new MosaicId('foo:token'); // replace with mosaic full name

const mosaicSupplyChangeTransaction = MosaicSupplyChangeTransaction.create(
    Deadline.create(),
    mosaicID,
    MosaicSupplyType.Increase,
    UInt64.fromUint(2000000),
    NetworkType.MIJIN_TEST,
);

const signedTransaction = account.sign(mosaicSupplyChangeTransaction);

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

transactionHttp.announce(signedTransaction).subscribe(x => console.log(x));
        // Replace with private key
        final String privateKey = "";

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

        // Replace with mosaic id
        final MosaicId mosaicId = new MosaicId("foo:token"); // replace with mosaic full name

        MosaicSupplyChangeTransaction mosaicSupplyChangeTransaction = MosaicSupplyChangeTransaction.create(
                new Deadline(2, ChronoUnit.HOURS),
                mosaicId,
                MosaicSupplyType.INCREASE,
                BigInteger.valueOf(2000000),
                NetworkType.MIJIN_TEST
        );

        final SignedTransaction signedTransaction = account.sign(mosaicSupplyChangeTransaction);

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

        transactionHttp.announce(signedTransaction).toFuture().get();
// Replace with a private key
const privateKey = process.env.PRIVATE_KEY;

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

// Replace with mosaic id
const mosaicID = new MosaicId('foo:token'); // replace with mosaic full name

const mosaicSupplyChangeTransaction = MosaicSupplyChangeTransaction.create(
    Deadline.create(),
    mosaicID,
    MosaicSupplyType.Increase,
    UInt64.fromUint(2000000),
    NetworkType.MIJIN_TEST,
);

const signedTransaction = account.sign(mosaicSupplyChangeTransaction);

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

transactionHttp.announce(signedTransaction).subscribe(x => console.log(x));

What’s next?

You could try to decrease your mosaic supply by changing MosaicSupplyType.Increase for MosaicSupplyType.Decrease.