@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
|
@@ -1,29 +1,302 @@
|
|
|
1
1
|
import { Abi, Address, Hex } from 'viem';
|
|
2
|
+
/**
|
|
3
|
+
* Parameters for an EVM transaction
|
|
4
|
+
* @interface TransactionParams
|
|
5
|
+
* @example
|
|
6
|
+
* ```typescript
|
|
7
|
+
* // ERC20 token transfer
|
|
8
|
+
* const transferTx: TransactionParams = {
|
|
9
|
+
* target: '0xA0b86a33E6417e4681831442Ff7Bd6c25b5d9C7a', // USDC contract
|
|
10
|
+
* data: encodeFunctionData({
|
|
11
|
+
* abi: erc20Abi,
|
|
12
|
+
* functionName: 'transfer',
|
|
13
|
+
* args: ['0x742d35Cc6634C0532925a3b8D4C2CA1c1DfF0bE8', parseUnits('100', 6)]
|
|
14
|
+
* })
|
|
15
|
+
* };
|
|
16
|
+
*
|
|
17
|
+
* // Native token transfer
|
|
18
|
+
* const nativeTx: TransactionParams = {
|
|
19
|
+
* target: '0x742d35Cc6634C0532925a3b8D4C2CA1c1DfF0bE8',
|
|
20
|
+
* data: '0x',
|
|
21
|
+
* value: parseEther('1') // 1 ETH
|
|
22
|
+
* };
|
|
23
|
+
*
|
|
24
|
+
* // Contract interaction with gas limit
|
|
25
|
+
* const contractTx: TransactionParams = {
|
|
26
|
+
* target: '0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984', // UNI contract
|
|
27
|
+
* data: encodeFunctionData({
|
|
28
|
+
* abi: uniswapAbi,
|
|
29
|
+
* functionName: 'swapExactTokensForTokens',
|
|
30
|
+
* args: [amountIn, amountOutMin, path, to, deadline]
|
|
31
|
+
* }),
|
|
32
|
+
* gas: 200000n // Custom gas limit
|
|
33
|
+
* };
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
2
36
|
export interface TransactionParams {
|
|
37
|
+
/** Contract or recipient address */
|
|
3
38
|
readonly target: Address;
|
|
39
|
+
/** Encoded function call data or '0x' for simple transfers */
|
|
4
40
|
readonly data: Hex;
|
|
41
|
+
/** Amount of native currency to send (optional) */
|
|
5
42
|
readonly value?: bigint;
|
|
43
|
+
/** Gas limit for the transaction (optional) */
|
|
6
44
|
readonly gas?: bigint;
|
|
7
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* Data returned for each executed transaction
|
|
48
|
+
* @interface TransactionReturnData
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* // Successful transaction result
|
|
52
|
+
* const successData: TransactionReturnData = {
|
|
53
|
+
* message: "Transaction confirmed",
|
|
54
|
+
* hash: "0x1234567890abcdef..."
|
|
55
|
+
* };
|
|
56
|
+
*
|
|
57
|
+
* // Failed transaction result
|
|
58
|
+
* const failedData: TransactionReturnData = {
|
|
59
|
+
* message: "Transaction failed: insufficient gas",
|
|
60
|
+
* hash: "0xabcdef1234567890..."
|
|
61
|
+
* };
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
8
64
|
export interface TransactionReturnData {
|
|
65
|
+
/** Status message or error description */
|
|
9
66
|
readonly message: string;
|
|
67
|
+
/** Transaction hash on the blockchain */
|
|
10
68
|
readonly hash: Hex;
|
|
11
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* Complete result of transaction execution containing all transaction results
|
|
72
|
+
* @interface TransactionReturn
|
|
73
|
+
* @example
|
|
74
|
+
* ```typescript
|
|
75
|
+
* // Batch transaction result
|
|
76
|
+
* const batchResult: TransactionReturn = {
|
|
77
|
+
* data: [
|
|
78
|
+
* {
|
|
79
|
+
* message: "Approval confirmed",
|
|
80
|
+
* hash: "0x1111..."
|
|
81
|
+
* },
|
|
82
|
+
* {
|
|
83
|
+
* message: "Swap completed",
|
|
84
|
+
* hash: "0x2222..."
|
|
85
|
+
* }
|
|
86
|
+
* ]
|
|
87
|
+
* };
|
|
88
|
+
*
|
|
89
|
+
* // Single transaction result
|
|
90
|
+
* const singleResult: TransactionReturn = {
|
|
91
|
+
* data: [{
|
|
92
|
+
* message: "Transfer successful",
|
|
93
|
+
* hash: "0x3333..."
|
|
94
|
+
* }]
|
|
95
|
+
* };
|
|
96
|
+
*
|
|
97
|
+
* // Process results
|
|
98
|
+
* batchResult.data.forEach((tx, index) => {
|
|
99
|
+
* console.log(`Transaction ${index + 1}: ${tx.message} - ${tx.hash}`);
|
|
100
|
+
* });
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
12
103
|
export interface TransactionReturn {
|
|
104
|
+
/** Array of transaction results */
|
|
13
105
|
readonly data: TransactionReturnData[];
|
|
14
106
|
}
|
|
107
|
+
/**
|
|
108
|
+
* Properties for sending one or multiple transactions
|
|
109
|
+
* @interface SendTransactionProps
|
|
110
|
+
* @example
|
|
111
|
+
* ```typescript
|
|
112
|
+
* // Single transaction
|
|
113
|
+
* const singleTxProps: SendTransactionProps = {
|
|
114
|
+
* chainId: 1, // Ethereum mainnet
|
|
115
|
+
* account: '0x742d35Cc6634C0532925a3b8D4C2CA1c1DfF0bE8',
|
|
116
|
+
* transactions: [{
|
|
117
|
+
* target: '0xA0b86a33E6417e4681831442Ff7Bd6c25b5d9C7a',
|
|
118
|
+
* data: encodeFunctionData({
|
|
119
|
+
* abi: erc20Abi,
|
|
120
|
+
* functionName: 'transfer',
|
|
121
|
+
* args: [recipient, amount]
|
|
122
|
+
* })
|
|
123
|
+
* }]
|
|
124
|
+
* };
|
|
125
|
+
*
|
|
126
|
+
* // Batch transactions (approve + swap)
|
|
127
|
+
* const batchTxProps: SendTransactionProps = {
|
|
128
|
+
* chainId: 137, // Polygon
|
|
129
|
+
* account: userAddress,
|
|
130
|
+
* transactions: [
|
|
131
|
+
* {
|
|
132
|
+
* target: tokenAddress,
|
|
133
|
+
* data: encodeFunctionData({
|
|
134
|
+
* abi: erc20Abi,
|
|
135
|
+
* functionName: 'approve',
|
|
136
|
+
* args: [spenderAddress, amount]
|
|
137
|
+
* })
|
|
138
|
+
* },
|
|
139
|
+
* {
|
|
140
|
+
* target: dexRouterAddress,
|
|
141
|
+
* data: encodeFunctionData({
|
|
142
|
+
* abi: dexRouterAbi,
|
|
143
|
+
* functionName: 'swapExactTokensForTokens',
|
|
144
|
+
* args: [amountIn, amountOutMin, path, userAddress, deadline]
|
|
145
|
+
* })
|
|
146
|
+
* }
|
|
147
|
+
* ]
|
|
148
|
+
* };
|
|
149
|
+
*
|
|
150
|
+
* // Usage in adapter function
|
|
151
|
+
* const result = await options.evm.sendTransactions(batchTxProps);
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
15
154
|
export interface SendTransactionProps {
|
|
155
|
+
/** Chain ID where transactions will be executed */
|
|
16
156
|
readonly chainId: number;
|
|
157
|
+
/** Account address that will execute the transactions */
|
|
17
158
|
readonly account: Address;
|
|
159
|
+
/** Array of transactions to execute */
|
|
18
160
|
readonly transactions: TransactionParams[];
|
|
19
161
|
}
|
|
162
|
+
/**
|
|
163
|
+
* Properties for deploying a smart contract
|
|
164
|
+
* @interface ContractProps
|
|
165
|
+
* @example
|
|
166
|
+
* ```typescript
|
|
167
|
+
* // ERC20 token deployment
|
|
168
|
+
* const tokenContract: ContractProps = {
|
|
169
|
+
* abi: [
|
|
170
|
+
* {
|
|
171
|
+
* "inputs": [
|
|
172
|
+
* {"name": "_name", "type": "string"},
|
|
173
|
+
* {"name": "_symbol", "type": "string"},
|
|
174
|
+
* {"name": "_totalSupply", "type": "uint256"}
|
|
175
|
+
* ],
|
|
176
|
+
* "stateMutability": "nonpayable",
|
|
177
|
+
* "type": "constructor"
|
|
178
|
+
* }
|
|
179
|
+
* ],
|
|
180
|
+
* bytecode: "0x608060405234801561001057600080fd5b50...",
|
|
181
|
+
* args: ["MyToken", "MTK", parseUnits("1000000", 18)]
|
|
182
|
+
* };
|
|
183
|
+
*
|
|
184
|
+
* // Simple contract without constructor arguments
|
|
185
|
+
* const simpleContract: ContractProps = {
|
|
186
|
+
* abi: contractAbi,
|
|
187
|
+
* bytecode: "0x608060405234801561001057600080fd5b50..."
|
|
188
|
+
* };
|
|
189
|
+
*
|
|
190
|
+
* // Multi-signature wallet deployment
|
|
191
|
+
* const multisigContract: ContractProps = {
|
|
192
|
+
* abi: multisigAbi,
|
|
193
|
+
* bytecode: multisigBytecode,
|
|
194
|
+
* args: [
|
|
195
|
+
* ['0x1111...', '0x2222...', '0x3333...'], // owners
|
|
196
|
+
* 2 // required confirmations
|
|
197
|
+
* ]
|
|
198
|
+
* };
|
|
199
|
+
* ```
|
|
200
|
+
*/
|
|
20
201
|
export interface ContractProps {
|
|
202
|
+
/** Contract ABI (Application Binary Interface) */
|
|
21
203
|
readonly abi: Abi | any[];
|
|
204
|
+
/** Contract bytecode for deployment */
|
|
22
205
|
readonly bytecode: Hex;
|
|
206
|
+
/** Constructor arguments (optional) */
|
|
23
207
|
readonly args?: any[];
|
|
24
208
|
}
|
|
209
|
+
/**
|
|
210
|
+
* Properties for deploying one or multiple smart contracts
|
|
211
|
+
* @interface DeployContractProps
|
|
212
|
+
* @example
|
|
213
|
+
* ```typescript
|
|
214
|
+
* // Single contract deployment
|
|
215
|
+
* const deployProps: DeployContractProps = {
|
|
216
|
+
* chainId: 1, // Ethereum mainnet
|
|
217
|
+
* account: '0x742d35Cc6634C0532925a3b8D4C2CA1c1DfF0bE8',
|
|
218
|
+
* contracts: [{
|
|
219
|
+
* abi: erc20Abi,
|
|
220
|
+
* bytecode: erc20Bytecode,
|
|
221
|
+
* args: ["MyToken", "MTK", parseUnits("1000000", 18)]
|
|
222
|
+
* }]
|
|
223
|
+
* };
|
|
224
|
+
*
|
|
225
|
+
* // Multiple contract deployment (factory pattern)
|
|
226
|
+
* const factoryDeployProps: DeployContractProps = {
|
|
227
|
+
* chainId: 137, // Polygon
|
|
228
|
+
* account: deployerAddress,
|
|
229
|
+
* contracts: [
|
|
230
|
+
* {
|
|
231
|
+
* abi: factoryAbi,
|
|
232
|
+
* bytecode: factoryBytecode,
|
|
233
|
+
* args: [feeRecipient, defaultFee]
|
|
234
|
+
* },
|
|
235
|
+
* {
|
|
236
|
+
* abi: implementationAbi,
|
|
237
|
+
* bytecode: implementationBytecode,
|
|
238
|
+
* args: []
|
|
239
|
+
* }
|
|
240
|
+
* ]
|
|
241
|
+
* };
|
|
242
|
+
*
|
|
243
|
+
* // DeFi protocol deployment
|
|
244
|
+
* const defiProtocolDeploy: DeployContractProps = {
|
|
245
|
+
* chainId: 42161, // Arbitrum
|
|
246
|
+
* account: protocolDeployer,
|
|
247
|
+
* contracts: [
|
|
248
|
+
* {
|
|
249
|
+
* abi: vaultAbi,
|
|
250
|
+
* bytecode: vaultBytecode,
|
|
251
|
+
* args: [underlyingToken, "Yield Vault", "YV"]
|
|
252
|
+
* },
|
|
253
|
+
* {
|
|
254
|
+
* abi: strategyAbi,
|
|
255
|
+
* bytecode: strategyBytecode,
|
|
256
|
+
* args: [vaultAddress, farmingPoolAddress]
|
|
257
|
+
* },
|
|
258
|
+
* {
|
|
259
|
+
* abi: governanceAbi,
|
|
260
|
+
* bytecode: governanceBytecode,
|
|
261
|
+
* args: [governanceToken, votingDelay, votingPeriod]
|
|
262
|
+
* }
|
|
263
|
+
* ]
|
|
264
|
+
* };
|
|
265
|
+
*
|
|
266
|
+
* // Usage in adapter function
|
|
267
|
+
* async function deployContracts(deployProps: DeployContractProps) {
|
|
268
|
+
* const deployedAddresses = await options.evm.deployContracts(deployProps);
|
|
269
|
+
*
|
|
270
|
+
* deployedAddresses.forEach((address, index) => {
|
|
271
|
+
* console.log(`Contract ${index + 1} deployed at: ${address}`);
|
|
272
|
+
* });
|
|
273
|
+
*
|
|
274
|
+
* return deployedAddresses;
|
|
275
|
+
* }
|
|
276
|
+
*
|
|
277
|
+
* // Contract verification helper
|
|
278
|
+
* function prepareContractVerification(
|
|
279
|
+
* deployProps: DeployContractProps,
|
|
280
|
+
* deployedAddresses: Address[]
|
|
281
|
+
* ) {
|
|
282
|
+
* return deployProps.contracts.map((contract, index) => ({
|
|
283
|
+
* address: deployedAddresses[index],
|
|
284
|
+
* contractName: `Contract${index + 1}`,
|
|
285
|
+
* sourceCode: getSourceCodeFromBytecode(contract.bytecode),
|
|
286
|
+
* abi: JSON.stringify(contract.abi),
|
|
287
|
+
* constructorArgs: contract.args ? encodeAbiParameters(
|
|
288
|
+
* getConstructorInputs(contract.abi),
|
|
289
|
+
* contract.args
|
|
290
|
+
* ) : undefined
|
|
291
|
+
* }));
|
|
292
|
+
* }
|
|
293
|
+
* ```
|
|
294
|
+
*/
|
|
25
295
|
export interface DeployContractProps {
|
|
296
|
+
/** Chain ID where contracts will be deployed */
|
|
26
297
|
readonly chainId: number;
|
|
298
|
+
/** Account address that will deploy the contracts */
|
|
27
299
|
readonly account: Address;
|
|
300
|
+
/** Array of contracts to deploy */
|
|
28
301
|
readonly contracts: ContractProps[];
|
|
29
302
|
}
|
|
@@ -1,14 +1,100 @@
|
|
|
1
1
|
import { Address, PublicClient } from 'viem';
|
|
2
2
|
import { TransactionParams } from '../types';
|
|
3
|
+
/**
|
|
4
|
+
* Properties for checking and preparing token approval transactions
|
|
5
|
+
* @interface Props
|
|
6
|
+
*/
|
|
3
7
|
interface Props {
|
|
8
|
+
/** Approval parameters */
|
|
4
9
|
readonly args: {
|
|
10
|
+
/** Account address that owns the tokens */
|
|
5
11
|
readonly account: Address;
|
|
12
|
+
/** Token contract address */
|
|
6
13
|
readonly target: Address;
|
|
14
|
+
/** Address that will be approved to spend tokens */
|
|
7
15
|
readonly spender: Address;
|
|
16
|
+
/** Amount of tokens to approve */
|
|
8
17
|
readonly amount: bigint;
|
|
9
18
|
};
|
|
19
|
+
/** Ethereum provider for reading contract state */
|
|
10
20
|
readonly provider: PublicClient;
|
|
21
|
+
/** Transaction array to append approval transactions to */
|
|
11
22
|
readonly transactions: TransactionParams[];
|
|
12
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Checks current token allowance and prepares approval transactions if needed
|
|
26
|
+
* @param props - The approval check parameters
|
|
27
|
+
* @description Handles special cases like USDT that require zero allowance before setting new approval
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* // Basic usage in a swap function
|
|
31
|
+
* async function prepareSwapTransactions() {
|
|
32
|
+
* const transactions: TransactionParams[] = [];
|
|
33
|
+
*
|
|
34
|
+
* await checkToApprove({
|
|
35
|
+
* args: {
|
|
36
|
+
* account: '0x742d35Cc6634C0532925a3b8D4C2CA1c1DfF0bE8',
|
|
37
|
+
* target: '0xA0b86a33E6417e4681831442Ff7Bd6c25b5d9C7a', // USDC
|
|
38
|
+
* spender: '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D', // Uniswap V2 Router
|
|
39
|
+
* amount: parseUnits('100', 6) // 100 USDC
|
|
40
|
+
* },
|
|
41
|
+
* provider: publicClient,
|
|
42
|
+
* transactions
|
|
43
|
+
* });
|
|
44
|
+
*
|
|
45
|
+
* // Add swap transaction
|
|
46
|
+
* transactions.push({
|
|
47
|
+
* target: routerAddress,
|
|
48
|
+
* data: swapCalldata
|
|
49
|
+
* });
|
|
50
|
+
*
|
|
51
|
+
* return transactions;
|
|
52
|
+
* }
|
|
53
|
+
*
|
|
54
|
+
* // Example with USDT (requires zero allowance)
|
|
55
|
+
* async function approveUSDT() {
|
|
56
|
+
* const transactions: TransactionParams[] = [];
|
|
57
|
+
*
|
|
58
|
+
* await checkToApprove({
|
|
59
|
+
* args: {
|
|
60
|
+
* account: userAddress,
|
|
61
|
+
* target: '0xdAC17F958D2ee523a2206206994597C13D831ec7', // USDT
|
|
62
|
+
* spender: spenderAddress,
|
|
63
|
+
* amount: parseUnits('50', 6)
|
|
64
|
+
* },
|
|
65
|
+
* provider: ethereumProvider,
|
|
66
|
+
* transactions
|
|
67
|
+
* });
|
|
68
|
+
*
|
|
69
|
+
* // Will generate two transactions for USDT:
|
|
70
|
+
* // 1. approve(spender, 0) - reset to zero
|
|
71
|
+
* // 2. approve(spender, amount) - set new allowance
|
|
72
|
+
* }
|
|
73
|
+
*
|
|
74
|
+
* // Integration with adapter function
|
|
75
|
+
* async function executeSwap(tokenIn: Address, tokenOut: Address, amountIn: bigint) {
|
|
76
|
+
* const transactions: TransactionParams[] = [];
|
|
77
|
+
*
|
|
78
|
+
* // Check and prepare approval if needed
|
|
79
|
+
* if (tokenIn !== NATIVE_ADDRESS) {
|
|
80
|
+
* await checkToApprove({
|
|
81
|
+
* args: {
|
|
82
|
+
* account: await options.evm.getAddress(),
|
|
83
|
+
* target: tokenIn,
|
|
84
|
+
* spender: DEX_ROUTER_ADDRESS,
|
|
85
|
+
* amount: amountIn
|
|
86
|
+
* },
|
|
87
|
+
* provider: options.evm.getProvider(chainId),
|
|
88
|
+
* transactions
|
|
89
|
+
* });
|
|
90
|
+
* }
|
|
91
|
+
*
|
|
92
|
+
* // Add swap transaction
|
|
93
|
+
* transactions.push(buildSwapTransaction(tokenIn, tokenOut, amountIn));
|
|
94
|
+
*
|
|
95
|
+
* return await options.evm.sendTransactions({ transactions });
|
|
96
|
+
* }
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
13
99
|
export declare function checkToApprove({ args, transactions, provider }: Props): Promise<void>;
|
|
14
100
|
export {};
|
|
@@ -1,2 +1,83 @@
|
|
|
1
1
|
import { EvmChain } from '../../constants';
|
|
2
|
+
/**
|
|
3
|
+
* Converts an EVM chain name to its corresponding numeric chain ID
|
|
4
|
+
* @param chain - The EVM chain name
|
|
5
|
+
* @returns The numeric chain ID for the specified chain
|
|
6
|
+
* @throws {Error} When the chain name is not supported
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* // Get chain IDs for different networks
|
|
10
|
+
* const ethereumId = getChainFromName(Chain.ETHEREUM); // 1
|
|
11
|
+
* const polygonId = getChainFromName(Chain.POLYGON); // 137
|
|
12
|
+
* const baseId = getChainFromName(Chain.BASE); // 8453
|
|
13
|
+
*
|
|
14
|
+
* // Use in provider configuration
|
|
15
|
+
* function createProvider(chain: EvmChain) {
|
|
16
|
+
* const chainId = getChainFromName(chain);
|
|
17
|
+
* return new PublicClient({
|
|
18
|
+
* chain: { id: chainId, name: chain },
|
|
19
|
+
* transport: http(getRpcUrl(chainId))
|
|
20
|
+
* });
|
|
21
|
+
* }
|
|
22
|
+
*
|
|
23
|
+
* // Validate and switch wallet chain
|
|
24
|
+
* async function switchWalletChain(targetChain: EvmChain) {
|
|
25
|
+
* try {
|
|
26
|
+
* const chainId = getChainFromName(targetChain);
|
|
27
|
+
*
|
|
28
|
+
* await window.ethereum.request({
|
|
29
|
+
* method: 'wallet_switchEthereumChain',
|
|
30
|
+
* params: [{ chainId: `0x${chainId.toString(16)}` }]
|
|
31
|
+
* });
|
|
32
|
+
*
|
|
33
|
+
* console.log(`Switched to ${targetChain} (Chain ID: ${chainId})`);
|
|
34
|
+
* } catch (error) {
|
|
35
|
+
* console.error(`Failed to switch to ${targetChain}:`, error);
|
|
36
|
+
* }
|
|
37
|
+
* }
|
|
38
|
+
*
|
|
39
|
+
* // Use in multi-chain adapter function
|
|
40
|
+
* async function getTokenBalance(chain: EvmChain, tokenAddress: string, userAddress: string) {
|
|
41
|
+
* const chainId = getChainFromName(chain);
|
|
42
|
+
* const provider = options.evm.getProvider(chainId);
|
|
43
|
+
*
|
|
44
|
+
* return await provider.readContract({
|
|
45
|
+
* address: tokenAddress,
|
|
46
|
+
* abi: erc20Abi,
|
|
47
|
+
* functionName: 'balanceOf',
|
|
48
|
+
* args: [userAddress]
|
|
49
|
+
* });
|
|
50
|
+
* }
|
|
51
|
+
*
|
|
52
|
+
* // Error handling example
|
|
53
|
+
* function safeGetChainId(chain: string): number | null {
|
|
54
|
+
* try {
|
|
55
|
+
* return getChainFromName(chain as EvmChain);
|
|
56
|
+
* } catch (error) {
|
|
57
|
+
* console.warn(`Unsupported chain: ${chain}`);
|
|
58
|
+
* return null;
|
|
59
|
+
* }
|
|
60
|
+
* }
|
|
61
|
+
*
|
|
62
|
+
* // Batch operations across multiple chains
|
|
63
|
+
* async function getMultiChainData(chains: EvmChain[]) {
|
|
64
|
+
* const results = await Promise.allSettled(
|
|
65
|
+
* chains.map(async (chain) => {
|
|
66
|
+
* const chainId = getChainFromName(chain);
|
|
67
|
+
* const provider = options.evm.getProvider(chainId);
|
|
68
|
+
*
|
|
69
|
+
* return {
|
|
70
|
+
* chain,
|
|
71
|
+
* chainId,
|
|
72
|
+
* blockNumber: await provider.getBlockNumber()
|
|
73
|
+
* };
|
|
74
|
+
* })
|
|
75
|
+
* );
|
|
76
|
+
*
|
|
77
|
+
* return results
|
|
78
|
+
* .filter((result): result is PromiseFulfilledResult<any> => result.status === 'fulfilled')
|
|
79
|
+
* .map(result => result.value);
|
|
80
|
+
* }
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
2
83
|
export declare function getChainFromName(chain: EvmChain): number;
|
|
@@ -1 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts a numeric chain ID to its corresponding EVM chain name
|
|
3
|
+
* @param chainId - The numeric chain ID to convert
|
|
4
|
+
* @returns The chain name corresponding to the provided chain ID
|
|
5
|
+
* @throws {Error} When the chain ID is not supported
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* // Get chain names from IDs
|
|
9
|
+
* const ethereumName = getChainName(1); // 'ethereum'
|
|
10
|
+
* const polygonName = getChainName(137); // 'polygon'
|
|
11
|
+
* const baseName = getChainName(8453); // 'base'
|
|
12
|
+
*
|
|
13
|
+
* // Handle wallet chain changes
|
|
14
|
+
* window.ethereum.on('chainChanged', (chainId: string) => {
|
|
15
|
+
* try {
|
|
16
|
+
* const decimalChainId = parseInt(chainId, 16);
|
|
17
|
+
* const chainName = getChainName(decimalChainId);
|
|
18
|
+
* console.log(`Switched to ${chainName} (Chain ID: ${decimalChainId})`);
|
|
19
|
+
*
|
|
20
|
+
* // Update UI or reload data for new chain
|
|
21
|
+
* updateUIForChain(chainName);
|
|
22
|
+
* } catch (error) {
|
|
23
|
+
* console.warn(`Unsupported chain ID: ${chainId}`);
|
|
24
|
+
* showUnsupportedChainWarning();
|
|
25
|
+
* }
|
|
26
|
+
* });
|
|
27
|
+
*
|
|
28
|
+
* // Validate user's current chain
|
|
29
|
+
* async function validateCurrentChain() {
|
|
30
|
+
* const chainId = await window.ethereum.request({ method: 'eth_chainId' });
|
|
31
|
+
* const decimalChainId = parseInt(chainId, 16);
|
|
32
|
+
*
|
|
33
|
+
* try {
|
|
34
|
+
* const chainName = getChainName(decimalChainId);
|
|
35
|
+
* return { supported: true, chainName, chainId: decimalChainId };
|
|
36
|
+
* } catch (error) {
|
|
37
|
+
* return { supported: false, chainId: decimalChainId };
|
|
38
|
+
* }
|
|
39
|
+
* }
|
|
40
|
+
*
|
|
41
|
+
* // Display user-friendly chain information
|
|
42
|
+
* function formatChainInfo(chainId: number) {
|
|
43
|
+
* try {
|
|
44
|
+
* const chainName = getChainName(chainId);
|
|
45
|
+
* const displayNames = {
|
|
46
|
+
* ethereum: 'Ethereum Mainnet',
|
|
47
|
+
* polygon: 'Polygon',
|
|
48
|
+
* bsc: 'Binance Smart Chain',
|
|
49
|
+
* arbitrum: 'Arbitrum One',
|
|
50
|
+
* base: 'Base',
|
|
51
|
+
* optimism: 'Optimism'
|
|
52
|
+
* };
|
|
53
|
+
*
|
|
54
|
+
* return {
|
|
55
|
+
* name: chainName,
|
|
56
|
+
* displayName: displayNames[chainName] || chainName,
|
|
57
|
+
* chainId
|
|
58
|
+
* };
|
|
59
|
+
* } catch (error) {
|
|
60
|
+
* return {
|
|
61
|
+
* name: 'unknown',
|
|
62
|
+
* displayName: `Unknown Chain (${chainId})`,
|
|
63
|
+
* chainId
|
|
64
|
+
* };
|
|
65
|
+
* }
|
|
66
|
+
* }
|
|
67
|
+
*
|
|
68
|
+
* // Use in multi-chain token tracking
|
|
69
|
+
* async function getTokenInfo(tokenAddress: string, chainId: number) {
|
|
70
|
+
* try {
|
|
71
|
+
* const chainName = getChainName(chainId);
|
|
72
|
+
* const provider = options.evm.getProvider(chainId);
|
|
73
|
+
*
|
|
74
|
+
* const [name, symbol, decimals] = await Promise.all([
|
|
75
|
+
* provider.readContract({
|
|
76
|
+
* address: tokenAddress,
|
|
77
|
+
* abi: erc20Abi,
|
|
78
|
+
* functionName: 'name'
|
|
79
|
+
* }),
|
|
80
|
+
* provider.readContract({
|
|
81
|
+
* address: tokenAddress,
|
|
82
|
+
* abi: erc20Abi,
|
|
83
|
+
* functionName: 'symbol'
|
|
84
|
+
* }),
|
|
85
|
+
* provider.readContract({
|
|
86
|
+
* address: tokenAddress,
|
|
87
|
+
* abi: erc20Abi,
|
|
88
|
+
* functionName: 'decimals'
|
|
89
|
+
* })
|
|
90
|
+
* ]);
|
|
91
|
+
*
|
|
92
|
+
* return {
|
|
93
|
+
* chain: chainName,
|
|
94
|
+
* chainId,
|
|
95
|
+
* address: tokenAddress,
|
|
96
|
+
* name,
|
|
97
|
+
* symbol,
|
|
98
|
+
* decimals
|
|
99
|
+
* };
|
|
100
|
+
* } catch (error) {
|
|
101
|
+
* throw new Error(`Failed to get token info on chain ${chainId}: ${error.message}`);
|
|
102
|
+
* }
|
|
103
|
+
* }
|
|
104
|
+
*
|
|
105
|
+
* // Safe version that returns null instead of throwing
|
|
106
|
+
* function safeGetChainName(chainId: number): string | null {
|
|
107
|
+
* try {
|
|
108
|
+
* return getChainName(chainId);
|
|
109
|
+
* } catch (error) {
|
|
110
|
+
* return null;
|
|
111
|
+
* }
|
|
112
|
+
* }
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
1
115
|
export declare function getChainName(chainId: number): string;
|