Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SampleDocs add epochAdjustment #597

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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 @@ -8,7 +8,6 @@ import {
Mosaic,
NamespaceId,
NetworkCurrencies,
NetworkType,
PlainMessage,
PublicAccount,
RepositoryFactoryHttp,
Expand All @@ -17,102 +16,111 @@ import {
UInt64,
} from 'symbol-sdk';

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

/* start block 01 */
const networkType = NetworkType.TEST_NET;
/* start block 01 */
const networkType = await repositoryFactory.getNetworkType().toPromise();

// replace with alice private key
const alicePrivatekey = '';
const aliceAccount = Account.createFromPrivateKey(alicePrivatekey, networkType);
// replace with alice private key
const alicePrivatekey = '';
const aliceAccount = Account.createFromPrivateKey(
alicePrivatekey,
networkType,
);

// replace with bob public key
const bobPublicKey = '';
const bobPublicAccount = PublicAccount.createFromPublicKey(
bobPublicKey,
networkType,
);
// replace with bob public key
const bobPublicKey = '';
const bobPublicAccount = PublicAccount.createFromPublicKey(
bobPublicKey,
networkType,
);

const aliceTransferTransaction = TransferTransaction.create(
Deadline.create(epochAdjustment),
bobPublicAccount.address,
[NetworkCurrencies.PUBLIC.currency.createRelative(1000)],
PlainMessage.create('payout'),
networkType,
);
const aliceTransferTransaction = TransferTransaction.create(
Deadline.create(epochAdjustment),
bobPublicAccount.address,
[NetworkCurrencies.PUBLIC.currency.createRelative(1000)],
PlainMessage.create('payout'),
networkType,
);

const bobTransferTransaction = TransferTransaction.create(
Deadline.create(epochAdjustment),
aliceAccount.address,
[new Mosaic(new NamespaceId('collectible'), UInt64.fromUint(1))],
PlainMessage.create('payout'),
networkType,
);
const bobTransferTransaction = TransferTransaction.create(
Deadline.create(epochAdjustment),
aliceAccount.address,
[new Mosaic(new NamespaceId('collectible'), UInt64.fromUint(1))],
PlainMessage.create('payout'),
networkType,
);

const aggregateTransaction = AggregateTransaction.createComplete(
Deadline.create(epochAdjustment),
[
aliceTransferTransaction.toAggregate(aliceAccount.publicAccount),
bobTransferTransaction.toAggregate(bobPublicAccount),
],
networkType,
[],
UInt64.fromUint(2000000),
);
/* end block 01 */
const aggregateTransaction = AggregateTransaction.createComplete(
Deadline.create(epochAdjustment),
[
aliceTransferTransaction.toAggregate(aliceAccount.publicAccount),
bobTransferTransaction.toAggregate(bobPublicAccount),
],
networkType,
[],
UInt64.fromUint(2000000),
);
/* end block 01 */

/* start block 02 */
// replace with meta.networkGenerationHash (nodeUrl + '/node/info')
const generationHash =
'1DFB2FAA9E7F054168B0C5FCB84F4DEB62CC2B4D317D861F3168D161F54EA78B';
/* start block 02 */
// replace with meta.networkGenerationHash (nodeUrl + '/node/info')
const generationHash =
'1DFB2FAA9E7F054168B0C5FCB84F4DEB62CC2B4D317D861F3168D161F54EA78B';

const signedTransactionNotComplete = aliceAccount.sign(
aggregateTransaction,
generationHash,
);
console.log(signedTransactionNotComplete.payload);
/* end block 02 */
const signedTransactionNotComplete = aliceAccount.sign(
aggregateTransaction,
generationHash,
);
console.log(signedTransactionNotComplete.payload);
/* end block 02 */

/* start block 03 */
// replace with bob private key
const bobPrivateKey = '';
const bobAccount = Account.createFromPrivateKey(bobPrivateKey, networkType);
const cosignedTransactionBob = CosignatureTransaction.signTransactionPayload(
bobAccount,
signedTransactionNotComplete.payload,
generationHash,
);
console.log(cosignedTransactionBob.signature);
console.log(cosignedTransactionBob.parentHash);
/* end block 03 */
/* start block 03 */
// replace with bob private key
const bobPrivateKey = '';
const bobAccount = Account.createFromPrivateKey(bobPrivateKey, networkType);
const cosignedTransactionBob = CosignatureTransaction.signTransactionPayload(
bobAccount,
signedTransactionNotComplete.payload,
generationHash,
);
console.log(cosignedTransactionBob.signature);
console.log(cosignedTransactionBob.parentHash);
/* end block 03 */

/* start block 04 */
const cosignatureSignedTransactions = [
new CosignatureSignedTransaction(
cosignedTransactionBob.parentHash,
cosignedTransactionBob.signature,
cosignedTransactionBob.signerPublicKey,
),
];
const rectreatedAggregateTransactionFromPayload = TransactionMapping.createFromPayload(
signedTransactionNotComplete.payload,
) as AggregateTransaction;
/* start block 04 */
const cosignatureSignedTransactions = [
new CosignatureSignedTransaction(
cosignedTransactionBob.parentHash,
cosignedTransactionBob.signature,
cosignedTransactionBob.signerPublicKey,
),
];
const rectreatedAggregateTransactionFromPayload = TransactionMapping.createFromPayload(
signedTransactionNotComplete.payload,
) as AggregateTransaction;

const signedTransactionComplete = aliceAccount.signTransactionGivenSignatures(
rectreatedAggregateTransactionFromPayload,
cosignatureSignedTransactions,
generationHash,
);
console.log(signedTransactionComplete.hash);
const signedTransactionComplete = aliceAccount.signTransactionGivenSignatures(
rectreatedAggregateTransactionFromPayload,
cosignatureSignedTransactions,
generationHash,
);
console.log(signedTransactionComplete.hash);

// replace with node endpoint
const nodeUrl = 'http://api-01.us-east-1.testnet.symboldev.network:3000';
const repositoryFactory = new RepositoryFactoryHttp(nodeUrl);
const transactionHttp = repositoryFactory.createTransactionRepository();
// replace with node endpoint

transactionHttp.announce(signedTransactionComplete).subscribe(
(x) => console.log(x),
(err) => console.error(err),
);
/* end block 04 */
const transactionHttp = repositoryFactory.createTransactionRepository();

transactionHttp.announce(signedTransactionComplete).subscribe(
(x) => console.log(x),
(err) => console.error(err),
);
/* end block 04 */
};
example().then();
Loading