Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
22 changes: 4 additions & 18 deletions src/drips-client.ts → src/contracts/drips-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import type {
ExtractAbiFunction,
ExtractAbiFunctionNames,
} from 'abitype';
import {dripsAbi, type DripsAbi} from './drips-abi';
import {Contract, TransactionResponse} from 'ethers';
import appSettings from './appSettings';
import {getContractRunner} from './getWalletInstance';
import appSettings from '../appSettings';
import {getContractRunner} from '../getWalletInstance';
import {dripsAbi, DripsAbi} from './drips-abi';
import {unwrapEthersResult, UnwrappedEthersResult} from './unwrapEthersResult';

const {
network: {
Expand Down Expand Up @@ -89,18 +90,3 @@ export async function dripsWriteContract<
);
}
}

export function unwrapEthersResult<T>(
result: T | T[],
): UnwrappedEthersResult<T> | UnwrappedEthersResult<T[]> {
if (Array.isArray(result) && result.length === 1) {
return result[0] as UnwrappedEthersResult<T>;
}
return result as UnwrappedEthersResult<T[]>;
}

export type UnwrappedEthersResult<T> = T extends [infer U]
? U
: T extends readonly [infer U]
? U
: T;
78 changes: 78 additions & 0 deletions src/contracts/repoSubAccountDriver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import type {
AbiFunction,
AbiParametersToPrimitiveTypes,
ExtractAbiFunction,
ExtractAbiFunctionNames,
} from 'abitype';
import {Contract} from 'ethers';
import appSettings from '../appSettings';
import {getContractRunner} from '../getWalletInstance';
import {unwrapEthersResult, UnwrappedEthersResult} from './unwrapEthersResult';
import {
repoSubAccountDriverAbi,
RepoSubAccountDriverAbi,
} from './repoSubAccountDriverAbi';

const {
network: {
contracts: {repoSubAccountDriver: contractAddress},
name: networkName,
},
} = appSettings;

let contractInstance: Contract | null = null;

async function getRepoSubAccountContract(): Promise<Contract> {
if (contractInstance) {
return contractInstance;
}

if (!contractAddress) {
throw new Error(`No contract address configured for chain: ${networkName}`);
}

try {
contractInstance = new Contract(
contractAddress,
repoSubAccountDriverAbi,
await getContractRunner(),
);
return contractInstance;
} catch (error) {
throw new Error(
`Failed to initialize contract: ${error instanceof Error ? error.message : 'Unknown error'}`,
);
}
}

export async function repoSubAccountReadContract<
functionName extends ExtractAbiFunctionNames<
RepoSubAccountDriverAbi,
'pure' | 'view'
>,
abiFunction extends AbiFunction = ExtractAbiFunction<
RepoSubAccountDriverAbi,
functionName
>,
>(config: {
functionName:
| functionName
| ExtractAbiFunctionNames<RepoSubAccountDriverAbi, 'pure' | 'view'>;
args: AbiParametersToPrimitiveTypes<abiFunction['inputs'], 'inputs'>;
}): Promise<
UnwrappedEthersResult<
AbiParametersToPrimitiveTypes<abiFunction['outputs'], 'outputs'>
>
> {
try {
const repoSubAccount = await getRepoSubAccountContract();
const {functionName: func, args} = config;
const result = await repoSubAccount[func](...args);

return unwrapEthersResult(result);
} catch (error: any) {
throw new Error(
`Read operation '${config.functionName}' failed: ${error.message}`,
);
}
}
Loading