Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,32 +36,44 @@ Sending a synchronous transaction

Alice is developing an app to send 10 |privatenetworkcurrency| to Bob and wants to know if the transaction has reached the network before sending Bob an email.

1. Create a new ``.ts`` file. Then, define and sign a :doc:`TransferTransaction <../../concepts/transfer-transaction>`.
1. Create a new source file, define and sign a :doc:`TransferTransaction <../../concepts/transfer-transaction>`.

.. example-code::

.. viewsource:: ../../resources/examples/typescript/monitor/TurningTheAsynchronousTransactionAnnouncementIntoSynchronous.ts
.. viewsource:: ../../resources/examples/typescript/monitor/TurningTheAsynchronousTransactionAnnouncementIntoSynchronousViaPromise.ts
:language: typescript
:start-after: /* start block 01 */
:end-before: /* end block 01 */

.. viewsource:: ../../resources/examples/typescript/monitor/TurningTheAsynchronousTransactionAnnouncementIntoSynchronous.js
.. viewsource:: ../../resources/examples/typescript/monitor/TurningTheAsynchronousTransactionAnnouncementIntoSynchronousViaPromise.js
:language: javascript
:start-after: /* start block 01 */
:end-before: /* end block 01 */

2. Once signed, :doc:`announce the transaction <../../concepts/transaction>` using ``TransactionService.announce`` instead of ``TransactionHttp.announce``.
2. Once signed, announce the transaction using a ``TransactionService`` instead of a ``TransactionFactory``. This service connects the necessary listeners so your code can just focus on the transaction's result. For example, using promises:

.. example-code::

.. viewsource:: ../../resources/examples/typescript/monitor/TurningTheAsynchronousTransactionAnnouncementIntoSynchronous.ts
.. viewsource:: ../../resources/examples/typescript/monitor/TurningTheAsynchronousTransactionAnnouncementIntoSynchronousViaPromise.ts
:language: typescript
:start-after: /* start block 02 */
:end-before: /* end block 02 */

.. viewsource:: ../../resources/examples/typescript/monitor/TurningTheAsynchronousTransactionAnnouncementIntoSynchronous.js
.. viewsource:: ../../resources/examples/typescript/monitor/TurningTheAsynchronousTransactionAnnouncementIntoSynchronousViaPromise.js
:language: javascript
:start-after: /* start block 02 */
:end-before: /* end block 02 */

.. note:: The function ``TransactionService.announce()`` will respond successfully if the transaction reaches the network and does not have validation errors. You might still need to :doc:`wait for several confirmations <../../concepts/transaction>` before executing additional actions.
3. You can also connect the listeners yourself to get notified of more events. For example:

.. example-code::

.. viewsource:: ../../resources/examples/typescript/monitor/TurningTheAsynchronousTransactionAnnouncementIntoSynchronousViaListener.ts
:language: typescript
:start-after: /* start block 02 */
:end-before: /* end block 02 */

.. viewsource:: ../../resources/examples/typescript/monitor/TurningTheAsynchronousTransactionAnnouncementIntoSynchronousViaListener.js
:language: javascript
:start-after: /* start block 02 */
:end-before: /* end block 02 */
2 changes: 1 addition & 1 deletion source/guides/namespace/link-a-namespace-to-a-mosaic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Method #02: Using the SDK
:start-after: /* start block 02 */
:end-before: /* end block 02 */

.. note:: If you want to unlink the alias, change alias action type to ``AliasActionType.Unlink``.
.. note:: If you want to unlink the alias, change alias action type to ``AliasAction.Unlink``.

.. _sending-a-transfer-transaction-with-an-aliased-mosaic:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import { RepositoryFactoryHttp, UInt64 } from 'symbol-sdk';

/* start block 01 */
// replace with node endpoint
// Replace with node endpoint
const nodeUrl = 'http://api-01.us-east-1.testnet.symboldev.network:3000';
const repositoryFactory = new RepositoryFactoryHttp(nodeUrl);
const blockHttp = repositoryFactory.createBlockRepository();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import { ChainInfo, RepositoryFactoryHttp } from 'symbol-sdk';

/* start block 01 */
// replace with node endpoint
// Replace with node endpoint
const nodeUrl = 'http://api-01.us-east-1.testnet.symboldev.network:3000';
const repositoryFactory = new RepositoryFactoryHttp(nodeUrl);
const chainHttp = repositoryFactory.createChainRepository();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
import { NamespaceId, RepositoryFactoryHttp } from 'symbol-sdk';

/* start block 01 */
// replace with namespace name
// Replace with namespace name
const namespaceId = new NamespaceId('symbol.xym');

// replace with node endpoint
// Replace with node endpoint
const nodeUrl = 'http://api-01.us-east-1.testnet.symboldev.network:3000';
const repositoryFactory = new RepositoryFactoryHttp(nodeUrl);
const namespaceHttp = repositoryFactory.createNamespaceRepository();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,74 +23,94 @@ import {
Deadline,
Mosaic,
NamespaceId,
NetworkType,
PlainMessage,
RepositoryFactoryHttp,
TransactionService,
TransferTransaction,
UInt64,
} from 'symbol-sdk';

