Sending payouts with aggregate complete transaction

Send transactions to different accounts atomically, using an aggregate complete transaction.

Background

Dan wants to send mosaics to Alice and Bob.

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

Sending transactions to different recipients atomically

He chooses to send an aggregate complete transaction, so both will receive the funds at the same time.

Prerequisites

Let’s get into some code

  1. Dan creates two transfer transaction with two different recipients, and wrap them in an aggregate transaction.
const transactionHttp = new TransactionHttp('http://localhost:3000');

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

const brotherAddress = 'SDG4WG-FS7EQJ-KFQKXM-4IUCQG-PXUW5H-DJVIJB-OXJG';
const brotherAccount = Address.createFromRawAddress(brotherAddress);

const sisterAddress = 'SCGPXB-2A7T4I-W5MQCX-FQY4UQ-W5JNU5-F55HGK-HBUN';
const sisterAccount = Address.createFromRawAddress(sisterAddress);

const amount = XEM.createRelative(10); // 10 xem represent 10 000 000 micro xem

const brotherTransferTransaction = TransferTransaction.create(Deadline.create(), brotherAccount, [amount], PlainMessage.create('payout'), NetworkType.MIJIN_TEST);
const sisterTransferTransaction = TransferTransaction.create(Deadline.create(), sisterAccount, [amount], PlainMessage.create('payout'), NetworkType.MIJIN_TEST);

const aggregateTransaction = AggregateTransaction.createComplete(
    Deadline.create(),
    [brotherTransferTransaction.toAggregate(account.publicAccount),
        sisterTransferTransaction.toAggregate(account.publicAccount)],
    NetworkType.MIJIN_TEST,
    []
);
        // Replace with private key
        final String privateKey = "";

        final Address brotherAddress = Address.createFromRawAddress("SDG4WG-FS7EQJ-KFQKXM-4IUCQG-PXUW5H-DJVIJB-OXJG");
        final Address sisterAddress = Address.createFromRawAddress("SCGPXB-2A7T4I-W5MQCX-FQY4UQ-W5JNU5-F55HGK-HBUN");

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

        final XEM xem = XEM.createRelative(BigInteger.valueOf(10)); // 10 xem represent 10 000 000 micro xem

        final TransferTransaction brotherTransferTransaction = TransferTransaction.create(
                Deadline.create(2, HOURS),
                brotherAddress,
                Collections.singletonList(xem),
                PlainMessage.create("payout"),
                NetworkType.MIJIN_TEST
        );

        final TransferTransaction sisterTransferTransaction = TransferTransaction.create(
                Deadline.create(2, HOURS),
                sisterAddress,
                Collections.singletonList(xem),
                PlainMessage.create("payout"),
                NetworkType.MIJIN_TEST
        );

        final AggregateTransaction aggregateTransaction = AggregateTransaction.createComplete(
                Deadline.create(2, HOURS),
                Arrays.asList(
                        brotherTransferTransaction.toAggregate(account.getPublicAccount()),
                        sisterTransferTransaction.toAggregate(account.getPublicAccount())
                ),
                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 brotherAddress = 'SDG4WG-FS7EQJ-KFQKXM-4IUCQG-PXUW5H-DJVIJB-OXJG';
const brotherAccount = Address.createFromRawAddress(brotherAddress);

const sisterAddress = 'SCGPXB-2A7T4I-W5MQCX-FQY4UQ-W5JNU5-F55HGK-HBUN';
const sisterAccount = Address.createFromRawAddress(sisterAddress);

const amount = XEM.createRelative(10); // 10 xem represent 10 000 000 micro xem

const brotherTransferTransaction = TransferTransaction.create(Deadline.create(), brotherAccount, [amount], PlainMessage.create('payout'), NetworkType.MIJIN_TEST);
const sisterTransferTransaction = TransferTransaction.create(Deadline.create(), sisterAccount, [amount], PlainMessage.create('payout'), NetworkType.MIJIN_TEST);

const aggregateTransaction = AggregateTransaction.createComplete(
    Deadline.create(),
    [brotherTransferTransaction.toAggregate(account.publicAccount),
        sisterTransferTransaction.toAggregate(account.publicAccount)],
    NetworkType.MIJIN_TEST,
    []
);

Do you know the difference between aggregate complete and aggregate bonded? In this case, one private key can sign all the transactions in the aggregate, so it is aggregate complete.

That means that there is no need to lock funds to send the transaction. If valid, it will be accepted by the network.

  1. Sign and announce the transaction.
const signedTransaction = account.sign(aggregateTransaction);

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

        final SignedTransaction signedTransaction = account.sign(aggregateTransaction);

        transactionHttp.announce(signedTransaction).toFuture().get();
const signedTransaction = account.sign(aggregateTransaction);

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

What’s next?

Send an aggregate bonded transaction by following creating an escrow with aggregate bonded transaction guide.