forked from jtgi/automod
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhypersub.ts
90 lines (82 loc) · 2.67 KB
/
hypersub.ts
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
import { getAddress, getContract } from "viem";
import { polygon } from "viem/chains";
import { hypersubAbi721 } from "~/lib/abis";
import { formatHash } from "~/lib/utils.server";
import { clientsByChainId } from "~/lib/viem.server";
import { CheckFunction, CheckFunctionArgs, RuleDefinition } from "~/rules/rules.type";
async function holdsActiveHypersub(args: CheckFunctionArgs) {
const { user, rule } = args;
const { chainId, contractAddress, name } = rule.args;
const client = clientsByChainId[chainId];
if (!client) {
throw new Error(`No client found for chainId: ${chainId}`);
}
let isSubscribed = false;
const contract = getContract({
address: getAddress(contractAddress),
abi: hypersubAbi721,
client,
});
for (const address of [user.custody_address, ...user.verifications]) {
const balance = await contract.read.balanceOf([getAddress(address)]);
if (balance > 0) {
isSubscribed = true;
break;
}
}
return {
result: isSubscribed,
message: isSubscribed
? `User holds an active hypersub (${name || formatHash(contractAddress)})`
: `User does not hold an active hypersub (${name || formatHash(contractAddress)})`,
};
}
type RuleName = "requireActiveHypersub";
export const hypersubRulesDefinitions: Record<RuleName, RuleDefinition> = {
requireActiveHypersub: {
name: "requireActiveHypersub",
author: "Hypersub",
authorUrl: "https://hypersub.withfarbic.xyz",
authorIcon: `/icons/fabric.svg`,
allowMultiple: true,
category: "all",
friendlyName: "Subscribes on Hypersub",
checkType: "user",
description: "Check if the user has an active subscription to a hypersub.",
hidden: false,
invertable: true,
args: {
chainId: {
type: "select",
friendlyName: "Chain",
description: "",
required: true,
options: [
{ value: "1", label: "Ethereum" },
{ value: "10", label: "Optimism" },
{ value: "8453", label: "Base" },
{ value: "7777777", label: "Zora" },
{ value: String(polygon.id), label: "Polygon" },
],
},
contractAddress: {
type: "string",
required: true,
pattern: "0x[a-fA-F0-9]{40}",
placeholder: "0xdead...",
friendlyName: "Contract Address",
description: "",
},
name: {
type: "string",
required: true,
friendlyName: "Membership Name",
placeholder: "e.g. Buoy Pro",
description: "The name of the membership for display in the UI.",
},
},
},
};
export const hypersubRulesFunction: Record<RuleName, CheckFunction> = {
requireActiveHypersub: holdsActiveHypersub,
};