@sign-global/tokentable 0.3.0 → 0.4.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (138) hide show
  1. package/.github/workflows/beta.yml +28 -0
  2. package/dist/cjs/airdrop/AirdropClient.d.ts +46 -0
  3. package/dist/cjs/airdrop/AirdropClient.js +244 -0
  4. package/dist/cjs/airdrop/AirdropClient.js.map +1 -0
  5. package/dist/cjs/airdrop/SignatureAirdropClient.d.ts +26 -0
  6. package/dist/cjs/airdrop/SignatureAirdropClient.js +109 -0
  7. package/dist/cjs/airdrop/SignatureAirdropClient.js.map +1 -0
  8. package/dist/cjs/airdrop/common/index.d.ts +1 -1
  9. package/dist/cjs/airdrop/constants/index.d.ts +4 -0
  10. package/dist/cjs/airdrop/constants/index.js +5 -1
  11. package/dist/cjs/airdrop/constants/index.js.map +1 -1
  12. package/dist/cjs/airdrop/core/useWeb3ConnectWallet/WalletBase.d.ts +32 -0
  13. package/dist/cjs/airdrop/core/useWeb3ConnectWallet/WalletBase.js +71 -0
  14. package/dist/cjs/airdrop/core/useWeb3ConnectWallet/WalletBase.js.map +1 -0
  15. package/dist/cjs/airdrop/core/useWeb3ConnectWallet/WalletFactory.d.ts +8 -0
  16. package/dist/cjs/airdrop/core/useWeb3ConnectWallet/WalletFactory.js +38 -0
  17. package/dist/cjs/airdrop/core/useWeb3ConnectWallet/WalletFactory.js.map +1 -0
  18. package/dist/cjs/airdrop/core/useWeb3ConnectWallet/index.d.ts +2 -0
  19. package/dist/cjs/airdrop/core/useWeb3ConnectWallet/index.js +21 -0
  20. package/dist/cjs/airdrop/core/useWeb3ConnectWallet/index.js.map +1 -0
  21. package/dist/cjs/airdrop/core/useWeb3ConnectWallet/providers/evm/EvmWallet.d.ts +26 -0
  22. package/dist/cjs/airdrop/core/useWeb3ConnectWallet/providers/evm/EvmWallet.js +70 -0
  23. package/dist/cjs/airdrop/core/useWeb3ConnectWallet/providers/evm/EvmWallet.js.map +1 -0
  24. package/dist/cjs/airdrop/core/useWeb3ConnectWallet/providers/evm/index.d.ts +9 -0
  25. package/dist/cjs/airdrop/core/useWeb3ConnectWallet/providers/evm/index.js +35 -0
  26. package/dist/cjs/airdrop/core/useWeb3ConnectWallet/providers/evm/index.js.map +1 -0
  27. package/dist/cjs/airdrop/core/useWeb3ConnectWallet/providers/index.d.ts +3 -0
  28. package/dist/cjs/airdrop/core/useWeb3ConnectWallet/providers/index.js +14 -0
  29. package/dist/cjs/airdrop/core/useWeb3ConnectWallet/providers/index.js.map +1 -0
  30. package/dist/cjs/airdrop/core/useWeb3ConnectWallet/providers/solana/index.d.ts +27 -0
  31. package/dist/cjs/airdrop/core/useWeb3ConnectWallet/providers/solana/index.js +119 -0
  32. package/dist/cjs/airdrop/core/useWeb3ConnectWallet/providers/solana/index.js.map +1 -0
  33. package/dist/cjs/airdrop/core/useWeb3ConnectWallet/providers/ton/TonWallet.d.ts +23 -0
  34. package/dist/cjs/airdrop/core/useWeb3ConnectWallet/providers/ton/TonWallet.js +117 -0
  35. package/dist/cjs/airdrop/core/useWeb3ConnectWallet/providers/ton/TonWallet.js.map +1 -0
  36. package/dist/cjs/airdrop/core/useWeb3ConnectWallet/providers/ton/index.d.ts +9 -0
  37. package/dist/cjs/airdrop/core/useWeb3ConnectWallet/providers/ton/index.js +29 -0
  38. package/dist/cjs/airdrop/core/useWeb3ConnectWallet/providers/ton/index.js.map +1 -0
  39. package/dist/cjs/airdrop/core/useWeb3ConnectWallet/types.d.ts +38 -0
  40. package/dist/cjs/airdrop/core/useWeb3ConnectWallet/types.js +30 -0
  41. package/dist/cjs/airdrop/core/useWeb3ConnectWallet/types.js.map +1 -0
  42. package/dist/cjs/airdrop/core/useWeb3ConnectWallet/utils/index.d.ts +8 -0
  43. package/dist/cjs/airdrop/core/useWeb3ConnectWallet/utils/index.js +41 -0
  44. package/dist/cjs/airdrop/core/useWeb3ConnectWallet/utils/index.js.map +1 -0
  45. package/dist/cjs/airdrop/index.d.ts +4 -46
  46. package/dist/cjs/airdrop/index.js +22 -241
  47. package/dist/cjs/airdrop/index.js.map +1 -1
  48. package/dist/cjs/airdrop/services/index.d.ts +15 -0
  49. package/dist/cjs/airdrop/services/index.js +16 -1
  50. package/dist/cjs/airdrop/services/index.js.map +1 -1
  51. package/dist/cjs/airdrop/types/index.d.ts +18 -4
  52. package/dist/cjs/airdrop/types/index.js +7 -1
  53. package/dist/cjs/airdrop/types/index.js.map +1 -1
  54. package/dist/cjs/index.d.ts +1 -1
  55. package/dist/cjs/index.js +7 -1
  56. package/dist/cjs/index.js.map +1 -1
  57. package/dist/cjs/utils/utils.d.ts +1 -0
  58. package/dist/cjs/utils/utils.js +2 -1
  59. package/dist/cjs/utils/utils.js.map +1 -1
  60. package/dist/esm/airdrop/AirdropClient.d.ts +46 -0
  61. package/dist/esm/airdrop/AirdropClient.js +240 -0
  62. package/dist/esm/airdrop/AirdropClient.js.map +1 -0
  63. package/dist/esm/airdrop/SignatureAirdropClient.d.ts +26 -0
  64. package/dist/esm/airdrop/SignatureAirdropClient.js +105 -0
  65. package/dist/esm/airdrop/SignatureAirdropClient.js.map +1 -0
  66. package/dist/esm/airdrop/common/index.d.ts +1 -1
  67. package/dist/esm/airdrop/constants/index.d.ts +4 -0
  68. package/dist/esm/airdrop/constants/index.js +4 -0
  69. package/dist/esm/airdrop/constants/index.js.map +1 -1
  70. package/dist/esm/airdrop/core/useWeb3ConnectWallet/WalletBase.d.ts +32 -0
  71. package/dist/esm/airdrop/core/useWeb3ConnectWallet/WalletBase.js +67 -0
  72. package/dist/esm/airdrop/core/useWeb3ConnectWallet/WalletBase.js.map +1 -0
  73. package/dist/esm/airdrop/core/useWeb3ConnectWallet/WalletFactory.d.ts +8 -0
  74. package/dist/esm/airdrop/core/useWeb3ConnectWallet/WalletFactory.js +34 -0
  75. package/dist/esm/airdrop/core/useWeb3ConnectWallet/WalletFactory.js.map +1 -0
  76. package/dist/esm/airdrop/core/useWeb3ConnectWallet/index.d.ts +2 -0
  77. package/dist/esm/airdrop/core/useWeb3ConnectWallet/index.js +3 -0
  78. package/dist/esm/airdrop/core/useWeb3ConnectWallet/index.js.map +1 -0
  79. package/dist/esm/airdrop/core/useWeb3ConnectWallet/providers/evm/EvmWallet.d.ts +26 -0
  80. package/dist/esm/airdrop/core/useWeb3ConnectWallet/providers/evm/EvmWallet.js +65 -0
  81. package/dist/esm/airdrop/core/useWeb3ConnectWallet/providers/evm/EvmWallet.js.map +1 -0
  82. package/dist/esm/airdrop/core/useWeb3ConnectWallet/providers/evm/index.d.ts +9 -0
  83. package/dist/esm/airdrop/core/useWeb3ConnectWallet/providers/evm/index.js +29 -0
  84. package/dist/esm/airdrop/core/useWeb3ConnectWallet/providers/evm/index.js.map +1 -0
  85. package/dist/esm/airdrop/core/useWeb3ConnectWallet/providers/index.d.ts +3 -0
  86. package/dist/esm/airdrop/core/useWeb3ConnectWallet/providers/index.js +5 -0
  87. package/dist/esm/airdrop/core/useWeb3ConnectWallet/providers/index.js.map +1 -0
  88. package/dist/esm/airdrop/core/useWeb3ConnectWallet/providers/solana/index.d.ts +27 -0
  89. package/dist/esm/airdrop/core/useWeb3ConnectWallet/providers/solana/index.js +109 -0
  90. package/dist/esm/airdrop/core/useWeb3ConnectWallet/providers/solana/index.js.map +1 -0
  91. package/dist/esm/airdrop/core/useWeb3ConnectWallet/providers/ton/TonWallet.d.ts +23 -0
  92. package/dist/esm/airdrop/core/useWeb3ConnectWallet/providers/ton/TonWallet.js +112 -0
  93. package/dist/esm/airdrop/core/useWeb3ConnectWallet/providers/ton/TonWallet.js.map +1 -0
  94. package/dist/esm/airdrop/core/useWeb3ConnectWallet/providers/ton/index.d.ts +9 -0
  95. package/dist/esm/airdrop/core/useWeb3ConnectWallet/providers/ton/index.js +24 -0
  96. package/dist/esm/airdrop/core/useWeb3ConnectWallet/providers/ton/index.js.map +1 -0
  97. package/dist/esm/airdrop/core/useWeb3ConnectWallet/types.d.ts +38 -0
  98. package/dist/esm/airdrop/core/useWeb3ConnectWallet/types.js +27 -0
  99. package/dist/esm/airdrop/core/useWeb3ConnectWallet/types.js.map +1 -0
  100. package/dist/esm/airdrop/core/useWeb3ConnectWallet/utils/index.d.ts +8 -0
  101. package/dist/esm/airdrop/core/useWeb3ConnectWallet/utils/index.js +35 -0
  102. package/dist/esm/airdrop/core/useWeb3ConnectWallet/utils/index.js.map +1 -0
  103. package/dist/esm/airdrop/index.d.ts +4 -46
  104. package/dist/esm/airdrop/index.js +4 -239
  105. package/dist/esm/airdrop/index.js.map +1 -1
  106. package/dist/esm/airdrop/services/index.d.ts +15 -0
  107. package/dist/esm/airdrop/services/index.js +12 -0
  108. package/dist/esm/airdrop/services/index.js.map +1 -1
  109. package/dist/esm/airdrop/types/index.d.ts +18 -4
  110. package/dist/esm/airdrop/types/index.js +6 -0
  111. package/dist/esm/airdrop/types/index.js.map +1 -1
  112. package/dist/esm/index.d.ts +1 -1
  113. package/dist/esm/index.js +1 -1
  114. package/dist/esm/index.js.map +1 -1
  115. package/dist/esm/utils/utils.d.ts +1 -0
  116. package/dist/esm/utils/utils.js +1 -0
  117. package/dist/esm/utils/utils.js.map +1 -1
  118. package/package.json +15 -3
  119. package/src/airdrop/AirdropClient.ts +301 -0
  120. package/src/airdrop/SignatureAirdropClient.ts +120 -0
  121. package/src/airdrop/constants/index.ts +5 -0
  122. package/src/airdrop/core/useWeb3ConnectWallet/WalletBase.ts +92 -0
  123. package/src/airdrop/core/useWeb3ConnectWallet/WalletFactory.tsx +35 -0
  124. package/src/airdrop/core/useWeb3ConnectWallet/index.ts +3 -0
  125. package/src/airdrop/core/useWeb3ConnectWallet/providers/evm/EvmWallet.ts +90 -0
  126. package/src/airdrop/core/useWeb3ConnectWallet/providers/evm/index.tsx +43 -0
  127. package/src/airdrop/core/useWeb3ConnectWallet/providers/index.ts +4 -0
  128. package/src/airdrop/core/useWeb3ConnectWallet/providers/solana/index.tsx +141 -0
  129. package/src/airdrop/core/useWeb3ConnectWallet/providers/ton/TonWallet.ts +149 -0
  130. package/src/airdrop/core/useWeb3ConnectWallet/providers/ton/index.tsx +41 -0
  131. package/src/airdrop/core/useWeb3ConnectWallet/types.ts +45 -0
  132. package/src/airdrop/core/useWeb3ConnectWallet/utils/index.ts +61 -0
  133. package/src/airdrop/index.ts +4 -304
  134. package/src/airdrop/services/index.ts +29 -1
  135. package/src/airdrop/types/index.ts +22 -5
  136. package/src/index.ts +9 -1
  137. package/src/utils/utils.ts +2 -0
  138. package/tsconfig.json +2 -2
