Skip to content

Commit

Permalink
Remove unused interface, misc cleanup (#66)
Browse files Browse the repository at this point in the history
* Comments cleanup

* Use map to avoid unnecessary type casting

* Fix AbiEntry comments

* Removed unused interface

* Rename  to

* Add changeset
  • Loading branch information
ryangoree authored Apr 30, 2024
1 parent b7e26fe commit 919f525
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 35 deletions.
5 changes: 5 additions & 0 deletions .changeset/seven-queens-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@delvtech/evm-client": minor
---

Renamed `deleteReadMatch` to `deleteReadsMatching`
10 changes: 6 additions & 4 deletions packages/evm-client/src/cache/utils/createSimpleCacheKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export function createCachedReadContract<TAbi extends Abi = Abi>({
cache.delete(key);
},

deleteReadMatch(...args) {
deleteReadsMatching(...args) {
const [functionName, functionArgs, options] = args;

const sourceKey = createSimpleCacheKey([
Expand Down
40 changes: 33 additions & 7 deletions packages/evm-client/src/contract/types/AbiEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Erc20Abi, "event">;
* // -> "Approval" | "Transfer"
* ```
*/
export type AbiEntryName<
TAbi extends Abi,
Expand All @@ -29,6 +31,7 @@ export type AbiEntryName<
* Get the ABI entry for a specific type, name, and state mutability.
*
* @example
* ```ts
* type ApproveEntry = AbiEntry<Erc20Abi, "function", "approve">;
* // ->
* // {
Expand All @@ -38,6 +41,7 @@ export type AbiEntryName<
* // outputs: [{ name: "", type: "bool" }];
* // stateMutability: "nonpayable";
* // }
* ```
*/
export type AbiEntry<
TAbi extends Abi,
Expand All @@ -53,8 +57,10 @@ export type AbiEntry<
* Get the parameters for a specific ABI entry.
*
* @example
* ```ts
* type ApproveParameters = AbiParameters<Erc20Abi, "function", "approve", "inputs">;
* // -> [{ name: "spender", type: "address" }, { name: "value", type: "uint256" }]
* ```
*/
export type AbiParameters<
TAbi extends Abi = Abi,
Expand All @@ -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<TParameters extends readonly AbiParameter[]> = {
[K in keyof TParameters]: TParameters[K] extends infer TParameter extends
Expand All @@ -84,13 +96,18 @@ type WithDefaultNames<TParameters extends readonly AbiParameter[]> = {
/**
* 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
Expand All @@ -112,8 +129,8 @@ type NamedParametersToObject<
? // Exclude `number` to ensure only the specific index keys are
// included and not `number` itself
Exclude<K, number>
: 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<TParameters[K], TParameterKind>
: never; // <- Prototype value
}
Expand All @@ -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[],
Expand All @@ -152,11 +172,13 @@ export type AbiParametersToObject<
* Get an array of primitive types for any ABI parameters.
*
* @example
* ```ts
* type ApproveInput = AbiArrayType<Erc20Abi, "function", "approve", "inputs">;
* // -> [`${string}`, bigint]
* // -> [`0x${string}`, bigint]
*
* type BalanceOutput = AbiArrayType<Erc20Abi, "function", "balanceOf", "outputs">;
* // -> [bigint]
* ```
*/
export type AbiArrayType<
TAbi extends Abi,
Expand All @@ -179,11 +201,13 @@ export type AbiArrayType<
* Get an object of primitive types for any ABI parameters.
*
* @example
* ```ts
* type ApproveArgs = AbiObjectType<Erc20Abi, "function", "approve", "inputs">;
* // -> { spender: `${string}`, value: bigint }
* // -> { spender: `0x${string}`, value: bigint }
*
* type Balance = AbiObjectType<Erc20Abi, "function", "balanceOf", "outputs">;
* // -> { balance: bigint }
* ```
*/
export type AbiObjectType<
TAbi extends Abi,
Expand All @@ -204,6 +228,7 @@ export type AbiObjectType<
* - __No parameters:__ `undefined`.
*
* @example
* ```ts
* type ApproveArgs = AbiFriendlyType<Erc20Abi, "function", "approve", "inputs">;
* // -> { spender: `${string}`, value: bigint }
*
Expand All @@ -212,6 +237,7 @@ export type AbiObjectType<
*
* type DecimalArgs = AbiFriendlyType<Erc20Abi, "function", "decimals", "inputs">;
* // -> undefined
* ```
*/
export type AbiFriendlyType<
TAbi extends Abi,
Expand Down
20 changes: 4 additions & 16 deletions packages/evm-client/src/contract/types/CachedContract.ts
Original file line number Diff line number Diff line change
@@ -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<TAbi>,
> {
functionName: TFunctionName;
args: FunctionArgs<TAbi, TFunctionName>;
options: ContractReadOptions;
partialMatch?: boolean;
}

export interface CachedReadContract<TAbi extends Abi = Abi>
extends ReadContract<TAbi> {
cache: SimpleCache;
Expand All @@ -26,7 +15,7 @@ export interface CachedReadContract<TAbi extends Abi = Abi>
deleteRead<TFunctionName extends FunctionName<TAbi>>(
...[functionName, args, options]: ContractReadArgs<TAbi, TFunctionName>
): void;
deleteReadMatch<TFunctionName extends FunctionName<TAbi>>(
deleteReadsMatching<TFunctionName extends FunctionName<TAbi>>(
...[functionName, args, options]: DeepPartial<
ContractReadArgs<TAbi, TFunctionName>
>
Expand All @@ -37,8 +26,7 @@ export interface CachedReadWriteContract<TAbi extends Abi = Abi>
extends CachedReadContract<TAbi>,
ReadWriteContract<TAbi> {}



/** Recursively make all properties in T partial. */
type DeepPartial<T> = Partial<{
[K in keyof T]: DeepPartial<T[K]>;
}>;
}>;
5 changes: 1 addition & 4 deletions packages/evm-client/src/contract/utils/objectToArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,7 @@ export function objectToArray<
const valueObject: Record<string, unknown> =
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<TAbi, TItemType, TName, TParameterKind>;
}
2 changes: 1 addition & 1 deletion packages/evm-client/src/network/types/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 919f525

Please sign in to comment.