Sending a transfer transaction
Transfer mosaics and messages between two accounts.
Prerequisites
- Finish the getting started section.
- XPX-Chain-SDK or XPX-Chain-CLI.
- A text editor or IDE.
- An account with
XPX
.
Background Information
Alice wants to send 10 XPX
to Bob, whose address is VD5DT3-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 life cycle, we recommend you to open three new terminals. The first terminal monitors announced transactions validation errors.
xpx2-cli monitor status
Monitoring unconfirmed
shows you which transactions have reached the network, but not are not included in a block yet.
xpx2-cli monitor unconfirmed
Once a transaction is included, you will see it under the confirmed
terminal.
xpx2-cli monitor confirmed
Getting into some code
- Create the transfer transaction, including Bob's address as the recipient and 10
XPX
.
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)
account, err := client.NewAccountFromPrivateKey(os.Getenv("PRIVATE_KEY"))
if err != nil {
panic(err)
}
address, err := sdk.NewAddressFromRaw("VD5DT3-CH4BLA-BL5HIM-EKP2TA-PUKF4N-Y3L5HR-IR54")
if err != nil {
panic(err)
}
transferTransaction, err := client.NewTransferTransaction(
sdk.NewDeadline(time.Hour),
address,
[]*sdk.Mosaic{sdk.XpxRelative(10)},
sdk.NewPlainMessage("Welcome to Sirius Chain"),
)
if err != nil {
panic(err)
}
const recipientAddress = Address.createFromRawAddress('VD5DT3-CH4BLA-BL5HIM-EKP2TA-PUKF4N-Y3L5HR-IR54');
const transferTransaction = TransferTransaction.create(
Deadline.create(),
recipientAddress,
[NetworkCurrencyMosaic.createRelative(10)],
PlainMessage.create('Welcome To Sirius Chain'),
NetworkType.TEST_NET);
const recipientAddress = Address.createFromRawAddress('VD5DT3-CH4BLA-BL5HIM-EKP2TA-PUKF4N-Y3L5HR-IR54');
const transferTransaction = TransferTransaction.create(
Deadline.create(),
recipientAddress,
[NetworkCurrencyMosaic.createRelative(10)],
PlainMessage.create('Welcome To Sirius Chain'),
NetworkType.TEST_NET);
final String recipientAddress = "VD5DT3-CH4BLA-BL5HIM-EKP2TA-PUKF4N-Y3L5HR-IR54";
final TransferTransaction transferTransaction = TransferTransaction.create(
Deadline.create(2, HOURS),
Address.createFromRawAddress(recipientAddress),
Collections.singletonList(NetworkCurrencyMosaic.createRelative(BigInteger.valueOf(10))),
PlainMessage.create("Welcome To Sirius Chain"),
NetworkType.TEST_NET
);
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:
mosaicId, err := sdk.NewMosaicIdFromNonceAndOwner(nonce, account.PublicAccount.PublicKey)
if err != nil {
panic(err)
}
myMosaic, err := sdk.NewMosaic(mosaicId, 10)
if err != nil {
panic(err)
}
mosaics := []*sdk.Mosaic{sdk.XpxRelative(10), myMosaic}
var myMosaicId = new MosaicId("<MosaicHexString>");
var myMosaic = new Mosaic(myMosaicId, UInt64.fromUint(10));
var mosaics = [
myMosaic,
NetworkCurrencyMosaic.createRelative(10)
];
var myMosaicId = new MosaicId("<MosaicHexString>");
var myMosaic = new Mosaic(myMosaicId, UInt64.fromUint(10));
var mosaics = [
myMosaic,
NetworkCurrencyMosaic.createRelative(10)
];
MosaicId myMosaicId = new MosaicId("<MosaicHexString>");
Mosaic myMosaic = new Mosaic(myMosaicId, BigInteger.valueOf(10));
List<Mosaic> mosaics = Arrays.asList(
myMosaic,
NetworkCurrencyMosaic.createRelative(BigInteger.valueOf(10))
);
Note
Sirius Chain mainly works with absolute amounts. To get an absolute amount, multiply the amount of assets you want to send by the power of 10 with divisibility as the exponent. For example, if the mosaic has a divisibility of 2, to send 10 units (relative), you should define 1000 (absolute) instead.
- Sign the transaction with Alice account.
signedTransaction, err := account.Sign(transferTransaction)
if err != nil {
panic(err)
}
const privateKey = process.env.PRIVATE_KEY as string;
const account = Account.createFromPrivateKey(privateKey, NetworkType.TEST_NET);
const signedTransaction = account.sign(transferTransaction, generationHash);
const privateKey = process.env.PRIVATE_KEY;
const account = Account.createFromPrivateKey(privateKey, NetworkType.TEST_NET);
const signedTransaction = account.sign(transferTransaction, generationHash);
// Replace with private key
final String privateKey = "<privateKey>";
final Account account = Account.createFromPrivateKey(privateKey, NetworkType.TEST_NET);
final SignedTransaction signedTransaction = account.sign(transferTransaction, generationHash);
- Once signed, you can announce the transaction to the network.
_, err = client.Transaction.Announce(context.Background(), signedTransaction)
if err != nil {
panic(err)
}
const transactionHttp = new TransactionHttp('http://bctestnet1.brimstone.xpxsirius.io:3000');
transactionHttp
.announce(signedTransaction)
.subscribe(x => console.log(x), err => console.error(err));
const transactionHttp = new TransactionHttp('http://bctestnet1.brimstone.xpxsirius.io:3000');
transactionHttp
.announce(signedTransaction)
.subscribe(x => console.log(x), err => console.error(err));
final TransactionHttp transactionHttp = new TransactionHttp("http://bctestnet1.brimstone.xpxsirius.io:3000");
transactionHttp.announce(signedTransaction).toFuture().get();
- Open the terminal where you are monitoring account transactions
status
. It should be vacant. 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.