@@ -0,0 +1,301 @@
1
+ import { TonClient } from '@ton/ton';
2
+ import { getAirdropPro, getAirdropProject, getAirdropQuery, getAirdropContractConfig } from './services';
3
+ import {
4
+ ChainType,
5
+ ETokenType,
6
+ EvmAirdropVersionEnum,
7
+ IAirdropClaim,
8
+ IAirdropOptions,
9
+ IContractConfig,
10
+ IProject,
11
+ IWalletClient
12
+ } from './types';
13
+ import { getTonProjectIdByAddress } from './common';
14
+ import { AirdropStrategyFactory } from './strategies/AirdropStrategyFactory';
15
+ import { IAirdropStrategy } from './strategies/IAirdropStrategy';
16
+ import { strategyCache } from './strategies/StrategyCache';
17
+
18
+ export class AirdropClient {
19
+ private tonClient?: TonClient;
20
+ private slug?: string;
21
+ private projectId?: string;
22
+ private endpoint?: string;
23
+ private project: IProject | undefined;
24
+ protected claims?: IAirdropClaim[];
25
+ private projectPromise: Promise<void> | undefined;
26
+ private claimHook: ((v: any) => any) | undefined;
27
+ private contractConfig: IContractConfig | null = null;
28
+
29
+ constructor(params: IAirdropOptions) {
30
+ if ('slug' in params) {
31
+ this.slug = params.slug;
32
+ }
33
+ this.projectId = params.projectId;
34
+ this.endpoint = params.endpoint;
35
+ this.projectPromise = this.initializeProjects();
36
+ }
37
+
38
+ setTonClient(tonClient: TonClient) {
39
+ this.tonClient = tonClient;
40
+ }
41
+
42
+ private getTonClient(): TonClient | undefined {
43
+ if (!this.tonClient) {
44
+ return undefined;
45
+ }
46
+ return this.tonClient;
47
+ }
48
+
49
+ private async fetchProjectFromSlug(address?: string): Promise<string | undefined> {
50
+ if (!this.slug) return undefined;
51
+
52
+ const res = await getAirdropPro(this.slug, this.endpoint);
53
+ const projectMap = res?.childProjectMapping || {};
54
+
55
+ return getTonProjectIdByAddress({
56
+ address,
57
+ slug: this.slug,
58
+ data: projectMap
59
+ });
60
+ }
61
+
62
+ private async fetchProjectDetails(projectId: string): Promise<void> {
63
+ this.project = await getAirdropProject(projectId, this.endpoint);
64
+ }
65
+
66
+ protected async initializeProjects(address?: string) {
67
+ try {
68
+ let projectId = this.projectId;
69
+
70
+ // If slug is provided, try to fetch project ID from slug
71
+ if (!projectId) {
72
+ projectId = await this.fetchProjectFromSlug(address);
73
+ }
74
+
75
+ if (!projectId) {
76
+ return;
77
+ }
78
+
79
+ // Fetch project details
80
+ await this.fetchProjectDetails(projectId);
81
+ } catch (error: any) {
82
+ throw new Error(`Failed to initialize project: ${error.message}`);
83
+ }
84
+ }
85
+
86
+ async getProjectInfo() {
87
+ await this.projectPromise;
88
+ if (!this.project) throw new Error('Project not found');
89
+ return this.project;
90
+ }
91
+
92
+ async getAirdropClaims(data: { address: string }) {
93
+ if (this.slug) {
94
+ await this.initializeProjects(data.address); // Fetch project details by address & slug
95
+ }
96
+ const project = await this.getProjectInfo();
97
+
98
+ const res = await getAirdropQuery(
99
+ {
100
+ recipient: data.address,
101
+ projectId: project!.id,
102
+ recipientType: project!.recipientType
103
+ },
104
+ this.endpoint
105
+ );
106
+ const claimsRes = res.claims?.map((it) => ({
107
+ ...it,
108
+ project: project!,
109
+ claimId: project!.chainType === ChainType.Evm ? `${project!.id}:${it.leaf}` : `${project!.id}:${it.index}`
110
+ }));
111
+ this.claims = claimsRes;
112
+ return this.claims;
113
+ }
114
+
115
+ public async getStrategy(walletClient?: IWalletClient): Promise<IAirdropStrategy> {
116
+ const project = await this.getProjectInfo();
117
+ const version = this.contractConfig?.version || undefined;
118
+
119
+ const hasWallet = !!walletClient;
120
+
121
+ // Try to get cached strategy
122
+ let strategy = strategyCache.getStrategy(project.contractAddress, project.chainId, project.chainType, hasWallet);
123
+
124
+ if (!strategy) {
125
+ // Create new strategy if not cached
126
+ strategy = AirdropStrategyFactory.createStrategy({
127
+ chainType: project.chainType,
128
+ contractAddress: project.contractAddress,
129
+ chainId: project.chainId,
130
+ walletClient,
131
+ tonClient: this.getTonClient(),
132
+ version
133
+ });
134
+
135
+ // Initialize the strategy
136
+ await strategy.initialize(walletClient);
137
+
138
+ // Cache the initialized strategy
139
+ strategyCache.setStrategy(project.contractAddress, project.chainId, project.chainType, strategy, hasWallet);
140
+ } else if (hasWallet) {
141
+ // If we have a wallet and the strategy exists, just initialize with the new wallet
142
+ await strategy.initialize(walletClient);
143
+ }
144
+
145
+ return strategy;
146
+ }
147
+
148
+ private getClaimDataById(claimId: string) {
149
+ const claimData = this.claims?.find((it) => it.claimId === claimId);
150
+ if (!claimData) throw new Error('Claim data not found');
151
+ return claimData;
152
+ }
153
+
154
+ setClaimHook(fn: (v: any) => any) {
155
+ this.claimHook = fn;
156
+ }
157
+
158
+ private getDefaultExtraData() {
159
+ return {
160
+ isLegacy: true,
161
+ attestationId: BigInt(1)
162
+ };
163
+ }
164
+
165
+ private async calculateClaimFee(project: IProject, claimAmount: string): Promise<string | bigint | undefined> {
166
+ if (project.chainType === ChainType.Evm) {
167
+ const version = await this.getVersion();
168
+ const isNativeFreeVersion =
169
+ project.tokenType === ETokenType.Native &&
170
+ [EvmAirdropVersionEnum.V3, EvmAirdropVersionEnum.V4].includes(version as EvmAirdropVersionEnum);
171
+ return isNativeFreeVersion ? undefined : await this.getClaimFee(BigInt(claimAmount));
172
+ }
173
+
174
+ if (project.chainType === ChainType.Ton) {
175
+ return await this.getClaimFee(BigInt(claimAmount));
176
+ }
177
+
178
+ return undefined;
179
+ }
180
+
181
+ async claimAirdrop(
182
+ { claimId, walletClient }: { claimId: string; walletClient: IWalletClient },
183
+ options?: { onLoading?: () => void }
184
+ ) {
185
+ // Get claim data and initialize strategy
186
+ const claimData = this.getClaimDataById(claimId);
187
+ const strategy = await this.getStrategy(walletClient);
188
+ const project = await this.getProjectInfo();
189
+
190
+ // Process extra data
191
+ const extraData =
192
+ project.chainType === ChainType.Ton
193
+ ? undefined
194
+ : this.claimHook
195
+ ? this.claimHook(claimData)
196
+ : this.getDefaultExtraData();
197
+
198
+ // Calculate claim value based on project type
199
+ const value = await this.calculateClaimFee(project, claimData.amount);
200
+
201
+ // Execute claim with processed data
202
+ return strategy.claim(
203
+ {
204
+ ...claimData,
205
+ extraData,
206
+ value
207
+ },
208
+ options
209
+ );
210
+ }
211
+
212
+ async checkClaimed(claimId: string): Promise<boolean> {
213
+ const claimData = this.getClaimDataById(claimId);
214
+ const strategy = await this.getStrategy();
215
+ return strategy.checkClaimed(claimData);
216
+ }
217
+
218
+ private async getContractConfig(): Promise<IContractConfig | null> {
219
+ try {
220
+ const project = await this.getProjectInfo();
221
+
222
+ if (!this.contractConfig) {
223
+ this.contractConfig = await getAirdropContractConfig(
224
+ {
225
+ chainId: project.chainId,
226
+ chainType: project.chainType,
227
+ contractAddress: project.contractAddress
228
+ },
229
+ this.endpoint
230
+ );
231
+ }
232
+ return this.contractConfig;
233
+ } catch (error) {
234
+ console.error('Error fetching contract config:', error);
235
+ return null;
236
+ }
237
+ }
238
+
239
+ async getVersion(): Promise<string> {
240
+ const config = await this.getContractConfig();
241
+ if (config?.version) {
242
+ return config.version;
243
+ }
244
+
245
+ const strategy = await this.getStrategy();
246
+ return strategy.getVersion();
247
+ }
248
+
249
+ async getPaused(): Promise<boolean> {
250
+ const config = await this.getContractConfig();
251
+ if (config && config?.paused !== null) {
252
+ return config.paused;
253
+ }
254
+
255
+ const strategy = await this.getStrategy();
256
+ if (!strategy.getPaused) {
257
+ throw new Error('getPaused is not supported for this chain');
258
+ }
259
+ return strategy.getPaused();
260
+ }
261
+
262
+ async getClaimFee(amount?: bigint): Promise<string | bigint> {
263
+ const config = await this.getContractConfig();
264
+ if (config?.claimFee) {
265
+ return config.claimFee;
266
+ }
267
+
268
+ const strategy = await this.getStrategy();
269
+ if (!strategy.getClaimFee) {
270
+ throw new Error('getClaimFee is not supported for this chain');
271
+ }
272
+ return strategy.getClaimFee(this.project?.contractAddress || '', amount || BigInt(0));
273
+ }
274
+
275
+ // params claimId[]
276
+ async batchCheckClaimed(): Promise<{ result: boolean; status: string }[]> {
277
+ const strategy = await this.getStrategy();
278
+ if (!strategy.batchCheckClaimed) {
279
+ throw new Error('batchCheckClaimed is not supported for this chain');
280
+ }
281
+ const leafs = this.claims?.map((it) => it.leaf);
282
+ if (!leafs) return [];
283
+ return strategy.batchCheckClaimed(leafs);
284
+ }
285
+
286
+ async getNFT(): Promise<string> {
287
+ const strategy = await this.getStrategy();
288
+ if (!strategy.getNFT) {
289
+ throw new Error('getNFT is not supported for this chain');
290
+ }
291
+ return strategy.getNFT();
292
+ }
293
+
294
+ async getKycThreshold(): Promise<bigint> {
295
+ const strategy = await this.getStrategy();
296
+ if (!strategy.getKycThreshold) {
297
+ throw new Error('getKycThreshold is not supported for this chain');
298
+ }
299
+ return strategy.getKycThreshold();
300
+ }
301
+ }
@@ -0,0 +1,120 @@
1
+ import { CONST } from './constants';
2
+ import { WalletFactory } from './core/useWeb3ConnectWallet';
3
+ import { ChainType } from './core/useWeb3ConnectWallet/types';
4
+ import { bindWallet, connectTwitter, getAirdropProject } from './services';
5
+ import { AirdropSigIdentityTypeEnum, IAirdropOptions, IProject } from './types';
6
+
7
+ export class SignatureAirdropClient {
8
+ private slug?: string;
9
+ private projectId?: string;
10
+ private endpoint?: string;
11
+ private project: IProject | undefined;
12
+ protected claims?: any[];
13
+ private projectPromise: Promise<void> | undefined;
14
+ private claimHook: ((v: any) => any) | undefined;
15
+ // private contractConfig: IContractConfig | null = null;
16
+ constructor(params: IAirdropOptions & { project?: IProject }) {
17
+ if ('slug' in params) {
18
+ this.slug = params.slug;
19
+ }
20
+ this.projectId = params.projectId;
21
+ this.endpoint = params.endpoint;
22
+ if (this.project) {
23
+ this.project = params.project;
24
+ } else {
25
+ this.projectPromise = this.initializeProjects();
26
+ }
27
+ }
28
+
29
+ private async fetchProjectDetails(projectId: string): Promise<void> {
30
+ this.project = await getAirdropProject(projectId, this.endpoint);
31
+ }
32
+
33
+ protected async initializeProjects() {
34
+ try {
35
+ let projectId = this.projectId;
36
+
37
+ if (!projectId) {
38
+ return;
39
+ }
40
+
41
+ await this.fetchProjectDetails(projectId);
42
+ } catch (error: any) {
43
+ throw new Error(`Failed to initialize project: ${error.message}`);
44
+ }
45
+ }
46
+
47
+ async getProjectInfo() {
48
+ if (this.projectPromise) {
49
+ await this.projectPromise;
50
+ }
51
+ if (!this.project) throw new Error('Project not found');
52
+ return this.project;
53
+ }
54
+
55
+ async connectX({ onSuccess, onError }: { onSuccess: () => void; onError: () => void }) {
56
+ const project = await this.getProjectInfo();
57
+ const onWindowMessage = async (message: any) => {
58
+ if (message.data.name && message.data.name === CONST.TWITTER_AUTH_SUCCESS) {
59
+ const { isError, code, redirectUri } = message.data.data || {};
60
+ try {
61
+ await connectTwitter({ code, redirectUri: redirectUri }, this.endpoint);
62
+ onSuccess();
63
+ if (isError) {
64
+ onError();
65
+ }
66
+ } catch (error) {
67
+ onError();
68
+ } finally {
69
+ window.removeEventListener('message', onWindowMessage);
70
+ }
71
+ }
72
+ };
73
+
74
+ window.addEventListener('message', onWindowMessage);
75
+ const authorizeUrl = project.identities.find(
76
+ (item: any) => item.identityType === AirdropSigIdentityTypeEnum.Twitter
77
+ )?.authorizeUrl;
78
+ window.open(authorizeUrl + '&state=state', '_blank', 'width=600,height=800');
79
+ }
80
+
81
+ async connectWallet({
82
+ chainType,
83
+ onSuccess,
84
+ onError
85
+ }: {
86
+ chainType: ChainType;
87
+ onSuccess: () => void;
88
+ onError: (error: any) => void;
89
+ }) {
90
+ try {
91
+ const project = await this.getProjectInfo();
92
+ const wallet = WalletFactory.getWallet(chainType);
93
+ const signResult = await wallet.signin(CONST.WELCOME_SIGNATURE_STATEMENT);
94
+
95
+ const { message, signature } = signResult || {};
96
+
97
+ const recipientTypeMap = {
98
+ [ChainType.Evm]: AirdropSigIdentityTypeEnum.EVMWallet,
99
+ [ChainType.Solana]: AirdropSigIdentityTypeEnum.SolanaWallet,
100
+ [ChainType.Ton]: ''
101
+ };
102
+
103
+ const payload = {
104
+ signature: signature as string,
105
+ message: message as string,
106
+ key: wallet.publicKey || wallet.address!,
107
+ projectId: project.id,
108
+ timestamp: Date.now(),
109
+ recipientType: recipientTypeMap[chainType]
110
+ };
111
+
112
+ await bindWallet(payload, this.endpoint);
113
+ onSuccess();
114
+ } catch (error) {
115
+ onError(error);
116
+ } finally {
117
+ onSuccess();
118
+ }
119
+ }
120
+ }
@@ -1,3 +1,8 @@
1
1
  export * from './chain-config';
