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

Consolidate caching logic, reduce types, add more testing utils #89

Merged
merged 10 commits into from
Nov 29, 2024
5 changes: 5 additions & 0 deletions .changeset/few-otters-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@delvtech/drift": patch
---

Added a `BaseClient` class which contains most of the functionality that used to live in the `Drift` class and centralized logic for cache operations and namespace resolution, using the chain id as a default. This also removes a circular dependency between `Drift` and `Contract`.
5 changes: 5 additions & 0 deletions .changeset/five-readers-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@delvtech/drift": patch
---

Replaced the `createClientCache` function with a `ClientCache` class which requires a namespace or namespace resolver. All methods are async to allow for dynamic namespace resolution and external cache implementations.
5 changes: 5 additions & 0 deletions .changeset/fluffy-spiders-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@delvtech/drift": patch
---

Renamed `ContractEvent` to `EventLog` to be more consistent with other adapter types like `FunctionArgs`.
5 changes: 5 additions & 0 deletions .changeset/heavy-keys-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@delvtech/drift": patch
---

Refactored `Drift` and `MockDrift` to extend `BaseClient` and `MockClient`.
5 changes: 5 additions & 0 deletions .changeset/hot-cooks-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@delvtech/drift": patch
---

Renamed constructor `Param` types to `Config`.
5 changes: 5 additions & 0 deletions .changeset/lazy-eels-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@delvtech/drift": patch
---

Added new testing utils: `getRandomInt`, `getRandomHex`, `getRandomAddress`, `createStubBlock`, `createStubTransaction`, `createStubTransactionReceipt`
5 changes: 5 additions & 0 deletions .changeset/moody-carrots-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@delvtech/drift": patch
---

Added a `MockClient` class which mimics `BaseClient`.
5 changes: 5 additions & 0 deletions .changeset/moody-tigers-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@delvtech/drift": patch
---

Removed `isReadWriteAdapter` util which simply checked if the `write` property was a function.
5 changes: 5 additions & 0 deletions .changeset/odd-spies-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@delvtech/drift": patch
---

Removed `Adapter` and `Network` prefix from param types, e.g. `AdapterReadParams` to just `ReadParams`, `NetworkGetBlockParams` to just `GetBlockParams`. Removed redundant types.
5 changes: 5 additions & 0 deletions .changeset/rare-scissors-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@delvtech/drift": patch
---

Replaced the `createLruSimpleCache` function with a `LruSimpleCache` class.
5 changes: 5 additions & 0 deletions .changeset/tidy-shirts-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@delvtech/drift": patch
---

Refactored `Contract` to go through a `BaseClient` instead of repeating cache and adapter logic.
5 changes: 5 additions & 0 deletions .changeset/warm-spoons-applaud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@delvtech/drift": patch
---

Patched a bug with the built-in `OxAdapter` in which `onMined` was never called after write operations.
2 changes: 0 additions & 2 deletions apps/example/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,3 @@ const balance = await contract.read("balanceOf", {
});

console.log("Balance:", balance);


4 changes: 1 addition & 3 deletions packages/drift-ethers-v5/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,5 @@
"publishConfig": {
"access": "public"
},
"files": [
"dist"
]
"files": ["dist"]
}
6 changes: 3 additions & 3 deletions packages/drift-ethers-v5/src/EthersReadAdapter.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type {
Address,
Block,
ContractEvent,
DecodedFunctionData,
EventLog,
FunctionArgs,
Hash,
Transaction,
Expand Down Expand Up @@ -66,7 +66,7 @@ describe("EthersReadAdapter", () => {
gasPrice: expect.any(BigInt),
input: expect.any(String),
nonce: expect.any(BigInt),
type: expect.any(String) || undefined,
type: expect.any(String || undefined),
value: expect.any(BigInt),
blockHash: expect.any(String),
blockNumber: expect.any(BigInt),
Expand Down Expand Up @@ -121,7 +121,7 @@ describe("EthersReadAdapter", () => {
blockNumber: expect.any(BigInt),
data: expect.any(String),
transactionHash: expect.any(String),
} as ContractEvent<typeof erc20.abi, "Transfer">),
} as EventLog<typeof erc20.abi, "Transfer">),
);
});

