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

Improves SorobanDataBuilder construction and adds a getter #686

Merged
merged 2 commits into from
Sep 1, 2023
Merged
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
26 changes: 18 additions & 8 deletions src/sorobandata_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ 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
*
* @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.
Expand All @@ -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: [] }),
Expand All @@ -49,8 +49,13 @@ export class SorobanDataBuilder {
ext: new xdr.ExtensionPoint(0),
refundableFee: new xdr.Int64(0)
});
} else if (
Shaptic marked this conversation as resolved.
Show resolved Hide resolved
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;
Expand Down Expand Up @@ -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();
}
}
6 changes: 6 additions & 0 deletions test/unit/sorobandata_builder_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];

Expand Down
Loading