2
2
 
3
3
  export const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
4
+
5
+ export const CONST = {
6
+ TWITTER_AUTH_SUCCESS: 'twitter:auth:success',
7
+ WELCOME_SIGNATURE_STATEMENT: 'Welcome to TokenTable!'
8
+ } as const;
@@ -0,0 +1,92 @@
1
+ import { ISignResult } from './types';
2
+ import { prepareSignMessage } from './utils';
3
+
4
+ export abstract class WalletBase<S> {
5
+ type: string;
6
+ provider: any;
7
+ address?: string;
8
+ publicKey?: string;
9
+ chainId?: number | string;
10
+ protocolTag?: string;
11
+ authType: string;
12
+ userAddress?: string;
13
+ network?: string;
14
+
15
+ constructor(type: string, userAddress?: string) {
16
+ this.type = type;
17
+ this.provider = null;
18
+ this.address = '';
19
+ this.chainId = 1;
20
+ this.authType = '';
21
+ this.userAddress = userAddress || '';
22
+ // this.init();
23
+ }
24
+
25
+ // abstract init(): void;
26
+
27
+ // abstract init(): void;
28
+ protected get isConnected(): boolean {
29
+ throw new Error('Not implemented');
30
+ }
31
+
32
+ abstract connect(_?: string): void;
33
+
34
+ // common sign
35
+ abstract sign(_: string, _1?: string): Promise<ISignResult>;
36
+
37
+ // checkAddressValidity(): boolean {
38
+ // console.log(this.userAddress, this.address);
39
+ // if (!this.address) {
40
+ // // 兼容ton的情况,ton只有sign之后才有地址
41
+ // return true;
42
+ // }
43
+ // if (this.userAddress && this.address !== this.userAddress) {
44
+ // console.error('Wallet address and user address do not match.');
45
+ // globalVm.toggleAccountChangedModal(true);
46
+ // return false;
47
+ // }
48
+ // return true;
49
+ // }
50
+
51
+ async safeSign(_: string, _1?: string): Promise<ISignResult> {
52
+ // if (!this.checkAddressValidity()) {
53
+ // throw new Error('Wallet address and user address do not match.');
54
+ // }
55
+ return this.sign(_, _1);
56
+ }
57
+
58
+ async signin(statement?: string): Promise<ISignResult> {
59
+ const msgRes = prepareSignMessage({ statement, chainId: this.chainId!, address: this.address! });
60
+ return this.sign(JSON.stringify(msgRes, null, ' '));
61
+ }
62
+
63
+ // for metamask sign of EIP-712
64
+ signByEIP712(_: string): Promise<ISignResult> {
65
+ throw new Error(`received message:${_},Method not implemented.`);
66
+ }
67
+
68
+ getSignEIP712Message(_: { chainId: number | string; info: string; hash: string }): string {
69
+ throw new Error(`received message:${JSON.stringify(_)},Method not implemented.`);
70
+ }
71
+
72
+ getWallet(): {
73
+ address?: string;
74
+ isConnected: boolean;
75
+ publicKey: string | undefined;
76
+ type: string;
77
+ network: string | undefined;
78
+ } {
79
+ // 获取钱包信息
80
+ return {
81
+ type: this.type,
82
+ isConnected: this.isConnected,
83
+ address: this.address,
84
+ publicKey: this.publicKey,
85
+ network: this.network
86
+ };
87
+ }
88
+
89
+ abstract getStore(): S;
90
+
91
+ // abstract decrypt(message: string, address: string): Promise<string>;
92
+ }
@@ -0,0 +1,35 @@
1
+ import { EvmWallet } from './providers/evm';
2
+ import { ChainType } from './types';
3
+ import { TonWallet } from './providers/ton';
4
+ import { SolWallet } from './providers/solana';
5
+
6
+ export abstract class WalletFactory {
7
+ private static instances: Record<string, EvmWallet | TonWallet | SolWallet> = {};
8
+ static getWallet(type: ChainType, userAddress?: string): EvmWallet | TonWallet | SolWallet {
9
+ if (!this.instances[type]) {
10
+ let WalletClass;
11
+ switch (type) {
12
+ case ChainType.Evm:
13
+ WalletClass = EvmWallet;
14
+ break;
15
+ case ChainType.Ton:
16
+ WalletClass = TonWallet;
17
+ break;
18
+ // case ChainType.Starknet:
19
+ // WalletClass = StarknetWallet;
20
+ // break;
21
+ case ChainType.Solana:
22
+ WalletClass = SolWallet;
23
+ break;
24
+ default:
25
+ throw new Error('Unsupported wallet type');
26
+ }
27
+
28
+ if (!WalletClass) {
29
+ throw new Error('Unsupported wallet type');
30
+ }
31
+ this.instances[type] = new WalletClass(type, userAddress);
32
+ }
33
+ return this.instances[type];
34
+ }
35
+ }
@@ -0,0 +1,3 @@
1
+ export { WalletFactory } from './WalletFactory';
2
+
3
+ export * from './providers';
@@ -0,0 +1,90 @@
1
+ import { useConnectModal } from '@rainbow-me/rainbowkit';
2
+ import { ResolvedRegister, useAccount, useWalletClient } from 'wagmi';
3
+ import { WalletBase } from '../../WalletBase';
4
+ import { disconnect, getAccount, signMessage } from 'wagmi/actions';
5
+ import { EventEmitter } from 'events';
6
+ import { ISignResult } from '../../types';
7
+ import { get4361Message, prepareSignMessage } from '../../utils';
8
+
9
+ export const eventBus = new EventEmitter();
10
+
11
+ const EVM_EVENT_KEY = 'evm_signin';
12
+
13
+ export interface EVMStoreType {
14
+ account: ReturnType<typeof useAccount> | null;
15
+ connectModal: ReturnType<typeof useConnectModal> | undefined;
16
+ walletClient: ReturnType<typeof useWalletClient> | undefined;
17
+ config: ResolvedRegister['config'] | undefined;
18
+ counter: number;
19
+ }
20
+
21
+ export const evmStore: EVMStoreType = {
22
+ account: null,
23
+ connectModal: undefined,
24
+ walletClient: undefined,
25
+ config: undefined,
26
+ counter: 0
27
+ };
28
+
29
+ export const getEventKey = () => {
30
+ return `${EVM_EVENT_KEY}_${evmStore.counter}`;
31
+ };
32
+
33
+ export class EvmWallet extends WalletBase<EVMStoreType> {
34
+ get isConnected() {
35
+ return !!(evmStore.account?.address && evmStore.account?.isConnected);
36
+ }
37
+
38
+ connect() {
39
+ evmStore?.connectModal?.openConnectModal?.();
40
+ }
41
+
42
+ async disconnect() {
43
+ await disconnect(evmStore.config!);
44
+ }
45
+
46
+ async sign(_message: string) {
47
+ const sign = await signMessage(evmStore.config!, {
48
+ message: _message
49
+ });
50
+ return {
51
+ message: _message,
52
+ signature: sign
53
+ };
54
+ }
55
+
56
+ override async signin(statement?: string): Promise<ISignResult> {
57
+ return new Promise((resolve) => {
58
+ const signCallback = (data: ReturnType<typeof useAccount>) => {
59
+ this.address = data.address;
60
+ this.chainId = data.chainId;
61
+ const msg = prepareSignMessage({ statement, chainId: data.chainId!, address: data.address! });
62
+ const fullMessage = get4361Message(msg);
63
+
64
+ const res = this.sign(fullMessage);
65
+
66
+ resolve(res);
67
+ };
68
+
69
+ const account = getAccount(evmStore.config!);
70
+ this.provider = evmStore.walletClient;
71
+
72
+ if (this.isConnected) {
73
+ signCallback(account);
74
+ return;
75
+ }
76
+
77
+ evmStore.counter++;
78
+ this.connect();
79
+ const eventKey = getEventKey();
80
+ eventBus.once(eventKey, (data) => {
81
+ console.log('listen event key success, event key = %s, data = %j', eventKey, data);
82
+ signCallback(data);
83
+ });
84
+ });
85
+ }
86
+
87
+ getStore(): EVMStoreType {
88
+ return evmStore;
89
+ }
90
+ }
@@ -0,0 +1,43 @@
1
+ import '@rainbow-me/rainbowkit/styles.css';
2
+ import { RainbowKitProvider, lightTheme, useConnectModal } from '@rainbow-me/rainbowkit';
3
+ import { ResolvedRegister, WagmiProvider, useAccount, useWalletClient } from 'wagmi';
4
+ import { ReactNode, useEffect } from 'react';
5
+
6
+ import { evmStore, getEventKey, eventBus } from './EvmWallet';
7
+
8
+ export { EvmWallet } from './EvmWallet';
9
+
10
+ export const EVMConnector = () => {
11
+ const connectModal = useConnectModal();
12
+ const walletClient = useWalletClient();
13
+ const account = useAccount();
14
+
15
+ evmStore.connectModal = connectModal;
16
+ evmStore.walletClient = walletClient;
17
+ evmStore.account = account;
18
+
19
+ useEffect(() => {
20
+ if (account.isConnected) {
21
+ const eventKey = getEventKey();
22
+ eventBus.emit(eventKey, account);
23
+ }
24
+ }, [account]);
25
+
26
+ return null;
27
+ };
28
+
29
+ export const EvmProvider = ({ children, config }: { children: ReactNode; config: ResolvedRegister['config'] }) => {
30
+ evmStore.config = config;
31
+ return (
32
+ <WagmiProvider config={config}>
33
+ <RainbowKitProvider
34
+ theme={lightTheme({
35
+ accentColor: '#DD8D58'
36
+ })}
37
+ >
38
+ {children}
39
+ <EVMConnector />
40
+ </RainbowKitProvider>
41
+ </WagmiProvider>
42
+ );
43
+ };
@@ -0,0 +1,4 @@
1
+ export { EvmWallet, EvmProvider } from './evm';
2
+ export { SolWallet, SolanaProvider } from './solana';
3
+ // export { StarknetWallet } from './starknet';
4
+ export { TonWallet, TonProvider } from './ton';