Sending mosaics and messages between two accounts

Transfer mosaics and messages between two accounts.

Prerequisites

Background

../../_images/transfer-transaction1.png

Sending a TransferTransaction

Alice wants to send 10 nem.xem to Bob, whose address is TBONKW-COWBZY-ZB2I5J-D3LSDB-QVBYHB-757VN3-SKPP.

Prerequisites

Method #01: Using the SDK

  1. In a new terminal, monitor which transactions involving the ticket vendor’s address are confirmed and which of them are rejected by the network.
nem2-cli monitor all --address <alice-address>
  1. Define the TransferTransaction, including Bob address as the recipient and attaching 10 nem.xem.
// replace with recipient address
const rawAddress = 'TBONKW-COWBZY-ZB2I5J-D3LSDB-QVBYHB-757VN3-SKPP';
const recipientAddress = Address.createFromRawAddress(rawAddress);
// replace with network type
const networkType = NetworkType.TEST_NET;
// replace with nem.xem id
const networkCurrencyMosaicId = new MosaicId('75AF035421401EF0');
// replace with network currency divisibility
const networkCurrencyDivisibility = 6;

const transferTransaction = TransferTransaction.create(
    Deadline.create(),
    recipientAddress,
    [new Mosaic (networkCurrencyMosaicId,
        UInt64.fromUint(10 * Math.pow(10, networkCurrencyDivisibility)))],
    PlainMessage.create('This is a test message'),
    networkType,
    UInt64.fromUint(2000000));
// replace with recipient address
const rawAddress = 'TBONKW-COWBZY-ZB2I5J-D3LSDB-QVBYHB-757VN3-SKPP';
const recipientAddress = nem2_sdk_1.Address.createFromRawAddress(rawAddress);
// replace with network type
const networkType = nem2_sdk_1.NetworkType.TEST_NET;
// replace with nem.xem id
const networkCurrencyMosaicId = new nem2_sdk_1.MosaicId('75AF035421401EF0');
// replace with network currency divisibility
const networkCurrencyDivisibility = 6;
const transferTransaction = nem2_sdk_1.TransferTransaction.create(nem2_sdk_1.Deadline.create(), recipientAddress, [new nem2_sdk_1.Mosaic(networkCurrencyMosaicId, nem2_sdk_1.UInt64.fromUint(10 * Math.pow(10, networkCurrencyDivisibility)))], nem2_sdk_1.PlainMessage.create('This is a test message'), networkType, nem2_sdk_1.UInt64.fromUint(2000000));

As you may have noticed, transfer transactions require an array of mosaics as a parameter. This permits sending transfer transactions with multiple mosaics at the same time.

If you own more than one mosaic, you can send them together in the same transaction:

    [new Mosaic( new MosaicId('7CDF3B117A3C40CC'), UInt64.fromUint(1000)),
        new Mosaic ( new MosaicId('75AF035421401EF0'), UInt64.fromUint(10 * Math.pow(10, 6)))],
[new nem2_sdk_1.Mosaic(new nem2_sdk_1.MosaicId('7CDF3B117A3C40CC'), nem2_sdk_1.UInt64.fromUint(1000)),
    new nem2_sdk_1.Mosaic(new nem2_sdk_1.MosaicId('75AF035421401EF0'), nem2_sdk_1.UInt64.fromUint(10 * Math.pow(10, 6)))], 

Note

Catapult works with absolute amounts. To get an absolute amount, multiply the number of assets you want to send by 10divisibility. For example, if the mosaic has divisibility 2, to send 10 units (relative) you should define 1000 (absolute) instead.

  1. Sign the transaction with Alice’s account.

Note

Include the first block generation hash to make the transaction only valid for your network. Open nodeUrl + '/block/1' in a new browser tab and copy the meta.generationHash value.

// replace with sender private key
const privateKey = '1111111111111111111111111111111111111111111111111111111111111111';
const account = Account.createFromPrivateKey(privateKey, networkType);
// replace with meta.generationHash (nodeUrl + '/block/1')
const networkGenerationHash = 'CC42AAD7BD45E8C276741AB2524BC30F5529AF162AD12247EF9A98D6B54A385B';
const signedTransaction = account.sign(transferTransaction, networkGenerationHash);
// replace with sender private key
const privateKey = '1111111111111111111111111111111111111111111111111111111111111111';
const account = nem2_sdk_1.Account.createFromPrivateKey(privateKey, networkType);
// replace with meta.generationHash (nodeUrl + '/block/1')
const networkGenerationHash = 'CC42AAD7BD45E8C276741AB2524BC30F5529AF162AD12247EF9A98D6B54A385B';
const signedTransaction = account.sign(transferTransaction, networkGenerationHash);
  1. Once signed, announce the transaction to the network.
// replace with node endpoint
const nodeUrl = 'http://api-harvest-20.us-west-1.nemtech.network:3000';
const repositoryFactory = new RepositoryFactoryHttp(nodeUrl, networkType, networkGenerationHash);
const transactionHttp = repositoryFactory.createTransactionRepository();

transactionHttp
    .announce(signedTransaction)
    .subscribe((x) => console.log(x), (err) => console.error(err));
// replace with node endpoint
const nodeUrl = 'http://api-harvest-20.us-west-1.nemtech.network:3000';
const repositoryFactory = new RepositoryFactoryHttp_1.RepositoryFactoryHttp(nodeUrl, networkType, networkGenerationHash);
const transactionHttp = repositoryFactory.createTransactionRepository();
transactionHttp
    .announce(signedTransaction)
    .subscribe((x) => console.log(x), (err) => console.error(err));

5. Open the terminal where you are monitoring the transaction’s status. The transaction should appear as confirmed after ±15 seconds and the amount defined gets transferred from the sender’s account to the recipient’s account. If the terminal raises an error, check the error code meaning here.

Method #02: Using the CLI

 nem2-cli transaction transfer --recipient-address TBONKW-COWBZY-ZB2I5J-D3LSDB-QVBYHB-757VN3-SKPP --mosaics @nem.xem::10000000 --message "This is a test message" --sync