aiia-vault-sdk 1.0.2 → 1.1.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/dist/SeedRoundFundraiser.d.ts +390 -3
- package/dist/SeedRoundFundraiser.js +1 -445
- package/dist/TradingVault.js +1 -521
- package/dist/common.js +1 -2
- package/dist/contracts/SeedRoundFundraiser.js +1 -2
- package/dist/contracts/TradingVault.js +1 -2
- package/dist/index.js +1 -19
- package/dist/types.js +1 -2
- package/dist/utils.js +1 -370
- package/package.json +6 -4
@@ -1,445 +1 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
-
};
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
-
exports.SeedRoundFundraiserSDK = void 0;
|
7
|
-
const ethers_1 = require("ethers");
|
8
|
-
const SeedRoundFundraiser_json_1 = __importDefault(require("./abis/SeedRoundFundraiser.json"));
|
9
|
-
const contracts_json_1 = __importDefault(require("./contracts.json"));
|
10
|
-
const utils_1 = require("./utils");
|
11
|
-
const whitelist_tokens_json_1 = __importDefault(require("./whitelist-tokens.json"));
|
12
|
-
class SeedRoundFundraiserSDK {
|
13
|
-
constructor(rpcUrls, contractAddress) {
|
14
|
-
this.name = "SeedRoundFundraiser";
|
15
|
-
this.isBootstrapped = false;
|
16
|
-
this.PRICE_PRECISION = 1e18;
|
17
|
-
this.whitelistedTokensArray = [];
|
18
|
-
this.formatEventArgs = (event) => {
|
19
|
-
const { eventName, args } = event;
|
20
|
-
switch (eventName) {
|
21
|
-
case "TokenWhitelisted":
|
22
|
-
return {
|
23
|
-
...event,
|
24
|
-
args: {
|
25
|
-
...args,
|
26
|
-
token: args.token.toLowerCase(),
|
27
|
-
price: Number(args.price) / this.PRICE_PRECISION,
|
28
|
-
},
|
29
|
-
};
|
30
|
-
case "TokenPriceUpdated":
|
31
|
-
return {
|
32
|
-
...event,
|
33
|
-
args: {
|
34
|
-
...args,
|
35
|
-
token: args.token.toLowerCase(),
|
36
|
-
oldPrice: Number(args.oldPrice) / this.PRICE_PRECISION,
|
37
|
-
newPrice: Number(args.newPrice) / this.PRICE_PRECISION,
|
38
|
-
},
|
39
|
-
};
|
40
|
-
case "TokenRemovedFromWhitelist":
|
41
|
-
return {
|
42
|
-
...event,
|
43
|
-
args: {
|
44
|
-
...args,
|
45
|
-
token: args.token.toLowerCase(),
|
46
|
-
},
|
47
|
-
};
|
48
|
-
case "RoundCreated":
|
49
|
-
case "RoundUpdated":
|
50
|
-
return {
|
51
|
-
...event,
|
52
|
-
args: {
|
53
|
-
...args,
|
54
|
-
roundId: Number(args.roundId),
|
55
|
-
startTime: Number(args.startTime),
|
56
|
-
endTime: Number(args.endTime),
|
57
|
-
targetFund: Number(args.targetFund) / this.PRICE_PRECISION,
|
58
|
-
totalAllocation: Number(args.totalAllocation),
|
59
|
-
maxFundPerAccount: Number(args.maxFundPerAccount) / this.PRICE_PRECISION,
|
60
|
-
},
|
61
|
-
};
|
62
|
-
case "RoundEnded":
|
63
|
-
return {
|
64
|
-
...event,
|
65
|
-
args: {
|
66
|
-
...args,
|
67
|
-
roundId: Number(args.roundId),
|
68
|
-
raisedFunds: Number(args.raisedFunds) / this.PRICE_PRECISION,
|
69
|
-
participants: Number(args.participants),
|
70
|
-
},
|
71
|
-
};
|
72
|
-
case "Contribution":
|
73
|
-
return {
|
74
|
-
...event,
|
75
|
-
args: {
|
76
|
-
...args,
|
77
|
-
roundId: Number(args.roundId),
|
78
|
-
contributor: args.contributor.toLowerCase(),
|
79
|
-
token: args.token.toLowerCase(),
|
80
|
-
amount: Number(args.amount),
|
81
|
-
fundAmount: Number(args.fundAmount) / this.PRICE_PRECISION,
|
82
|
-
tokenAllocation: Number(args.tokenAllocation),
|
83
|
-
},
|
84
|
-
};
|
85
|
-
case "TokensClaimed":
|
86
|
-
return {
|
87
|
-
...event,
|
88
|
-
args: {
|
89
|
-
...args,
|
90
|
-
roundId: Number(args.roundId),
|
91
|
-
user: args.user.toLowerCase(),
|
92
|
-
amount: Number(args.amount),
|
93
|
-
},
|
94
|
-
};
|
95
|
-
case "ProjectTokenUpdated":
|
96
|
-
return {
|
97
|
-
...event,
|
98
|
-
args: {
|
99
|
-
...args,
|
100
|
-
oldToken: args.oldToken.toLowerCase(),
|
101
|
-
newToken: args.newToken.toLowerCase(),
|
102
|
-
},
|
103
|
-
};
|
104
|
-
case "ClaimingEnabledUpdated":
|
105
|
-
case "RefundEnabledUpdated":
|
106
|
-
return {
|
107
|
-
...event,
|
108
|
-
args: {
|
109
|
-
...args,
|
110
|
-
roundId: Number(args.roundId),
|
111
|
-
enabled: args.enabled,
|
112
|
-
},
|
113
|
-
};
|
114
|
-
case "Refunded":
|
115
|
-
return {
|
116
|
-
...event,
|
117
|
-
args: {
|
118
|
-
...args,
|
119
|
-
roundId: Number(args.roundId),
|
120
|
-
user: args.user.toLowerCase(),
|
121
|
-
token: args.token.toLowerCase(),
|
122
|
-
amount: Number(args.amount),
|
123
|
-
},
|
124
|
-
};
|
125
|
-
default:
|
126
|
-
return event;
|
127
|
-
}
|
128
|
-
};
|
129
|
-
// Convert single RPC to array if needed
|
130
|
-
const rpcArray = Array.isArray(rpcUrls) ? rpcUrls : [rpcUrls];
|
131
|
-
// Create providers for each RPC
|
132
|
-
this.providers = rpcArray.map((rpc) => new ethers_1.ethers.JsonRpcProvider(rpc));
|
133
|
-
// Get a random provider for initial setup
|
134
|
-
const initialProvider = (0, utils_1.getRandomProvider)(this.providers);
|
135
|
-
// Determine contract address based on RPC URL if not provided
|
136
|
-
this.contractAddress = (0, utils_1.resolveContractAddress)(rpcArray[0], this.name, contracts_json_1.default, contractAddress);
|
137
|
-
// Initialize contract with initial provider
|
138
|
-
this.contract = new ethers_1.ethers.Contract(this.contractAddress, SeedRoundFundraiser_json_1.default.abi, initialProvider);
|
139
|
-
// Determine network name from RPC URL
|
140
|
-
this.networkName = this.getNetworkNameFromRpc(rpcArray[0]);
|
141
|
-
// Import whitelisted tokens from JSON file
|
142
|
-
this.importWhitelistedTokens();
|
143
|
-
}
|
144
|
-
getNetworkNameFromRpc(rpcUrl) {
|
145
|
-
const rpcLower = rpcUrl.toLowerCase();
|
146
|
-
// Extract network name from RPC URL or use a default method
|
147
|
-
if (rpcLower.includes("sepolia.base")) {
|
148
|
-
return "base-sepolia";
|
149
|
-
}
|
150
|
-
else if (rpcLower.includes("sepolia")) {
|
151
|
-
return "sepolia";
|
152
|
-
}
|
153
|
-
else if (rpcLower.includes("mainnet")) {
|
154
|
-
return "mainnet";
|
155
|
-
}
|
156
|
-
else if (rpcLower.includes("localhost") ||
|
157
|
-
rpcLower.includes("127.0.0.1")) {
|
158
|
-
return "hardhat";
|
159
|
-
}
|
160
|
-
// Default to sepolia if can't determine
|
161
|
-
return "sepolia";
|
162
|
-
}
|
163
|
-
importWhitelistedTokens() {
|
164
|
-
try {
|
165
|
-
// Try to read the whitelist-tokens.json file
|
166
|
-
// Check if the network exists in the whitelist data
|
167
|
-
if (whitelist_tokens_json_1.default[this.networkName]) {
|
168
|
-
this.whitelistedTokensArray = whitelist_tokens_json_1.default[this.networkName].map((address) => address.toLowerCase());
|
169
|
-
console.log(`Imported ${this.whitelistedTokensArray.length} whitelisted tokens for ${this.networkName} network`);
|
170
|
-
}
|
171
|
-
else {
|
172
|
-
console.log(`No whitelisted tokens found for ${this.networkName} network`);
|
173
|
-
}
|
174
|
-
}
|
175
|
-
catch (error) {
|
176
|
-
console.error("Error importing whitelisted tokens:", error);
|
177
|
-
}
|
178
|
-
}
|
179
|
-
// Get whitelisted token info from the array
|
180
|
-
async getWhitelistedTokenInfo(tokenAddress) {
|
181
|
-
// Convert to lowercase for case-insensitive comparison
|
182
|
-
const normalizedAddress = tokenAddress.toLowerCase();
|
183
|
-
// Check if token is in the whitelisted array
|
184
|
-
if (!this.whitelistedTokensArray.includes(normalizedAddress)) {
|
185
|
-
return null;
|
186
|
-
}
|
187
|
-
// Get token info from contract
|
188
|
-
const tokenInfo = await this.getWhitelistedToken(normalizedAddress);
|
189
|
-
// Skip if not whitelisted in contract
|
190
|
-
if (!tokenInfo.isWhitelisted) {
|
191
|
-
return null;
|
192
|
-
}
|
193
|
-
let symbol = "ETH";
|
194
|
-
let decimals = 18;
|
195
|
-
// Get token symbol and decimals
|
196
|
-
if (normalizedAddress !== ethers_1.ethers.ZeroAddress) {
|
197
|
-
[symbol, decimals] = await Promise.all([
|
198
|
-
(0, utils_1.getTokenSymbol)(normalizedAddress, this.getRandomProvider()),
|
199
|
-
(0, utils_1.getTokenDecimals)(normalizedAddress, this.getRandomProvider()),
|
200
|
-
]);
|
201
|
-
}
|
202
|
-
// Get latest active round to determine max contribution
|
203
|
-
const latestRoundId = await this.getLatestRoundId();
|
204
|
-
let maxTokenContribution = 0;
|
205
|
-
if (latestRoundId >= 0) {
|
206
|
-
const roundInfo = await this.getRound(latestRoundId);
|
207
|
-
if (roundInfo.exists && !roundInfo.ended) {
|
208
|
-
// Calculate max token contribution based on maxFundPerAccount and token price
|
209
|
-
// maxTokens = (maxFundPerAccount / tokenPrice) * 10^decimals
|
210
|
-
maxTokenContribution = roundInfo.maxFundPerAccount / tokenInfo.price;
|
211
|
-
}
|
212
|
-
}
|
213
|
-
return {
|
214
|
-
address: normalizedAddress,
|
215
|
-
isWhitelisted: tokenInfo.isWhitelisted,
|
216
|
-
price: tokenInfo.price,
|
217
|
-
symbol: symbol,
|
218
|
-
maxContribution: maxTokenContribution,
|
219
|
-
maxContributionRaw: Math.floor(maxTokenContribution * Math.pow(10, decimals)),
|
220
|
-
decimals: decimals,
|
221
|
-
};
|
222
|
-
}
|
223
|
-
// Get all whitelisted tokens info
|
224
|
-
async getAllWhitelistedTokensInfo() {
|
225
|
-
const results = [];
|
226
|
-
for (const tokenAddress of this.whitelistedTokensArray) {
|
227
|
-
const tokenInfo = await this.getWhitelistedTokenInfo(tokenAddress);
|
228
|
-
if (tokenInfo) {
|
229
|
-
results.push(tokenInfo);
|
230
|
-
}
|
231
|
-
}
|
232
|
-
return results;
|
233
|
-
}
|
234
|
-
getRandomProvider() {
|
235
|
-
return (0, utils_1.getRandomProvider)(this.providers);
|
236
|
-
}
|
237
|
-
getContractWithRandomProvider() {
|
238
|
-
return new ethers_1.ethers.Contract(this.contractAddress, SeedRoundFundraiser_json_1.default.abi, this.getRandomProvider());
|
239
|
-
}
|
240
|
-
async bootstrap() {
|
241
|
-
if (this.isBootstrapped) {
|
242
|
-
return;
|
243
|
-
}
|
244
|
-
// Check health of all RPCs and remove inactive ones
|
245
|
-
const healthChecks = await Promise.all(this.providers.map((provider, index) => (0, utils_1.checkRpcHealth)(provider, index)));
|
246
|
-
// Filter out inactive providers
|
247
|
-
this.providers = this.providers.filter((_, index) => healthChecks[index]);
|
248
|
-
if (this.providers.length === 0) {
|
249
|
-
throw new Error("No active RPC providers available");
|
250
|
-
}
|
251
|
-
this.isBootstrapped = true;
|
252
|
-
}
|
253
|
-
async signAndSendTransaction(tx, wallet, callbacks = {}) {
|
254
|
-
return (0, utils_1.signAndSendTransaction)(tx, wallet, () => this.getRandomProvider(), callbacks, this.contract);
|
255
|
-
}
|
256
|
-
// Transaction builders
|
257
|
-
async buildAddWhitelistedTokenTx(token, price) {
|
258
|
-
const priceBigInt = BigInt(Math.floor(price * this.PRICE_PRECISION));
|
259
|
-
return await this.contract.addWhitelistedToken.populateTransaction(token, priceBigInt);
|
260
|
-
}
|
261
|
-
async buildUpdateTokenPriceTx(token, newPrice) {
|
262
|
-
const priceBigInt = BigInt(Math.floor(newPrice * this.PRICE_PRECISION));
|
263
|
-
return await this.contract.updateTokenPrice.populateTransaction(token, priceBigInt);
|
264
|
-
}
|
265
|
-
async buildRemoveWhitelistedTokenTx(token) {
|
266
|
-
return await this.contract.removeWhitelistedToken.populateTransaction(token);
|
267
|
-
}
|
268
|
-
async buildCreateRoundTx(startTime, endTime, targetFund, totalAllocation, maxFundPerAccount) {
|
269
|
-
const targetFundBigInt = BigInt(Math.floor(targetFund * this.PRICE_PRECISION));
|
270
|
-
const maxFundPerAccountBigInt = BigInt(Math.floor(maxFundPerAccount * this.PRICE_PRECISION));
|
271
|
-
const decimals = await (0, utils_1.getTokenDecimals)(await this.getProjectToken(), this.getRandomProvider());
|
272
|
-
const totalAllocationBigInt = BigInt(Math.floor(totalAllocation * Math.pow(10, decimals)));
|
273
|
-
return await this.contract.createRound.populateTransaction(startTime, endTime, targetFundBigInt, totalAllocationBigInt, maxFundPerAccountBigInt);
|
274
|
-
}
|
275
|
-
async buildUpdateRoundTx(roundId, startTime, endTime, targetFund, totalAllocation, maxFundPerAccount) {
|
276
|
-
const targetFundBigInt = BigInt(Math.floor(targetFund * this.PRICE_PRECISION));
|
277
|
-
const maxFundPerAccountBigInt = BigInt(Math.floor(maxFundPerAccount * this.PRICE_PRECISION));
|
278
|
-
return await this.contract.updateRound.populateTransaction(roundId, startTime, endTime, targetFundBigInt, totalAllocation, maxFundPerAccountBigInt);
|
279
|
-
}
|
280
|
-
async buildEndRoundTx(roundId) {
|
281
|
-
return await this.contract.endRound.populateTransaction(roundId);
|
282
|
-
}
|
283
|
-
async buildSetClaimingEnabledTx(roundId, enabled) {
|
284
|
-
return await this.contract.setClaimingEnabled.populateTransaction(roundId, enabled);
|
285
|
-
}
|
286
|
-
async buildSetRefundEnabledTx(roundId, enabled) {
|
287
|
-
return await this.contract.setRefundEnabled.populateTransaction(roundId, enabled);
|
288
|
-
}
|
289
|
-
async buildContributeTx(roundId, token, amount) {
|
290
|
-
let decimals = 18;
|
291
|
-
if (token !== ethers_1.ethers.ZeroAddress) {
|
292
|
-
decimals = await (0, utils_1.getTokenDecimals)(token, this.getRandomProvider());
|
293
|
-
}
|
294
|
-
const amountWei = ethers_1.ethers.parseUnits(amount.toString(), decimals);
|
295
|
-
const tx = await this.contract.contribute.populateTransaction(roundId, token, token === ethers_1.ethers.ZeroAddress ? 0 : amountWei);
|
296
|
-
if (token === ethers_1.ethers.ZeroAddress) {
|
297
|
-
tx.value = amountWei;
|
298
|
-
}
|
299
|
-
return tx;
|
300
|
-
}
|
301
|
-
async buildClaimTokensTx() {
|
302
|
-
return await this.contract.claimTokens.populateTransaction();
|
303
|
-
}
|
304
|
-
async buildRefundTx() {
|
305
|
-
return await this.contract.refund.populateTransaction();
|
306
|
-
}
|
307
|
-
async buildWithdrawFundsTx(token, amount) {
|
308
|
-
const decimals = await (0, utils_1.getTokenDecimals)(token, this.getRandomProvider());
|
309
|
-
const amountBigInt = BigInt(Math.floor(amount * Math.pow(10, decimals)));
|
310
|
-
return await this.contract.withdrawFunds.populateTransaction(token, amountBigInt);
|
311
|
-
}
|
312
|
-
async buildClaimTokensByRoundIdTx(roundId) {
|
313
|
-
return await this.contract.claimTokensByRoundId.populateTransaction(roundId);
|
314
|
-
}
|
315
|
-
// Read methods
|
316
|
-
async getWhitelistedToken(token) {
|
317
|
-
const result = await this.contract.whitelistedTokens(token);
|
318
|
-
return {
|
319
|
-
isWhitelisted: result.isWhitelisted,
|
320
|
-
price: Number(result.price) / this.PRICE_PRECISION,
|
321
|
-
};
|
322
|
-
}
|
323
|
-
async getRound(roundId) {
|
324
|
-
const result = await this.contract.rounds(roundId);
|
325
|
-
return {
|
326
|
-
startTime: Number(result.startTime),
|
327
|
-
endTime: Number(result.endTime),
|
328
|
-
targetFund: Number(result.targetFund) / this.PRICE_PRECISION,
|
329
|
-
totalAllocation: Number(result.totalAllocation),
|
330
|
-
maxFundPerAccount: Number(result.maxFundPerAccount) / this.PRICE_PRECISION,
|
331
|
-
exists: result.exists,
|
332
|
-
ended: result.ended,
|
333
|
-
claimingEnabled: result.claimingEnabled,
|
334
|
-
refundEnabled: result.refundEnabled,
|
335
|
-
};
|
336
|
-
}
|
337
|
-
async getUserContribution(roundId, user) {
|
338
|
-
const result = await this.contract.userContributions(roundId, user);
|
339
|
-
return {
|
340
|
-
fundAmount: Number(result.fundAmount) / this.PRICE_PRECISION,
|
341
|
-
tokenAllocation: Number(result.tokenAllocation),
|
342
|
-
claimed: result.claimed,
|
343
|
-
refunded: result.refunded,
|
344
|
-
contributedToken: result.contributedToken,
|
345
|
-
contributedAmount: Number(result.contributedAmount),
|
346
|
-
};
|
347
|
-
}
|
348
|
-
async getRoundRaisedFunds(roundId) {
|
349
|
-
const result = await this.contract.roundRaisedFunds(roundId);
|
350
|
-
return Number(result) / this.PRICE_PRECISION;
|
351
|
-
}
|
352
|
-
async getRoundParticipants(roundId) {
|
353
|
-
const result = await this.contract.roundParticipants(roundId);
|
354
|
-
return Number(result);
|
355
|
-
}
|
356
|
-
async getUserParticipatedRound(user) {
|
357
|
-
const result = await this.contract.userParticipatedRound(user);
|
358
|
-
return Number(result);
|
359
|
-
}
|
360
|
-
async getProjectToken() {
|
361
|
-
return await this.contract.projectToken();
|
362
|
-
}
|
363
|
-
async getTotalRounds() {
|
364
|
-
const result = await this.contract.totalRounds();
|
365
|
-
return Number(result);
|
366
|
-
}
|
367
|
-
async getTotalRaisedFunds() {
|
368
|
-
const result = await this.contract.totalRaisedFunds();
|
369
|
-
return Number(result) / this.PRICE_PRECISION;
|
370
|
-
}
|
371
|
-
async isRoundActive(roundId) {
|
372
|
-
return await this.contract.isRoundActive(roundId);
|
373
|
-
}
|
374
|
-
/**
|
375
|
-
* Get the ID of the latest round
|
376
|
-
* @returns The ID of the latest round or -1 if no rounds exist
|
377
|
-
*/
|
378
|
-
async getLatestRoundId() {
|
379
|
-
// Get total number of rounds
|
380
|
-
const totalRounds = await this.getTotalRounds();
|
381
|
-
// If no rounds exist, return -1
|
382
|
-
if (totalRounds === 0) {
|
383
|
-
return -1;
|
384
|
-
}
|
385
|
-
// Latest round ID (0-indexed, so subtract 1)
|
386
|
-
return totalRounds - 1;
|
387
|
-
}
|
388
|
-
/**
|
389
|
-
* Get comprehensive information about a specific round
|
390
|
-
* @param roundId The ID of the round to get information for
|
391
|
-
* @returns An object containing all information about the specified round
|
392
|
-
*/
|
393
|
-
async getRoundInfo(roundId) {
|
394
|
-
// Get total number of rounds
|
395
|
-
const totalRounds = await this.getTotalRounds();
|
396
|
-
// Check if roundId is valid
|
397
|
-
if (roundId < 0 || roundId >= totalRounds) {
|
398
|
-
throw new Error(`Invalid round ID: ${roundId}`);
|
399
|
-
}
|
400
|
-
// Get round configuration
|
401
|
-
const roundConfig = await this.getRound(roundId);
|
402
|
-
// Get additional information
|
403
|
-
const raisedFunds = await this.getRoundRaisedFunds(roundId);
|
404
|
-
const participants = await this.getRoundParticipants(roundId);
|
405
|
-
const isActive = await this.isRoundActive(roundId);
|
406
|
-
return {
|
407
|
-
roundId,
|
408
|
-
config: roundConfig,
|
409
|
-
raisedFunds,
|
410
|
-
participants,
|
411
|
-
isActive,
|
412
|
-
};
|
413
|
-
}
|
414
|
-
async getUserTotalContribution(user) {
|
415
|
-
const result = await this.contract.getUserTotalContribution(user);
|
416
|
-
return {
|
417
|
-
totalFundAmount: Number(result.totalFundAmount) / this.PRICE_PRECISION,
|
418
|
-
totalTokenAllocation: Number(result.totalTokenAllocation),
|
419
|
-
};
|
420
|
-
}
|
421
|
-
async getTransactionStatus(txHash, maxRetries = 10) {
|
422
|
-
return await (0, utils_1.getTransactionStatus)(this.getRandomProvider(), txHash, maxRetries);
|
423
|
-
}
|
424
|
-
getContractAddress() {
|
425
|
-
return this.contractAddress;
|
426
|
-
}
|
427
|
-
async getAllEvents(fromBlock, toBlock) {
|
428
|
-
await this.bootstrap();
|
429
|
-
return (0, utils_1.getAllEvents)(this.contract, () => this.getRandomProvider(), () => this.getContractWithRandomProvider(), fromBlock, toBlock);
|
430
|
-
}
|
431
|
-
async streamEvents(fromBlock, onEvent, saveLatestBlock, batchSize = 1000, sleepTime = 5000) {
|
432
|
-
await this.bootstrap();
|
433
|
-
return (0, utils_1.streamEvents)({
|
434
|
-
getProvider: () => this.getRandomProvider(),
|
435
|
-
getAllEvents: (fromBlock, toBlock) => this.getAllEvents(fromBlock, toBlock),
|
436
|
-
formatEvent: (event) => this.formatEventArgs(event),
|
437
|
-
onEvent,
|
438
|
-
saveLatestBlock,
|
439
|
-
fromBlock,
|
440
|
-
batchSize,
|
441
|
-
sleepTime,
|
442
|
-
});
|
443
|
-
}
|
444
|
-
}
|
445
|
-
exports.SeedRoundFundraiserSDK = SeedRoundFundraiserSDK;
|
1
|
+
"use strict";var t=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.SeedRoundFundraiserSDK=void 0;const e=require("ethers"),n=t(require("./abis/SeedRoundFundraiser.json")),r=t(require("./contracts.json")),o=require("./utils"),a=t(require("./whitelist-tokens.json"));exports.SeedRoundFundraiserSDK=class{constructor(t,a){this.name="SeedRoundFundraiser",this.isBootstrapped=!1,this.PRICE_PRECISION=1e18,this.whitelistedTokensArray=[],this.formatEventArgs=t=>{const{eventName:e,args:n}=t;switch(e){case"TokenWhitelisted":return{...t,args:{...n,token:n.token.toLowerCase(),price:Number(n.price)/this.PRICE_PRECISION}};case"TokenPriceUpdated":return{...t,args:{...n,token:n.token.toLowerCase(),oldPrice:Number(n.oldPrice)/this.PRICE_PRECISION,newPrice:Number(n.newPrice)/this.PRICE_PRECISION}};case"TokenRemovedFromWhitelist":return{...t,args:{...n,token:n.token.toLowerCase()}};case"RoundCreated":case"RoundUpdated":return{...t,args:{...n,roundId:Number(n.roundId),startTime:Number(n.startTime),endTime:Number(n.endTime),targetFund:Number(n.targetFund)/this.PRICE_PRECISION,totalAllocation:Number(n.totalAllocation),maxFundPerAccount:Number(n.maxFundPerAccount)/this.PRICE_PRECISION}};case"RoundEnded":return{...t,args:{...n,roundId:Number(n.roundId),raisedFunds:Number(n.raisedFunds)/this.PRICE_PRECISION,participants:Number(n.participants)}};case"Contribution":return{...t,args:{...n,roundId:Number(n.roundId),contributor:n.contributor.toLowerCase(),token:n.token.toLowerCase(),amount:Number(n.amount),fundAmount:Number(n.fundAmount)/this.PRICE_PRECISION,tokenAllocation:Number(n.tokenAllocation)}};case"TokensClaimed":return{...t,args:{...n,roundId:Number(n.roundId),user:n.user.toLowerCase(),amount:Number(n.amount)}};case"ProjectTokenUpdated":return{...t,args:{...n,oldToken:n.oldToken.toLowerCase(),newToken:n.newToken.toLowerCase()}};case"ClaimingEnabledUpdated":case"RefundEnabledUpdated":return{...t,args:{...n,roundId:Number(n.roundId),enabled:n.enabled}};case"Refunded":return{...t,args:{...n,roundId:Number(n.roundId),user:n.user.toLowerCase(),token:n.token.toLowerCase(),amount:Number(n.amount)}};default:return t}};const i=Array.isArray(t)?t:[t];this.providers=i.map((t=>new e.ethers.JsonRpcProvider(t)));const s=(0,o.getRandomProvider)(this.providers);this.contractAddress=(0,o.resolveContractAddress)(i[0],this.name,r.default,a),this.contract=new e.ethers.Contract(this.contractAddress,n.default.abi,s),this.networkName=this.getNetworkNameFromRpc(i[0]),this.importWhitelistedTokens()}getNetworkNameFromRpc(t){const e=t.toLowerCase();return e.includes("sepolia.base")?"base-sepolia":e.includes("sepolia")?"sepolia":e.includes("mainnet")?"mainnet":e.includes("localhost")||e.includes("127.0.0.1")?"hardhat":"sepolia"}importWhitelistedTokens(){try{a.default[this.networkName]?(this.whitelistedTokensArray=a.default[this.networkName].map((t=>t.toLowerCase())),console.log(`Imported ${this.whitelistedTokensArray.length} whitelisted tokens for ${this.networkName} network`)):console.log(`No whitelisted tokens found for ${this.networkName} network`)}catch(t){console.error("Error importing whitelisted tokens:",t)}}async getWhitelistedTokenInfo(t){const n=t.toLowerCase();if(!this.whitelistedTokensArray.includes(n))return null;const r=await this.getWhitelistedToken(n);if(!r.isWhitelisted)return null;let a="ETH",i=18;n!==e.ethers.ZeroAddress&&([a,i]=await Promise.all([(0,o.getTokenSymbol)(n,this.getRandomProvider()),(0,o.getTokenDecimals)(n,this.getRandomProvider())]));const s=await this.getLatestRoundId();let d=0;if(s>=0){const t=await this.getRound(s);t.exists&&!t.ended&&(d=t.maxFundPerAccount/r.price)}return{address:n,isWhitelisted:r.isWhitelisted,price:r.price,symbol:a,maxContribution:d,maxContributionRaw:Math.floor(d*Math.pow(10,i)),decimals:i}}async getAllWhitelistedTokensInfo(){const t=[];for(const e of this.whitelistedTokensArray){const n=await this.getWhitelistedTokenInfo(e);n&&t.push(n)}return t}getRandomProvider(){return(0,o.getRandomProvider)(this.providers)}getContractWithRandomProvider(){return new e.ethers.Contract(this.contractAddress,n.default.abi,this.getRandomProvider())}async bootstrap(){if(this.isBootstrapped)return;const t=await Promise.all(this.providers.map(((t,e)=>(0,o.checkRpcHealth)(t,e))));if(this.providers=this.providers.filter(((e,n)=>t[n])),0===this.providers.length)throw new Error("No active RPC providers available");this.isBootstrapped=!0}async signAndSendTransaction(t,e,n={}){return(0,o.signAndSendTransaction)(t,e,(()=>this.getRandomProvider()),n,this.contract)}async buildAddWhitelistedTokenTx(t,e){const n=BigInt(Math.floor(e*this.PRICE_PRECISION));return await this.contract.addWhitelistedToken.populateTransaction(t,n)}async buildUpdateTokenPriceTx(t,e){const n=BigInt(Math.floor(e*this.PRICE_PRECISION));return await this.contract.updateTokenPrice.populateTransaction(t,n)}async buildRemoveWhitelistedTokenTx(t){return await this.contract.removeWhitelistedToken.populateTransaction(t)}async buildCreateRoundTx(t,e,n,r,a){const i=BigInt(Math.floor(n*this.PRICE_PRECISION)),s=BigInt(Math.floor(a*this.PRICE_PRECISION)),d=await(0,o.getTokenDecimals)(await this.getProjectToken(),this.getRandomProvider()),u=BigInt(Math.floor(r*Math.pow(10,d)));return await this.contract.createRound.populateTransaction(t,e,i,u,s)}async buildUpdateRoundTx(t,e,n,r,o,a){const i=BigInt(Math.floor(r*this.PRICE_PRECISION)),s=BigInt(Math.floor(a*this.PRICE_PRECISION));return await this.contract.updateRound.populateTransaction(t,e,n,i,o,s)}async buildEndRoundTx(t){return await this.contract.endRound.populateTransaction(t)}async buildSetClaimingEnabledTx(t,e){return await this.contract.setClaimingEnabled.populateTransaction(t,e)}async buildSetRefundEnabledTx(t,e){return await this.contract.setRefundEnabled.populateTransaction(t,e)}async buildContributeTx(t,n,r){let a=18;n!==e.ethers.ZeroAddress&&(a=await(0,o.getTokenDecimals)(n,this.getRandomProvider()));const i=e.ethers.parseUnits(r.toString(),a),s=await this.contract.contribute.populateTransaction(t,n,n===e.ethers.ZeroAddress?0:i);return n===e.ethers.ZeroAddress&&(s.value=i),s}async buildClaimTokensTx(){return await this.contract.claimTokens.populateTransaction()}async buildRefundTx(){return await this.contract.refund.populateTransaction()}async buildWithdrawFundsTx(t,e){const n=await(0,o.getTokenDecimals)(t,this.getRandomProvider()),r=BigInt(Math.floor(e*Math.pow(10,n)));return await this.contract.withdrawFunds.populateTransaction(t,r)}async buildClaimTokensByRoundIdTx(t){return await this.contract.claimTokensByRoundId.populateTransaction(t)}async getWhitelistedToken(t){const e=await this.contract.whitelistedTokens(t);return{isWhitelisted:e.isWhitelisted,price:Number(e.price)/this.PRICE_PRECISION}}async getRound(t){const e=await this.contract.rounds(t);return{startTime:Number(e.startTime),endTime:Number(e.endTime),targetFund:Number(e.targetFund)/this.PRICE_PRECISION,totalAllocation:Number(e.totalAllocation),maxFundPerAccount:Number(e.maxFundPerAccount)/this.PRICE_PRECISION,exists:e.exists,ended:e.ended,claimingEnabled:e.claimingEnabled,refundEnabled:e.refundEnabled}}async getUserContribution(t,e){const n=await this.contract.userContributions(t,e);return{fundAmount:Number(n.fundAmount)/this.PRICE_PRECISION,tokenAllocation:Number(n.tokenAllocation),claimed:n.claimed,refunded:n.refunded,contributedToken:n.contributedToken,contributedAmount:Number(n.contributedAmount)}}async getRoundRaisedFunds(t){const e=await this.contract.roundRaisedFunds(t);return Number(e)/this.PRICE_PRECISION}async getRoundParticipants(t){const e=await this.contract.roundParticipants(t);return Number(e)}async getUserParticipatedRound(t){const e=await this.contract.userParticipatedRound(t);return Number(e)}async getProjectToken(){return await this.contract.projectToken()}async getTotalRounds(){const t=await this.contract.totalRounds();return Number(t)}async getTotalRaisedFunds(){const t=await this.contract.totalRaisedFunds();return Number(t)/this.PRICE_PRECISION}async isRoundActive(t){return await this.contract.isRoundActive(t)}async getLatestRoundId(){const t=await this.getTotalRounds();return 0===t?-1:t-1}async getRoundInfo(t){const e=await this.getTotalRounds();if(t<0||t>=e)throw new Error(`Invalid round ID: ${t}`);return{roundId:t,config:await this.getRound(t),raisedFunds:await this.getRoundRaisedFunds(t),participants:await this.getRoundParticipants(t),isActive:await this.isRoundActive(t)}}async getUserTotalContribution(t){const e=await this.contract.getUserTotalContribution(t);return{totalFundAmount:Number(e.totalFundAmount)/this.PRICE_PRECISION,totalTokenAllocation:Number(e.totalTokenAllocation)}}async getTransactionStatus(t,e=10){return await(0,o.getTransactionStatus)(this.getRandomProvider(),t,e)}getContractAddress(){return this.contractAddress}async getAllEvents(t,e){return await this.bootstrap(),(0,o.getAllEvents)(this.contract,(()=>this.getRandomProvider()),(()=>this.getContractWithRandomProvider()),t,e)}async streamEvents(t,e,n,r=1e3,a=5e3){return await this.bootstrap(),(0,o.streamEvents)({getProvider:()=>this.getRandomProvider(),getAllEvents:(t,e)=>this.getAllEvents(t,e),formatEvent:t=>this.formatEventArgs(t),onEvent:e,saveLatestBlock:n,fromBlock:t,batchSize:r,sleepTime:a})}async getUserRoundContribution(t,n){const[r,a]=await Promise.all([this.contract.getUserRoundContribution(t,n),this.contract.rounds(t)]),i={fundAmount:Number(r.fundAmount)/this.PRICE_PRECISION,tokenAllocation:Number(r.tokenAllocation),claimed:r.claimed,refunded:r.refunded,contributedToken:r.contributedToken,contributedAmount:Number(r.contributedAmount)},s=Math.floor(Date.now()/1e3),d=a.exists&&!a.ended&&s>=Number(a.startTime)&&s<=Number(a.endTime),u={contribution:i,roundInfo:{exists:a.exists,isActive:d,claimingEnabled:a.claimingEnabled,refundEnabled:a.refundEnabled}};if(i.fundAmount>0){const t=i.contributedToken;let n="ETH",r=18;return t!==e.ethers.ZeroAddress&&([n,r]=await Promise.all([(0,o.getTokenSymbol)(t,this.getRandomProvider()),(0,o.getTokenDecimals)(t,this.getRandomProvider())])),{...u,tokenInfo:{symbol:n,decimals:r}}}return u}};
|