From d812e64b30b23a0e27a2a52699cde9f1af10c4e0 Mon Sep 17 00:00:00 2001 From: amed83 Date: Tue, 12 Jun 2018 14:42:42 +0200 Subject: [PATCH 1/3] adding async method --- src/connection.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/connection.js b/src/connection.js index fb31fbc..e462b58 100644 --- a/src/connection.js +++ b/src/connection.js @@ -24,6 +24,7 @@ export default class Connection { 'outputs': 'outputs', 'transactions': 'transactions', 'transactionsSync': 'transactions?mode=sync', + 'transactionsAsync': 'transactions?mode=async', 'transactionsCommit': 'transactions?mode=commit', 'transactionsDetail': 'transactions/%(transactionId)s', 'assets': 'assets', @@ -118,7 +119,7 @@ export default class Connection { * @param transaction */ postTransaction(transaction) { - return this._req(this.getApiUrls('transactions'), { + return this._req(this.getApiUrls('transactionsCommit'), { method: 'POST', jsonBody: transaction }) @@ -134,6 +135,18 @@ export default class Connection { }) } + + /** + * @param transaction + */ + postTransactionAsync(transaction) { + return this._req(this.getApiUrls('transactionsAsync'), { + method: 'POST', + jsonBody: transaction + }) + } + + /** * @param transaction */ From 34c82a1ae8ded10571bd8b8a3d1e7fe931402a87 Mon Sep 17 00:00:00 2001 From: manolodewiner Date: Tue, 19 Jun 2018 16:52:23 +0200 Subject: [PATCH 2/3] refactor code & add test --- src/connection.js | 5 +--- test/connection/test_connection.js | 3 +++ test/integration/test_integration.js | 34 +++++++++++++++++++++++++++- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/connection.js b/src/connection.js index e462b58..0f9a2ef 100644 --- a/src/connection.js +++ b/src/connection.js @@ -119,10 +119,7 @@ export default class Connection { * @param transaction */ postTransaction(transaction) { - return this._req(this.getApiUrls('transactionsCommit'), { - method: 'POST', - jsonBody: transaction - }) + return this.postTransactionCommit(transaction) } /** diff --git a/test/connection/test_connection.js b/test/connection/test_connection.js index 960c11f..f9e1e7a 100644 --- a/test/connection/test_connection.js +++ b/test/connection/test_connection.js @@ -27,6 +27,9 @@ test('Generate API URLS', t => { 'blocksDetail': 'blocks/%(blockHeight)s', 'outputs': 'outputs', 'transactions': 'transactions', + 'transactionsSync': 'transactions?mode=sync', + 'transactionsAsync': 'transactions?mode=async', + 'transactionsCommit': 'transactions?mode=commit', 'transactionsDetail': 'transactions/%(transactionId)s', 'assets': 'assets', } diff --git a/test/integration/test_integration.js b/test/integration/test_integration.js index a3fa072..9862822 100644 --- a/test/integration/test_integration.js +++ b/test/integration/test_integration.js @@ -35,7 +35,39 @@ test('Valid CREATE transaction', t => { ) const txSigned = Transaction.signTransaction(tx, alice.privateKey) - return conn.postTransactionCommit(txSigned) + return conn.postTransaction(txSigned) + .then(resTx => t.truthy(resTx)) +}) + + +test('Valid CREATE transaction using async', t => { + const conn = new Connection(API_PATH) + + const tx = Transaction.makeCreateTransaction( + asset(), + metaData, + [aliceOutput], + alice.publicKey + ) + const txSigned = Transaction.signTransaction(tx, alice.privateKey) + + return conn.postTransactionAsync(txSigned) + .then(resTx => t.truthy(resTx)) +}) + + +test('Valid CREATE transaction using sync', t => { + const conn = new Connection(API_PATH) + + const tx = Transaction.makeCreateTransaction( + asset(), + metaData, + [aliceOutput], + alice.publicKey + ) + const txSigned = Transaction.signTransaction(tx, alice.privateKey) + + return conn.postTransactionSync(txSigned) .then(resTx => t.truthy(resTx)) }) From 9077d6ce34b3bc0feb208752761cd63c8b0e61be Mon Sep 17 00:00:00 2001 From: manolodewiner Date: Tue, 19 Jun 2018 16:58:42 +0200 Subject: [PATCH 3/3] update docs --- docs/source/readme.rst | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/docs/source/readme.rst b/docs/source/readme.rst index 7ba6d20..0ea5a81 100644 --- a/docs/source/readme.rst +++ b/docs/source/readme.rst @@ -51,14 +51,17 @@ Older versions anymore the `pollStatusAndFetchTransaction()` method as there are three different ways of posting a transaction: - - `async` using the `postTransaction`: the response will return immediately and not wait to see if the transaction is valid. + - `commit` using the `postTransaction` or the `postTransactionCommit`: the response will return after the transaction is committed to a block. - `sync` using the `postTransactionSync`: the response will return after the transaction is validated. - - `commit` using the `postTransactionCommit`: the response will return after the transaction is committed to a block. + - `async` using the `postTransactionAsync`: the response will return immediately and not wait to see if the transaction is valid. By default in the docs we will use the `postTransactionCommit` as is way of being sure that the transaction is validated and commited to a block, so there will not be any issue if you try to do any other action with the asset immediately. + Note: In order to not create breaking changes, both methods `postTransaction` and `postTransactionCommit` are kept although + they do exactly the same + **Version 3.2.x**