Skip to content

Commit

Permalink
chore(): added emthod to duplicate predicate
Browse files Browse the repository at this point in the history
  • Loading branch information
YaTut1901 committed Nov 14, 2024
1 parent 5dee7ff commit a82f744
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
20 changes: 20 additions & 0 deletions packages/account/src/predicate/predicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,26 @@ export class Predicate<
}
}

/**
* Creates a new Predicate instance with updated parameters while maintaining the original bytecode and ABI
*
* @param params - Object containing optional new configurableConstants and data
* @returns A new Predicate instance with the updated parameters
*/
toNewInstance(params: {
configurableConstants?: TConfigurables;
data?: TData;
}): Predicate<TData, TConfigurables> {
return new Predicate({
bytecode: this.bytes,
abi: this.interface?.jsonAbi,
provider: this.provider,
data: params.data ?? this.predicateData,
configurableConstants: params.configurableConstants,
loaderBytecode: this.loaderBytecode,
});
}

/**
* Populates the transaction data with predicate data.
*
Expand Down
8 changes: 7 additions & 1 deletion packages/account/test/fixtures/predicate-abi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,11 @@ export const predicateAbi: JsonAbi = {
],
loggedTypes: [],
messagesTypes: [],
configurables: [],
configurables: [
{
name: 'value',
concreteTypeId: 'b760f44fa5965c2474a3b471467a22c43185152129295af588b022ae50b50903',
offset: 0,
},
],
};
23 changes: 23 additions & 0 deletions packages/account/test/predicate-functions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,28 @@ describe('Predicate', () => {
});
}).toThrow('Cannot use ABI without "main" function');
});

it('creates a new instance with updated parameters', async () => {
using launched = await setupTestProviderAndWallets();
const { provider } = launched;

const predicate = new Predicate({
bytecode: predicateBytecode,
abi: predicateAbi,
provider,
configurableConstants: { value: false },
});

const newPredicate = predicate.toNewInstance({
configurableConstants: { value: true },
data: ['NADA'],
});

expect(newPredicate.predicateData).toEqual(['NADA']);
expect(newPredicate.bytes.slice(1)).toEqual(predicate.bytes.slice(1));
expect(newPredicate.bytes[0]).not.toEqual(predicate.bytes[0]);
expect(newPredicate.interface?.jsonAbi).toEqual(predicate.interface?.jsonAbi);
expect(newPredicate.provider).toEqual(predicate.provider);
});
});
});

0 comments on commit a82f744

Please sign in to comment.