From 5fafb150541c34b6d963087b8b1ce60113382ffb Mon Sep 17 00:00:00 2001 From: George Kudrayvtsev Date: Mon, 14 Aug 2023 18:09:12 -0700 Subject: [PATCH 1/5] Add more helper methods --- src/sorobandata_builder.js | 40 +++++++++++++++++++++++++++++++++----- types/index.d.ts | 12 ++++++++++-- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/src/sorobandata_builder.js b/src/sorobandata_builder.js index 8beea47b..f17c2d46 100644 --- a/src/sorobandata_builder.js +++ b/src/sorobandata_builder.js @@ -35,10 +35,8 @@ export class SorobanDataBuilder { constructor(sorobanData) { let data; - if (typeof sorobanData === 'string') { - data = SorobanDataBuilder.fromXDR(sorobanData, 'base64'); - } else if (ArrayBuffer.isView(sorobanData)) { - data = SorobanDataBuilder.fromXDR(sorobanData, 'raw'); + if (typeof sorobanData === 'string' || ArrayBuffer.isView(sorobanData)) { + data = SorobanDataBuilder.fromXDR(sorobanData); } else if (!sorobanData) { data = new xdr.SorobanTransactionData({ resources: new xdr.SorobanResources({ @@ -102,12 +100,24 @@ export class SorobanDataBuilder { return this; } + /** + * Appends the given ledger keys to the existing storage access footprint. + * @borrows {@link SorobanDataBuilder.setFootprint} + */ + appendFootprint(readOnly, readWrite) { + return setFootprint( + this.getReadOnly().concat(readOnly), + this.getReadWrite().concat(readWrite) + ); + } + /** * Sets the storage access footprint to be a certain set of ledger keys. * * You can also set each field explicitly via * {@link SorobanDataBuilder.setReadOnly} and - * {@link SorobanDataBuilder.setReadWrite}. + * {@link SorobanDataBuilder.setReadWrite} or add to the existing footprint + * via {@link SorobanDataBuilder.appendFootprint}. * * Passing `null|undefined` to either parameter will IGNORE the existing * values. If you want to clear them, pass `[]`, instead. @@ -160,4 +170,24 @@ export class SorobanDataBuilder { build() { return xdr.SorobanTransactionData.fromXDR(this._data.toXDR()); // clone } + + // + // getters follow + // + + /** @returns {xdr.LedgerKey[]} the read-only storage access pattern */ + getReadOnly() { + return this._data + .resources() + .footprint() + .readOnly(); + } + + /** @returns {xdr.LedgerKey[]} the read-write storage access pattern */ + getReadWrite() { + return this._data + .resources() + .footprint() + .readWrite(); + } } diff --git a/types/index.d.ts b/types/index.d.ts index 8bb10139..83858824 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1140,6 +1140,7 @@ export function humanizeEvents( export class SorobanDataBuilder { constructor(data?: string | xdr.SorobanTransactionData); + static fromXDR(data: Uint8Array|Buffer|string): SorobanDataBuilder; setRefundableFee(fee: IntLike): SorobanDataBuilder; setResources( @@ -1153,9 +1154,16 @@ export class SorobanDataBuilder { readOnly?: xdr.LedgerKey[] | null, readWrite?: xdr.LedgerKey[] | null ): SorobanDataBuilder; + appendFootprint( + readOnly?: xdr.LedgerKey[] | null, + readWrite?: xdr.LedgerKey[] | null + ): SorobanDataBuilder; + + setReadOnly(keys: xdr.LedgerKey[]): SorobanDataBuilder; + setReadWrite(keys: xdr.LedgerKey[]): SorobanDataBuilder; - setReadOnly(keys?: xdr.LedgerKey[]): SorobanDataBuilder; - setReadWrite(keys?: xdr.LedgerKey[]): SorobanDataBuilder; + getReadOnly(): xdr.LedgerKey[]; + getReadWrite(): xdr.LedgerKey[]; build(): xdr.SorobanTransactionData; } From 4181a4de5134c259f691b90b71538c0662aa4651 Mon Sep 17 00:00:00 2001 From: George Kudrayvtsev Date: Mon, 14 Aug 2023 18:40:34 -0700 Subject: [PATCH 2/5] Fixup the various TS signatures --- types/index.d.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index 83858824..7287d6d7 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1139,8 +1139,8 @@ export function humanizeEvents( ): SorobanEvent[]; export class SorobanDataBuilder { - constructor(data?: string | xdr.SorobanTransactionData); - static fromXDR(data: Uint8Array|Buffer|string): SorobanDataBuilder; + constructor(data?: string | Uint8Array | Buffer | xdr.SorobanTransactionData); + static fromXDR(data: Uint8Array | Buffer | string): SorobanDataBuilder; setRefundableFee(fee: IntLike): SorobanDataBuilder; setResources( @@ -1155,8 +1155,8 @@ export class SorobanDataBuilder { readWrite?: xdr.LedgerKey[] | null ): SorobanDataBuilder; appendFootprint( - readOnly?: xdr.LedgerKey[] | null, - readWrite?: xdr.LedgerKey[] | null + readOnly: xdr.LedgerKey[], + readWrite: xdr.LedgerKey[] ): SorobanDataBuilder; setReadOnly(keys: xdr.LedgerKey[]): SorobanDataBuilder; From ddad12d5478aea8f05ff269e33b7cbddc0b992e6 Mon Sep 17 00:00:00 2001 From: George Kudrayvtsev Date: Mon, 14 Aug 2023 18:44:33 -0700 Subject: [PATCH 3/5] Add tests, fixup self-call --- src/sorobandata_builder.js | 2 +- test/unit/sorobandata_builder_test.js | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/sorobandata_builder.js b/src/sorobandata_builder.js index f17c2d46..fe03555c 100644 --- a/src/sorobandata_builder.js +++ b/src/sorobandata_builder.js @@ -105,7 +105,7 @@ export class SorobanDataBuilder { * @borrows {@link SorobanDataBuilder.setFootprint} */ appendFootprint(readOnly, readWrite) { - return setFootprint( + return this.setFootprint( this.getReadOnly().concat(readOnly), this.getReadWrite().concat(readWrite) ); diff --git a/test/unit/sorobandata_builder_test.js b/test/unit/sorobandata_builder_test.js index cae369ef..5d320b43 100644 --- a/test/unit/sorobandata_builder_test.js +++ b/test/unit/sorobandata_builder_test.js @@ -59,6 +59,20 @@ describe('SorobanTransactionData can be built', function () { expect(data2.resources().footprint().readWrite()).to.eql([]); }); + it('appends footprints', function () { + const builder = new dataBuilder(); + + const data = builder + .setFootprint([key], [key]) + .appendFootprint([key, key], []); + const built = data.build(); + + expect(data.getReadOnly()).to.eql([key, key, key]); + expect(data.getReadWrite()).to.eql([key, key, key]); + expect(built.resources().footprint().readOnly()).to.eql([key, key, key]); + expect(built.resources().footprint().readWrite()).to.eql([key]); + }); + it('makes copies on build()', function () { const builder = new dataBuilder(); const first = builder.build(); From a4458e36f203c2b0b97d1a7dabf02169423d11bc Mon Sep 17 00:00:00 2001 From: George Kudrayvtsev Date: Mon, 14 Aug 2023 18:45:30 -0700 Subject: [PATCH 4/5] Lint, test fixup --- src/sorobandata_builder.js | 10 ++-------- test/unit/sorobandata_builder_test.js | 2 +- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/src/sorobandata_builder.js b/src/sorobandata_builder.js index fe03555c..fe2c900f 100644 --- a/src/sorobandata_builder.js +++ b/src/sorobandata_builder.js @@ -177,17 +177,11 @@ export class SorobanDataBuilder { /** @returns {xdr.LedgerKey[]} the read-only storage access pattern */ getReadOnly() { - return this._data - .resources() - .footprint() - .readOnly(); + return this._data.resources().footprint().readOnly(); } /** @returns {xdr.LedgerKey[]} the read-write storage access pattern */ getReadWrite() { - return this._data - .resources() - .footprint() - .readWrite(); + return this._data.resources().footprint().readWrite(); } } diff --git a/test/unit/sorobandata_builder_test.js b/test/unit/sorobandata_builder_test.js index 5d320b43..623087c5 100644 --- a/test/unit/sorobandata_builder_test.js +++ b/test/unit/sorobandata_builder_test.js @@ -68,7 +68,7 @@ describe('SorobanTransactionData can be built', function () { const built = data.build(); expect(data.getReadOnly()).to.eql([key, key, key]); - expect(data.getReadWrite()).to.eql([key, key, key]); + expect(data.getReadWrite()).to.eql([key]); expect(built.resources().footprint().readOnly()).to.eql([key, key, key]); expect(built.resources().footprint().readWrite()).to.eql([key]); }); From 5cf30c6cbffa04e2e6d60cf7057615584ea8e5b7 Mon Sep 17 00:00:00 2001 From: George Kudrayvtsev Date: Mon, 14 Aug 2023 18:47:26 -0700 Subject: [PATCH 5/5] Jsdoc fixup to shut up linter --- src/sorobandata_builder.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/sorobandata_builder.js b/src/sorobandata_builder.js index fe2c900f..e4e0d6da 100644 --- a/src/sorobandata_builder.js +++ b/src/sorobandata_builder.js @@ -102,7 +102,9 @@ export class SorobanDataBuilder { /** * Appends the given ledger keys to the existing storage access footprint. - * @borrows {@link SorobanDataBuilder.setFootprint} + * @param {xdr.LedgerKey[]} readOnly read-only keys to add + * @param {xdr.LedgerKey[]} readWrite read-write keys to add + * @returns {SorobanDataBuilder} this builder instance */ appendFootprint(readOnly, readWrite) { return this.setFootprint( @@ -123,11 +125,12 @@ export class SorobanDataBuilder { * values. If you want to clear them, pass `[]`, instead. * * @param {xdr.LedgerKey[]|null} [readOnly] the set of ledger keys to set in - * the read-only portion of the transaction's `sorobanData` + * the read-only portion of the transaction's `sorobanData`, or `null | + * undefined` to keep the existing keys * @param {xdr.LedgerKey[]|null} [readWrite] the set of ledger keys to set in - * the read-write portion of the transaction's `sorobanData` - * - * @returns {SorobanDataBuilder} + * the read-write portion of the transaction's `sorobanData`, or `null | + * undefined` to keep the existing keys + * @returns {SorobanDataBuilder} this builder instance */ setFootprint(readOnly, readWrite) { if (readOnly !== null) {