Skip to content

Commit

Permalink
chore: added static method to duplicate predicate
Browse files Browse the repository at this point in the history
  • Loading branch information
YaTut1901 committed Nov 29, 2024
1 parent ef94263 commit 1e148cb
Show file tree
Hide file tree
Showing 3 changed files with 543 additions and 0 deletions.
42 changes: 42 additions & 0 deletions packages/account/src/predicate/predicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export class Predicate<
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 @@ export class Predicate<
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 @@ export class Predicate<
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,
});
}
})();
}

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

0 comments on commit 1e148cb

Please sign in to comment.