Sending multiple transactions together

Send transactions to different accounts at the same time, using aggregate transactions.

Background

Dan wants to send mosaics to Alice and Bob. He could achieve this sending a couple of TransferTransactions.

However, to make sure Alice and Bob receive the funds at the same time, we are going to define an AggregateTransaction.

../../_images/aggregate-sending-payouts1.png

Sending transactions to different recipients atomically

Getting into some code

  1. Open a new file, and define two transfer transactions to send 10 cat.currency to different recipients.
const transactionHttp = new TransactionHttp('http://localhost:3000');

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

const aliceAddress =  process.env.ALICE_ADDRESS as string;
const aliceAccount = Address.createFromRawAddress(aliceAddress);

const bobAddress = process.env.BOB_ADDRESS as string;
const bobAccount = Address.createFromRawAddress(bobAddress);

const amount = NetworkCurrencyMosaic.createRelative(10);

const aliceTransferTransaction = TransferTransaction.create(Deadline.create(), aliceAccount, [amount], PlainMessage.create('payout'), NetworkType.MIJIN_TEST);
const bobTransferTransaction = TransferTransaction.create(Deadline.create(), bobAccount, [amount], PlainMessage.create('payout'), NetworkType.MIJIN_TEST);
const transactionHttp = new TransactionHttp('http://localhost:3000');

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

const aliceAddress =  process.env.ALICE_ADDRESS;
const aliceAccount = Address.createFromRawAddress(aliceAddress);

const bobAddress = process.env.BOB_ADDRESS;
const bobAccount = Address.createFromRawAddress(bobAddress);

const amount = NetworkCurrencyMosaic.createRelative(10);

const aliceTransferTransaction = TransferTransaction.create(Deadline.create(), aliceAccount, [amount], PlainMessage.create('payout'), NetworkType.MIJIN_TEST);
const bobTransferTransaction = TransferTransaction.create(Deadline.create(), bobAccount, [amount], PlainMessage.create('payout'), NetworkType.MIJIN_TEST);
  1. Wrap both transactions in an AggregateTransaction, adding Dan’s public account as the required signer. As one private key—Dan’s account—can sign all the transactions in the aggregate, we can define the transaction as complete.
const aggregateTransaction = AggregateTransaction.createComplete(
    Deadline.create(),
    [aliceTransferTransaction.toAggregate(account.publicAccount),
        bobTransferTransaction.toAggregate(account.publicAccount)],
    NetworkType.MIJIN_TEST,
    []
);
const aggregateTransaction = AggregateTransaction.createComplete(
    Deadline.create(),
    [aliceTransferTransaction.toAggregate(account.publicAccount),
            bobTransferTransaction.toAggregate(account.publicAccount)],
    NetworkType.MIJIN_TEST,
    []
);
  1. Sign and announce the transaction with Dan’s account.
const networkGenerationHash = process.env.NETWORK_GENERATION_HASH as string;
const signedTransaction = account.sign(aggregateTransaction, networkGenerationHash);

transactionHttp
    .announce(signedTransaction)
    .subscribe(x => console.log(x), err => console.error(err));
const networkGenerationHash = process.env.NETWORK_GENERATION_HASH;
const signedTransaction = account.sign(aggregateTransaction, networkGenerationHash);

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

What’s next?

Send an AggregateBondedTransaction following creating an escrow contract guide.