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 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
108 changes: 60 additions & 48 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;
configurableConstants: TConfigurables = {} as TConfigurables;

/**
* Creates an instance of the Predicate class.
Expand All @@ -65,21 +66,31 @@ export class Predicate<
data,
configurableConstants,
}: PredicateParams<TData, TConfigurables>) {
const { predicateBytes, predicateInterface } = Predicate.processPredicateData(
bytecode,
abi,
configurableConstants
);
const { predicateBytes, predicateInterface } = Predicate.processPredicateData(bytecode, abi);
const address = Address.fromB256(getPredicateRoot(predicateBytes));
super(address, provider);

this.bytes = predicateBytes;
this.interface = predicateInterface;
if (data !== undefined && data.length > 0) {

if (configurableConstants && Object.keys(configurableConstants).length) {
this.configurableConstants = configurableConstants;
}
if (data && data.length) {
this.predicateData = data;
}
}

/**
* Updates parameters of predicate
*
* @param params - Object containing optional new configurableConstants and data
*/
setParams(params: { configurableConstants?: TConfigurables; data?: TData }) {
this.predicateData = params.data ?? this.predicateData;
this.configurableConstants = params.configurableConstants ?? this.configurableConstants;
}

/**
* Populates the transaction data with predicate data.
*
Expand All @@ -100,7 +111,13 @@ export class Predicate<
request.inputs.filter(isRequestInputCoinOrMessage).forEach((input) => {
if (isRequestInputResourceFromOwner(input, this.address)) {
// eslint-disable-next-line no-param-reassign
input.predicate = hexlify(this.bytes);
input.predicate = hexlify(
this.setConfigurableConstants(
this.bytes,
this.configurableConstants ?? {},
this.interface
)
);
// eslint-disable-next-line no-param-reassign
input.predicateData = hexlify(this.getPredicateData());
// eslint-disable-next-line no-param-reassign
Expand Down Expand Up @@ -152,14 +169,9 @@ export class Predicate<
*
* @param bytes - The bytes of the predicate.
* @param jsonAbi - The JSON ABI of the predicate.
* @param configurableConstants - Optional configurable constants for the predicate.
* @returns An object containing the new predicate bytes and interface.
*/
private static processPredicateData(
bytes: BytesLike,
jsonAbi: JsonAbi,
configurableConstants?: { [name: string]: unknown }
) {
private static processPredicateData(bytes: BytesLike, jsonAbi: JsonAbi) {
let predicateBytes = arrayify(bytes);
const abiInterface: Interface = new Interface(jsonAbi);

Expand All @@ -170,14 +182,6 @@ export class Predicate<
);
}

if (configurableConstants && Object.keys(configurableConstants).length) {
predicateBytes = Predicate.setConfigurableConstants(
predicateBytes,
configurableConstants,
abiInterface
);
}

return {
predicateBytes,
predicateInterface: abiInterface,
Expand All @@ -202,7 +206,9 @@ export class Predicate<
);
return resources.map((resource) => ({
...resource,
predicate: hexlify(this.bytes),
predicate: hexlify(
this.setConfigurableConstants(this.bytes, this.configurableConstants ?? {}, this.interface)
),
predicateData: hexlify(this.getPredicateData()),
}));
}
Expand All @@ -216,11 +222,41 @@ export class Predicate<
override generateFakeResources(coins: FakeResources[]): Array<Resource> {
return super.generateFakeResources(coins).map((coin) => ({
...coin,
predicate: hexlify(this.bytes),
predicate: hexlify(
this.setConfigurableConstants(this.bytes, this.configurableConstants ?? {}, this.interface)
),
predicateData: hexlify(this.getPredicateData()),
}));
}

/**
*
* @param account - The account used to pay the deployment costs.
* @returns The _blobId_ and a _waitForResult_ callback that returns the deployed predicate
* once the blob deployment transaction finishes.
*
* The returned loader predicate will have the same configurable constants
* as the original predicate which was used to generate the loader predicate.
*/
async deploy<T = this>(account: Account) {
return deployScriptOrPredicate<T>({
deployer: account,
abi: this.interface.jsonAbi,
bytecode: this.setConfigurableConstants(
this.bytes,
this.configurableConstants ?? {},
this.interface
),
loaderInstanceCallback: (loaderBytecode, newAbi) =>
new Predicate({
bytecode: loaderBytecode,
abi: newAbi,
provider: this.provider,
data: this.predicateData,
}) as T,
});
}

/**
* Sets the configurable constants for the predicate.
*
Expand All @@ -229,7 +265,7 @@ export class Predicate<
* @param abiInterface - The ABI interface of the predicate.
* @returns The mutated bytes with the configurable constants set.
*/
private static setConfigurableConstants(
private setConfigurableConstants(
bytes: Uint8Array,
configurableConstants: { [name: string]: unknown },
abiInterface: Interface
Expand Down Expand Up @@ -306,28 +342,4 @@ export class Predicate<

return index;
}

/**
*
* @param account - The account used to pay the deployment costs.
* @returns The _blobId_ and a _waitForResult_ callback that returns the deployed predicate
* once the blob deployment transaction finishes.
*
* The returned loader predicate will have the same configurable constants
* as the original predicate which was used to generate the loader predicate.
*/
async deploy<T = this>(account: Account) {
return deployScriptOrPredicate<T>({
deployer: account,
abi: this.interface.jsonAbi,
bytecode: this.bytes,
loaderInstanceCallback: (loaderBytecode, newAbi) =>
new Predicate({
bytecode: loaderBytecode,
abi: newAbi,
provider: this.provider,
data: this.predicateData,
}) as T,
});
}
}
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,
},
],
};
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