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

feat: added method to duplicate predicate #3432

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
1e148cb
chore: added static method to duplicate predicate
YaTut1901 Nov 29, 2024
95cc305
Merge branch 'master' into YaTut1901/chore/predicate-duplication-static
Dhaiwat10 Dec 5, 2024
bac1e98
chore: refactored toNewInstance method
YaTut1901 Dec 5, 2024
db6106a
Merge branch 'master' into YaTut1901/chore/predicate-duplication-static
YaTut1901 Dec 9, 2024
8c2e49c
chore: fixed some lint errors
YaTut1901 Dec 9, 2024
486d942
chore: added changeset
YaTut1901 Dec 9, 2024
cf7cf7f
Merge branch 'master' into YaTut1901/chore/predicate-duplication-static
Torres-ssf Dec 9, 2024
cede5c2
Merge branch 'master' into YaTut1901/chore/predicate-duplication-static
YaTut1901 Dec 10, 2024
d8979d6
Update packages/fuel-gauge/src/predicate/predicate-duplication.test.ts
YaTut1901 Dec 10, 2024
da551ba
Update packages/fuel-gauge/src/predicate/predicate-duplication.test.ts
YaTut1901 Dec 10, 2024
33d79a4
Update packages/fuel-gauge/src/predicate/predicate-duplication.test.ts
YaTut1901 Dec 10, 2024
1c55a2e
Update packages/fuel-gauge/src/predicate/predicate-duplication.test.ts
YaTut1901 Dec 10, 2024
d3b488a
Update packages/fuel-gauge/src/predicate/utils/predicate/fundPredicat…
YaTut1901 Dec 10, 2024
dd87424
Update packages/fuel-gauge/src/predicate/utils/predicate/fundPredicat…
YaTut1901 Dec 10, 2024
3a324b5
chore: removed helpers
YaTut1901 Dec 10, 2024
47b01ad
chore: typing in fund function
YaTut1901 Dec 10, 2024
bf2c404
chore: refactored fundPredicate function to fundAccount
YaTut1901 Dec 10, 2024
639c9c4
Merge branch 'master' into YaTut1901/chore/predicate-duplication-static
YaTut1901 Dec 11, 2024
3fb9750
Merge branch 'master' into YaTut1901/chore/predicate-duplication-static
YaTut1901 Dec 13, 2024
81c8cdf
feat: added method to duplicate predicate
YaTut1901 Dec 13, 2024
5f2e7a2
Merge branch 'master' into YaTut1901/chore/predicate-duplication-static
YaTut1901 Dec 17, 2024
f73ea72
Merge branch 'master' into YaTut1901/chore/predicate-duplication-static
arboleya Dec 31, 2024
c58f023
Merge branch 'master' into YaTut1901/chore/predicate-duplication-static
arboleya Jan 2, 2025
204862b
Merge branch 'master' into YaTut1901/chore/predicate-duplication-static
nedsalk Jan 2, 2025
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
42 changes: 42 additions & 0 deletions packages/account/src/predicate/predicate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { JsonAbi, InputValue } from '@fuel-ts/abi-coder';

Check failure on line 1 in packages/account/src/predicate/predicate.ts

View workflow job for this annotation

GitHub Actions / Lint

File has too many classes (2). Maximum allowed is 1
import { Interface } from '@fuel-ts/abi-coder';
import { Address } from '@fuel-ts/address';
import { ErrorCode, FuelError } from '@fuel-ts/errors';
Expand Down Expand Up @@ -48,6 +48,7 @@
bytes: Uint8Array;
predicateData: TData = [] as unknown as TData;
interface: Interface;
initialBytecode: Uint8Array;

/**
* Creates an instance of the Predicate class.
Expand All @@ -73,6 +74,7 @@
const address = Address.fromB256(getPredicateRoot(predicateBytes));
super(address, provider);

this.initialBytecode = arrayify(bytecode);
this.bytes = predicateBytes;
this.interface = predicateInterface;
if (data !== undefined && data.length > 0) {
Expand Down Expand Up @@ -147,6 +149,46 @@
return mainFn?.encodeArguments(this.predicateData) || new Uint8Array();
}

/**
* Creates a new Predicate instance from an existing Predicate instance.
* @param instance - The existing Predicate instance.
* @returns A new Predicate instance with the same bytecode, ABI and provider but with the ability to set the data and configurable constants.
*/
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,
});
}
})();
}
petertonysmith94 marked this conversation as resolved.
Show resolved Hide resolved

/**
* Processes the predicate data and returns the altered bytecode and interface.
*
Expand Down
Loading
Loading