From 97c008408e165dd84694baa3b02a9ff0a3edff2e Mon Sep 17 00:00:00 2001 From: Jason Paulos Date: Mon, 20 Mar 2023 16:33:02 -0700 Subject: [PATCH 1/3] Tweaks to simulation support --- src/client/v2/algod/simulateTransaction.ts | 10 +++---- src/composer.ts | 31 ++++++++++++---------- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/client/v2/algod/simulateTransaction.ts b/src/client/v2/algod/simulateTransaction.ts index 0d7a0ace7..0d81ab298 100644 --- a/src/client/v2/algod/simulateTransaction.ts +++ b/src/client/v2/algod/simulateTransaction.ts @@ -61,14 +61,12 @@ export default class SimulateRawTransactions extends JSONRequest< Buffer.from(this.txnBytesToPost), txHeaders ); - return res.body; + return this.prepare(res.body); } // eslint-disable-next-line class-methods-use-this - prepare(body: Uint8Array) { - if (body && body.byteLength > 0) { - return encoding.decode(body) as SimulateResponse; - } - return undefined; + prepare(body: Uint8Array): SimulateResponse { + const decoded = encoding.decode(body); + return SimulateResponse.from_obj_for_encoding(decoded); } } diff --git a/src/composer.ts b/src/composer.ts index a938d7639..99dd2b590 100644 --- a/src/composer.ts +++ b/src/composer.ts @@ -635,9 +635,7 @@ export class AtomicTransactionComposer { for (const [txnIndex, method] of this.methodCalls) { const txID = this.txIDs[txnIndex]; const pendingInfo = - simulateResponse['txn-groups'][0]['txn-results'][txnIndex][ - 'txn-result' - ]; + simulateResponse.txnGroups[0].txnResults[txnIndex].txnResult; const methodResult: ABIResult = { txID, @@ -649,7 +647,7 @@ export class AtomicTransactionComposer { AtomicTransactionComposer.parseMethodResponse( method, methodResult, - pendingInfo + pendingInfo.get_obj_for_encoding() ) ); } @@ -710,24 +708,29 @@ export class AtomicTransactionComposer { for (const [txnIndex, method] of this.methodCalls) { const txID = txIDs[txnIndex]; - const methodResult: ABIResult = { + let methodResult: ABIResult = { txID, rawReturnValue: new Uint8Array(), method, }; - const pendingInfo = - txnIndex === firstMethodCallIndex - ? confirmedTxnInfo - : // eslint-disable-next-line no-await-in-loop - await client.pendingTransactionInformation(txID).do(); - methodResults.push( - AtomicTransactionComposer.parseMethodResponse( + try { + const pendingInfo = + txnIndex === firstMethodCallIndex + ? confirmedTxnInfo + : // eslint-disable-next-line no-await-in-loop + await client.pendingTransactionInformation(txID).do(); + + methodResult = AtomicTransactionComposer.parseMethodResponse( method, methodResult, pendingInfo - ) - ); + ); + } catch (err) { + methodResult.decodeError = err; + } + + methodResults.push(methodResult); } return { From 6f4d1234cd24134f643bc352941dc43b68258196 Mon Sep 17 00:00:00 2001 From: algochoi <86622919+algochoi@users.noreply.github.com> Date: Tue, 21 Mar 2023 10:34:58 -0400 Subject: [PATCH 2/3] Fix object destructuring for simulate response --- src/client/v2/algod/simulateTransaction.ts | 4 +--- tests/cucumber/steps/steps.js | 20 ++++++++------------ 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/client/v2/algod/simulateTransaction.ts b/src/client/v2/algod/simulateTransaction.ts index 0d81ab298..20934f50b 100644 --- a/src/client/v2/algod/simulateTransaction.ts +++ b/src/client/v2/algod/simulateTransaction.ts @@ -1,5 +1,4 @@ import { Buffer } from 'buffer'; -import * as encoding from '../../../encoding/encoding'; import { concatArrays } from '../../../utils/utils'; import HTTPClient from '../../client'; import JSONRequest from '../jsonrequest'; @@ -66,7 +65,6 @@ export default class SimulateRawTransactions extends JSONRequest< // eslint-disable-next-line class-methods-use-this prepare(body: Uint8Array): SimulateResponse { - const decoded = encoding.decode(body); - return SimulateResponse.from_obj_for_encoding(decoded); + return SimulateResponse.from_obj_for_encoding(body); } } diff --git a/tests/cucumber/steps/steps.js b/tests/cucumber/steps/steps.js index ea1230ee5..90e09b13e 100644 --- a/tests/cucumber/steps/steps.js +++ b/tests/cucumber/steps/steps.js @@ -4644,7 +4644,7 @@ module.exports = function getSteps(options) { Then( 'the simulation should succeed without any failure message', async function () { - assert.deepStrictEqual(true, this.simulateResponse['would-succeed']); + assert.deepStrictEqual(true, this.simulateResponse.wouldSucceed); } ); @@ -4656,14 +4656,13 @@ module.exports = function getSteps(options) { const txnIndexes = stringPath.map((n) => parseInt(n, 10)); const groupNum = parseInt(txnGroupIndex, 10); - assert.deepStrictEqual(false, this.simulateResponse['would-succeed']); + assert.deepStrictEqual(false, this.simulateResponse.wouldSucceed); // Check for missing signature flag for (const txnIndex of txnIndexes) { assert.deepStrictEqual( true, - this.simulateResponse['txn-groups'][groupNum]['txn-results'][ - txnIndex - ]['missing-signature'] + this.simulateResponse.txnGroups[groupNum].txnResults[txnIndex] + .missingSignature ); } } @@ -4679,18 +4678,15 @@ module.exports = function getSteps(options) { const stringPath = failAt.split(','); const failPath = stringPath.map((n) => parseInt(n, 10)); - const failedMessage = this.simulateResponse['txn-groups'][groupNum][ - 'failure-message' - ]; - assert.deepStrictEqual(false, this.simulateResponse['would-succeed']); + const failedMessage = this.simulateResponse.txnGroups[groupNum] + .failureMessage; + assert.deepStrictEqual(false, this.simulateResponse.wouldSucceed); const errorContainsString = failedMessage.includes(errorMsg); assert.deepStrictEqual(true, errorContainsString); // Check path array // deepStrictEqual fails for firefox tests, so compare array manually. - const failedAt = this.simulateResponse['txn-groups'][groupNum][ - 'failed-at' - ]; + const { failedAt } = this.simulateResponse.txnGroups[groupNum]; assert.strictEqual(failPath.length, failedAt.length); for (let i = 0; i < failPath.length; i++) { assert.strictEqual(failPath[i], failedAt[i]); From 48477388763851ba93efbd60191348d496e20387 Mon Sep 17 00:00:00 2001 From: algochoi <86622919+algochoi@users.noreply.github.com> Date: Tue, 21 Mar 2023 14:17:37 -0400 Subject: [PATCH 3/3] Get response as msgpack --- src/client/v2/algod/simulateTransaction.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/client/v2/algod/simulateTransaction.ts b/src/client/v2/algod/simulateTransaction.ts index 20934f50b..1bfcd49c7 100644 --- a/src/client/v2/algod/simulateTransaction.ts +++ b/src/client/v2/algod/simulateTransaction.ts @@ -1,4 +1,5 @@ import { Buffer } from 'buffer'; +import * as encoding from '../../../encoding/encoding'; import { concatArrays } from '../../../utils/utils'; import HTTPClient from '../../client'; import JSONRequest from '../jsonrequest'; @@ -58,13 +59,16 @@ export default class SimulateRawTransactions extends JSONRequest< const res = await this.c.post( this.path(), Buffer.from(this.txnBytesToPost), - txHeaders + txHeaders, + this.query, + false ); return this.prepare(res.body); } // eslint-disable-next-line class-methods-use-this prepare(body: Uint8Array): SimulateResponse { - return SimulateResponse.from_obj_for_encoding(body); + const decoded = encoding.decode(body); + return SimulateResponse.from_obj_for_encoding(decoded); } }