From 919f525ec00314fed7560b2191bd154f2da292d7 Mon Sep 17 00:00:00 2001 From: Ryan Goree Date: Tue, 30 Apr 2024 15:45:46 -0500 Subject: [PATCH] Remove unused interface, misc cleanup (#66) * Comments cleanup * Use map to avoid unnecessary type casting * Fix AbiEntry comments * Removed unused interface * Rename to * Add changeset --- .changeset/seven-queens-play.md | 5 +++ .../src/cache/utils/createSimpleCacheKey.ts | 10 +++-- .../factories/CachedReadContract.test.ts | 4 +- .../factories/createCachedReadContract.ts | 2 +- .../evm-client/src/contract/types/AbiEntry.ts | 40 +++++++++++++++---- .../src/contract/types/CachedContract.ts | 20 ++-------- .../src/contract/utils/objectToArray.ts | 5 +-- .../src/network/types/Transaction.ts | 2 +- 8 files changed, 53 insertions(+), 35 deletions(-) create mode 100644 .changeset/seven-queens-play.md diff --git a/.changeset/seven-queens-play.md b/.changeset/seven-queens-play.md new file mode 100644 index 00000000..37e0f5eb --- /dev/null +++ b/.changeset/seven-queens-play.md @@ -0,0 +1,5 @@ +--- +"@delvtech/evm-client": minor +--- + +Renamed `deleteReadMatch` to `deleteReadsMatching` diff --git a/packages/evm-client/src/cache/utils/createSimpleCacheKey.ts b/packages/evm-client/src/cache/utils/createSimpleCacheKey.ts index 48511826..9ea96184 100644 --- a/packages/evm-client/src/cache/utils/createSimpleCacheKey.ts +++ b/packages/evm-client/src/cache/utils/createSimpleCacheKey.ts @@ -7,16 +7,18 @@ type DefinedValue = NonNullable< /** * Converts a given raw key into a `SimpleCacheKey``. * - * The method ensures that any given raw key, regardless of its structure, - * is converted into a format suitable for consistent cache key referencing. + * The method ensures that any given raw key, regardless of its structure, is + * converted into a format suitable for consistent cache key referencing. * * - For scalar (string, number, boolean), it returns them directly. * - For arrays, it recursively processes each element. - * - For objects, it sorts the keys and then recursively processes each value, ensuring consistent key generation. + * - For objects, it sorts the keys and then recursively processes each value, + * ensuring consistent key generation. * - For other types, it attempts to convert the raw key to a string. * * @param rawKey - The raw input to be converted into a cache key. - * @returns A standardized cache key suitable for consistent referencing within the cache. + * @returns A standardized cache key suitable for consistent referencing within + * the cache. */ export function createSimpleCacheKey(rawKey: DefinedValue): SimpleCacheKey { switch (typeof rawKey) { diff --git a/packages/evm-client/src/contract/factories/CachedReadContract.test.ts b/packages/evm-client/src/contract/factories/CachedReadContract.test.ts index d8b5b3cd..3ccc2d57 100644 --- a/packages/evm-client/src/contract/factories/CachedReadContract.test.ts +++ b/packages/evm-client/src/contract/factories/CachedReadContract.test.ts @@ -101,7 +101,7 @@ describe('createCachedReadContract', () => { expect(bobValue).toBe(200n); // Deleting anything that matches a balanceOf call - cachedContract.deleteReadMatch('balanceOf'); + cachedContract.deleteReadsMatching('balanceOf'); // Request bob and alice's balance again const aliceValue2 = await cachedContract.read('balanceOf', { @@ -138,7 +138,7 @@ describe('createCachedReadContract', () => { await cachedContract.read('allowance', bobArgs); // Deleting any allowance calls where BOB is the spender - cachedContract.deleteReadMatch('allowance', { spender: BOB }); + cachedContract.deleteReadsMatching('allowance', { spender: BOB }); // Request bob and alice's allowance again await cachedContract.read('allowance', aliceArgs); diff --git a/packages/evm-client/src/contract/factories/createCachedReadContract.ts b/packages/evm-client/src/contract/factories/createCachedReadContract.ts index 76389cdb..64ab7e13 100644 --- a/packages/evm-client/src/contract/factories/createCachedReadContract.ts +++ b/packages/evm-client/src/contract/factories/createCachedReadContract.ts @@ -92,7 +92,7 @@ export function createCachedReadContract({ cache.delete(key); }, - deleteReadMatch(...args) { + deleteReadsMatching(...args) { const [functionName, functionArgs, options] = args; const sourceKey = createSimpleCacheKey([ diff --git a/packages/evm-client/src/contract/types/AbiEntry.ts b/packages/evm-client/src/contract/types/AbiEntry.ts index 246e5bfa..7c66e37a 100644 --- a/packages/evm-client/src/contract/types/AbiEntry.ts +++ b/packages/evm-client/src/contract/types/AbiEntry.ts @@ -17,8 +17,10 @@ export type NamedAbiParameter = AbiParameter & { name: string }; * Get a union of possible names for an abi item type. * * @example + * ```ts * type Erc20EventNames = AbiEntryName; * // -> "Approval" | "Transfer" + * ``` */ export type AbiEntryName< TAbi extends Abi, @@ -29,6 +31,7 @@ export type AbiEntryName< * Get the ABI entry for a specific type, name, and state mutability. * * @example + * ```ts * type ApproveEntry = AbiEntry; * // -> * // { @@ -38,6 +41,7 @@ export type AbiEntryName< * // outputs: [{ name: "", type: "bool" }]; * // stateMutability: "nonpayable"; * // } + * ``` */ export type AbiEntry< TAbi extends Abi, @@ -53,8 +57,10 @@ export type AbiEntry< * Get the parameters for a specific ABI entry. * * @example + * ```ts * type ApproveParameters = AbiParameters; * // -> [{ name: "spender", type: "address" }, { name: "value", type: "uint256" }] + * ``` */ export type AbiParameters< TAbi extends Abi = Abi, @@ -71,6 +77,12 @@ export type AbiParameters< /** * Add default names to any ABI parameters that are missing a name. The default * name is the index of the parameter. + * + * @example + * ```ts + * type Parameters = WithDefaultNames<[{ name: "spender", type: "address" }, { type: "uint256" }]>; + * // -> [{ name: "spender", type: "address" }, { name: "1", type: "uint256" }] + * ``` */ type WithDefaultNames = { [K in keyof TParameters]: TParameters[K] extends infer TParameter extends @@ -84,13 +96,18 @@ type WithDefaultNames = { /** * Convert an array or tuple of named abi parameters to an object type with the * parameter names as keys and their primitive types as values. If a parameter - * has no name, it's index is used as the key. + * has an empty name, it's index is used as the key. + * + * @example + * ```ts + * type Parameters = NamedParametersToObject<[{ name: "spender", type: "address" }, { name: "", type: "uint256" }]>; + * // -> { spender: `${string}`, "1": bigint } + * ``` */ type NamedParametersToObject< TParameters extends readonly NamedAbiParameter[], TParameterKind extends AbiParameterKind = AbiParameterKind, > = Prettify< - // <- Combine all the keys into a single object { // For every parameter name, excluding empty names, add a key to the object // for the parameter name @@ -112,8 +129,8 @@ type NamedParametersToObject< ? // Exclude `number` to ensure only the specific index keys are // included and not `number` itself Exclude - : never // <- Prototype key (e.g., `length`, `toString`) - : never]: TParameters[K] extends NamedAbiParameter + : never // <- Key for named parameters (already handled above) + : never /* <- Prototype key (e.g., `length`, `toString`) */]: TParameters[K] extends NamedAbiParameter ? AbiParameterToPrimitiveType : never; // <- Prototype value } @@ -134,10 +151,13 @@ type NamedParametersToObject< * Convert an array or tuple of abi parameters to an object type. * * @example + * ```ts * type ApproveArgs = AbiParametersToObject<[ * { name: "spender", type: "address" }, * { name: "value", type: "uint256" } - * ]>; // -> { spender: `${string}`, value: bigint } + * ]>; + * // -> { spender: `0x${string}`, value: bigint } + * ``` */ export type AbiParametersToObject< TParameters extends readonly AbiParameter[], @@ -152,11 +172,13 @@ export type AbiParametersToObject< * Get an array of primitive types for any ABI parameters. * * @example + * ```ts * type ApproveInput = AbiArrayType; - * // -> [`${string}`, bigint] + * // -> [`0x${string}`, bigint] * * type BalanceOutput = AbiArrayType; * // -> [bigint] + * ``` */ export type AbiArrayType< TAbi extends Abi, @@ -179,11 +201,13 @@ export type AbiArrayType< * Get an object of primitive types for any ABI parameters. * * @example + * ```ts * type ApproveArgs = AbiObjectType; - * // -> { spender: `${string}`, value: bigint } + * // -> { spender: `0x${string}`, value: bigint } * * type Balance = AbiObjectType; * // -> { balance: bigint } + * ``` */ export type AbiObjectType< TAbi extends Abi, @@ -204,6 +228,7 @@ export type AbiObjectType< * - __No parameters:__ `undefined`. * * @example + * ```ts * type ApproveArgs = AbiFriendlyType; * // -> { spender: `${string}`, value: bigint } * @@ -212,6 +237,7 @@ export type AbiObjectType< * * type DecimalArgs = AbiFriendlyType; * // -> undefined + * ``` */ export type AbiFriendlyType< TAbi extends Abi, diff --git a/packages/evm-client/src/contract/types/CachedContract.ts b/packages/evm-client/src/contract/types/CachedContract.ts index 750a39c1..f1db9aab 100644 --- a/packages/evm-client/src/contract/types/CachedContract.ts +++ b/packages/evm-client/src/contract/types/CachedContract.ts @@ -1,23 +1,12 @@ import { Abi } from 'abitype'; import { ContractReadArgs, - ContractReadOptions, ReadContract, ReadWriteContract, } from 'src/contract/types/Contract'; -import { FunctionArgs, FunctionName } from 'src/contract/types/Function'; +import { FunctionName } from 'src/contract/types/Function'; import { SimpleCache } from 'src/exports'; -interface DeleteReadOptionsObj< - TAbi extends Abi, - TFunctionName extends FunctionName, -> { - functionName: TFunctionName; - args: FunctionArgs; - options: ContractReadOptions; - partialMatch?: boolean; -} - export interface CachedReadContract extends ReadContract { cache: SimpleCache; @@ -26,7 +15,7 @@ export interface CachedReadContract deleteRead>( ...[functionName, args, options]: ContractReadArgs ): void; - deleteReadMatch>( + deleteReadsMatching>( ...[functionName, args, options]: DeepPartial< ContractReadArgs > @@ -37,8 +26,7 @@ export interface CachedReadWriteContract extends CachedReadContract, ReadWriteContract {} - - +/** Recursively make all properties in T partial. */ type DeepPartial = Partial<{ [K in keyof T]: DeepPartial; -}>; \ No newline at end of file +}>; diff --git a/packages/evm-client/src/contract/utils/objectToArray.ts b/packages/evm-client/src/contract/utils/objectToArray.ts index f733fcf8..5766a0fc 100644 --- a/packages/evm-client/src/contract/utils/objectToArray.ts +++ b/packages/evm-client/src/contract/utils/objectToArray.ts @@ -83,10 +83,7 @@ export function objectToArray< const valueObject: Record = value && typeof value === 'object' ? value : {}; - const array: unknown[] = []; - parameters.forEach(({ name }, i) => { - array.push(valueObject[name || i]); - }); + const array = parameters.map(({ name }, i) => valueObject[name || i]); return array as AbiArrayType; } diff --git a/packages/evm-client/src/network/types/Transaction.ts b/packages/evm-client/src/network/types/Transaction.ts index 967c74d7..109a48e8 100644 --- a/packages/evm-client/src/network/types/Transaction.ts +++ b/packages/evm-client/src/network/types/Transaction.ts @@ -8,7 +8,7 @@ export interface TransactionInfo { } /** Basic legacy compatible transaction */ -// https://github.com/ethereum/execution-apis/blob/main/src/schemas/transaction.yaml#L184 +// https://github.com/ethereum/execution-apis/blob/e8727564bb74a1ebcd22a933b7b57eb7b71a11c3/src/schemas/transaction.yaml#L184 export interface Transaction extends TransactionInfo { type: `0x${string}`; nonce: number;