diff --git a/.changeset/gold-tigers-ring.md b/.changeset/gold-tigers-ring.md new file mode 100644 index 0000000..8bffc05 --- /dev/null +++ b/.changeset/gold-tigers-ring.md @@ -0,0 +1,5 @@ +--- +'@apibara/starknet': patch +--- + +Include weak header flag diff --git a/examples/simple-client/src/main.ts b/examples/simple-client/src/main.ts index 57b733d..572b676 100644 --- a/examples/simple-client/src/main.ts +++ b/examples/simple-client/src/main.ts @@ -30,17 +30,19 @@ async function main() { // Encode address to the wire format. const address = FieldElement.fromBigInt( - '0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7' + // '0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7' + '0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc8' ) const transferKey = [FieldElement.fromBigInt(hash.getSelectorFromName('Transfer'))] // Create stream filter. The client will only receive the specified data. // + // - header: included only if any other filter matches (`weak: true`) // - events: all transfer events from the eth contract // - state update: all storage diffs from the eth contract const filter = Filter.create() - .withHeader() + .withHeader({ weak: true }) .addEvent((ev) => ev.withFromAddress(address).withKeys(transferKey)) .withStateUpdate((su) => su.addStorageDiff((st) => st.withContractAddress(address))) .encode() diff --git a/packages/starknet/src/filter.ts b/packages/starknet/src/filter.ts index a8463a2..fc8cb63 100644 --- a/packages/starknet/src/filter.ts +++ b/packages/starknet/src/filter.ts @@ -39,9 +39,13 @@ export class FilterBuilder { /** * Include header in the returned data. + * + * If the `weak` flag is set, the block header will be included only if any + * other filter matches. */ - withHeader() { - this.inner.header = {} + withHeader(args?: { weak?: boolean }) { + const { weak } = args ?? {} + this.inner.header = { weak } return this } diff --git a/packages/starknet/src/proto/filter.proto b/packages/starknet/src/proto/filter.proto index 580a50b..3c727b2 100644 --- a/packages/starknet/src/proto/filter.proto +++ b/packages/starknet/src/proto/filter.proto @@ -19,10 +19,9 @@ message Filter { } // Filter header. -// -// This filter matches _all_ headers, so it's only necessary -// to include it in the filter to receive header data. message HeaderFilter { + // If true, only include headers if any other filter matches. + bool weak = 1; } // Filter transactions. diff --git a/packages/starknet/src/proto/generated.d.ts b/packages/starknet/src/proto/generated.d.ts index 59f2e07..1ac43de 100644 --- a/packages/starknet/src/proto/generated.d.ts +++ b/packages/starknet/src/proto/generated.d.ts @@ -132,6 +132,9 @@ export namespace apibara { /** Properties of a HeaderFilter. */ interface IHeaderFilter { + + /** HeaderFilter weak */ + weak?: (boolean|null); } /** Represents a HeaderFilter. */ @@ -143,6 +146,9 @@ export namespace apibara { */ constructor(properties?: apibara.starknet.v1alpha2.IHeaderFilter); + /** HeaderFilter weak. */ + public weak: boolean; + /** * Creates a new HeaderFilter instance using the specified properties. * @param [properties] Properties to set diff --git a/packages/starknet/src/proto/generated.js b/packages/starknet/src/proto/generated.js index c213051..52f163c 100644 --- a/packages/starknet/src/proto/generated.js +++ b/packages/starknet/src/proto/generated.js @@ -413,6 +413,7 @@ $root.apibara = (function() { * Properties of a HeaderFilter. * @memberof apibara.starknet.v1alpha2 * @interface IHeaderFilter + * @property {boolean|null} [weak] HeaderFilter weak */ /** @@ -430,6 +431,14 @@ $root.apibara = (function() { this[keys[i]] = properties[keys[i]]; } + /** + * HeaderFilter weak. + * @member {boolean} weak + * @memberof apibara.starknet.v1alpha2.HeaderFilter + * @instance + */ + HeaderFilter.prototype.weak = false; + /** * Creates a new HeaderFilter instance using the specified properties. * @function create @@ -454,6 +463,8 @@ $root.apibara = (function() { HeaderFilter.encode = function encode(message, writer) { if (!writer) writer = $Writer.create(); + if (message.weak != null && Object.hasOwnProperty.call(message, "weak")) + writer.uint32(/* id 1, wireType 0 =*/8).bool(message.weak); return writer; }; @@ -488,6 +499,10 @@ $root.apibara = (function() { while (reader.pos < end) { var tag = reader.uint32(); switch (tag >>> 3) { + case 1: { + message.weak = reader.bool(); + break; + } default: reader.skipType(tag & 7); break; @@ -523,6 +538,9 @@ $root.apibara = (function() { HeaderFilter.verify = function verify(message) { if (typeof message !== "object" || message === null) return "object expected"; + if (message.weak != null && message.hasOwnProperty("weak")) + if (typeof message.weak !== "boolean") + return "weak: boolean expected"; return null; }; @@ -537,7 +555,10 @@ $root.apibara = (function() { HeaderFilter.fromObject = function fromObject(object) { if (object instanceof $root.apibara.starknet.v1alpha2.HeaderFilter) return object; - return new $root.apibara.starknet.v1alpha2.HeaderFilter(); + var message = new $root.apibara.starknet.v1alpha2.HeaderFilter(); + if (object.weak != null) + message.weak = Boolean(object.weak); + return message; }; /** @@ -549,8 +570,15 @@ $root.apibara = (function() { * @param {$protobuf.IConversionOptions} [options] Conversion options * @returns {Object.} Plain object */ - HeaderFilter.toObject = function toObject() { - return {}; + HeaderFilter.toObject = function toObject(message, options) { + if (!options) + options = {}; + var object = {}; + if (options.defaults) + object.weak = false; + if (message.weak != null && message.hasOwnProperty("weak")) + object.weak = message.weak; + return object; }; /**