Skip to content

Commit

Permalink
Add getSignerAddress to Drift client
Browse files Browse the repository at this point in the history
  • Loading branch information
ryangoree committed Oct 1, 2024
1 parent 6cd5165 commit 52d1a3b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
6 changes: 6 additions & 0 deletions packages/drift/src/adapter/MockAdapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions packages/drift/src/adapter/MockAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { ReadWriteAdapter } from "src/adapter/types";

export class MockAdapter implements ReadWriteAdapter {
network = new MockNetwork();
getSignerAddress = async () => "0xMockSigner";
readContract = <TAbi extends Abi>(abi: TAbi) => new ReadContractStub(abi);
readWriteContract = <TAbi extends Abi>(abi: TAbi) =>
new ReadWriteContractStub(abi);
Expand Down
13 changes: 8 additions & 5 deletions packages/drift/src/adapter/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: <TAbi extends Abi>(
abi: TAbi,
address: string,
) => AdapterReadContract<TAbi>;
readWriteContract?: <TAbi extends Abi>(
}

export interface ReadWriteAdapter extends ReadAdapter {
// Write-only properties
getSignerAddress: () => Promise<string>;
readWriteContract: <TAbi extends Abi>(
abi: TAbi,
address: string,
) => AdapterReadWriteContract<TAbi>;
}

export type ReadAdapter = Omit<Adapter, "readWriteContract">;
export type ReadWriteAdapter = RequiredKeys<Adapter, "readWriteContract">;
export type Adapter = ReadAdapter | ReadWriteAdapter;
26 changes: 22 additions & 4 deletions packages/drift/src/drift/Drift.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,22 @@ export interface DriftOptions<TCache extends SimpleCache = SimpleCache> {
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,
> {
readonly adapter: TAdapter;
cache: DriftCache<TCache>;
namespace?: string;

// Write-only property definitions //

getSignerAddress: TAdapter extends ReadWriteAdapter
? () => Promise<string>
: undefined;

write: TAdapter extends ReadWriteAdapter
? <
TAbi extends Abi,
Expand All @@ -58,14 +65,25 @@ export class Drift<
) => Promise<string>
: undefined;

// Implementation //

constructor(
adapter: TAdapter,
{ cache, namespace }: DriftOptions<TCache> = {},
) {
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({
Expand All @@ -83,7 +101,7 @@ export class Drift<
: (undefined as any);
}

isReadWrite = (): this is Drift<ReadWriteAdapter, TCache> =>
isReadWrite = (): this is Drift<TAdapter & ReadWriteAdapter, TCache> =>
isReadWriteAdapter(this.adapter);

contract = <TAbi extends Abi>({
Expand Down

0 comments on commit 52d1a3b

Please sign in to comment.