Skip to content

Commit

Permalink
feat: implement TypeDoc in Predicate (#1163)
Browse files Browse the repository at this point in the history
* feat: add typedoc to predicate package

* feat: add typedoc doc blocks to applicable predicate classes/functions

* feat: add pred package to typedoc

* chore: changeset

* chore: linting

---------

Co-authored-by: danielbate <--global>
  • Loading branch information
danielbate committed Aug 10, 2023
1 parent cb8e073 commit 8f760fd
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .changeset/rare-timers-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
3 changes: 3 additions & 0 deletions apps/docs/scripts/typedoc-postbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ const recreateInternalLinks = () => {
// Module replacements
{ regex: 'fuel_ts_address.md', replacement: '/api/Address/index.md' },
{ regex: 'fuel_ts_interfaces.md', replacement: '/api/Interfaces/index.md' },
{ regex: 'fuel_ts_predicate.md', replacement: '/api/Predicate/index.md' },
{ regex: 'fuel_ts_wallet.md', replacement: '/api/Wallet/index.md' },
{ regex: 'fuel_ts_contract.md', replacement: '/api/Contract/index.md' },
{ regex: 'fuel_ts_script.md', replacement: '/api/Script/index.md' },
Expand All @@ -112,6 +113,8 @@ const recreateInternalLinks = () => {
{ regex: 'interfaces-AbstractAccount.md', replacement: '/api/Interfaces/AbstractAccount.md' },
{ regex: 'interfaces-AbstractAddress.md', replacement: '/api/Interfaces/AbstractAddress.md' },
{ regex: 'interfaces-AbstractContract.md', replacement: '/api/Interfaces/AbstractContract.md' },
// Predicate replacements
{ regex: 'predicate-Predicate', replacement: '/api/Predicate/Predicate' },
// Wallet replacements
{ regex: 'wallet-Account.md', replacement: '/api/Wallet/Account.md' },
{ regex: 'wallet-BaseWalletUnlocked.md', replacement: '/api/Wallet/BaseWalletUnlocked.md' },
Expand Down
1 change: 1 addition & 0 deletions apps/docs/typedoc.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"entryPoints": [
"../../packages/address",
"../../packages/interfaces",
"../../packages/predicate",
"../../packages/wallet",
"../../packages/contract",
"../../packages/script",
Expand Down
52 changes: 52 additions & 0 deletions packages/predicate/src/predicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,23 @@ import { getPredicateRoot } from './utils';

const logger = new Logger(versions.FUELS);

/**
* `Predicate` provides methods to populate transaction data with predicate information and sending transactions with them.
*/
export class Predicate<ARGS extends InputValue[]> extends Account {
bytes: Uint8Array;
predicateData: Uint8Array = Uint8Array.from([]);
interface?: Interface;

/**
* Creates an instance of the Predicate class.
*
* @param bytes - The bytes of the predicate.
* @param chainId - The chain ID for which the predicate is used.
* @param jsonAbi - The JSON ABI of the predicate.
* @param provider - The provider used to interact with the blockchain.
* @param configurableConstants - Optional configurable constants for the predicate.
*/
constructor(
bytes: BytesLike,
chainId: number,
Expand All @@ -49,6 +61,12 @@ export class Predicate<ARGS extends InputValue[]> extends Account {
this.interface = predicateInterface;
}

/**
* Populates the transaction data with predicate data.
*
* @param transactionRequestLike - The transaction request-like object.
* @returns The transaction request with predicate data.
*/
populateTransactionPredicateData(transactionRequestLike: TransactionRequestLike) {
const request = transactionRequestify(transactionRequestLike);

Expand All @@ -64,16 +82,34 @@ export class Predicate<ARGS extends InputValue[]> extends Account {
return request;
}

/**
* Sends a transaction with the populated predicate data.
*
* @param transactionRequestLike - The transaction request-like object.
* @returns A promise that resolves to the transaction response.
*/
sendTransaction(transactionRequestLike: TransactionRequestLike): Promise<TransactionResponse> {
const transactionRequest = this.populateTransactionPredicateData(transactionRequestLike);
return super.sendTransaction(transactionRequest);
}

/**
* Simulates a transaction with the populated predicate data.
*
* @param transactionRequestLike - The transaction request-like object.
* @returns A promise that resolves to the call result.
*/
simulateTransaction(transactionRequestLike: TransactionRequestLike): Promise<CallResult> {
const transactionRequest = this.populateTransactionPredicateData(transactionRequestLike);
return super.simulateTransaction(transactionRequest);
}

/**
* Sets data for the predicate.
*
* @param args - Arguments for the predicate function.
* @returns The Predicate instance with updated predicate data.
*/
setData<T extends ARGS>(...args: T) {
const mainFn = this.interface?.functions.main;
const paddedCode = new ByteArrayCoder(this.bytes.length).encode(this.bytes);
Expand All @@ -89,6 +125,14 @@ export class Predicate<ARGS extends InputValue[]> extends Account {
return this;
}

/**
* Processes the predicate data and returns the altered bytecode and interface.
*
* @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,
Expand Down Expand Up @@ -122,6 +166,14 @@ export class Predicate<ARGS extends InputValue[]> extends Account {
};
}

/**
* Sets the configurable constants for the predicate.
*
* @param bytes - The bytes of the predicate.
* @param configurableConstants - Configurable constants to be set.
* @param abiInterface - The ABI interface of the predicate.
* @returns The mutated bytes with the configurable constants set.
*/
private static setConfigurableConstants(
bytes: Uint8Array,
configurableConstants: { [name: string]: unknown },
Expand Down
9 changes: 9 additions & 0 deletions packages/predicate/src/utils/getPredicateRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ import { hash, uint64ToBytesBE } from '@fuel-ts/hasher';
import { calcRoot } from '@fuel-ts/merkle';
import { chunkAndPadBytes } from '@fuel-ts/utils';

/**
* @hidden
*
* Calculates the predicate root for a given bytecode and chain ID.
*
* @param bytecode - The bytecode represented as a BytesLike object.
* @param chainId - The ID of the chain associated with the bytecode.
* @returns The predicate root as a string.
*/
export const getPredicateRoot = (bytecode: BytesLike, chainId: number): string => {
const chunkSize = 16 * 1024;
const bytes = arrayify(bytecode);
Expand Down
4 changes: 4 additions & 0 deletions packages/predicate/tsdoc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/tsdoc/v0/tsdoc.schema.json",
"extends": ["../../tsdoc.base.json"]
}
5 changes: 5 additions & 0 deletions packages/predicate/typedoc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"$schema": "https://typedoc.org/schema.json",
"extends": ["../../typedoc.base.json"],
"entryPoints": ["src/index.ts"]
}

0 comments on commit 8f760fd

Please sign in to comment.