-
Notifications
You must be signed in to change notification settings - Fork 306
/
RedPacket.ts
238 lines (224 loc) · 7.83 KB
/
RedPacket.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
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
import {
createIndicator,
createNextIndicator,
createPageable,
type Pageable,
type PageIndicator,
getSiteType,
EnhanceableSite,
} from '@masknet/shared-base'
import urlcat from 'urlcat'
import { fetchJSON } from '../entry-helpers.js'
import { FireflyRedPacketAPI } from '../entry-types.js'
const siteType = getSiteType()
const SITE_URL = siteType === EnhanceableSite.Firefly ? location.origin : 'https://firefly.mask.social'
const FIREFLY_ROOT_URL = process.env.NEXT_PUBLIC_FIREFLY_API_URL || 'https://api.firefly.land'
function fetchFireflyJSON<T>(url: string, init?: RequestInit): Promise<T> {
return fetchJSON<T>(url, {
...init,
headers: {
'Content-Type': 'application/json',
...init?.headers,
},
})
}
export class FireflyRedPacket {
static async parse(options: FireflyRedPacketAPI.ParseOptions) {
const url = urlcat(FIREFLY_ROOT_URL, '/v1/misc/redpacket/parse')
const { data } = await fetchFireflyJSON<FireflyRedPacketAPI.ParseResponse>(url, {
method: 'POST',
body: JSON.stringify(options),
})
return data
}
static async getPayloadUrls(from: string, amount?: string, type?: string, symbol?: string, decimals?: number) {
const url = urlcat(FIREFLY_ROOT_URL, '/v1/redpacket/themeList')
const { data } = await fetchJSON<FireflyRedPacketAPI.ThemeListResponse>(url)
return data.list.map((theme) => ({
themeId: theme.tid,
backgroundImageUrl: theme.cover.bg_image,
backgroundColor: theme.cover.bg_color,
url: urlcat(SITE_URL, '/api/rp', {
'theme-id': theme.tid,
usage: 'payload',
from,
amount,
type,
symbol,
decimals,
}),
}))
}
static async getPayloadUrlByThemeId(
themeId: string,
from: string,
amount?: string,
type?: string,
symbol?: string,
decimals?: number,
) {
const url = urlcat(FIREFLY_ROOT_URL, 'v1/redpacket/themeById', {
themeId,
})
const { data } = await fetchJSON<FireflyRedPacketAPI.ThemeByIdResponse>(url)
return {
themeId,
url: urlcat(SITE_URL, '/api/rp', {
'theme-id': themeId,
usage: 'payload',
from,
amount,
type,
symbol,
decimals,
}),
backgroundImageUrl: data.cover.bg_image,
backgroundColor: data.cover.bg_color,
}
}
static async getCoverUrlByRpid(
rpid: string,
symbol?: string,
decimals?: number,
shares?: number,
amount?: string,
from?: string,
message?: string,
remainingAmount?: string,
remainingShares?: string,
) {
const url = urlcat(FIREFLY_ROOT_URL, 'v1/redpacket/themeById', {
rpid,
})
const { data } = await fetchJSON<FireflyRedPacketAPI.ThemeByIdResponse>(url)
// Just discard default theme, and this RedPacket will be treated as created from Mask
if (data.is_default) return null
return {
themeId: data.tid,
backgroundImageUrl: data.normal.bg_image,
backgroundColor: data.normal.bg_color,
url: urlcat(SITE_URL, '/api/rp', {
'theme-id': data.tid,
usage: 'cover',
symbol,
decimals,
shares,
amount,
from,
message,
'remaining-amount': remainingAmount,
'remaining-shares': remainingShares,
}),
}
}
static async createPublicKey(
themeId: string,
shareFrom: string,
payloads: FireflyRedPacketAPI.StrategyPayload[],
): Promise<HexString> {
const url = urlcat(FIREFLY_ROOT_URL, '/v1/redpacket/createPublicKey')
const { data } = await fetchFireflyJSON<FireflyRedPacketAPI.PublicKeyResponse>(url, {
method: 'POST',
body: JSON.stringify({
themeId,
shareFrom,
claimFrom: FireflyRedPacketAPI.SourceType.FireflyPC,
claimStrategy: JSON.stringify(payloads),
}),
})
return data.publicKey
}
static async updateClaimStrategy(
rpid: string,
reactions: FireflyRedPacketAPI.PostReaction[],
claimPlatform: FireflyRedPacketAPI.ClaimPlatform[],
publicKey: string,
): Promise<void> {
const url = urlcat(FIREFLY_ROOT_URL, '/v1/redpacket/updateClaimStrategy')
await fetchFireflyJSON(url, {
method: 'POST',
body: JSON.stringify({
publicKey,
rpid,
postReaction: reactions,
claimPlatform,
}),
})
}
static async createClaimSignature(
options: FireflyRedPacketAPI.CheckClaimStrategyStatusOptions,
): Promise<HexString> {
const url = urlcat(FIREFLY_ROOT_URL, '/v1/redpacket/claim')
const { data } = await fetchFireflyJSON<FireflyRedPacketAPI.ClaimResponse>(url, {
method: 'POST',
body: JSON.stringify(options),
})
return data.signedMessage
}
static async getHistory<
T extends FireflyRedPacketAPI.ActionType,
R = T extends FireflyRedPacketAPI.ActionType.Claim ? FireflyRedPacketAPI.RedPacketClaimedInfo
: FireflyRedPacketAPI.RedPacketSentInfo,
>(
actionType: T,
from: HexString,
platform: FireflyRedPacketAPI.SourceType,
indicator?: PageIndicator,
): Promise<Pageable<R, PageIndicator>> {
const url = urlcat(FIREFLY_ROOT_URL, '/v1/redpacket/history', {
address: from,
redpacketType: actionType,
claimFrom: platform,
cursor: indicator?.id,
size: 20,
})
const { data } = await fetchJSON<FireflyRedPacketAPI.HistoryResponse>(url, {
method: 'GET',
})
return createPageable(
data.list.map((v) => ({ ...v, chain_id: Number(v.chain_id) })) as R[],
createIndicator(indicator),
createNextIndicator(indicator, data.cursor?.toString()),
)
}
static async getClaimHistory(
redpacket_id: string,
indicator?: PageIndicator,
): Promise<FireflyRedPacketAPI.RedPacketClaimListInfo> {
const url = urlcat(FIREFLY_ROOT_URL, '/v1/redpacket/claimHistory', {
redpacketId: redpacket_id,
cursor: indicator?.id,
size: 20,
})
const { data } = await fetchJSON<FireflyRedPacketAPI.ClaimHistoryResponse>(url, {
method: 'GET',
})
return { ...data, chain_id: Number(data.chain_id) }
}
static async checkClaimStrategyStatus(options: FireflyRedPacketAPI.CheckClaimStrategyStatusOptions) {
const url = urlcat(FIREFLY_ROOT_URL, '/v1/redpacket/checkClaimStrategyStatus')
return fetchFireflyJSON<FireflyRedPacketAPI.CheckClaimStrategyStatusResponse>(url, {
method: 'POST',
body: JSON.stringify(options),
})
}
static async finishClaiming(
rpid: string,
platform: FireflyRedPacketAPI.PlatformType,
profileId: string,
handle: string,
txHash: string,
) {
const url = urlcat(FIREFLY_ROOT_URL, '/v1/redpacket/finishClaiming')
return fetchFireflyJSON<FireflyRedPacketAPI.Response<string>>(url, {
method: 'POST',
body: JSON.stringify({
rpid,
claimPlatform: platform,
claimProfileId: profileId,
claimHandle: handle,
txHash,
}),
})
}
}