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 method to duplicate predicate #3395

Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
20 changes: 20 additions & 0 deletions packages/account/src/predicate/predicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,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<TData, TConfigurables>({
bytecode: this.bytes,
abi: this.interface?.jsonAbi,
provider: this.provider,
data: params.data ?? this.predicateData,
configurableConstants: params.configurableConstants,
loaderBytecode: this.loaderBytecode,
YaTut1901 marked this conversation as resolved.
Show resolved Hide resolved
});
}

/**
* 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
YaTut1901 marked this conversation as resolved.
Show resolved Hide resolved
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);
});
});
});
2 changes: 1 addition & 1 deletion templates/nextjs/src/components/Predicate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function Predicate() {
successNotification,
} = useNotification();
useNotification();
const [predicate, setPredicate] = useState<FuelPredicate<InputValue[]>>();
const [predicate, setPredicate] = useState<FuelPredicate<InputValue[], undefined>>();
const [predicatePin, setPredicatePin] = useState<string>();
const [isLoading, setIsLoading] = useState(false);

Expand Down
2 changes: 1 addition & 1 deletion templates/vite/src/components/Predicate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default function Predicate() {
transactionSuccessNotification,
successNotification,
} = useNotification();
const [predicate, setPredicate] = useState<FuelPredicate<InputValue[]>>();
const [predicate, setPredicate] = useState<FuelPredicate<InputValue[], undefined>>();
const [predicatePin, setPredicatePin] = useState<string>();
const [isLoading, setIsLoading] = useState(false);

Expand Down
Loading