From a82f744673bb1d9fe91a9646c4b6adfee631fa03 Mon Sep 17 00:00:00 2001 From: YaTut1901 Date: Thu, 14 Nov 2024 13:18:51 +0200 Subject: [PATCH] chore(): added emthod to duplicate predicate --- packages/account/src/predicate/predicate.ts | 20 ++++++++++++++++ .../account/test/fixtures/predicate-abi.ts | 8 ++++++- .../account/test/predicate-functions.test.ts | 23 +++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/packages/account/src/predicate/predicate.ts b/packages/account/src/predicate/predicate.ts index 3d8957f883b..4bad1dfa9c3 100644 --- a/packages/account/src/predicate/predicate.ts +++ b/packages/account/src/predicate/predicate.ts @@ -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 { + 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. * diff --git a/packages/account/test/fixtures/predicate-abi.ts b/packages/account/test/fixtures/predicate-abi.ts index 91bf0b52c06..b8a792d2907 100644 --- a/packages/account/test/fixtures/predicate-abi.ts +++ b/packages/account/test/fixtures/predicate-abi.ts @@ -30,5 +30,11 @@ export const predicateAbi: JsonAbi = { ], loggedTypes: [], messagesTypes: [], - configurables: [], + configurables: [ + { + name: 'value', + concreteTypeId: 'b760f44fa5965c2474a3b471467a22c43185152129295af588b022ae50b50903', + offset: 0, + }, + ], }; diff --git a/packages/account/test/predicate-functions.test.ts b/packages/account/test/predicate-functions.test.ts index 88d385e02de..9142a85fb0c 100644 --- a/packages/account/test/predicate-functions.test.ts +++ b/packages/account/test/predicate-functions.test.ts @@ -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); + }); }); });