From 52d1a3bc6e300ae24337b6d823a2aaf1c60e20f8 Mon Sep 17 00:00:00 2001 From: Ryan Goree Date: Tue, 1 Oct 2024 14:13:15 -0500 Subject: [PATCH] Add `getSignerAddress` to `Drift` client --- .../drift/src/adapter/MockAdapter.test.ts | 6 +++++ packages/drift/src/adapter/MockAdapter.ts | 1 + packages/drift/src/adapter/types.ts | 13 ++++++---- packages/drift/src/drift/Drift.ts | 26 ++++++++++++++++--- 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/packages/drift/src/adapter/MockAdapter.test.ts b/packages/drift/src/adapter/MockAdapter.test.ts index eb4e6d23..24e1255d 100644 --- a/packages/drift/src/adapter/MockAdapter.test.ts +++ b/packages/drift/src/adapter/MockAdapter.test.ts @@ -16,6 +16,12 @@ describe("MockAdapter", () => { expect(block).toBe(blockStub); }); + it("Stubs the signer address", async () => { + const adapter = new MockAdapter(); + const signer = await adapter.getSignerAddress(); + expect(signer).toBeTypeOf("string"); + }); + it("Creates mock read contracts", async () => { const mockAdapter = new MockAdapter(); const contract = mockAdapter.readContract(IERC20.abi); diff --git a/packages/drift/src/adapter/MockAdapter.ts b/packages/drift/src/adapter/MockAdapter.ts index 9b66f68e..f808cb2b 100644 --- a/packages/drift/src/adapter/MockAdapter.ts +++ b/packages/drift/src/adapter/MockAdapter.ts @@ -6,6 +6,7 @@ import type { ReadWriteAdapter } from "src/adapter/types"; export class MockAdapter implements ReadWriteAdapter { network = new MockNetwork(); + getSignerAddress = async () => "0xMockSigner"; readContract = (abi: TAbi) => new ReadContractStub(abi); readWriteContract = (abi: TAbi) => new ReadWriteContractStub(abi); diff --git a/packages/drift/src/adapter/types.ts b/packages/drift/src/adapter/types.ts index 5fb9d428..986e6ab9 100644 --- a/packages/drift/src/adapter/types.ts +++ b/packages/drift/src/adapter/types.ts @@ -4,19 +4,22 @@ import type { AdapterReadWriteContract, } from "src/adapter/contract/types/Contract"; import type { AdapterNetwork } from "src/adapter/network/AdapterNetwork"; -import type { RequiredKeys } from "src/utils/types"; -export interface Adapter { +export interface ReadAdapter { network: AdapterNetwork; readContract: ( abi: TAbi, address: string, ) => AdapterReadContract; - readWriteContract?: ( +} + +export interface ReadWriteAdapter extends ReadAdapter { + // Write-only properties + getSignerAddress: () => Promise; + readWriteContract: ( abi: TAbi, address: string, ) => AdapterReadWriteContract; } -export type ReadAdapter = Omit; -export type ReadWriteAdapter = RequiredKeys; +export type Adapter = ReadAdapter | ReadWriteAdapter; diff --git a/packages/drift/src/drift/Drift.ts b/packages/drift/src/drift/Drift.ts index 281d35da..6e87cdbe 100644 --- a/packages/drift/src/drift/Drift.ts +++ b/packages/drift/src/drift/Drift.ts @@ -40,8 +40,8 @@ export interface DriftOptions { namespace?: string; } -// This is the one place where the Read/ReadWrite distinction is skipped in -// favor of a unified entrypoint to the Drift API. +// This is the one place where the Read/ReadWrite concepts are combined into a +// single class in favor of a unified entrypoint to the Drift API. export class Drift< TAdapter extends Adapter = Adapter, TCache extends SimpleCache = SimpleCache, @@ -49,6 +49,13 @@ export class Drift< readonly adapter: TAdapter; cache: DriftCache; namespace?: string; + + // Write-only property definitions // + + getSignerAddress: TAdapter extends ReadWriteAdapter + ? () => Promise + : undefined; + write: TAdapter extends ReadWriteAdapter ? < TAbi extends Abi, @@ -58,6 +65,8 @@ export class Drift< ) => Promise : undefined; + // Implementation // + constructor( adapter: TAdapter, { cache, namespace }: DriftOptions = {}, @@ -65,7 +74,16 @@ export class Drift< this.adapter = adapter; this.cache = createDriftCache(cache); this.namespace = namespace; - this.write = this.isReadWrite() + + // Write-only property assignment // + + const isReadWrite = this.isReadWrite(); + + this.getSignerAddress = isReadWrite + ? () => this.adapter.getSignerAddress() + : (undefined as any); + + this.write = isReadWrite ? async ({ abi, address, fn, args, onMined, ...writeOptions }) => { if (isReadWriteAdapter(this.adapter)) { const txHash = await createCachedReadWriteContract({ @@ -83,7 +101,7 @@ export class Drift< : (undefined as any); } - isReadWrite = (): this is Drift => + isReadWrite = (): this is Drift => isReadWriteAdapter(this.adapter); contract = ({