Skip to content

Commit

Permalink
Refactor clients to remove circular dep, cleanup types
Browse files Browse the repository at this point in the history
  • Loading branch information
ryangoree committed Nov 29, 2024
1 parent a8a4f5f commit 01a8004
Show file tree
Hide file tree
Showing 38 changed files with 1,982 additions and 1,810 deletions.
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/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/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.
42 changes: 21 additions & 21 deletions packages/drift/src/adapter/MockAdapter.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { MockAdapter } from "src/adapter/MockAdapter";
import type {
AdapterDecodeFunctionDataParams,
AdapterEncodeFunctionDataParams,
AdapterGetEventsParams,
AdapterReadParams,
AdapterWriteParams,
DecodeFunctionDataParams,
EncodeFunctionDataParams,
GetEventsParams,
ReadParams,
WriteParams,
} from "src/adapter/types/Adapter";
import type { Block } from "src/adapter/types/Block";
import type { ContractEvent } from "src/adapter/types/Event";
import type { EventLog } from "src/adapter/types/Event";
import type { DecodedFunctionData } from "src/adapter/types/Function";
import type {
Transaction,
Expand Down Expand Up @@ -281,12 +281,12 @@ describe("MockAdapter", () => {

it("Can be stubbed with specific params", async () => {
const adapter = new MockAdapter();
const params1: AdapterEncodeFunctionDataParams<Erc20Abi, "balanceOf"> = {
const params1: EncodeFunctionDataParams<Erc20Abi, "balanceOf"> = {
abi: erc20.abi,
fn: "balanceOf",
args: { account: "0x1" },
};
const params2: AdapterEncodeFunctionDataParams<Erc20Abi, "balanceOf"> = {
const params2: EncodeFunctionDataParams<Erc20Abi, "balanceOf"> = {
...params1,
args: { account: "0x2" },
};
Expand Down Expand Up @@ -344,7 +344,7 @@ describe("MockAdapter", () => {

it("Can be stubbed with specific params", async () => {
const adapter = new MockAdapter();
const params1: AdapterDecodeFunctionDataParams<Erc20Abi, "balanceOf"> = {
const params1: DecodeFunctionDataParams<Erc20Abi, "balanceOf"> = {
abi: erc20.abi,
fn: "balanceOf",
data: "0x1",
Expand All @@ -353,7 +353,7 @@ describe("MockAdapter", () => {
functionName: "balanceOf",
args: { account: "0x1" },
};
const params2: AdapterDecodeFunctionDataParams<Erc20Abi, "balanceOf"> = {
const params2: DecodeFunctionDataParams<Erc20Abi, "balanceOf"> = {
...params1,
data: "0x2",
};
Expand Down Expand Up @@ -407,17 +407,17 @@ describe("MockAdapter", () => {

it("Can be stubbed with specific params", async () => {
const adapter = new MockAdapter();
const params1: AdapterGetEventsParams<Erc20Abi, "Transfer"> = {
const params1: GetEventsParams<Erc20Abi, "Transfer"> = {
abi: erc20.abi,
address: "0x1",
event: "Transfer",
filter: { from: "0x1" },
};
const params2: AdapterGetEventsParams<Erc20Abi, "Transfer"> = {
const params2: GetEventsParams<Erc20Abi, "Transfer"> = {
...params1,
filter: { from: "0x2" },
};
const events1: ContractEvent<Erc20Abi, "Transfer">[] = [
const events1: EventLog<Erc20Abi, "Transfer">[] = [
{
eventName: "Transfer",
args: {
Expand All @@ -427,7 +427,7 @@ describe("MockAdapter", () => {
},
},
];
const events2: ContractEvent<Erc20Abi, "Transfer">[] = [
const events2: EventLog<Erc20Abi, "Transfer">[] = [
{
eventName: "Transfer",
args: {
Expand All @@ -445,7 +445,7 @@ describe("MockAdapter", () => {

it("Can be stubbed with partial params", async () => {
const adapter = new MockAdapter();
const events: ContractEvent<Erc20Abi, "Transfer">[] = [
const events: EventLog<Erc20Abi, "Transfer">[] = [
{
eventName: "Transfer",
args: {
Expand Down Expand Up @@ -490,13 +490,13 @@ describe("MockAdapter", () => {

it("Can be stubbed with specific params", async () => {
const adapter = new MockAdapter();
const params1: AdapterReadParams<Erc20Abi, "allowance"> = {
const params1: ReadParams<Erc20Abi, "allowance"> = {
abi: erc20.abi,
address: "0x1",
fn: "allowance",
args: { owner: "0x1", spender: "0x1" },
};
const params2: AdapterReadParams<Erc20Abi, "allowance"> = {
const params2: ReadParams<Erc20Abi, "allowance"> = {
...params1,
args: { owner: "0x2", spender: "0x2" },
};
Expand Down Expand Up @@ -544,13 +544,13 @@ describe("MockAdapter", () => {

it("Can be stubbed with specific args", async () => {
const adapter = new MockAdapter();
const params1: AdapterWriteParams<Erc20Abi, "transfer"> = {
const params1: WriteParams<Erc20Abi, "transfer"> = {
abi: erc20.abi,
address: "0x1",
fn: "transfer",
args: { to: "0x1", amount: 123n },
};
const params2: AdapterWriteParams<Erc20Abi, "transfer"> = {
const params2: WriteParams<Erc20Abi, "transfer"> = {
...params1,
args: { to: "0x2", amount: 123n },
};
Expand Down Expand Up @@ -598,13 +598,13 @@ describe("MockAdapter", () => {

it("Can be stubbed with specific args", async () => {
const adapter = new MockAdapter();
const params1: AdapterWriteParams<Erc20Abi, "transfer"> = {
const params1: WriteParams<Erc20Abi, "transfer"> = {
abi: erc20.abi,
address: "0x",
fn: "transfer",
args: { to: "0x1", amount: 123n },
};
const params2: AdapterWriteParams<Erc20Abi, "transfer"> = {
const params2: WriteParams<Erc20Abi, "transfer"> = {
...params1,
args: { to: "0x2", amount: 123n },
};
Expand Down
Loading

0 comments on commit 01a8004

Please sign in to comment.