@heyanon/sdk 2.3.2 → 2.3.4
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/adapter/misc.d.ts +35 -0
- package/dist/adapter/transformers/toResult.d.ts +24 -0
- package/dist/adapter/types.d.ts +123 -7
- package/dist/blockchain/constants/chains.d.ts +93 -0
- package/dist/blockchain/constants/types.d.ts +33 -0
- package/dist/blockchain/evm/constants/chains.d.ts +56 -0
- package/dist/blockchain/evm/constants/misc.d.ts +69 -0
- package/dist/blockchain/evm/constants/weth9.d.ts +76 -1
- package/dist/blockchain/evm/types.d.ts +273 -0
- package/dist/blockchain/evm/utils/checkToApprove.d.ts +86 -0
- package/dist/blockchain/evm/utils/getChainFromName.d.ts +81 -0
- package/dist/blockchain/evm/utils/getChainName.d.ts +114 -0
- package/dist/blockchain/evm/utils/getUnwrapData.d.ts +114 -0
- package/dist/blockchain/evm/utils/getWrapAddress.d.ts +147 -0
- package/dist/blockchain/evm/utils/getWrapData.d.ts +189 -0
- package/dist/blockchain/evm/utils/getWrappedNative.d.ts +198 -0
- package/dist/blockchain/evm/utils/isEvmChain.d.ts +183 -0
- package/dist/blockchain/evm/utils/isNativeAddress.d.ts +221 -0
- package/dist/blockchain/evm/utils/isNativeAddress.test.d.ts +1 -0
- package/dist/blockchain/solana/types.d.ts +291 -0
- package/dist/blockchain/solana/utils/buildV0Transaction.d.ts +184 -0
- package/dist/blockchain/solana/utils/toPublicKey.d.ts +194 -0
- package/dist/blockchain/ton/types.d.ts +410 -0
- package/dist/index.js +83 -26
- package/dist/index.mjs +84 -28
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/is-address.d.ts +7 -0
- package/dist/utils/is-address.spec.d.ts +1 -0
- package/dist/utils/retry.d.ts +218 -0
- package/dist/utils/sleep.d.ts +239 -0
- package/dist/utils/stringify.d.ts +235 -0
- package/dist/utils/stringify.spec.d.ts +1 -0
- package/package.json +2 -1
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { erc20Abi, getAddress, encodeFunctionData } from 'viem';
|
|
1
|
+
import { erc20Abi, getAddress, encodeFunctionData, isAddress as isAddress$1 } from 'viem';
|
|
2
2
|
import { ethereumTokens, optimismTokens, bscTokens, polygonTokens, zkSyncTokens, kavaTokens, avalancheTokens, arbitrumTokens, metisTokens, baseTokens, sonicTokens, Token } from '@real-wagmi/sdk';
|
|
3
3
|
import { TransactionMessage, VersionedTransaction, PublicKey } from '@solana/web3.js';
|
|
4
|
+
import { Address } from '@ton/ton';
|
|
4
5
|
|
|
5
6
|
var __defProp = Object.defineProperty;
|
|
6
7
|
var __export = (target, all) => {
|
|
@@ -36,32 +37,6 @@ function stringify(value, space) {
|
|
|
36
37
|
return JSON.stringify(value, (_key, value2) => typeof value2 === "bigint" ? value2.toString() : value2, space);
|
|
37
38
|
}
|
|
38
39
|
|
|
39
|
-
// src/adapter/transformers/toResult.ts
|
|
40
|
-
function toResult(data, error = false) {
|
|
41
|
-
const formatedData = typeof data === "string" ? data : stringify(data);
|
|
42
|
-
return {
|
|
43
|
-
success: !error,
|
|
44
|
-
data: error ? `ERROR: ${formatedData}` : formatedData
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// src/adapter/misc.ts
|
|
49
|
-
var AdapterTag = /* @__PURE__ */ ((AdapterTag2) => {
|
|
50
|
-
AdapterTag2["ALM"] = "ALM";
|
|
51
|
-
AdapterTag2["STAKE"] = "Stake";
|
|
52
|
-
AdapterTag2["LENDING"] = "Lending";
|
|
53
|
-
AdapterTag2["FARM"] = "Farm";
|
|
54
|
-
AdapterTag2["PERPETUALS"] = "Perpetuals";
|
|
55
|
-
AdapterTag2["BRIDGE"] = "Bridge";
|
|
56
|
-
AdapterTag2["LIMIT_ORDER"] = "Limit orders";
|
|
57
|
-
AdapterTag2["DEX"] = "DEX";
|
|
58
|
-
AdapterTag2["LP"] = "LP";
|
|
59
|
-
AdapterTag2["GAMES"] = "Games";
|
|
60
|
-
AdapterTag2["CEX"] = "CEX";
|
|
61
|
-
AdapterTag2["LAUNCHPAD"] = "Launchpad";
|
|
62
|
-
return AdapterTag2;
|
|
63
|
-
})(AdapterTag || {});
|
|
64
|
-
|
|
65
40
|
// src/blockchain/evm/index.ts
|
|
66
41
|
var evm_exports = {};
|
|
67
42
|
__export(evm_exports, {
|
|
@@ -87,7 +62,9 @@ __export(utils_exports, {
|
|
|
87
62
|
isEvmChain: () => isEvmChain
|
|
88
63
|
});
|
|
89
64
|
var ZERO_ALLOWANCE = {
|
|
65
|
+
/** Ethereum mainnet tokens requiring zero allowance reset */
|
|
90
66
|
1: ["0xdAC17F958D2ee523a2206206994597C13D831ec7"]
|
|
67
|
+
// USDT
|
|
91
68
|
};
|
|
92
69
|
async function checkToApprove({ args, transactions, provider }) {
|
|
93
70
|
const { account, target, spender, amount } = args;
|
|
@@ -172,39 +149,69 @@ var WalletType = /* @__PURE__ */ ((WalletType2) => {
|
|
|
172
149
|
|
|
173
150
|
// src/blockchain/evm/constants/chains.ts
|
|
174
151
|
var ChainIds = {
|
|
152
|
+
/** Ethereum Mainnet - Chain ID: 1 */
|
|
175
153
|
["ethereum" /* ETHEREUM */]: 1,
|
|
154
|
+
/** Optimism - Chain ID: 10 */
|
|
176
155
|
["optimism" /* OPTIMISM */]: 10,
|
|
156
|
+
/** Binance Smart Chain - Chain ID: 56 */
|
|
177
157
|
["bsc" /* BSC */]: 56,
|
|
158
|
+
/** Gnosis Chain - Chain ID: 100 */
|
|
178
159
|
["gnosis" /* GNOSIS */]: 100,
|
|
160
|
+
/** Polygon - Chain ID: 137 */
|
|
179
161
|
["polygon" /* POLYGON */]: 137,
|
|
162
|
+
/** Sonic - Chain ID: 146 */
|
|
180
163
|
["sonic" /* SONIC */]: 146,
|
|
164
|
+
/** zkSync Era - Chain ID: 324 */
|
|
181
165
|
["zksync" /* ZKSYNC */]: 324,
|
|
166
|
+
/** Metis Andromeda - Chain ID: 1088 */
|
|
182
167
|
["metis" /* METIS */]: 1088,
|
|
168
|
+
/** Kava EVM - Chain ID: 2222 */
|
|
183
169
|
["kava_evm" /* KAVA_EVM */]: 2222,
|
|
170
|
+
/** Base - Chain ID: 8453 */
|
|
184
171
|
["base" /* BASE */]: 8453,
|
|
172
|
+
/** Avalanche C-Chain - Chain ID: 43114 */
|
|
185
173
|
["avalanche" /* AVALANCHE */]: 43114,
|
|
174
|
+
/** Arbitrum One - Chain ID: 42161 */
|
|
186
175
|
["arbitrum" /* ARBITRUM */]: 42161,
|
|
176
|
+
/** Scroll - Chain ID: 534352 */
|
|
187
177
|
["scroll" /* SCROLL */]: 534352,
|
|
178
|
+
/** HyperEVM - Chain ID: 999 */
|
|
188
179
|
["hyperevm" /* HYPEREVM */]: 999,
|
|
180
|
+
/** Plasma - Chain ID: 9745 */
|
|
189
181
|
["plasma" /* PLASMA */]: 9745
|
|
190
182
|
};
|
|
191
183
|
|
|
192
184
|
// src/blockchain/evm/constants/weth9.ts
|
|
193
185
|
var WETH9 = {
|
|
186
|
+
/** Wrapped Ether (WETH) on Ethereum */
|
|
194
187
|
["ethereum" /* ETHEREUM */]: ethereumTokens.weth,
|
|
188
|
+
/** Wrapped Ether (WETH) on Optimism */
|
|
195
189
|
["optimism" /* OPTIMISM */]: optimismTokens.weth,
|
|
190
|
+
/** Wrapped BNB (WBNB) on Binance Smart Chain */
|
|
196
191
|
["bsc" /* BSC */]: bscTokens.wbnb,
|
|
192
|
+
/** Wrapped POL (WPOL) on Polygon */
|
|
197
193
|
["polygon" /* POLYGON */]: polygonTokens.wpol,
|
|
194
|
+
/** Wrapped Ether (WETH) on zkSync Era */
|
|
198
195
|
["zksync" /* ZKSYNC */]: zkSyncTokens.weth,
|
|
196
|
+
/** Wrapped KAVA (WKAVA) on Kava EVM */
|
|
199
197
|
["kava_evm" /* KAVA_EVM */]: kavaTokens.wkava,
|
|
198
|
+
/** Wrapped AVAX (WAVAX) on Avalanche */
|
|
200
199
|
["avalanche" /* AVALANCHE */]: avalancheTokens.wavax,
|
|
200
|
+
/** Wrapped Ether (WETH) on Arbitrum */
|
|
201
201
|
["arbitrum" /* ARBITRUM */]: arbitrumTokens.weth,
|
|
202
|
+
/** Wrapped METIS (WMETIS) on Metis */
|
|
202
203
|
["metis" /* METIS */]: metisTokens.wmetis,
|
|
204
|
+
/** Wrapped Ether (WETH) on Base */
|
|
203
205
|
["base" /* BASE */]: baseTokens.weth,
|
|
206
|
+
/** Wrapped S (WS) on Sonic */
|
|
204
207
|
["sonic" /* SONIC */]: sonicTokens.ws,
|
|
208
|
+
/** Wrapped Ether (WETH) on Scroll */
|
|
205
209
|
["scroll" /* SCROLL */]: new Token(ChainIds["scroll" /* SCROLL */], "0x5300000000000000000000000000000000000004", 18, "WETH", "Wrapped Ether"),
|
|
210
|
+
/** Wrapped xDAI (WXDAI) on Gnosis Chain */
|
|
206
211
|
["gnosis" /* GNOSIS */]: new Token(ChainIds["gnosis" /* GNOSIS */], "0xe91D153E0b41518A2Ce8Dd3D7944Fa863463a97d", 18, "WXDAI", "Wrapped XDAI"),
|
|
212
|
+
/** Wrapped HYPE (WHYPE) on HyperEVM */
|
|
207
213
|
["hyperevm" /* HYPEREVM */]: new Token(ChainIds["hyperevm" /* HYPEREVM */], "0x5555555555555555555555555555555555555555", 18, "WHYPE", "Wrapped HYPE"),
|
|
214
|
+
/** Wrapped XPL (WXPL) on Plasma */
|
|
208
215
|
["plasma" /* PLASMA */]: new Token(ChainIds["plasma" /* PLASMA */], "0x6100E367285b01F48D07953803A2d8dCA5D19873", 18, "WXPL", "Wrapped XPL")
|
|
209
216
|
};
|
|
210
217
|
|
|
@@ -566,5 +573,54 @@ __export(ton_exports, {
|
|
|
566
573
|
|
|
567
574
|
// src/blockchain/ton/types.ts
|
|
568
575
|
var types_exports3 = {};
|
|
576
|
+
function isAddress(value) {
|
|
577
|
+
let result = { valid: false };
|
|
578
|
+
try {
|
|
579
|
+
result.valid = isAddress$1(value);
|
|
580
|
+
if (result.valid) {
|
|
581
|
+
result.walletType = "evm" /* EVM */;
|
|
582
|
+
}
|
|
583
|
+
} catch (_error) {
|
|
584
|
+
}
|
|
585
|
+
try {
|
|
586
|
+
solana_exports.utils.toPublicKey(value);
|
|
587
|
+
result.valid = true;
|
|
588
|
+
result.walletType = "solana" /* SOLANA */;
|
|
589
|
+
} catch (_error) {
|
|
590
|
+
}
|
|
591
|
+
try {
|
|
592
|
+
Address.parse(value);
|
|
593
|
+
result.valid = true;
|
|
594
|
+
result.walletType = "ton" /* TON */;
|
|
595
|
+
} catch (_error) {
|
|
596
|
+
}
|
|
597
|
+
return result;
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
// src/adapter/transformers/toResult.ts
|
|
601
|
+
function toResult(data, error = false) {
|
|
602
|
+
const formatedData = typeof data === "string" ? data : stringify(data);
|
|
603
|
+
return {
|
|
604
|
+
success: !error,
|
|
605
|
+
data: error ? `ERROR: ${formatedData}` : formatedData
|
|
606
|
+
};
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
// src/adapter/misc.ts
|
|
610
|
+
var AdapterTag = /* @__PURE__ */ ((AdapterTag2) => {
|
|
611
|
+
AdapterTag2["ALM"] = "ALM";
|
|
612
|
+
AdapterTag2["STAKE"] = "Stake";
|
|
613
|
+
AdapterTag2["LENDING"] = "Lending";
|
|
614
|
+
AdapterTag2["FARM"] = "Farm";
|
|
615
|
+
AdapterTag2["PERPETUALS"] = "Perpetuals";
|
|
616
|
+
AdapterTag2["BRIDGE"] = "Bridge";
|
|
617
|
+
AdapterTag2["LIMIT_ORDER"] = "Limit orders";
|
|
618
|
+
AdapterTag2["DEX"] = "DEX";
|
|
619
|
+
AdapterTag2["LP"] = "LP";
|
|
620
|
+
AdapterTag2["GAMES"] = "Games";
|
|
621
|
+
AdapterTag2["CEX"] = "CEX";
|
|
622
|
+
AdapterTag2["LAUNCHPAD"] = "Launchpad";
|
|
623
|
+
return AdapterTag2;
|
|
624
|
+
})(AdapterTag || {});
|
|
569
625
|
|
|
570
|
-
export { AdapterTag, Chain, evm_exports as EVM, solana_exports as Solana, ton_exports as TON, WalletType, allChains, allEvmChains, retry, sleep, stringify, toResult };
|
|
626
|
+
export { AdapterTag, Chain, evm_exports as EVM, solana_exports as Solana, ton_exports as TON, WalletType, allChains, allEvmChains, isAddress, retry, sleep, stringify, toResult };
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/utils/retry.d.ts
CHANGED
|
@@ -1,6 +1,224 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options for configuring retry behavior
|
|
3
|
+
* @interface Options
|
|
4
|
+
*/
|
|
1
5
|
interface Options {
|
|
6
|
+
/** Number of retry attempts (default: 0) */
|
|
2
7
|
readonly retries?: number;
|
|
8
|
+
/** Delay between retry attempts in milliseconds (optional) */
|
|
3
9
|
readonly delayMs?: number;
|
|
4
10
|
}
|
|
11
|
+
/**
|
|
12
|
+
* Executes an async function with automatic retry logic on failure
|
|
13
|
+
* @template T - The return type of the function
|
|
14
|
+
* @param fn - The async function to execute with retry logic
|
|
15
|
+
* @param options - Configuration options for retry behavior
|
|
16
|
+
* @returns Promise that resolves with the function result or rejects after all retries are exhausted
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* // Basic retry without delay
|
|
20
|
+
* const result = await retry(
|
|
21
|
+
* async () => {
|
|
22
|
+
* const response = await fetch('https://api.example.com/data');
|
|
23
|
+
* if (!response.ok) throw new Error('Network error');
|
|
24
|
+
* return response.json();
|
|
25
|
+
* },
|
|
26
|
+
* { retries: 3 }
|
|
27
|
+
* );
|
|
28
|
+
*
|
|
29
|
+
* // Retry with delay between attempts
|
|
30
|
+
* const dataWithDelay = await retry(
|
|
31
|
+
* async () => {
|
|
32
|
+
* const connection = await database.connect();
|
|
33
|
+
* return await connection.query('SELECT * FROM users');
|
|
34
|
+
* },
|
|
35
|
+
* { retries: 5, delayMs: 1000 } // Wait 1 second between retries
|
|
36
|
+
* );
|
|
37
|
+
*
|
|
38
|
+
* // Blockchain transaction with exponential backoff simulation
|
|
39
|
+
* async function sendTransactionWithRetry(transaction: TransactionParams) {
|
|
40
|
+
* let attempt = 0;
|
|
41
|
+
*
|
|
42
|
+
* return await retry(
|
|
43
|
+
* async () => {
|
|
44
|
+
* attempt++;
|
|
45
|
+
* console.log(`Attempt ${attempt}: Sending transaction...`);
|
|
46
|
+
*
|
|
47
|
+
* const result = await options.evm.sendTransactions({
|
|
48
|
+
* transactions: [transaction]
|
|
49
|
+
* });
|
|
50
|
+
*
|
|
51
|
+
* if (!result.data[0].message.includes('confirmed')) {
|
|
52
|
+
* throw new Error('Transaction not confirmed');
|
|
53
|
+
* }
|
|
54
|
+
*
|
|
55
|
+
* return result;
|
|
56
|
+
* },
|
|
57
|
+
* { retries: 3, delayMs: 2000 }
|
|
58
|
+
* );
|
|
59
|
+
* }
|
|
60
|
+
*
|
|
61
|
+
* // API call with retry for rate limiting
|
|
62
|
+
* async function fetchTokenPrice(tokenAddress: string) {
|
|
63
|
+
* return await retry(
|
|
64
|
+
* async () => {
|
|
65
|
+
* const response = await fetch(`https://api.coingecko.com/api/v3/tokens/${tokenAddress}`);
|
|
66
|
+
*
|
|
67
|
+
* if (response.status === 429) {
|
|
68
|
+
* throw new Error('Rate limited');
|
|
69
|
+
* }
|
|
70
|
+
*
|
|
71
|
+
* if (!response.ok) {
|
|
72
|
+
* throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
73
|
+
* }
|
|
74
|
+
*
|
|
75
|
+
* return await response.json();
|
|
76
|
+
* },
|
|
77
|
+
* { retries: 5, delayMs: 3000 } // 3 second delay for rate limits
|
|
78
|
+
* );
|
|
79
|
+
* }
|
|
80
|
+
*
|
|
81
|
+
* // RPC provider failover
|
|
82
|
+
* async function getBlockNumberWithFailover(providers: PublicClient[]) {
|
|
83
|
+
* let providerIndex = 0;
|
|
84
|
+
*
|
|
85
|
+
* return await retry(
|
|
86
|
+
* async () => {
|
|
87
|
+
* if (providerIndex >= providers.length) {
|
|
88
|
+
* throw new Error('All providers failed');
|
|
89
|
+
* }
|
|
90
|
+
*
|
|
91
|
+
* const provider = providers[providerIndex];
|
|
92
|
+
*
|
|
93
|
+
* try {
|
|
94
|
+
* return await provider.getBlockNumber();
|
|
95
|
+
* } catch (error) {
|
|
96
|
+
* providerIndex++;
|
|
97
|
+
* throw error;
|
|
98
|
+
* }
|
|
99
|
+
* },
|
|
100
|
+
* { retries: providers.length - 1, delayMs: 500 }
|
|
101
|
+
* );
|
|
102
|
+
* }
|
|
103
|
+
*
|
|
104
|
+
* // Solana transaction confirmation
|
|
105
|
+
* async function confirmSolanaTransaction(signature: string, connection: Connection) {
|
|
106
|
+
* return await retry(
|
|
107
|
+
* async () => {
|
|
108
|
+
* const status = await connection.getSignatureStatus(signature);
|
|
109
|
+
*
|
|
110
|
+
* if (!status.value) {
|
|
111
|
+
* throw new Error('Transaction not found');
|
|
112
|
+
* }
|
|
113
|
+
*
|
|
114
|
+
* if (status.value.err) {
|
|
115
|
+
* throw new Error(`Transaction failed: ${JSON.stringify(status.value.err)}`);
|
|
116
|
+
* }
|
|
117
|
+
*
|
|
118
|
+
* if (!status.value.confirmationStatus) {
|
|
119
|
+
* throw new Error('Transaction not confirmed yet');
|
|
120
|
+
* }
|
|
121
|
+
*
|
|
122
|
+
* return status.value;
|
|
123
|
+
* },
|
|
124
|
+
* { retries: 30, delayMs: 1000 } // Check every second for 30 seconds
|
|
125
|
+
* );
|
|
126
|
+
* }
|
|
127
|
+
*
|
|
128
|
+
* // TON client operations with retry
|
|
129
|
+
* async function getTonAccountWithRetry(address: Address, client: Client) {
|
|
130
|
+
* return await retry(
|
|
131
|
+
* async () => {
|
|
132
|
+
* try {
|
|
133
|
+
* return await client.api.accounts.getAccount(address.toString());
|
|
134
|
+
* } catch (error) {
|
|
135
|
+
* if (error.message.includes('429')) {
|
|
136
|
+
* throw new Error('Rate limited - will retry');
|
|
137
|
+
* }
|
|
138
|
+
* if (error.message.includes('timeout')) {
|
|
139
|
+
* throw new Error('Timeout - will retry');
|
|
140
|
+
* }
|
|
141
|
+
* // Don't retry for other errors
|
|
142
|
+
* throw error;
|
|
143
|
+
* }
|
|
144
|
+
* },
|
|
145
|
+
* { retries: 3, delayMs: 2000 }
|
|
146
|
+
* );
|
|
147
|
+
* }
|
|
148
|
+
*
|
|
149
|
+
* // File operations with retry
|
|
150
|
+
* async function readFileWithRetry(filePath: string) {
|
|
151
|
+
* return await retry(
|
|
152
|
+
* async () => {
|
|
153
|
+
* const fs = await import('fs/promises');
|
|
154
|
+
* return await fs.readFile(filePath, 'utf-8');
|
|
155
|
+
* },
|
|
156
|
+
* { retries: 3, delayMs: 100 }
|
|
157
|
+
* );
|
|
158
|
+
* }
|
|
159
|
+
*
|
|
160
|
+
* // Complex retry with custom logic
|
|
161
|
+
* async function smartContractCallWithRetry(
|
|
162
|
+
* contractAddress: Address,
|
|
163
|
+
* methodName: string,
|
|
164
|
+
* args: any[],
|
|
165
|
+
* provider: PublicClient
|
|
166
|
+
* ) {
|
|
167
|
+
* let gasPrice = await provider.getGasPrice();
|
|
168
|
+
*
|
|
169
|
+
* return await retry(
|
|
170
|
+
* async () => {
|
|
171
|
+
* try {
|
|
172
|
+
* return await provider.readContract({
|
|
173
|
+
* address: contractAddress,
|
|
174
|
+
* abi: contractAbi,
|
|
175
|
+
* functionName: methodName,
|
|
176
|
+
* args
|
|
177
|
+
* });
|
|
178
|
+
* } catch (error) {
|
|
179
|
+
* if (error.message.includes('gas')) {
|
|
180
|
+
* // Increase gas price for next attempt
|
|
181
|
+
* gasPrice = gasPrice * 110n / 100n; // +10%
|
|
182
|
+
* console.log(`Increasing gas price to ${gasPrice}`);
|
|
183
|
+
* }
|
|
184
|
+
* throw error;
|
|
185
|
+
* }
|
|
186
|
+
* },
|
|
187
|
+
* { retries: 3, delayMs: 1000 }
|
|
188
|
+
* );
|
|
189
|
+
* }
|
|
190
|
+
*
|
|
191
|
+
* // Error handling patterns
|
|
192
|
+
* async function handleOperationWithRetry<T>(
|
|
193
|
+
* operation: () => Promise<T>,
|
|
194
|
+
* operationName: string
|
|
195
|
+
* ): Promise<T> {
|
|
196
|
+
* try {
|
|
197
|
+
* return await retry(operation, { retries: 3, delayMs: 1000 });
|
|
198
|
+
* } catch (error) {
|
|
199
|
+
* console.error(`${operationName} failed after all retries:`, error);
|
|
200
|
+
*
|
|
201
|
+
* // Log for monitoring
|
|
202
|
+
* if (typeof error === 'object' && error !== null) {
|
|
203
|
+
* console.error('Final error details:', {
|
|
204
|
+
* message: error.message,
|
|
205
|
+
* stack: error.stack,
|
|
206
|
+
* operation: operationName
|
|
207
|
+
* });
|
|
208
|
+
* }
|
|
209
|
+
*
|
|
210
|
+
* throw new Error(`${operationName} failed: ${error.message}`);
|
|
211
|
+
* }
|
|
212
|
+
* }
|
|
213
|
+
*
|
|
214
|
+
* // Usage in adapter functions
|
|
215
|
+
* async function executeWithRetry<T>(
|
|
216
|
+
* operation: () => Promise<T>,
|
|
217
|
+
* retryConfig: Options = { retries: 3, delayMs: 1000 }
|
|
218
|
+
* ): Promise<T> {
|
|
219
|
+
* return await retry(operation, retryConfig);
|
|
220
|
+
* }
|
|
221
|
+
* ```
|
|
222
|
+
*/
|
|
5
223
|
export declare function retry<T>(fn: () => Promise<T>, options?: Options): Promise<T>;
|
|
6
224
|
export {};
|
package/dist/utils/sleep.d.ts
CHANGED
|
@@ -1 +1,240 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a promise that resolves after the specified number of milliseconds
|
|
3
|
+
* @param ms - Number of milliseconds to sleep/wait
|
|
4
|
+
* @returns Promise that resolves after the specified delay
|
|
5
|
+
* @example
|
|
6
|
+
* ```typescript
|
|
7
|
+
* // Basic usage - wait for 1 second
|
|
8
|
+
* await sleep(1000);
|
|
9
|
+
* console.log('Executed after 1 second');
|
|
10
|
+
*
|
|
11
|
+
* // Wait between operations
|
|
12
|
+
* console.log('Starting operation...');
|
|
13
|
+
* await sleep(2000);
|
|
14
|
+
* console.log('Continuing after 2 seconds...');
|
|
15
|
+
*
|
|
16
|
+
* // Use in blockchain operations for rate limiting
|
|
17
|
+
* async function batchProcessTransactions(transactions: TransactionParams[]) {
|
|
18
|
+
* const results = [];
|
|
19
|
+
*
|
|
20
|
+
* for (const tx of transactions) {
|
|
21
|
+
* const result = await options.evm.sendTransactions({
|
|
22
|
+
* transactions: [tx]
|
|
23
|
+
* });
|
|
24
|
+
* results.push(result);
|
|
25
|
+
*
|
|
26
|
+
* // Wait 3 seconds between transactions to avoid rate limits
|
|
27
|
+
* await sleep(3000);
|
|
28
|
+
* }
|
|
29
|
+
*
|
|
30
|
+
* return results;
|
|
31
|
+
* }
|
|
32
|
+
*
|
|
33
|
+
* // Polling with delays
|
|
34
|
+
* async function pollTransactionStatus(hash: string, maxAttempts: number = 30) {
|
|
35
|
+
* for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
36
|
+
* const receipt = await provider.getTransactionReceipt(hash);
|
|
37
|
+
*
|
|
38
|
+
* if (receipt) {
|
|
39
|
+
* return receipt;
|
|
40
|
+
* }
|
|
41
|
+
*
|
|
42
|
+
* console.log(`Attempt ${attempt}: Transaction not confirmed yet, waiting...`);
|
|
43
|
+
* await sleep(2000); // Wait 2 seconds before next check
|
|
44
|
+
* }
|
|
45
|
+
*
|
|
46
|
+
* throw new Error('Transaction confirmation timeout');
|
|
47
|
+
* }
|
|
48
|
+
*
|
|
49
|
+
* // Gradual data loading with delays
|
|
50
|
+
* async function loadUserPortfolio(userAddress: string, chains: EvmChain[]) {
|
|
51
|
+
* const portfolio = [];
|
|
52
|
+
*
|
|
53
|
+
* for (const chain of chains) {
|
|
54
|
+
* console.log(`Loading ${chain} data...`);
|
|
55
|
+
*
|
|
56
|
+
* const chainData = await getUserBalances(userAddress, chain);
|
|
57
|
+
* portfolio.push({ chain, data: chainData });
|
|
58
|
+
*
|
|
59
|
+
* // Small delay to prevent overwhelming APIs
|
|
60
|
+
* await sleep(500);
|
|
61
|
+
* }
|
|
62
|
+
*
|
|
63
|
+
* return portfolio;
|
|
64
|
+
* }
|
|
65
|
+
*
|
|
66
|
+
* // Retry with increasing delays (exponential backoff)
|
|
67
|
+
* async function exponentialBackoffOperation<T>(
|
|
68
|
+
* operation: () => Promise<T>,
|
|
69
|
+
* maxRetries: number = 5
|
|
70
|
+
* ): Promise<T> {
|
|
71
|
+
* let lastError: Error;
|
|
72
|
+
*
|
|
73
|
+
* for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
|
74
|
+
* try {
|
|
75
|
+
* return await operation();
|
|
76
|
+
* } catch (error) {
|
|
77
|
+
* lastError = error as Error;
|
|
78
|
+
*
|
|
79
|
+
* if (attempt === maxRetries) {
|
|
80
|
+
* throw lastError;
|
|
81
|
+
* }
|
|
82
|
+
*
|
|
83
|
+
* const delayMs = Math.pow(2, attempt) * 1000; // 2s, 4s, 8s, 16s, 32s
|
|
84
|
+
* console.log(`Attempt ${attempt} failed, retrying in ${delayMs}ms...`);
|
|
85
|
+
* await sleep(delayMs);
|
|
86
|
+
* }
|
|
87
|
+
* }
|
|
88
|
+
*
|
|
89
|
+
* throw lastError!;
|
|
90
|
+
* }
|
|
91
|
+
*
|
|
92
|
+
* // Animation delays in UI
|
|
93
|
+
* async function animatedTokenTransfer(fromAddress: string, toAddress: string, amount: bigint) {
|
|
94
|
+
* console.log('🚀 Initiating transfer...');
|
|
95
|
+
* await sleep(500);
|
|
96
|
+
*
|
|
97
|
+
* console.log('⏳ Preparing transaction...');
|
|
98
|
+
* const tx = await buildTransferTransaction(fromAddress, toAddress, amount);
|
|
99
|
+
* await sleep(800);
|
|
100
|
+
*
|
|
101
|
+
* console.log('📝 Signing transaction...');
|
|
102
|
+
* await sleep(1000);
|
|
103
|
+
*
|
|
104
|
+
* console.log('📡 Broadcasting to network...');
|
|
105
|
+
* const result = await options.evm.sendTransactions({ transactions: [tx] });
|
|
106
|
+
* await sleep(500);
|
|
107
|
+
*
|
|
108
|
+
* console.log('✅ Transfer complete!');
|
|
109
|
+
* return result;
|
|
110
|
+
* }
|
|
111
|
+
*
|
|
112
|
+
* // Throttled API calls
|
|
113
|
+
* async function throttledApiCalls<T>(
|
|
114
|
+
* apiCalls: (() => Promise<T>)[],
|
|
115
|
+
* delayBetweenCalls: number = 1000
|
|
116
|
+
* ): Promise<T[]> {
|
|
117
|
+
* const results: T[] = [];
|
|
118
|
+
*
|
|
119
|
+
* for (let i = 0; i < apiCalls.length; i++) {
|
|
120
|
+
* const result = await apiCalls[i]();
|
|
121
|
+
* results.push(result);
|
|
122
|
+
*
|
|
123
|
+
* // Don't wait after the last call
|
|
124
|
+
* if (i < apiCalls.length - 1) {
|
|
125
|
+
* await sleep(delayBetweenCalls);
|
|
126
|
+
* }
|
|
127
|
+
* }
|
|
128
|
+
*
|
|
129
|
+
* return results;
|
|
130
|
+
* }
|
|
131
|
+
*
|
|
132
|
+
* // Blockchain confirmation waiting
|
|
133
|
+
* async function waitForConfirmations(
|
|
134
|
+
* transactionHash: string,
|
|
135
|
+
* requiredConfirmations: number = 6,
|
|
136
|
+
* checkInterval: number = 15000 // 15 seconds
|
|
137
|
+
* ) {
|
|
138
|
+
* let confirmations = 0;
|
|
139
|
+
*
|
|
140
|
+
* while (confirmations < requiredConfirmations) {
|
|
141
|
+
* const receipt = await provider.getTransactionReceipt(transactionHash);
|
|
142
|
+
*
|
|
143
|
+
* if (!receipt) {
|
|
144
|
+
* console.log('Transaction not found, waiting...');
|
|
145
|
+
* await sleep(checkInterval);
|
|
146
|
+
* continue;
|
|
147
|
+
* }
|
|
148
|
+
*
|
|
149
|
+
* const currentBlock = await provider.getBlockNumber();
|
|
150
|
+
* confirmations = currentBlock - receipt.blockNumber + 1;
|
|
151
|
+
*
|
|
152
|
+
* console.log(`Confirmations: ${confirmations}/${requiredConfirmations}`);
|
|
153
|
+
*
|
|
154
|
+
* if (confirmations < requiredConfirmations) {
|
|
155
|
+
* await sleep(checkInterval);
|
|
156
|
+
* }
|
|
157
|
+
* }
|
|
158
|
+
*
|
|
159
|
+
* console.log('✅ Transaction fully confirmed!');
|
|
160
|
+
* }
|
|
161
|
+
*
|
|
162
|
+
* // Solana transaction confirmation with sleep
|
|
163
|
+
* async function confirmSolanaTransactionWithSleep(
|
|
164
|
+
* signature: string,
|
|
165
|
+
* connection: Connection,
|
|
166
|
+
* timeout: number = 60000
|
|
167
|
+
* ) {
|
|
168
|
+
* const startTime = Date.now();
|
|
169
|
+
*
|
|
170
|
+
* while (Date.now() - startTime < timeout) {
|
|
171
|
+
* const status = await connection.getSignatureStatus(signature);
|
|
172
|
+
*
|
|
173
|
+
* if (status.value?.confirmationStatus === 'confirmed' ||
|
|
174
|
+
* status.value?.confirmationStatus === 'finalized') {
|
|
175
|
+
* return status.value;
|
|
176
|
+
* }
|
|
177
|
+
*
|
|
178
|
+
* await sleep(1000); // Check every second
|
|
179
|
+
* }
|
|
180
|
+
*
|
|
181
|
+
* throw new Error('Transaction confirmation timeout');
|
|
182
|
+
* }
|
|
183
|
+
*
|
|
184
|
+
* // TON transaction monitoring
|
|
185
|
+
* async function monitorTonTransaction(hash: string, client: Client) {
|
|
186
|
+
* const maxAttempts = 30;
|
|
187
|
+
*
|
|
188
|
+
* for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
189
|
+
* try {
|
|
190
|
+
* const tx = await client.api.blockchain.getBlockchainTransaction(hash);
|
|
191
|
+
*
|
|
192
|
+
* if (tx.success) {
|
|
193
|
+
* return tx;
|
|
194
|
+
* } else {
|
|
195
|
+
* throw new Error('Transaction failed');
|
|
196
|
+
* }
|
|
197
|
+
* } catch (error) {
|
|
198
|
+
* if (attempt === maxAttempts) {
|
|
199
|
+
* throw error;
|
|
200
|
+
* }
|
|
201
|
+
*
|
|
202
|
+
* console.log(`Attempt ${attempt}: Transaction not ready, waiting 2 seconds...`);
|
|
203
|
+
* await sleep(2000);
|
|
204
|
+
* }
|
|
205
|
+
* }
|
|
206
|
+
* }
|
|
207
|
+
*
|
|
208
|
+
* // Progressive loading with sleep
|
|
209
|
+
* async function progressiveDataLoad(addresses: string[], progressCallback?: (progress: number) => void) {
|
|
210
|
+
* const results = [];
|
|
211
|
+
* const total = addresses.length;
|
|
212
|
+
*
|
|
213
|
+
* for (let i = 0; i < addresses.length; i++) {
|
|
214
|
+
* const address = addresses[i];
|
|
215
|
+
* const data = await getAddressData(address);
|
|
216
|
+
* results.push(data);
|
|
217
|
+
*
|
|
218
|
+
* // Update progress
|
|
219
|
+
* const progress = ((i + 1) / total) * 100;
|
|
220
|
+
* progressCallback?.(progress);
|
|
221
|
+
*
|
|
222
|
+
* // Brief pause between loads
|
|
223
|
+
* if (i < addresses.length - 1) {
|
|
224
|
+
* await sleep(200);
|
|
225
|
+
* }
|
|
226
|
+
* }
|
|
227
|
+
*
|
|
228
|
+
* return results;
|
|
229
|
+
* }
|
|
230
|
+
*
|
|
231
|
+
* // Utility functions with sleep
|
|
232
|
+
* const sleepSeconds = (seconds: number) => sleep(seconds * 1000);
|
|
233
|
+
* const sleepMinutes = (minutes: number) => sleep(minutes * 60 * 1000);
|
|
234
|
+
*
|
|
235
|
+
* // Usage examples
|
|
236
|
+
* // await sleepSeconds(5); // Sleep for 5 seconds
|
|
237
|
+
* // await sleepMinutes(1); // Sleep for 1 minute
|
|
238
|
+
* ```
|
|
239
|
+
*/
|
|
1
240
|
export declare const sleep: (ms: number) => Promise<unknown>;
|