Sending a transfer transaction

Transfer mosaics and messages between two accounts.

Prerequisites

Background

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

Sending a transfer Transaction

Alice wants to send 10 XEM to Bob, whose address is SD5DT3-CH4BLA-BL5HIM-EKP2TA-PUKF4N-Y3L5HR-IR54.

Monitoring the transaction

Once an account announces a transaction, the server will always return an OK response. Receiving an OK response does not mean the transaction is valid. A good practice is to monitor transactions before being announced.

To understand the transaction lifecycle, we recommend you to open three new terminals. The first terminal monitors announced transactions validation errors.

$> nem2-cli monitor status

Monitoring unconfirmed shows you which transactions have reached the network, but not are not included in a block yet.

$> nem2-cli monitor unconfirmed

Once a transaction is included, you will see it under the confirmed terminal.

$> nem2-cli monitor confirmed

Let’s get into some code

Alice wants sends 10 XEM to Bob. She wants to include a message, for example Welcome to NEM.

  1. Create the transfer transaction, by including Bob address as the recipient, adding 10 XEM and the message requested.
const recipientAddress = Address.createFromRawAddress('SD5DT3-CH4BLA-BL5HIM-EKP2TA-PUKF4N-Y3L5HR-IR54');

const transferTransaction = TransferTransaction.create(
    Deadline.create(),
    recipientAddress,
    [XEM.createRelative(10)],
    PlainMessage.create('Welcome To NEM'),
    NetworkType.MIJIN_TEST);
        final String recipientAddress = "SD5DT3-CH4BLA-BL5HIM-EKP2TA-PUKF4N-Y3L5HR-IR54";

        final TransferTransaction transferTransaction = TransferTransaction.create(
            Deadline.create(2, HOURS),
            Address.createFromRawAddress(recipientAddress),
            Collections.singletonList(XEM.createRelative(BigInteger.valueOf(10))),
            PlainMessage.create("Welcome To NEM"),
            NetworkType.MIJIN_TEST
        );
const recipientAddress = Address.createFromRawAddress('SD5DT3-CH4BLA-BL5HIM-EKP2TA-PUKF4N-Y3L5HR-IR54');

const transferTransaction = TransferTransaction.create(
    Deadline.create(),
    recipientAddress,
    [XEM.createRelative(10)],
    PlainMessage.create('Welcome To NEM'),
    NetworkType.MIJIN_TEST);

Although the transaction is created, it has not been announced to the network yet. Alice must sign the transaction with her account first so that the network can verify its authenticity.

  1. Sign the transaction with Alice account.
const privateKey = process.env.PRIVATE_KEY as string;

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

const signedTransaction = account.sign(transferTransaction);
        // Replace with private key
        final String privateKey = "";

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

        final SignedTransaction signedTransaction = account.sign(transferTransaction);
const privateKey = process.env.PRIVATE_KEY;

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

const signedTransaction = account.sign(transferTransaction);
  1. Once signed, you can announce the transaction to the network.
const transactionHttp = new TransactionHttp('http://localhost:3000');

transactionHttp
    .announce(signedTransaction)
    .subscribe(x => console.log(x), err => console.error(err));
        final TransactionHttp transactionHttp = new TransactionHttp("http://localhost:3000");

        transactionHttp.announce(signedTransaction).toFuture().get();
const transactionHttp = new TransactionHttp('http://localhost:3000');

transactionHttp
    .announce(signedTransaction)
    .subscribe(x => console.log(x), err => console.error(err));
 nem2-cli transaction transfer --recipient SD5DT3-CH4BLA-BL5HIM-EKP2TA-PUKF4N-Y3L5HR-IR54 --mosaics nem:xem::10000000 --message "Welcome to NEM"
  1. Open the terminal where you are monitoring account transactions status. It should be empty. If there is an error, you can check the error code here.

A new transaction should have appeared in the terminal where you are monitoring unconfirmed. At this point, the transaction has reached the network, but it is not clear if it will get included in a block.

If it is included in a block, the transaction gets processed, and the amount stated in the transaction gets transferred from the sender’s account to the recipient’s account.

What’s next?

Send multiple mosaics in the same transaction.

Adding multiple mosaics

../../_images/transfer-transaction-multiple-mosaics.png

Sending multiple mosaics in the same transaction

As you may have noticed, transfer transactions require an array of mosaics as a parameter, allowing to send transfer transactions with multiple mosaics at the same time.

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

    [new Mosaic( new MosaicId('alice:token'), UInt64.fromUint(10)),
        XEM.createRelative(10)],
                Arrays.asList(
                        new Mosaic(new MosaicId("alice:token"), BigInteger.valueOf(10)),
                        XEM.createRelative(BigInteger.valueOf(10))
                ),
    [new Mosaic( new MosaicId('alice:token'), UInt64.fromUint(10)),
        XEM.createRelative(10)],
nem2-cli transaction transfer --recipient SD5DT3-CH4BLA-BL5HIM-EKP2TA-PUKF4N-Y3L5HR-IR54 --mosaics alice:token::10,nem:xem::10000000 --message "sending multiple mosaics"