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 cat.currency 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 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

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

const transferTransaction = TransferTransaction.create(
    Deadline.create(),
    recipientAddress,
    [NetworkCurrencyMosaic.createRelative(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,
    [NetworkCurrencyMosaic.createRelative(10)],
    PlainMessage.create('Welcome To NEM'),
    NetworkType.MIJIN_TEST);

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, you can send them together in the same transaction:

    [new Mosaic( new MosaicId('7cdf3b117a3c40cc'), UInt64.fromUint(10)),
        NetworkCurrencyMosaic.createRelative(10)],
    [new Mosaic( new MosaicId('7cdf3b117a3c40cc'), UInt64.fromUint(10)),
        NetworkCurrencyMosaic.createRelative(10)],
  1. Sign the transaction with Alice’s account, so that the network can verify its authenticity.
const privateKey = process.env.PRIVATE_KEY as string;

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

const 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, 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));
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 @cat.currency::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 meaning 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.