From 564520abb095b1980e98129e4d04a44d3ac8cc4f Mon Sep 17 00:00:00 2001 From: George Kudrayvtsev Date: Thu, 31 Aug 2023 13:46:19 -0700 Subject: [PATCH 1/2] Better falsy behavior and getters --- src/sorobandata_builder.js | 26 ++++++++++++++++++-------- types/index.d.ts | 1 + 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/sorobandata_builder.js b/src/sorobandata_builder.js index e4e0d6da..27f9a35e 100644 --- a/src/sorobandata_builder.js +++ b/src/sorobandata_builder.js @@ -6,7 +6,8 @@ import xdr from './xdr'; * * This is recommended for when you are building * {@link Operation.bumpFootprintExpiration} / - * {@link Operation.restoreFootprint} operations to avoid (re)building the entire + * {@link Operation.restoreFootprint} operations and need to + * {@link TransactionBuilder.setSorobanData} to avoid (re)building the entire * data structure from scratch. * * @constructor @@ -14,7 +15,8 @@ import xdr from './xdr'; * @param {string | xdr.SorobanTransactionData} [sorobanData] either a * base64-encoded string that represents an * {@link xdr.SorobanTransactionData} instance or an XDR instance itself - * (it will be copied); if omitted, it starts with an empty instance + * (it will be copied); if omitted or "falsy" (e.g. an empty string), it + * starts with an empty instance * * @example * // You want to use an existing data blob but override specific parts. @@ -35,9 +37,7 @@ export class SorobanDataBuilder { constructor(sorobanData) { let data; - if (typeof sorobanData === 'string' || ArrayBuffer.isView(sorobanData)) { - data = SorobanDataBuilder.fromXDR(sorobanData); - } else if (!sorobanData) { + if (!sorobanData) { data = new xdr.SorobanTransactionData({ resources: new xdr.SorobanResources({ footprint: new xdr.LedgerFootprint({ readOnly: [], readWrite: [] }), @@ -49,8 +49,13 @@ export class SorobanDataBuilder { ext: new xdr.ExtensionPoint(0), refundableFee: new xdr.Int64(0) }); + } else if ( + typeof sorobanData === 'string' || + ArrayBuffer.isView(sorobanData) + ) { + data = SorobanDataBuilder.fromXDR(sorobanData); } else { - data = xdr.SorobanTransactionData.fromXDR(sorobanData.toXDR()); // copy + data = SorobanDataBuilder.fromXDR(sorobanData.toXDR()); // copy } this._data = data; @@ -180,11 +185,16 @@ export class SorobanDataBuilder { /** @returns {xdr.LedgerKey[]} the read-only storage access pattern */ getReadOnly() { - return this._data.resources().footprint().readOnly(); + return this.getFootprint().readOnly(); } /** @returns {xdr.LedgerKey[]} the read-write storage access pattern */ getReadWrite() { - return this._data.resources().footprint().readWrite(); + return this.getFootprint().readWrite(); + } + + /** @returns {xdr.LedgerFootprint} the storage access pattern */ + getFootprint() { + return this._data.resources().footprint(); } } diff --git a/types/index.d.ts b/types/index.d.ts index a7477086..6d992063 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1163,6 +1163,7 @@ export class SorobanDataBuilder { setReadOnly(keys: xdr.LedgerKey[]): SorobanDataBuilder; setReadWrite(keys: xdr.LedgerKey[]): SorobanDataBuilder; + getFootprint(): xdr.LedgerFootprint; getReadOnly(): xdr.LedgerKey[]; getReadWrite(): xdr.LedgerKey[]; From adcc67d49c64cc45be17299e190eba01edf0b2cf Mon Sep 17 00:00:00 2001 From: George Kudrayvtsev Date: Fri, 1 Sep 2023 10:50:15 -0700 Subject: [PATCH 2/2] Add testing stub for default ctor --- test/unit/sorobandata_builder_test.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/unit/sorobandata_builder_test.js b/test/unit/sorobandata_builder_test.js index 623087c5..181f8705 100644 --- a/test/unit/sorobandata_builder_test.js +++ b/test/unit/sorobandata_builder_test.js @@ -34,6 +34,12 @@ describe('SorobanTransactionData can be built', function () { expect(fromRaw).to.eql(sentinel); expect(fromStr).to.eql(sentinel); + + const baseline = new dataBuilder().build(); + [null, '', 0].forEach((falsy) => { + const db = new dataBuilder(falsy).build(); + expect(db).to.eql(baseline); + }); }); it('sets properties as expected', function () {