@sign-global/tokentable 0.2.1 → 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.
- package/.github/workflows/beta.yml +28 -0
- package/dist/cjs/airdrop/AirdropClient.d.ts +46 -0
- package/dist/cjs/airdrop/AirdropClient.js +244 -0
- package/dist/cjs/airdrop/AirdropClient.js.map +1 -0
- package/dist/cjs/airdrop/SignatureAirdropClient.d.ts +26 -0
- package/dist/cjs/airdrop/SignatureAirdropClient.js +109 -0
- package/dist/cjs/airdrop/SignatureAirdropClient.js.map +1 -0
- package/dist/cjs/airdrop/common/index.d.ts +1 -1
- package/dist/cjs/airdrop/constants/chain-config.js +1 -1
- package/dist/cjs/airdrop/constants/index.d.ts +4 -0
- package/dist/cjs/airdrop/constants/index.js +5 -1
- package/dist/cjs/airdrop/constants/index.js.map +1 -1
- package/dist/cjs/airdrop/core/useWeb3ConnectWallet/WalletBase.d.ts +32 -0
- package/dist/cjs/airdrop/core/useWeb3ConnectWallet/WalletBase.js +71 -0
- package/dist/cjs/airdrop/core/useWeb3ConnectWallet/WalletBase.js.map +1 -0
- package/dist/cjs/airdrop/core/useWeb3ConnectWallet/WalletFactory.d.ts +8 -0
- package/dist/cjs/airdrop/core/useWeb3ConnectWallet/WalletFactory.js +38 -0
- package/dist/cjs/airdrop/core/useWeb3ConnectWallet/WalletFactory.js.map +1 -0
- package/dist/cjs/airdrop/core/useWeb3ConnectWallet/index.d.ts +2 -0
- package/dist/cjs/airdrop/core/useWeb3ConnectWallet/index.js +21 -0
- package/dist/cjs/airdrop/core/useWeb3ConnectWallet/index.js.map +1 -0
- package/dist/cjs/airdrop/core/useWeb3ConnectWallet/providers/evm/EvmWallet.d.ts +26 -0
- package/dist/cjs/airdrop/core/useWeb3ConnectWallet/providers/evm/EvmWallet.js +70 -0
- package/dist/cjs/airdrop/core/useWeb3ConnectWallet/providers/evm/EvmWallet.js.map +1 -0
- package/dist/cjs/airdrop/core/useWeb3ConnectWallet/providers/evm/index.d.ts +9 -0
- package/dist/cjs/airdrop/core/useWeb3ConnectWallet/providers/evm/index.js +35 -0
- package/dist/cjs/airdrop/core/useWeb3ConnectWallet/providers/evm/index.js.map +1 -0
- package/dist/cjs/airdrop/core/useWeb3ConnectWallet/providers/index.d.ts +3 -0
- package/dist/cjs/airdrop/core/useWeb3ConnectWallet/providers/index.js +14 -0
- package/dist/cjs/airdrop/core/useWeb3ConnectWallet/providers/index.js.map +1 -0
- package/dist/cjs/airdrop/core/useWeb3ConnectWallet/providers/solana/index.d.ts +27 -0
- package/dist/cjs/airdrop/core/useWeb3ConnectWallet/providers/solana/index.js +119 -0
- package/dist/cjs/airdrop/core/useWeb3ConnectWallet/providers/solana/index.js.map +1 -0
- package/dist/cjs/airdrop/core/useWeb3ConnectWallet/providers/ton/TonWallet.d.ts +23 -0
- package/dist/cjs/airdrop/core/useWeb3ConnectWallet/providers/ton/TonWallet.js +117 -0
- package/dist/cjs/airdrop/core/useWeb3ConnectWallet/providers/ton/TonWallet.js.map +1 -0
- package/dist/cjs/airdrop/core/useWeb3ConnectWallet/providers/ton/index.d.ts +9 -0
- package/dist/cjs/airdrop/core/useWeb3ConnectWallet/providers/ton/index.js +29 -0
- package/dist/cjs/airdrop/core/useWeb3ConnectWallet/providers/ton/index.js.map +1 -0
- package/dist/cjs/airdrop/core/useWeb3ConnectWallet/types.d.ts +38 -0
- package/dist/cjs/airdrop/core/useWeb3ConnectWallet/types.js +30 -0
- package/dist/cjs/airdrop/core/useWeb3ConnectWallet/types.js.map +1 -0
- package/dist/cjs/airdrop/core/useWeb3ConnectWallet/utils/index.d.ts +8 -0
- package/dist/cjs/airdrop/core/useWeb3ConnectWallet/utils/index.js +41 -0
- package/dist/cjs/airdrop/core/useWeb3ConnectWallet/utils/index.js.map +1 -0
- package/dist/cjs/airdrop/index.d.ts +4 -46
- package/dist/cjs/airdrop/index.js +22 -244
- package/dist/cjs/airdrop/index.js.map +1 -1
- package/dist/cjs/airdrop/services/index.d.ts +15 -0
- package/dist/cjs/airdrop/services/index.js +16 -1
- package/dist/cjs/airdrop/services/index.js.map +1 -1
- package/dist/cjs/airdrop/types/index.d.ts +18 -6
- package/dist/cjs/airdrop/types/index.js +7 -1
- package/dist/cjs/airdrop/types/index.js.map +1 -1
- package/dist/cjs/index.d.ts +1 -1
- package/dist/cjs/index.js +7 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/utils/utils.d.ts +1 -0
- package/dist/cjs/utils/utils.js +2 -1
- package/dist/cjs/utils/utils.js.map +1 -1
- package/dist/esm/airdrop/AirdropClient.d.ts +46 -0
- package/dist/esm/airdrop/AirdropClient.js +240 -0
- package/dist/esm/airdrop/AirdropClient.js.map +1 -0
- package/dist/esm/airdrop/SignatureAirdropClient.d.ts +26 -0
- package/dist/esm/airdrop/SignatureAirdropClient.js +105 -0
- package/dist/esm/airdrop/SignatureAirdropClient.js.map +1 -0
- package/dist/esm/airdrop/common/index.d.ts +1 -1
- package/dist/esm/airdrop/constants/chain-config.js +1 -1
- package/dist/esm/airdrop/constants/index.d.ts +4 -0
- package/dist/esm/airdrop/constants/index.js +4 -0
- package/dist/esm/airdrop/constants/index.js.map +1 -1
- package/dist/esm/airdrop/core/useWeb3ConnectWallet/WalletBase.d.ts +32 -0
- package/dist/esm/airdrop/core/useWeb3ConnectWallet/WalletBase.js +67 -0
- package/dist/esm/airdrop/core/useWeb3ConnectWallet/WalletBase.js.map +1 -0
- package/dist/esm/airdrop/core/useWeb3ConnectWallet/WalletFactory.d.ts +8 -0
- package/dist/esm/airdrop/core/useWeb3ConnectWallet/WalletFactory.js +34 -0
- package/dist/esm/airdrop/core/useWeb3ConnectWallet/WalletFactory.js.map +1 -0
- package/dist/esm/airdrop/core/useWeb3ConnectWallet/index.d.ts +2 -0
- package/dist/esm/airdrop/core/useWeb3ConnectWallet/index.js +3 -0
- package/dist/esm/airdrop/core/useWeb3ConnectWallet/index.js.map +1 -0
- package/dist/esm/airdrop/core/useWeb3ConnectWallet/providers/evm/EvmWallet.d.ts +26 -0
- package/dist/esm/airdrop/core/useWeb3ConnectWallet/providers/evm/EvmWallet.js +65 -0
- package/dist/esm/airdrop/core/useWeb3ConnectWallet/providers/evm/EvmWallet.js.map +1 -0
- package/dist/esm/airdrop/core/useWeb3ConnectWallet/providers/evm/index.d.ts +9 -0
- package/dist/esm/airdrop/core/useWeb3ConnectWallet/providers/evm/index.js +29 -0
- package/dist/esm/airdrop/core/useWeb3ConnectWallet/providers/evm/index.js.map +1 -0
- package/dist/esm/airdrop/core/useWeb3ConnectWallet/providers/index.d.ts +3 -0
- package/dist/esm/airdrop/core/useWeb3ConnectWallet/providers/index.js +5 -0
- package/dist/esm/airdrop/core/useWeb3ConnectWallet/providers/index.js.map +1 -0
- package/dist/esm/airdrop/core/useWeb3ConnectWallet/providers/solana/index.d.ts +27 -0
- package/dist/esm/airdrop/core/useWeb3ConnectWallet/providers/solana/index.js +109 -0
- package/dist/esm/airdrop/core/useWeb3ConnectWallet/providers/solana/index.js.map +1 -0
- package/dist/esm/airdrop/core/useWeb3ConnectWallet/providers/ton/TonWallet.d.ts +23 -0
- package/dist/esm/airdrop/core/useWeb3ConnectWallet/providers/ton/TonWallet.js +112 -0
- package/dist/esm/airdrop/core/useWeb3ConnectWallet/providers/ton/TonWallet.js.map +1 -0
- package/dist/esm/airdrop/core/useWeb3ConnectWallet/providers/ton/index.d.ts +9 -0
- package/dist/esm/airdrop/core/useWeb3ConnectWallet/providers/ton/index.js +24 -0
- package/dist/esm/airdrop/core/useWeb3ConnectWallet/providers/ton/index.js.map +1 -0
- package/dist/esm/airdrop/core/useWeb3ConnectWallet/types.d.ts +38 -0
- package/dist/esm/airdrop/core/useWeb3ConnectWallet/types.js +27 -0
- package/dist/esm/airdrop/core/useWeb3ConnectWallet/types.js.map +1 -0
- package/dist/esm/airdrop/core/useWeb3ConnectWallet/utils/index.d.ts +8 -0
- package/dist/esm/airdrop/core/useWeb3ConnectWallet/utils/index.js +35 -0
- package/dist/esm/airdrop/core/useWeb3ConnectWallet/utils/index.js.map +1 -0
- package/dist/esm/airdrop/index.d.ts +4 -46
- package/dist/esm/airdrop/index.js +4 -242
- package/dist/esm/airdrop/index.js.map +1 -1
- package/dist/esm/airdrop/services/index.d.ts +15 -0
- package/dist/esm/airdrop/services/index.js +12 -0
- package/dist/esm/airdrop/services/index.js.map +1 -1
- package/dist/esm/airdrop/types/index.d.ts +18 -6
- package/dist/esm/airdrop/types/index.js +6 -0
- package/dist/esm/airdrop/types/index.js.map +1 -1
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/utils/utils.d.ts +1 -0
- package/dist/esm/utils/utils.js +1 -0
- package/dist/esm/utils/utils.js.map +1 -1
- package/package.json +15 -3
- package/src/airdrop/AirdropClient.ts +301 -0
- package/src/airdrop/SignatureAirdropClient.ts +120 -0
- package/src/airdrop/constants/chain-config.ts +1 -1
- package/src/airdrop/constants/index.ts +5 -0
- package/src/airdrop/core/useWeb3ConnectWallet/WalletBase.ts +92 -0
- package/src/airdrop/core/useWeb3ConnectWallet/WalletFactory.tsx +35 -0
- package/src/airdrop/core/useWeb3ConnectWallet/index.ts +3 -0
- package/src/airdrop/core/useWeb3ConnectWallet/providers/evm/EvmWallet.ts +90 -0
- package/src/airdrop/core/useWeb3ConnectWallet/providers/evm/index.tsx +43 -0
- package/src/airdrop/core/useWeb3ConnectWallet/providers/index.ts +4 -0
- package/src/airdrop/core/useWeb3ConnectWallet/providers/solana/index.tsx +141 -0
- package/src/airdrop/core/useWeb3ConnectWallet/providers/ton/TonWallet.ts +149 -0
- package/src/airdrop/core/useWeb3ConnectWallet/providers/ton/index.tsx +41 -0
- package/src/airdrop/core/useWeb3ConnectWallet/types.ts +45 -0
- package/src/airdrop/core/useWeb3ConnectWallet/utils/index.ts +61 -0
- package/src/airdrop/index.ts +4 -309
- package/src/airdrop/services/index.ts +29 -1
- package/src/airdrop/tests/index.test.ts +5 -8
- package/src/airdrop/types/index.ts +22 -6
- package/src/index.ts +9 -1
- package/src/utils/utils.ts +2 -0
- 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
|
+
}
|
|
@@ -19,7 +19,7 @@ import { mevmDevNet, tonMainNet, tonTestNet } from './network';
|
|
|
19
19
|
export const ChainConfig = {
|
|
20
20
|
[mainnet.id]: {
|
|
21
21
|
contractAddress: '0x0993Dd5384BcD116A3543889BC6ffA6C98948631',
|
|
22
|
-
rpcUrl: 'https://eth-mainnet.g.alchemy.com/v2/
|
|
22
|
+
rpcUrl: 'https://eth-mainnet.g.alchemy.com/v2/udrqNPSB6i5n5L6QSM31Ng72h_hFOrVT'
|
|
23
23
|
},
|
|
24
24
|
[bsc.id]: {
|
|
25
25
|
contractAddress: '0x6c241FF6d5c3f074b49aC10872770529d9e27BA4',
|
|
@@ -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,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
|
+
};
|