Skip to content

Commit

Permalink
starknet: include weak header flag
Browse files Browse the repository at this point in the history
  • Loading branch information
fracek committed Jan 18, 2023
1 parent 9a58f52 commit 99d0d32
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/gold-tigers-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@apibara/starknet': patch
---

Include weak header flag
6 changes: 4 additions & 2 deletions examples/simple-client/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 6 additions & 2 deletions packages/starknet/src/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
5 changes: 2 additions & 3 deletions packages/starknet/src/proto/filter.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions packages/starknet/src/proto/generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ export namespace apibara {

/** Properties of a HeaderFilter. */
interface IHeaderFilter {

/** HeaderFilter weak */
weak?: (boolean|null);
}

/** Represents a HeaderFilter. */
Expand All @@ -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
Expand Down
34 changes: 31 additions & 3 deletions packages/starknet/src/proto/generated.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ $root.apibara = (function() {
* Properties of a HeaderFilter.
* @memberof apibara.starknet.v1alpha2
* @interface IHeaderFilter
* @property {boolean|null} [weak] HeaderFilter weak
*/

/**
Expand All @@ -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
Expand All @@ -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;
};

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
};

Expand All @@ -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;
};

/**
Expand All @@ -549,8 +570,15 @@ $root.apibara = (function() {
* @param {$protobuf.IConversionOptions} [options] Conversion options
* @returns {Object.<string,*>} 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;
};

/**
Expand Down

0 comments on commit 99d0d32

Please sign in to comment.