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

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
19 changes: 19 additions & 0 deletions packages/account/src/predicate/predicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,25 @@ 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,
});
}
Comment on lines +89 to +100
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this can be a static method on the Predicate class like so:

const predicateInstance = new Predicate(...);
const newInstance = Predicate.fromInstance(predicateInstance);

Feels like a better API to me. Thoughts? @FuelLabs/sdk-ts


/**
* 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,
},
],
};
27 changes: 27 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,32 @@ 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 },
data: ['DADA'],
});

expect(predicate.predicateData).toEqual(['DADA']);
expect(predicate.bytes[0]).toEqual(0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check seems redundant, can we check the entire bytecode?


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);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import type { InputValue, BigNumberish, WalletUnlocked, Predicate } from 'fuels';
import { ScriptTransactionRequest, BN } from 'fuels';

export const fundPredicate = async <T extends InputValue[]>(
export const fundPredicate = async <
T extends InputValue[] = InputValue[],
C extends { [name: string]: unknown } | undefined = { [name: string]: unknown },
>(
wallet: WalletUnlocked,
predicate: Predicate<T>,
predicate: Predicate<T, C>,
amountToPredicate: BigNumberish,
utxosAmount: number = 1
): Promise<BN> => {
Expand Down
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