Skip to content

Commit

Permalink
Revert hyperdrive-js-core
Browse files Browse the repository at this point in the history
  • Loading branch information
ryangoree committed Nov 8, 2024
1 parent c756ceb commit b6141c7
Show file tree
Hide file tree
Showing 80 changed files with 1,534 additions and 1,102 deletions.
51 changes: 42 additions & 9 deletions packages/hyperdrive-js-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,17 @@
"typecheck": "tsc --noEmit"
},
"peerDependencies": {
"@delvtech/drift": "^0.0.1-beta.11"
"@delvtech/evm-client": "^0.5.1"
},
"dependencies": {
"@delvtech/fixed-point-wasm": "^0.0.6",
"@delvtech/hyperdrive-artifacts": "^1.0.18",
"@delvtech/hyperdrive-wasm": "^0.15.3",
"lodash.groupby": "^4.6.0",
"lodash.mapvalues": "^4.6.0",
"semver": "^7.6.3"
"lodash.mapvalues": "^4.6.0"
},
"devDependencies": {
"@delvtech/drift": "^0.0.1-beta.11",
"@delvtech/evm-client": "^0.5.1",
"@hyperdrive/eslint-config": "*",
"@hyperdrive/prettier-config": "*",
"@hyperdrive/tsconfig": "*",
Expand All @@ -48,13 +47,47 @@
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js",
"require": "./dist/index.cjs"
"default": "./dist/index.js"
},
"./factory/*": {
"types": "./dist/factory/*.d.ts",
"default": "./dist/factory/*.js"
},
"./hyperdrive/*": {
"types": "./dist/hyperdrive/*.d.ts",
"default": "./dist/hyperdrive/*.js"
},
"./registry/*": {
"types": "./dist/registry/*.d.ts",
"default": "./dist/registry/*.js"
},
"./token/*": {
"types": "./dist/token/*.d.ts",
"default": "./dist/token/*.js"
},
"./contract": {
"types": "./dist/contract.d.ts",
"default": "./dist/contract.js"
},
"./errors": {
"types": "./dist/errors.d.ts",
"default": "./dist/errors.js"
},
"./model": {
"types": "./dist/model.d.ts",
"default": "./dist/model.js"
},
"./utils": {
"types": "./dist/utils.d.ts",
"default": "./dist/utils.js"
},
"./v1.0.14": {
"types": "./dist/v1.0.14.d.ts",
"default": "./dist/v1.0.14.js",
"require": "./dist/v1.0.14.cjs"
"types": "./dist/v1.0.14/index.d.ts",
"default": "./dist/v1.0.14/index.js"
},
"./v1.0.14/*": {
"types": "./dist/v1.0.14/*.d.ts",
"default": "./dist/v1.0.14/*.js"
},
"./package.json": "./package.json"
},
Expand Down
91 changes: 91 additions & 0 deletions packages/hyperdrive-js-core/src/base/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,98 @@
/**
* Combines members of an intersection into a readable type.
* @see https://twitter.com/mattpocockuk/status/1622730173446557697?s=20&t=NdpAcmEFXY01xkqU3KO0Mg
*/
export type Prettify<T> = {
[K in keyof T]: T[K];
} & unknown;

/**
* A generic constructor type.
*/
export type Constructor<
TInstanceType = any,
TArgs extends any[] = any[],
> = new (...args: TArgs) => TInstanceType;

/**
* Overrides properties of `T` with properties of `U`.
*/
export type Override<T, U> = Prettify<Omit<T, keyof U> & U>;

/**
* Convert members of a union to an intersection.
*
* @example
* ```ts
* type Union = { a: number } | { b: string };
* type Intersection = UnionToIntersection<Union>;
* // { a: number } & { b: string }
* ```
*
* @privateRemarks
* This works by taking advantage of [distributive conditional
* types](https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types),
* which allows conditional types to be applied to each member of a union type
* individually, and [contravarience in function argument
* types](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-6.html#strict-function-types).
*
* The conditional type `T extends any ? (x: T) => any : never` is used to
* create a function type for each member of the union that takes the member as
* an argument.
*
* Then, the union of function types is checked to see if it can be assigned to
* a single function type with an inferred argument type. TypeScript infers the
* argument type as the intersection of the union members since it's the only
* argument type that satisfies all members of the function type union.
*/
type UnionToIntersection<T> = (
T extends any ? (member: T) => any : never
) extends (member: infer R) => any
? R
: never;

/**
* Merge the keys of a union or intersection of objects into a single type.
*
* @example
* ```ts
* type GetBlockOptions = {
* includeTransactions?: boolean | undefined
* } & (
* | {
* blockHash?: string | undefined;
* blockNumber?: never | undefined;
* blockTag?: never | undefined;
* }
* | {
* blockHash?: never | undefined;
* blockNumber?: bigint | undefined;
* blockTag?: never | undefined;
* }
* | {
* blockHash?: never | undefined;
* blockNumber?: never | undefined;
* blockTag?: string | undefined;
* }
* )
*
* type Merged = MergeKeys<GetBlockOptions>;
* // {
* // includeTransactions?: boolean | undefined;
* // blockHash?: string | undefined;
* // blockNumber?: bigint | undefined;
* // blockTag?: string | undefined;
* // }
* ```
*/
export type MergeKeys<T> =
UnionToIntersection<T> extends infer I
? {
// Each key of the intersection is first checked against the union type,
// T. If it exists in every member of T, then T[K] will be a union of
// the value types. Otherwise, I[K] is used. I[K] is the value type of
// the key in the intersection which will be `never` for keys with
// conflicting value types.
[K in keyof I]: K extends keyof T ? T[K] : I[K];
}
: never;
10 changes: 5 additions & 5 deletions packages/hyperdrive-js-core/src/checkpoint/types.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import {
ContractEvent,
ContractReadOptions,
Event,
FunctionReturn,
Pretty,
} from "@delvtech/drift";
} from "@delvtech/evm-client";
import { Prettify } from "src/base/types";
import { HyperdriveAbi } from "src/hyperdrive/base/abi";

