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

chore: added static method to duplicate predicate #3432

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

YaTut1901
Copy link
Contributor

Summary

Added static method fromInstance which returns builder anonymous class which can be populated with constants and data and be assembled to Predicate instance.

Checklist

  • All changes are covered by tests (or not applicable)
  • All changes are documented (or not applicable)
  • I reviewed the entire PR myself (preferably, on GH UI)
  • I described all Breaking Changes (or there's none)

Copy link

vercel bot commented Nov 29, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
ts-docs-api ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 29, 2024 9:33am

Copy link

vercel bot commented Nov 29, 2024

@YaTut1901 is attempting to deploy a commit to the Fuel Labs Team on Vercel.

A member of the Team first needs to authorize it.

@YaTut1901
Copy link
Contributor Author

@Torres-ssf @danielbate @petertonysmith94 hello, could you please check this implementation and if that fits point me which docs it should be documented in?

Copy link

codspeed-hq bot commented Nov 29, 2024

CodSpeed Performance Report

Merging #3432 will degrade performances by 54.51%

Comparing YaTut1901:YaTut1901/chore/predicate-duplication-static (1e148cb) with master (ef94263)

Summary

❌ 1 regressions
✅ 17 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark master YaTut1901:YaTut1901/chore/predicate-duplication-static Change
should successfully transfer a single asset between wallets (x20 times) 61.5 ms 135.2 ms -54.51%

Comment on lines +157 to +190
static fromInstance<
TData extends InputValue[] = InputValue[],
TConfigurables extends { [name: string]: unknown } | undefined = { [name: string]: unknown },
>(instance: Predicate<TData, TConfigurables>) {
return new (class {
data: TData;
configurableConstants: TConfigurables;

constructor() {
this.data = instance.predicateData;
this.configurableConstants = {} as TConfigurables;
}

withData(data: TData): this {
this.data = data;
return this;
}

withConfigurableConstants(configurableConstants: TConfigurables): this {
this.configurableConstants = configurableConstants;
return this;
}

build(): Predicate<TData, TConfigurables> {
return new Predicate({
bytecode: instance.initialBytecode,
abi: instance.interface.jsonAbi,
provider: instance.provider,
data: this.data,
configurableConstants: this.configurableConstants,
});
}
})();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
static fromInstance<
TData extends InputValue[] = InputValue[],
TConfigurables extends { [name: string]: unknown } | undefined = { [name: string]: unknown },
>(instance: Predicate<TData, TConfigurables>) {
return new (class {
data: TData;
configurableConstants: TConfigurables;
constructor() {
this.data = instance.predicateData;
this.configurableConstants = {} as TConfigurables;
}
withData(data: TData): this {
this.data = data;
return this;
}
withConfigurableConstants(configurableConstants: TConfigurables): this {
this.configurableConstants = configurableConstants;
return this;
}
build(): Predicate<TData, TConfigurables> {
return new Predicate({
bytecode: instance.initialBytecode,
abi: instance.interface.jsonAbi,
provider: instance.provider,
data: this.data,
configurableConstants: this.configurableConstants,
});
}
})();
}
static fromInstance<
TData extends InputValue[] = InputValue[],
TConfigurables extends { [name: string]: unknown } | undefined = { [name: string]: unknown },
>(instance: Predicate<TData, TConfigurables>, overrides?: PredicateParams = {}) {
return new Predicate({
bytecode: instance.initialBytecode,
abi: instance.interface.abi,
provider: instance.provider,
data: instance.data,
configurableConstants: instance.configurableConstants,
...overrides,
});
}

Would the following not be simpler? We'd need to keep a reference to the initial configurableConstants with this example, but this could be omitted.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about something like this, but decide not to store constants. So you think that way is better?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be in favour of this route, but the team may have differing opinions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arboleya @Torres-ssf @danielbate will there be any other comments?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maintaining different APIs around predicate instantiation doesn't seem right to me, should be fluid from one to the next, the builder feels unnecessary IMO. I'm in favour of this change.

Additionally, does this function need to be static? Then we could avoid having to pass predicate as first param? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But in case if we have way to override any of fields via PredicateParams then for me there is no reason to have this method. Predicate can be created with constructor

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason why I decided to make a builder pattern here is to encapsulate logic of overriding only constants and data while remain other fields unchangeable

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you think? @danielbate @petertonysmith94

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could pick out the fields that are required to override:

overrides?: Pick<PredicateParams, 'data' | 'configurableConstants'> = {}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, that fits better. I will implement like this

Comment on lines +157 to +190
static fromInstance<
TData extends InputValue[] = InputValue[],
TConfigurables extends { [name: string]: unknown } | undefined = { [name: string]: unknown },
>(instance: Predicate<TData, TConfigurables>) {
return new (class {
data: TData;
configurableConstants: TConfigurables;

constructor() {
this.data = instance.predicateData;
this.configurableConstants = {} as TConfigurables;
}

withData(data: TData): this {
this.data = data;
return this;
}

withConfigurableConstants(configurableConstants: TConfigurables): this {
this.configurableConstants = configurableConstants;
return this;
}

build(): Predicate<TData, TConfigurables> {
return new Predicate({
bytecode: instance.initialBytecode,
abi: instance.interface.jsonAbi,
provider: instance.provider,
data: this.data,
configurableConstants: this.configurableConstants,
});
}
})();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maintaining different APIs around predicate instantiation doesn't seem right to me, should be fluid from one to the next, the builder feels unnecessary IMO. I'm in favour of this change.

Additionally, does this function need to be static? Then we could avoid having to pass predicate as first param? 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice range of tests 👍🏻

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danielbate in my previous PR you proposed to make it static. For me it seems good either

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
chore Issue is a chore
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enable creating a new predicate from an existing one
3 participants