forked from Cookie-Jar-DAO/cookie-jar-v3
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateV2CreateArgs.ts
More file actions
367 lines (335 loc) · 10.2 KB
/
createV2CreateArgs.ts
File metadata and controls
367 lines (335 loc) · 10.2 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import type { cookieJarFactoryAbi } from "@/generated";
import { isAddress, type ContractFunctionArgs } from "viem";
import {
HATS_PROTOCOL_ADDRESS,
POAP_TOKEN_ADDRESS,
ZERO_ADDRESS,
} from "@/lib/blockchain/constants";
import { AccessType, NFTType, type JarCreationFormData } from "./schemas/jarCreationSchema";
export const FACTORY_DEFAULT_FEE_SENTINEL = (2n ** 256n) - 1n;
export enum ContractAccessType {
Allowlist = 0,
ERC721 = 1,
ERC1155 = 2,
}
export type FactoryCreateCookieJarArgs = ContractFunctionArgs<
typeof cookieJarFactoryAbi,
"nonpayable",
"createCookieJar"
>;
export type JarConfigArg = FactoryCreateCookieJarArgs[0];
export type AccessConfigArg = FactoryCreateCookieJarArgs[1];
export type MultiTokenConfigArg = FactoryCreateCookieJarArgs[2];
function toAddressOrZero(value: unknown): `0x${string}` {
return typeof value === "string" && isAddress(value)
? (value as `0x${string}`)
: (ZERO_ADDRESS as `0x${string}`);
}
function toBigIntOr(defaultValue: bigint, value: unknown): bigint {
if (typeof value === "bigint") return value;
if (typeof value === "number" && Number.isFinite(value)) return BigInt(value);
if (typeof value === "string" && value.trim().length > 0) {
try {
return BigInt(value);
} catch {
return defaultValue;
}
}
return defaultValue;
}
function isMissingValue(value: unknown): boolean {
return (
value === undefined ||
value === null ||
(typeof value === "string" && value.trim().length === 0)
);
}
function toBigIntStrict(value: unknown): bigint | null {
if (typeof value === "bigint") return value;
if (typeof value === "number" && Number.isFinite(value)) {
return BigInt(Math.trunc(value));
}
if (typeof value === "string" && value.trim().length > 0) {
try {
return BigInt(value.trim());
} catch {
return null;
}
}
return null;
}
function protocolValue(
protocolConfig: JarCreationFormData["protocolConfig"],
keys: string[],
): unknown {
const raw = protocolConfig as unknown as Record<string, unknown>;
for (const key of keys) {
if (raw[key] !== undefined && raw[key] !== null && raw[key] !== "") {
return raw[key];
}
}
return undefined;
}
export function getFeePercentageOnDeposit(values: JarCreationFormData): bigint {
if (!values.enableCustomFee) {
return FACTORY_DEFAULT_FEE_SENTINEL;
}
const customFeePercent = values.customFee?.trim();
if (!customFeePercent) {
return FACTORY_DEFAULT_FEE_SENTINEL;
}
const feeBps = Math.round(Number.parseFloat(customFeePercent) * 100);
return BigInt(Number.isFinite(feeBps) ? Math.max(0, feeBps) : 0);
}
function resolveAccessConfig(values: JarCreationFormData): {
accessType: ContractAccessType;
nftRequirement: AccessConfigArg["nftRequirement"];
} {
const protocolConfig = values.protocolConfig;
if (values.accessType === AccessType.Allowlist) {
return {
accessType: ContractAccessType.Allowlist,
nftRequirement: {
nftContract: ZERO_ADDRESS as `0x${string}`,
tokenId: 0n,
minBalance: 0n,
isPoapEventGate: false,
},
};
}
if (values.accessType === AccessType.NFTGated) {
const nftAddress = toAddressOrZero(values.nftAddresses[0]);
const nftType = values.nftTypes[0];
return {
accessType:
nftType === NFTType.ERC1155
? ContractAccessType.ERC1155
: ContractAccessType.ERC721,
nftRequirement: {
nftContract: nftAddress,
tokenId: 0n,
minBalance: nftType === NFTType.ERC1155 ? 1n : 0n,
isPoapEventGate: false,
},
};
}
if (values.accessType === AccessType.POAP) {
const eventId = toBigIntOr(
0n,
protocolValue(protocolConfig, ["poapEventId", "eventId"]),
);
return {
accessType: ContractAccessType.ERC721,
nftRequirement: {
nftContract: toAddressOrZero(
protocolValue(protocolConfig, ["poapContractAddress"]),
),
tokenId: eventId,
minBalance: 0n,
isPoapEventGate: true,
},
};
}
if (values.accessType === AccessType.Unlock) {
return {
accessType: ContractAccessType.ERC721,
nftRequirement: {
nftContract: toAddressOrZero(
protocolValue(protocolConfig, ["unlockAddress"]),
),
tokenId: 0n,
minBalance: 0n,
isPoapEventGate: false,
},
};
}
if (values.accessType === AccessType.Hypercert) {
return {
accessType: ContractAccessType.ERC1155,
nftRequirement: {
nftContract: toAddressOrZero(
protocolValue(protocolConfig, [
"hypercertAddress",
"hypercertContract",
]),
),
tokenId: toBigIntOr(
0n,
protocolValue(protocolConfig, [
"hypercertTokenId",
"hypercertId",
"tokenId",
]),
),
minBalance: toBigIntOr(
1n,
protocolValue(protocolConfig, ["hypercertMinBalance"]),
),
isPoapEventGate: false,
},
};
}
// Hats maps to ERC-1155: tokenId == hatId, minBalance == 1.
return {
accessType: ContractAccessType.ERC1155,
nftRequirement: {
nftContract: toAddressOrZero(
protocolValue(protocolConfig, ["hatsAddress", "hatsContract"]),
),
tokenId: toBigIntOr(0n, protocolValue(protocolConfig, ["hatsId", "hatId"])),
minBalance: 1n,
isPoapEventGate: false,
},
};
}
export function getAccessConfigValidationError(
values: JarCreationFormData,
): string | undefined {
const protocolConfig = values.protocolConfig;
if (values.accessType === AccessType.NFTGated) {
if (!values.nftAddresses.length) {
return "At least one NFT address is required for NFT-gated access";
}
if (values.nftAddresses.length !== values.nftTypes.length) {
return "NFT addresses and NFT types must have the same length";
}
if (!isAddress(values.nftAddresses[0])) {
return "NFT address must be a valid Ethereum address";
}
return undefined;
}
if (values.accessType === AccessType.POAP) {
const rawEventId = protocolValue(protocolConfig, ["poapEventId", "eventId"]);
if (isMissingValue(rawEventId)) {
return "POAP event is required";
}
const eventId = toBigIntStrict(rawEventId);
if (eventId === null || eventId <= 0n) {
return "POAP event must be a valid number";
}
const poapContractAddress = protocolValue(protocolConfig, [
"poapContractAddress",
]);
if (poapContractAddress !== undefined && !isAddress(String(poapContractAddress))) {
return "POAP contract address must be a valid Ethereum address";
}
return undefined;
}
if (values.accessType === AccessType.Unlock) {
const unlockAddress = protocolValue(protocolConfig, ["unlockAddress"]);
if (isMissingValue(unlockAddress)) {
return "Unlock contract address is required";
}
if (!isAddress(String(unlockAddress))) {
return "Unlock contract address must be a valid Ethereum address";
}
return undefined;
}
if (values.accessType === AccessType.Hypercert) {
const hypercertAddress = protocolValue(protocolConfig, [
"hypercertAddress",
"hypercertContract",
]);
if (!hypercertAddress || !isAddress(String(hypercertAddress))) {
return "Hypercert contract address is required";
}
const rawHypercertTokenId = protocolValue(protocolConfig, [
"hypercertTokenId",
"hypercertId",
"tokenId",
]);
if (isMissingValue(rawHypercertTokenId)) {
return "Hypercert token ID is required";
}
const hypercertTokenId = toBigIntStrict(rawHypercertTokenId);
if (hypercertTokenId === null || hypercertTokenId <= 0n) {
return "Hypercert token ID must be a valid number";
}
const hypercertMinBalance = protocolValue(protocolConfig, [
"hypercertMinBalance",
]);
if (!isMissingValue(hypercertMinBalance)) {
const parsedMinBalance = toBigIntStrict(hypercertMinBalance);
if (parsedMinBalance === null || parsedMinBalance <= 0n) {
return "Hypercert minimum balance must be a valid number";
}
}
return undefined;
}
if (values.accessType === AccessType.Hats) {
const rawHatsId = protocolValue(protocolConfig, ["hatsId", "hatId"]);
if (isMissingValue(rawHatsId)) {
return "Hat ID is required";
}
const hatsId = toBigIntStrict(rawHatsId);
if (hatsId === null || hatsId <= 0n) {
return "Hat ID must be a valid number";
}
return undefined;
}
return undefined;
}
export function buildV2CreateCookieJarArgs(input: {
values: JarCreationFormData;
metadata: string;
parseAmount: (amount: string) => bigint;
}): FactoryCreateCookieJarArgs {
const { values, metadata, parseAmount } = input;
const { accessType, nftRequirement } = resolveAccessConfig(values);
const feePercentageOnDeposit = getFeePercentageOnDeposit(values);
const protocolConfig = values.protocolConfig;
const multiTokenConfig: MultiTokenConfigArg = {
enabled: values.autoSwapEnabled,
maxSlippagePercent: 500n,
minSwapAmount: 0n,
defaultFee: 3000,
};
const poapContractCandidate = protocolValue(protocolConfig, [
"poapContractAddress",
]);
const poapContractAddress = poapContractCandidate
? toAddressOrZero(poapContractCandidate)
: (POAP_TOKEN_ADDRESS as `0x${string}`);
const hatsContractCandidate = protocolValue(protocolConfig, [
"hatsAddress",
"hatsContract",
]);
const hatsContractAddress = hatsContractCandidate
? toAddressOrZero(hatsContractCandidate)
: (HATS_PROTOCOL_ADDRESS as `0x${string}`);
const normalizedNftRequirement = { ...nftRequirement };
if (values.accessType === AccessType.POAP) {
normalizedNftRequirement.nftContract = poapContractAddress;
}
if (values.accessType === AccessType.Hats && normalizedNftRequirement.nftContract === ZERO_ADDRESS) {
normalizedNftRequirement.nftContract = hatsContractAddress;
}
const jarConfig: JarConfigArg = {
jarOwner: values.jarOwnerAddress as `0x${string}`,
supportedCurrency: values.supportedCurrency as `0x${string}`,
feeCollector: ZERO_ADDRESS as `0x${string}`,
accessType,
withdrawalOption: values.withdrawalOption,
strictPurpose: values.strictPurpose,
emergencyWithdrawalEnabled: values.emergencyWithdrawalEnabled,
oneTimeWithdrawal: values.oneTimeWithdrawal,
fixedAmount: parseAmount(values.fixedAmount),
maxWithdrawal: parseAmount(values.maxWithdrawal),
withdrawalInterval: BigInt(values.withdrawalInterval || "0"),
minDeposit: 0n,
feePercentageOnDeposit,
maxWithdrawalPerPeriod: 0n,
metadata,
multiTokenConfig,
};
const accessConfig: AccessConfigArg = {
allowlist: [] as readonly `0x${string}`[],
nftRequirement: normalizedNftRequirement,
};
const args: FactoryCreateCookieJarArgs = [
jarConfig,
accessConfig,
multiTokenConfig,
];
return args;
}