Asking for mosaics with aggregate-bonded transaction
Ask an account to send you funds using an aggregate bonded transaction.
Prerequisites
- A text editor or IDE.
- Finish creating an escrow with aggregate bonded transaction guide.
- Have an account with
XPX
.
Getting into some code
Bob wants to ask Alice for 20 XPX
.
- Set up both Alice’s and Bob’s accounts.
conf, err := sdk.NewConfig(context.Background(), []string{"http://bctestnet1.brimstone.xpxsirius.io:3000"})
if err != nil {
panic(err)
}
// Use the default http client
client := sdk.NewClient(nil, conf)
alicePrivateKey := os.Getenv("PRIVATE_KEY")
aliceAccount, err := client.NewAccountFromPrivateKey(alicePrivateKey)
if err != nil {
panic(err)
}
bobPublicKey := "F82527075248B043994F1CAFD965F3848324C9ABFEC506BC05FBCF5DD7307C9D";
bobAccount, err := client.NewAccountFromPublicKey(bobPublicKey)
if err != nil {
panic(err)
}
const nodeUrl = 'http://bctestnet1.brimstone.xpxsirius.io:3000';
const transactionHttp = new TransactionHttp(nodeUrl);
const listener = new Listener(nodeUrl);
const alicePrivateKey = process.env.ALICE_PRIVATE_KEY as string;
const aliceAccount = Account.createFromPrivateKey(alicePrivateKey, NetworkType.TEST_NET);
const bobPublicKey = 'F82527075248B043994F1CAFD965F3848324C9ABFEC506BC05FBCF5DD7307C9D';
const bobAccount = PublicAccount.createFromPublicKey(bobPublicKey, NetworkType.TEST_NET);
const nodeUrl = 'http://bctestnet1.brimstone.xpxsirius.io:3000';
const transactionHttp = new TransactionHttp(nodeUrl);
const listener = new Listener(nodeUrl);
const alicePrivateKey = process.env.ALICE_PRIVATE_KEY;
const aliceAccount = Account.createFromPrivateKey(alicePrivateKey, NetworkType.TEST_NET);
const bobPublicKey = 'F82527075248B043994F1CAFD965F3848324C9ABFEC506BC05FBCF5DD7307C9D';
const bobAccount = PublicAccount.createFromPublicKey(bobPublicKey, NetworkType.TEST_NET);
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.concurrent.ExecutionException;
import static java.time.temporal.ChronoUnit.HOURS;
class AskingForMosaicsWithAggregateBondedTransaction {
@Test
void askingForMosaicsWithAggregateBondedTransaction() throws ExecutionException, InterruptedException, MalformedURLException {
// Replace with a Alice's private key
final String alicePrivateKey = "";
// Replace with a Bob's public key
final String bobPublicKey = "";
final Account aliceAccount = Account.createFromPrivateKey(alicePrivateKey, NetworkType.TEST_NET);
final PublicAccount bobPublicAccount = PublicAccount.createFromPublicKey(bobPublicKey, NetworkType.TEST_NET);
- Create an aggregate bonded transaction with two inner transactions:
- From Bob to Alice with the message
send me 20 xpx
transferTransaction1, err := client.NewTransferTransaction(sdk.NewDeadline(time.Hour), aliceAccount.PublicAccount.Address, []*sdk.Mosaic{}, sdk.NewPlainMessage("send me 20 XPX"))
if err != nil {
panic(err)
}
transferTransaction1.ToAggregate(bobAccount)
const transferTransaction1 = TransferTransaction.create(
Deadline.create(),
bobAccount.address,
[],
PlainMessage.create('send me 20 XPX'),
NetworkType.TEST_NET);
const transferTransaction1 = TransferTransaction.create(
Deadline.create(),
bobAccount.address,
[],
PlainMessage.create('send me 20 XPX'),
NetworkType.TEST_NET);
final TransferTransaction transferTransaction1 = TransferTransaction.create(
Deadline.create(2, HOURS),
bobPublicAccount.getAddress(),
Collections.emptyList(),
PlainMessage.create("send me 20 XPX"),
NetworkType.TEST_NET
);
- From Alice to Bob sending
20 xpx
transferTransaction2, err := client.NewTransferTransaction(sdk.NewDeadline(time.Hour), bobAccount.Address, []*sdk.Mosaic{sdk.XpxRelative(20)}, sdk.NewPlainMessage(""))
if err != nil {
panic(err)
}
transferTransaction2.ToAggregate(aliceAccount.PublicAccount)
const transferTransaction2 = TransferTransaction.create(
Deadline.create(),
aliceAccount.address,
[NetworkCurrencyMosaic.createRelative(20)],
EmptyMessage,
NetworkType.TEST_NET);
const transferTransaction2 = TransferTransaction.create(
Deadline.create(),
aliceAccount.address,
[NetworkCurrencyMosaic.createRelative(20)],
EmptyMessage,
NetworkType.TEST_NET);
final TransferTransaction transferTransaction2 = TransferTransaction.create(
Deadline.create(2, HOURS),
aliceAccount.getAddress(),
Collections.singletonList(NetworkCurrencyMosaic.createRelative(BigInteger.valueOf(20))),
PlainMessage.Empty,
NetworkType.TEST_NET
);
- Wrap the defined transactions in an aggregate bonded transaction:
aggregateTransaction, err := client.NewBondedAggregateTransaction(sdk.NewDeadline(time.Hour), []sdk.Transaction{transferTransaction1, transferTransaction2})
if err != nil {
panic(err)
}
const aggregateTransaction = AggregateTransaction.createBonded(
Deadline.create(),
[transferTransaction1.toAggregate(aliceAccount.publicAccount),
transferTransaction2.toAggregate(bobAccount)],
NetworkType.TEST_NET);
const signedTransaction = aliceAccount.sign(aggregateTransaction, generationHash);
const aggregateTransaction = AggregateTransaction.createBonded(
Deadline.create(),
[transferTransaction1.toAggregate(aliceAccount.publicAccount),
transferTransaction2.toAggregate(bobAccount)],
NetworkType.TEST_NET);
const signedTransaction = aliceAccount.sign(aggregateTransaction, generationHash);
final AggregateTransaction aggregateTransaction = new TransactionBuilderFactory().aggregateBonded()
.innerTransactions(Arrays.asList(
transferTransaction1.toAggregate(aliceAccount.getPublicAccount()),
transferTransaction2.toAggregate(bobPublicAccount)
)).deadline(new Deadline(2, ChronoUnit.HOURS)).networkType(NetworkType.TEST_NET);
final SignedTransaction aggregateSignedTransaction = aliceAccount.sign(aggregateTransaction, generationHash);
- Sign the aggregate bonded transaction with Alice’s account and announce it to the network. Remember to lock 10 nativeCurrency first. Alice will recover the locked mosaics if the aggregate transaction completes.
signedAggregateBoundedTransaction, err := aliceAccount.Sign(aggregateTransaction)
if err != nil {
panic(err)
}
lockFundsTransaction, err := client.NewLockFundsTransaction(
sdk.NewDeadline(time.Hour),
sdk.XpxRelative(10),
sdk.Duration(1000),
signedAggregateBoundedTransaction
)
if err != nil {
panic(err)
}
signedLockFundsTransaction, err := aliceAccount.Sign(lockFundsTransaction)
if err != nil {
panic(err)
}
_, err = client.Transaction.Announce(context.Background(), signedLockFundsTransaction)
if err != nil {
panic(err)
}
time.Sleep(time.Second * 30)
_, err = client.Transaction.AnnounceAggregateBonded(context.Background(), signedAggregateBoundedTransaction)
if err != nil {
panic(err)
}
const lockFundsTransaction = LockFundsTransaction.create(
Deadline.create(),
NetworkCurrencyMosaic.createRelative(10),
UInt64.fromUint(480),
signedTransaction,
NetworkType.TEST_NET);
const lockFundsTransactionSigned = aliceAccount.sign(lockFundsTransaction, generationHash);
listener.open().then(() => {
transactionHttp
.announce(lockFundsTransactionSigned)
.subscribe(x => console.log(x), err => console.error(err));
listener
.confirmed(aliceAccount.address)
.pipe(
filter((transaction) => transaction.transactionInfo !== undefined
&& transaction.transactionInfo.hash === lockFundsTransactionSigned.hash),
mergeMap(ignored => transactionHttp.announceAggregateBonded(signedTransaction))
)
.subscribe(announcedAggregateBonded => console.log(announcedAggregateBonded),
err => console.error(err));
});
const lockFundsTransaction = LockFundsTransaction.create(
Deadline.create(),
NetworkCurrencyMosaic.createRelative(10),
UInt64.fromUint(480),
signedTransaction,
NetworkType.TEST_NET);
const lockFundsTransactionSigned = aliceAccount.sign(lockFundsTransaction, generationHash);
listener.open().then(() => {
transactionHttp
.announce(lockFundsTransactionSigned)
.subscribe(x => console.log(x), err => console.error(err));
listener
.confirmed(aliceAccount.address)
.pipe(
filter((transaction) => transaction.transactionInfo !== undefined
&& transaction.transactionInfo.hash === lockFundsTransactionSigned.hash),
mergeMap(ignored => transactionHttp.announceAggregateBonded(signedTransaction))
)
.subscribe(announcedAggregateBonded => console.log(announcedAggregateBonded),
err => console.error(err));
});
// Creating the lock funds transaction and announce it
final LockFundsTransaction lockFundsTransaction = LockFundsTransaction.create(
Deadline.create(2, HOURS),
NetworkCurrencyMosaic.createRelative(BigInteger.valueOf(10)),
BigInteger.valueOf(1000),
aggregateSignedTransaction,
NetworkType.TEST_NET
);
final SignedTransaction lockFundsTransactionSigned = aliceAccount.sign(lockFundsTransaction, generationHash);
final TransactionHttp transactionHttp = new TransactionHttp("http://bctestnet1.brimstone.xpxsirius.io:3000");
transactionHttp.announce(lockFundsTransactionSigned).toFuture().get();
System.out.println(lockFundsTransactionSigned.getHash());
final Listener listener = new Listener("http://bctestnet1.brimstone.xpxsirius.io:3000");
listener.open().get();
final Transaction transaction = listener.confirmed(aliceAccount.getAddress()).take(1).toFuture().get();
transactionHttp.announceAggregateBonded(aggregateSignedTransaction).toFuture().get();
- If all goes well, Bob receives a notification to cosign the transaction. Check how to cosign the transaction with Bob's account in the following guide.