-
Notifications
You must be signed in to change notification settings - Fork 9
/
subscription.server.ts
132 lines (114 loc) · 3.25 KB
/
subscription.server.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
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
import { getAddress, getContract } from "viem";
import { db } from "./db.server";
import { base } from "viem/chains";
import { hypersubAbi721, hypersubAbiV2 } from "./abis";
import { neynar } from "./neynar.server";
import { clientsByChainId } from "./viem.server";
import { PlanType } from "./utils";
export async function syncSubscriptions() {
const activeUsers = await db.user.findMany({
where: {
plan: {
not: "vip",
},
},
});
for (const user of activeUsers) {
const plan = await refreshAccountStatus({ fid: user.id });
console.log(
`[subsync] ${user.name} plan: ${plan.plan}, expiry: ${
plan.expiresAt?.toISOString() || Infinity.toString()
}`
);
}
}
export async function getSubscriptionPlan(args: { fid: string; walletAddress?: string }): Promise<{
plan: PlanType;
tokenId: string | null;
expiresAt: Date | null;
}> {
const client = clientsByChainId[base.id];
const primeContractAddress = process.env.PRIME_CONTRACT_ADDRESS!;
const hypersubV2ContractAddress = process.env.HYPERSUBV2_CONTRACT_ADDRESS!;
const primeContract = getContract({
address: getAddress(primeContractAddress),
abi: hypersubAbi721,
client,
});
const hypersubV2Contract = getContract({
address: getAddress(hypersubV2ContractAddress),
abi: hypersubAbiV2,
client,
});
const rsp = await neynar.fetchBulkUsers([+args.fid]);
if (rsp.users.length === 0) {
throw new Error(`User not found: ${args.fid}`);
}
const user = rsp.users[0];
const addresses = [args.walletAddress, ...user.verified_addresses.eth_addresses].filter(
Boolean
) as string[];
for (const address of addresses) {
const [primeSecondsRemaining, v2SecondsRemaining] = await Promise.all([
primeContract.read.balanceOf([getAddress(address)]),
hypersubV2Contract.read.balanceOf([getAddress(address)]),
]);
if (v2SecondsRemaining > 0) {
const subInfo = await hypersubV2Contract.read.subscriptionOf([getAddress(address)]);
return {
plan: subInfo.tierId === 1 ? "ultra" : "prime",
tokenId: subInfo.tokenId.toString(),
expiresAt: new Date(subInfo.expiresAt * 1000),
};
} else if (primeSecondsRemaining > 0) {
const subInfo = await primeContract.read.subscriptionOf([getAddress(address)]);
const tokenId = subInfo[0];
return {
plan: "prime",
tokenId: tokenId.toString(),
expiresAt: new Date(Date.now() + Number(primeSecondsRemaining * 1000n)),
};
}
}
return {
plan: "basic",
expiresAt: null,
tokenId: null,
};
}
export async function refreshAccountStatus(args: { fid: string }) {
const user = await db.user.findFirst({
where: {
id: args.fid,
},
});
if (!user) {
return {
plan: "basic",
expiresAt: null,
tokenId: null,
};
}
if (user.plan === "vip") {
return {
plan: "vip",
tokenId: null,
expiresAt: null,
};
}
const plan = await getSubscriptionPlan({
fid: args.fid,
walletAddress: user.planWalletAddress ?? undefined,
});
await db.user.update({
where: {
id: args.fid,
},
data: {
plan: plan.plan,
planExpiry: plan.expiresAt,
planTokenId: plan.tokenId,
},
});
return plan;
}