Expand Down
44 changes: 20 additions & 24 deletions packages/drift-ethers-v5/src/EthersReadAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import {
type AdapterDecodeFunctionDataParams,
type AdapterEncodeFunctionDataParams,
type AdapterGetEventsParams,
type AdapterReadParams,
type AdapterWriteParams,
type Block,
type ContractEvent,
type DecodeFunctionDataParams,
type DecodedFunctionData,
DriftError,
type EncodeFunctionDataParams,
type EventLog,
type EventName,
type FunctionName,
type NetworkGetBalanceParams,
type NetworkGetBlockParams,
type NetworkGetTransactionParams,
type NetworkWaitForTransactionParams,
type GetBalanceParams,
type GetBlockParams,
type GetEventsParams,
type GetTransactionParams,
type ReadAdapter,
type ReadParams,
type Transaction,
type TransactionReceipt,
type WaitForTransactionParams,
type WriteParams,
arrayToObject,
convertType,
objectToArray,
Expand Down Expand Up @@ -67,11 +67,7 @@ export class EthersReadAdapter<TProvider extends Provider = Provider>
return BigInt(num);
}

async getBlock({
blockHash,
blockNumber,
blockTag,
}: NetworkGetBlockParams = {}) {
async getBlock({ blockHash, blockNumber, blockTag }: GetBlockParams = {}) {
const ethersBlock = await this.provider.getBlock(
blockParam(blockHash ?? blockNumber ?? blockTag ?? "latest"),
);
Expand Down Expand Up @@ -99,12 +95,12 @@ export class EthersReadAdapter<TProvider extends Provider = Provider>
return block;
}

async getBalance({ address, block }: NetworkGetBalanceParams) {
async getBalance({ address, block }: GetBalanceParams) {
const balance = await this.provider.getBalance(address, blockParam(block));
return balance.toBigInt();
}

async getTransaction({ hash }: NetworkGetTransactionParams) {
async getTransaction({ hash }: GetTransactionParams) {
const ethersTx = await this.provider.getTransaction(hash);
const tx: Transaction | undefined = ethersTx
? {
Expand All @@ -129,7 +125,7 @@ export class EthersReadAdapter<TProvider extends Provider = Provider>
return tx;
}

async waitForTransaction({ hash, timeout }: NetworkWaitForTransactionParams) {
async waitForTransaction({ hash, timeout }: WaitForTransactionParams) {
const ethersReceipt = await this.provider.waitForTransaction(
hash,
undefined,
Expand Down Expand Up @@ -160,7 +156,7 @@ export class EthersReadAdapter<TProvider extends Provider = Provider>
filter,
fromBlock,
toBlock,
}: AdapterGetEventsParams<TAbi, TEventName>) {
}: GetEventsParams<TAbi, TEventName>) {
const contract = new Contract(address, abi as EthersAbi, this.provider);

let eventFilter: string | EventFilter = eventName;
Expand All @@ -182,7 +178,7 @@ export class EthersReadAdapter<TProvider extends Provider = Provider>
);

return events.map((ethersEvent) => {
const event: ContractEvent<TAbi, TEventName> = {
const event: EventLog<TAbi, TEventName> = {
args: arrayToObject({
abi: abi as Abi,
kind: "inputs",
Expand All @@ -203,7 +199,7 @@ export class EthersReadAdapter<TProvider extends Provider = Provider>
async read<
TAbi extends Abi,
TFunctionName extends FunctionName<TAbi, "pure" | "view">,
>({ abi, address, fn, args, block }: AdapterReadParams<TAbi, TFunctionName>) {
>({ abi, address, fn, args, block }: ReadParams<TAbi, TFunctionName>) {
const contract = new Contract(address, abi as EthersAbi, this.provider);

const argsArray = objectToArray({
Expand All @@ -224,7 +220,7 @@ export class EthersReadAdapter<TProvider extends Provider = Provider>
async simulateWrite<
TAbi extends Abi,
TFunctionName extends FunctionName<TAbi, "nonpayable" | "payable">,
>(params: AdapterWriteParams<TAbi, TFunctionName>) {
>(params: WriteParams<TAbi, TFunctionName>) {
const { abi, address, args, fn, onMined, ...options } = params;
const contract = new Contract(address, abi as EthersAbi, this.provider);

Expand Down Expand Up @@ -253,7 +249,7 @@ export class EthersReadAdapter<TProvider extends Provider = Provider>
encodeFunctionData<
TAbi extends Abi,
TFunctionName extends FunctionName<TAbi>,
>({ abi, fn, args }: AdapterEncodeFunctionDataParams<TAbi, TFunctionName>) {
>({ abi, fn, args }: EncodeFunctionDataParams<TAbi, TFunctionName>) {
const iface = new Interface(abi as EthersAbi);

const arrayArgs = objectToArray({
Expand All @@ -270,7 +266,7 @@ export class EthersReadAdapter<TProvider extends Provider = Provider>
decodeFunctionData<
TAbi extends Abi,
TFunctionName extends FunctionName<TAbi>,
>({ abi, data, fn }: AdapterDecodeFunctionDataParams<TAbi, TFunctionName>) {
>({ abi, data, fn }: DecodeFunctionDataParams<TAbi, TFunctionName>) {
const iface = new Interface(abi as EthersAbi);
const { args, name } = iface.parseTransaction({ data }) || {};

Expand Down
8 changes: 4 additions & 4 deletions packages/drift-ethers-v5/src/EthersReadWriteAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {
type AdapterWriteParams,
type Address,
type FunctionName,
type Hash,
type HexString,
type ReadWriteAdapter,
type TransactionReceipt,
type WriteParams,
objectToArray,
} from "@delvtech/drift";
import type { Abi } from "abitype";
Expand Down Expand Up @@ -48,17 +48,17 @@ export class EthersReadWriteAdapter<
async simulateWrite<
TAbi extends Abi,
TFunctionName extends FunctionName<TAbi, "nonpayable" | "payable">,
>(params: AdapterWriteParams<TAbi, TFunctionName>) {
>(params: WriteParams<TAbi, TFunctionName>) {
return super.simulateWrite({
...params,
from: params.from ?? (await this.signer.getAddress()),
} as AdapterWriteParams<TAbi, TFunctionName>);
} as WriteParams<TAbi, TFunctionName>);
}

async write<
TAbi extends Abi,
TFunctionName extends FunctionName<TAbi, "nonpayable" | "payable">,
>(params: AdapterWriteParams<TAbi, TFunctionName>) {
>(params: WriteParams<TAbi, TFunctionName>) {
const {
abi,
address,
Expand Down
4 changes: 1 addition & 3 deletions packages/drift-ethers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,5 @@
"publishConfig": {
"access": "public"
},
"files": [
"dist"
]
"files": ["dist"]
}
4 changes: 2 additions & 2 deletions packages/drift-ethers/src/EthersReadAdapter.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type {
Address,
Block,
ContractEvent,
DecodedFunctionData,
EventLog,
FunctionArgs,
Hash,
TransactionReceipt,
Expand Down Expand Up @@ -119,7 +119,7 @@ describe("EthersReadAdapter", () => {
blockNumber: expect.any(BigInt),
data: expect.any(String),
transactionHash: expect.any(String),
} as ContractEvent<typeof erc20.abi, "Transfer">),
} as EventLog<typeof erc20.abi, "Transfer">),
);
});

Expand Down
Loading