forked from Cookie-Jar-DAO/cookie-jar-v3
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseContractAdapter.ts
More file actions
97 lines (93 loc) · 3.06 KB
/
useContractAdapter.ts
File metadata and controls
97 lines (93 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"use client";
import { useChainId } from "wagmi";
import { isV2Chain } from "@/config/supported-networks";
import { cookieJarAbi, cookieJarFactoryAbi } from "@/generated";
import {
cookieJarFactoryV1Abi,
cookieJarV1Abi,
} from "@/lib/blockchain/cookie-jar-v1-abi";
/**
* Contract configuration adapter for handling v1/v2 differences
*/
export interface ContractAdapter {
/** ABI to use for Cookie Jar contract */
cookieJarAbi: typeof cookieJarAbi | typeof cookieJarV1Abi;
/** ABI to use for Cookie Jar Factory contract */
cookieJarFactoryAbi:
| typeof cookieJarFactoryAbi
| typeof cookieJarFactoryV1Abi;
/** Function name mappings for v1/v2 differences */
functionNames: {
/** Allowlist/whitelist function names */
getAllowlist: "getAllowlist" | "getWhitelist";
grantAllowlist: "grantJarAllowlistRole" | "grantJarWhitelistRole";
revokeAllowlist: "revokeJarAllowlistRole" | "revokeJarWhitelistRole";
/** Withdrawal function names */
withdrawAllowlistMode: "withdrawAllowlistMode" | "withdrawWhitelistMode";
/** Role names */
allowlistRole: "JAR_ALLOWLISTED" | "JAR_WHITELISTED";
denylistRole: "JAR_DENYLISTED" | "JAR_BLACKLISTED";
/** Last withdrawal tracking */
lastWithdrawalAllowlist:
| "lastWithdrawalAllowlist"
| "lastWithdrawalWhitelist";
};
/** Whether this is a v2 contract (supports advanced features) */
isV2: boolean;
/** Chain ID for this contract */
chainId: number;
}
/**
* Custom hook to adapt contract interfaces based on v1/v2 differences
*
* Centralizes the logic for handling version differences between Cookie Jar
* v1 and v2 contracts. Automatically detects the version and provides the
* appropriate ABIs and function names.
*
* @param chainIdOverride - Optional chain ID override (uses current chain by default)
* @returns Contract adapter with version-specific configuration
*
* @example
* ```tsx
* const adapter = useContractAdapter();
*
* // Use version-aware function names
* const { data } = useReadContract({
* address: jarAddress,
* abi: adapter.cookieJarAbi,
* functionName: adapter.functionNames.getAllowlist,
* });
*
* // Check if v2 features are supported
* if (adapter.isV2) {
* // Use v2-specific features
* }
* ```
*/
export const useContractAdapter = (
chainIdOverride?: number,
): ContractAdapter => {
const chainId = chainIdOverride ?? useChainId();
const isV2 = isV2Chain(chainId);
return {
cookieJarAbi: isV2 ? cookieJarAbi : cookieJarV1Abi,
cookieJarFactoryAbi: isV2 ? cookieJarFactoryAbi : cookieJarFactoryV1Abi,
functionNames: {
getAllowlist: isV2 ? "getAllowlist" : "getWhitelist",
grantAllowlist: isV2 ? "grantJarAllowlistRole" : "grantJarWhitelistRole",
revokeAllowlist: isV2
? "revokeJarAllowlistRole"
: "revokeJarWhitelistRole",
withdrawAllowlistMode: isV2
? "withdrawAllowlistMode"
: "withdrawWhitelistMode",
allowlistRole: isV2 ? "JAR_ALLOWLISTED" : "JAR_WHITELISTED",
denylistRole: isV2 ? "JAR_DENYLISTED" : "JAR_BLACKLISTED",
lastWithdrawalAllowlist: isV2
? "lastWithdrawalAllowlist"
: "lastWithdrawalWhitelist",
},
isV2,
chainId,
};
};