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.test.ts
More file actions
213 lines (194 loc) · 5.75 KB
/
createV2CreateArgs.test.ts
File metadata and controls
213 lines (194 loc) · 5.75 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
import { decodeFunctionData, encodeFunctionData } from "viem";
import { describe, expect, it, vi } from "vitest";
import { cookieJarFactoryAbi } from "@/generated";
import {
FACTORY_DEFAULT_FEE_SENTINEL,
buildV2CreateCookieJarArgs,
getFeePercentageOnDeposit,
} from "@/hooks/jar/createV2CreateArgs";
import { ETH_ADDRESS, HATS_PROTOCOL_ADDRESS, POAP_TOKEN_ADDRESS } from "@/lib/blockchain/constants";
vi.mock("@/hooks/jar/schemas/jarCreationSchema", () => ({
AccessType: {
Allowlist: 0,
NFTGated: 1,
POAP: 2,
Unlock: 3,
Hypercert: 4,
Hats: 5,
},
NFTType: {
None: 0,
ERC721: 1,
ERC1155: 2,
},
}));
const AccessType = {
Allowlist: 0,
NFTGated: 1,
POAP: 2,
Unlock: 3,
Hypercert: 4,
Hats: 5,
} as const;
const NFTType = {
None: 0,
ERC721: 1,
ERC1155: 2,
} as const;
type JarCreationFormData = Parameters<typeof buildV2CreateCookieJarArgs>[0]["values"];
type ProtocolConfig = JarCreationFormData["protocolConfig"];
type MakeValuesOverrides = Partial<Omit<JarCreationFormData, "protocolConfig">> & {
protocolConfig?: Partial<ProtocolConfig>;
};
function makeValues(overrides: MakeValuesOverrides = {}): JarCreationFormData {
const baseValues: JarCreationFormData = {
jarName: "Test Jar",
jarOwnerAddress: "0x1234567890123456789012345678901234567890",
supportedCurrency: ETH_ADDRESS,
metadata: "Test metadata",
imageUrl: "",
externalLink: "",
showCustomCurrency: false,
customCurrencyAddress: "",
withdrawalOption: 0,
fixedAmount: "1",
maxWithdrawal: "2",
withdrawalInterval: "7",
strictPurpose: true,
emergencyWithdrawalEnabled: true,
oneTimeWithdrawal: false,
accessType: AccessType.Allowlist,
nftAddresses: [] as string[],
nftTypes: [] as number[],
protocolConfig: { accessType: "Allowlist" },
enableCustomFee: false,
customFee: "",
streamingEnabled: false,
requireStreamApproval: true,
maxStreamRate: "1.0",
minStreamDuration: "1",
autoSwapEnabled: false,
};
return {
...baseValues,
...overrides,
protocolConfig: {
...baseValues.protocolConfig,
...overrides.protocolConfig,
},
};
}
describe("buildV2CreateCookieJarArgs", () => {
it("encodes createCookieJar args compatible with current factory ABI", () => {
const args = buildV2CreateCookieJarArgs({
values: makeValues(),
metadata: "metadata",
parseAmount: (amount) => BigInt(Math.floor(Number.parseFloat(amount) * 1e18)),
});
const data = encodeFunctionData({
abi: cookieJarFactoryAbi,
functionName: "createCookieJar",
args,
});
expect(data.startsWith("0x")).toBe(true);
const decoded = decodeFunctionData({
abi: cookieJarFactoryAbi,
data,
});
expect(decoded.functionName).toBe("createCookieJar");
expect(decoded.args?.length).toBe(3);
});
it("uses default fee sentinel when custom fee is disabled", () => {
const fee = getFeePercentageOnDeposit(makeValues({ enableCustomFee: false }));
expect(fee).toBe(FACTORY_DEFAULT_FEE_SENTINEL);
});
it("uses explicit custom fee when provided", () => {
const fee = getFeePercentageOnDeposit(
makeValues({ enableCustomFee: true, customFee: "2.5" }),
);
expect(fee).toBe(250n);
});
it("supports explicit zero-percent fee", () => {
const fee = getFeePercentageOnDeposit(
makeValues({ enableCustomFee: true, customFee: "0" }),
);
expect(fee).toBe(0n);
});
it("maps NFT ERC721 access to contract enum ERC721", () => {
const [jarConfig, accessConfig] = buildV2CreateCookieJarArgs({
values: makeValues({
accessType: AccessType.NFTGated,
nftAddresses: ["0x1111111111111111111111111111111111111111"],
nftTypes: [NFTType.ERC721],
}),
metadata: "metadata",
parseAmount: () => 1n,
});
expect(jarConfig.accessType).toBe(1);
expect(accessConfig.nftRequirement.nftContract).toBe(
"0x1111111111111111111111111111111111111111",
);
});
it("maps NFT ERC1155 access to contract enum ERC1155", () => {
const [jarConfig] = buildV2CreateCookieJarArgs({
values: makeValues({
accessType: AccessType.NFTGated,
nftAddresses: ["0x1111111111111111111111111111111111111111"],
nftTypes: [NFTType.ERC1155],
}),
metadata: "metadata",
parseAmount: () => 1n,
});
expect(jarConfig.accessType).toBe(2);
});
it("maps POAP, Unlock, Hypercert, and Hats to supported contract enum domain", () => {
const [poapJar, poapAccess] = buildV2CreateCookieJarArgs({
values: makeValues({
accessType: AccessType.POAP,
protocolConfig: { accessType: "POAP", eventId: "1234" },
}),
metadata: "metadata",
parseAmount: () => 1n,
});
expect(poapJar.accessType).toBe(1);
expect(poapAccess.nftRequirement.nftContract).toBe(POAP_TOKEN_ADDRESS);
expect(poapAccess.nftRequirement.tokenId).toBe(1234n);
expect(poapAccess.nftRequirement.minBalance).toBe(1n);
const [unlockJar] = buildV2CreateCookieJarArgs({
values: makeValues({
accessType: AccessType.Unlock,
protocolConfig: {
accessType: "Unlock",
unlockAddress: "0x2222222222222222222222222222222222222222",
},
}),
metadata: "metadata",
parseAmount: () => 1n,
});
expect(unlockJar.accessType).toBe(1);
const [hypercertJar] = buildV2CreateCookieJarArgs({
values: makeValues({
accessType: AccessType.Hypercert,
protocolConfig: {
accessType: "Hypercert",
hypercertAddress: "0x3333333333333333333333333333333333333333",
hypercertTokenId: "42",
hypercertMinBalance: 5,
},
}),
metadata: "metadata",
parseAmount: () => 1n,
});
expect(hypercertJar.accessType).toBe(2);
const [hatsJar, hatsAccess] = buildV2CreateCookieJarArgs({
values: makeValues({
accessType: AccessType.Hats,
protocolConfig: { accessType: "Hats", hatsId: 99 },
}),
metadata: "metadata",
parseAmount: () => 1n,
});
expect(hatsJar.accessType).toBe(2);
expect(hatsAccess.nftRequirement.nftContract).toBe(HATS_PROTOCOL_ADDRESS);
});
});