// Retrieve from node's /network/properties or RepositoryFactory
const epochAdjustment = 123456789;
const example = async (): Promise<void> => {
try {
// Network information
const nodeUrl = 'http://api-01.us-east-1.testnet.symboldev.network:3000';
const repositoryFactory = new RepositoryFactoryHttp(nodeUrl);
const epochAdjustment = await repositoryFactory
.getEpochAdjustment()
.toPromise();
const networkType = await repositoryFactory.getNetworkType().toPromise();
const networkGenerationHash = await repositoryFactory
.getGenerationHash()
.toPromise();

/* start block 01 */
const aliasedMosaic = new Mosaic(
new NamespaceId('symbol.xym'),
UInt64.fromUint(1000000),
);
/* end block 01 */

/* start block 02 */
// replace with network type
const networkType = NetworkType.TEST_NET;
const transferTransaction = TransferTransaction.create(
Deadline.create(epochAdjustment),
Address.createFromRawAddress('TCHBDE-NCLKEB-ILBPWP-3JPB2X-NY64OE-7PYHHE-32I'),
[aliasedMosaic],
PlainMessage.create('Test aliased mosaic'),
networkType,
UInt64.fromUint(2000000),
);
/* start block 01 */
const aliasedMosaic = new Mosaic(
new NamespaceId('symbol.xym'),
UInt64.fromUint(1000000),
);
/* end block 01 */

// replace with sender private key
const privateKey =
'1111111111111111111111111111111111111111111111111111111111111111';
const account = Account.createFromPrivateKey(privateKey, networkType);
// replace with meta.networkGenerationHash (nodeUrl + '/node/info')
const networkGenerationHash =
'1DFB2FAA9E7F054168B0C5FCB84F4DEB62CC2B4D317D861F3168D161F54EA78B';
const signedTransaction = account.sign(
transferTransaction,
networkGenerationHash,
);
console.log(signedTransaction.hash);
/* end block 02 */
/* start block 02 */
const maxFee = UInt64.fromUint(2000000);
const transferTransaction = TransferTransaction.create(
Deadline.create(epochAdjustment),
Address.createFromRawAddress(
'TCHBDE-NCLKEB-ILBPWP-3JPB2X-NY64OE-7PYHHE-32I',
),
[aliasedMosaic],
PlainMessage.create('Test aliased mosaic'),
networkType,
maxFee,
);

/* start block 03 */
// replace with node endpoint
const nodeUrl = 'http://api-01.us-east-1.testnet.symboldev.network:3000';
const repositoryFactory = new RepositoryFactoryHttp(nodeUrl);
const receiptHttp = repositoryFactory.createReceiptRepository();
const transactionHttp = repositoryFactory.createTransactionRepository();
const listener = repositoryFactory.createListener();
const transactionService = new TransactionService(transactionHttp, receiptHttp);
// Replace with sender private key
const privateKey =
'1111111111111111111111111111111111111111111111111111111111111111';
const account = Account.createFromPrivateKey(privateKey, networkType);
const signedTransaction = account.sign(
transferTransaction,
networkGenerationHash,
);
console.log('Transaction hash: ' + signedTransaction.hash);
/* end block 02 */

listener.open().then(() => {
transactionService
.announce(signedTransaction, listener)
.pipe(
mergeMap((transaction) =>
transactionService.resolveAliases([transaction.transactionInfo!.hash!]),
),
map((transactions) => transactions[0] as TransferTransaction),
)
.subscribe(
(transaction) => {
console.log('Resolved MosaicId: ', transaction.mosaics[0].id.toHex());
listener.close();
},
(err) => console.log(err),
/* start block 03 */
const receiptHttp = repositoryFactory.createReceiptRepository();
const transactionHttp = repositoryFactory.createTransactionRepository();
const listener = repositoryFactory.createListener();
const transactionService = new TransactionService(
transactionHttp,
receiptHttp,
);
});
/* end block 03 */

listener.open().then(() => {
transactionService
.announce(signedTransaction, listener)
.pipe(
mergeMap((transaction) =>
transactionService.resolveAliases([
transaction.transactionInfo!.hash!,
]),
),
map((transactions) => transactions[0] as TransferTransaction),
)
.subscribe(
(transaction) => {
console.log(
'Resolved MosaicId: ',
transaction.mosaics[0].id.toHex(),
);
listener.close();
},
(err) => {
console.log(err);
listener.close();
},
);
});
/* end block 03 */
} catch (e) {
console.log(e);
}
};
example().then();
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,24 @@
import { RepositoryFactoryHttp } from 'symbol-sdk';

/* start block 01 */
// Replace with node endpoint
const nodeUrl = 'http://api-01.us-east-1.testnet.symboldev.network:3000';
const repositoryFactory = new RepositoryFactoryHttp(nodeUrl);
const listener = repositoryFactory.createListener();

listener.open().then(() => {
listener.newBlock().subscribe(
(block) => {
console.log(block);
listener.close();
},
(err) => console.error(err),
);
});
listener
.open()
.then(() => {
console.log('Waiting for new block to be harvested...');
listener.newBlock().subscribe(
(block) => {
console.log(block);
listener.close();
},
(err) => console.error(err),
);
})
.catch((e) => {
console.log(e);
});
/* end block 01 */

This file was deleted.

Loading