export type Checkpoint = Pretty<
export type Checkpoint = Prettify<
{
checkpointTime: bigint;
} & FunctionReturn<HyperdriveAbi, "getCheckpoint">
>;

export type CheckpointEvent = ContractEvent<HyperdriveAbi, "CreateCheckpoint">;
export type CheckpointEvent = Event<HyperdriveAbi, "CreateCheckpoint">;

export type GetCheckpointTimeParams = (
| {
Expand Down
32 changes: 0 additions & 32 deletions packages/hyperdrive-js-core/src/drift/ContractClient.ts

This file was deleted.

20 changes: 0 additions & 20 deletions packages/hyperdrive-js-core/src/drift/ReadWriteClient.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NetworkGetBlockOptions } from "@delvtech/drift";
import { NetworkGetBlockOptions } from "@delvtech/evm-client";
import { HyperdriveSdkError } from "./HyperdriveSdkError";

export class BlockNotFoundError extends HyperdriveSdkError {
Expand Down
13 changes: 4 additions & 9 deletions packages/hyperdrive-js-core/src/errors/HyperdriveSdkError.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { DriftError } from "@delvtech/drift";

export class HyperdriveSdkError extends DriftError {
constructor(message: string, options?: ErrorOptions) {
super(message, {
...options,
prefix: "ᛋ ",
name: "Hyperdrive SDK Error",
});
export class HyperdriveSdkError extends Error {
constructor(...[message, options]: Parameters<ErrorConstructor>) {
super(message, options);
this.name = "HyperdriveSdkError";
}
}
27 changes: 27 additions & 0 deletions packages/hyperdrive-js-core/src/evm-client/contractFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {
CachedReadContract,
CachedReadWriteContract,
SimpleCache,
} from "@delvtech/evm-client";
import { Abi } from "abitype";

export interface ContractFactoryOptions<TAbi extends Abi = Abi> {
abi: TAbi;
address: `0x${string}`;
cache?: SimpleCache;
namespace?: string;
}

/**
* A factory function that creates a `CachedReadContract` instance.
*/
export type ReadContractFactory = <TAbi extends Abi = Abi>(
options: ContractFactoryOptions<TAbi>,
) => CachedReadContract<TAbi>;

/**
* A factory function that creates a `CachedReadWriteContract` instance.
*/
export type ReadWriteContractFactory = <TAbi extends Abi = Abi>(
options: ContractFactoryOptions<TAbi>,
) => CachedReadWriteContract<TAbi>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import {
CachedReadWriteContract,
ContractReadOptions,
FunctionArgs,
FunctionName,
} from "@delvtech/evm-client";
import { Abi } from "abitype";

/**
* Clears the cache and calls the transaction handlers provided to a "Write"
* method on any CachedReadWriteContract class.
*
* This decorator accepts an argument of cache keys to clear. By default it will
* clear the entire cache.
*
* @example
* ```ts
* class ReadWriteFooBar extends CachedReadWriteContract {
*
* // Listen for tx complete and clear the entire cache
* @syncCacheWithTransaction()
* setFoo() {
* return this.contract.write("setFoo", []);
* }
*
* // Listen for tx complete and clear a partial or specific cache entry
* @syncCacheWithTransaction({ cacheEntries: [{ functionName: "getBar" }]})
* setBar() {
* return this.contract.write("setBar", []);
* }
* }
* ```
*
* @internal
*/
export function syncCacheWithTransaction<TAbi extends Abi>(options?: {
cacheEntries?: {
functionName?: FunctionName<TAbi>;
args?: FunctionArgs<TAbi, FunctionName<TAbi>>;
options?: ContractReadOptions;
}[];
}) {
return function (
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
target: any,
propertyKey: string,
descriptor: PropertyDescriptor,
): void {
const originalMethod = descriptor.value;

// Wrap the original method in a function that does the transaction
// side-effects we want after the tx completes
// eslint-disable-next-line @typescript-eslint/no-explicit-any
descriptor.value = async function (...args: any[]) {
// Access the target class instance from within a decorator
// @ts-expect-error The `this` keyword will be the target class instance
const network = this.network;
// @ts-expect-error The `this` keyword will be the target class instance
const contract = this.contract as CachedReadWriteContract;

// call the original function and await the hash
const hash = await originalMethod.apply(this, args);

// Dont await this part, we want it to happen in the background once the
// tx is completed
network.waitForTransaction(hash).then(() => {
if (options?.cacheEntries) {
options.cacheEntries.forEach((cacheKey) => {
return contract.deleteReadsMatching(
cacheKey.functionName,
cacheKey.args,
cacheKey.options,
);
});
} else {
contract.clearCache();
}
args[0]?.onTransactionCompleted?.(hash);
});

// Return the original method's result hash
return hash;
};
};
}
Loading

0 comments on commit b6141c7

Please sign in to comment.