@aptos-labs/cross-chain-core 5.6.1 → 5.6.2

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/README.md CHANGED
@@ -1,90 +1,332 @@
1
- # Aptos Cross Chain CCTP
1
+ # Aptos Cross-Chain Core
2
2
 
3
- A wrapper for CCTP cross chain transactions.
3
+ A TypeScript SDK for cross-chain USDC transfers to and from the Aptos blockchain using [Circle's CCTP](https://www.circle.com/en/cross-chain-transfer-protocol) (Cross-Chain Transfer Protocol) via the [Wormhole](https://wormhole.com/) bridge.
4
4
 
5
- #### Supported Providers
5
+ ## Overview
6
6
 
7
- - Wormhole
7
+ This package enables seamless USDC transfers between Aptos and other supported blockchains. It abstracts the complexity of cross-chain transactions, providing a simple API for:
8
8
 
9
- ### Usage
9
+ - **Transfers**: Move USDC from external chains (Solana, Ethereum, etc.) to Aptos
10
+ - **Withdrawals**: Move USDC from Aptos to external chains
11
+ - **Balance Queries**: Check USDC balances across all supported chains
12
+ - **Quote Estimation**: Get transfer costs and fees before executing
10
13
 
11
- Install the `@aptos-labs/cross-chain-core` package
14
+ ## Architecture
12
15
 
13
- ```cli
14
- pnpm i @aptos-labs/cross-chain-core
16
+ ```mermaid
17
+ flowchart TB
18
+ subgraph core [CrossChainCore]
19
+ CCCore[CrossChainCore Class]
20
+ Config[Config - chains/tokens]
21
+ Utils[Utils - logger, balance]
22
+ end
23
+
24
+ subgraph provider [WormholeProvider]
25
+ WH[WormholeProvider]
26
+ Quote[getQuote]
27
+ Transfer[transfer]
28
+ Withdraw[withdraw]
29
+ end
30
+
31
+ subgraph signers [Signers]
32
+ MainSigner[Signer - Router]
33
+ SolanaSig[SolanaSigner]
34
+ EthSig[EthereumSigner]
35
+ AptosSig[AptosSigner]
36
+ SuiSig[SuiSigner]
37
+ AptosLocal[AptosLocalSigner]
38
+ end
39
+
40
+ CCCore --> WH
41
+ WH --> MainSigner
42
+ MainSigner --> SolanaSig
43
+ MainSigner --> EthSig
44
+ MainSigner --> AptosSig
45
+ MainSigner --> SuiSig
15
46
  ```
16
47
 
17
- Import the `CrossChainCore` class
48
+ ## Supported Chains
18
49
 
19
- ```ts
20
- import { CrossChainCore } from "@aptos-labs/cross-chain-core";
50
+ | Network | Mainnet Chain ID | Testnet Chain ID | Context |
51
+ |---------|------------------|------------------|---------|
52
+ | Aptos | 1 | 2 | Destination/Source |
53
+ | Solana | mainnet-beta | devnet | Source/Destination |
54
+ | Ethereum | 1 | 11155111 (Sepolia) | Source/Destination |
55
+ | Base | 8453 | 84532 (Sepolia) | Source/Destination |
56
+ | Arbitrum | 42161 | 421614 (Sepolia) | Source/Destination |
57
+ | Avalanche | 43114 | 43113 (Fuji) | Source/Destination |
58
+ | Polygon | 137 | 80002 (Amoy) | Source/Destination |
59
+ | Sui | mainnet | testnet | Source/Destination |
60
+
61
+ ## Supported Tokens
62
+
63
+ Currently, only **USDC** is supported via Circle's Cross-Chain Transfer Protocol (CCTP).
64
+
65
+ ## How It Works
66
+
67
+ 1. **Initialize**: Create a `CrossChainCore` instance with your network configuration
68
+ 2. **Get Provider**: Obtain a `WormholeProvider` for executing transfers
69
+ 3. **Get Quote**: Calculate fees and transfer parameters
70
+ 4. **Execute Transfer**: Sign and submit the cross-chain transaction
71
+ 5. **Wait for Completion**: The SDK handles attestation and claiming on the destination chain
72
+
73
+ ### Transfer Flow (External Chain → Aptos)
74
+
75
+ ```
76
+ Source Chain Wallet → CCTP Burn → Wormhole Attestation → Aptos Claim → Aptos Wallet
77
+ ```
78
+
79
+ ### Withdrawal Flow (Aptos → External Chain)
80
+
81
+ ```
82
+ Aptos Wallet → CCTP Burn → Wormhole Attestation → External Chain Claim → Destination Wallet
83
+ ```
84
+
85
+ ## Signers
86
+
87
+ A **Signer** is a component that handles transaction signing and submission for a specific blockchain. The SDK uses signers to interact with the Wormhole bridge protocol, implementing the `SignAndSendSigner` interface from the Wormhole SDK.
88
+
89
+ ### Available Signers
90
+
91
+ | Signer | Chain | Description |
92
+ |--------|-------|-------------|
93
+ | `Signer` | All | Router signer that delegates to chain-specific signers based on context |
94
+ | `SolanaSigner` | Solana | Signs transactions via `SolanaDerivedWallet` |
95
+ | `EthereumSigner` | EVM chains | Signs transactions via `EIP1193DerivedWallet` |
96
+ | `AptosSigner` | Aptos | Signs transactions via wallet adapter (user interaction) |
97
+ | `AptosLocalSigner` | Aptos | Signs transactions with a local `Account` (programmatic) |
98
+ | `SuiSigner` | Sui | Signs transactions via `SuiDerivedWallet` |
99
+
100
+ ### Why Two Aptos Signers?
101
+
102
+ Cross-chain transfers involve two distinct phases, each with different signing requirements:
103
+
104
+ #### The Two-Phase Transfer Process
105
+
106
+ 1. **Initiate Phase** (Source Chain): User burns USDC on the source chain and creates an attestation
107
+ 2. **Claim Phase** (Destination Chain): The attested USDC is minted on the destination chain
108
+
109
+ These phases require different types of signing:
110
+
111
+ | Phase | Signer | User Interaction | Use Case |
112
+ |-------|--------|------------------|----------|
113
+ | Initiate (Withdraw) | `AptosSigner` | ✅ Required | User burns USDC from their Aptos wallet |
114
+ | Claim (Transfer) | `AptosLocalSigner` | ❌ Automatic | System claims USDC on behalf of user |
115
+
116
+ #### AptosSigner
117
+
118
+ Used for **withdrawals** where the user initiates a transaction from their Aptos wallet:
119
+
120
+ - Requires wallet adapter connection and user approval
121
+ - User must explicitly sign to burn their USDC
122
+ - Standard wallet interaction flow
123
+
124
+ #### AptosLocalSigner
125
+
126
+ Used for **transfers** where USDC arrives on Aptos from another chain:
127
+
128
+ - Signs programmatically with a local `Account` object
129
+ - No user interaction required
130
+ - Enables automatic claiming without user action
131
+
132
+ #### The Derived Wallet Advantage
133
+
134
+ When transferring from external chains (Solana, Ethereum, etc.) to Aptos, the SDK uses **derived wallets**. A derived wallet creates an Aptos account address mathematically linked to the user's external chain public key.
135
+
136
+ **This means users don't need an existing Aptos wallet to receive funds!**
137
+
138
+ The flow works like this:
139
+
140
+ 1. User connects their Solana wallet (for example)
141
+ 2. SDK derives a corresponding Aptos address from the Solana public key
142
+ 3. User signs the burn transaction on Solana
143
+ 4. USDC is automatically claimed on Aptos using `AptosLocalSigner`
144
+ 5. Funds appear in the derived Aptos address, controlled by the same Solana key
145
+
146
+ This design enables seamless onboarding: users can transfer USDC to Aptos using only their existing external chain wallet.
147
+
148
+ ## Installation
149
+
150
+ ```bash
151
+ pnpm add @aptos-labs/cross-chain-core
21
152
  ```
22
153
 
23
- Initialize `CrossChainCore` and chosen provider
154
+ or
155
+
156
+ ```bash
157
+ npm install @aptos-labs/cross-chain-core
158
+ ```
159
+
160
+ ## Quick Start
161
+
162
+ ```typescript
163
+ import { CrossChainCore, Network } from "@aptos-labs/cross-chain-core";
24
164
 
25
- ```ts
165
+ // Initialize the core with network configuration
26
166
  const crossChainCore = new CrossChainCore({
27
- dappConfig: { aptosNetwork: dappNetwork },
167
+ dappConfig: {
168
+ aptosNetwork: Network.TESTNET,
169
+ // Optional: Custom Solana RPC
170
+ solanaConfig: {
171
+ rpc: "https://api.devnet.solana.com"
172
+ }
173
+ },
28
174
  });
29
175
 
176
+ // Get the Wormhole provider
30
177
  const provider = crossChainCore.getProvider("Wormhole");
31
178
  ```
32
179
 
33
- Once you have your provider setup, you can use it to query for:
180
+ ## API Reference
34
181
 
35
- - `getQuote` can be used to calculate the cost and parameters for transferring tokens between different blockchain networks using the provider's cross-chain bridge.
182
+ ### CrossChainCore
36
183
 
37
- ```ts
38
- const quote = await provider?.getQuote({
39
- // How much to transfer
40
- amount,
41
- // The origin network involved (besides Aptos, e.g Solana, Ethereum)
42
- originChain: sourceChain,
43
- // The transaction type:
44
- // transfer - transfer funds from a cross chain (Solana, Ethereum) wallet to Aptos
45
- // withdraw - transfer funds from an Aptos wallet to a cross chain (Solana, Ethereum) wallet
46
- type: "transfer",
184
+ The main entry point for cross-chain operations.
185
+
186
+ ```typescript
187
+ const core = new CrossChainCore({
188
+ dappConfig: {
189
+ aptosNetwork: Network.TESTNET | Network.MAINNET,
190
+ disableTelemetry?: boolean,
191
+ solanaConfig?: {
192
+ rpc?: string,
193
+ priorityFeeConfig?: {
194
+ percentile?: number,
195
+ percentileMultiple?: number,
196
+ min?: number,
197
+ max?: number,
198
+ }
199
+ }
200
+ }
47
201
  });
48
202
  ```
49
203
 
50
- The function returns pricing, fees, and transfer details needed to execute the cross-chain transaction.
204
+ #### `getProvider(providerType: "Wormhole")`
205
+
206
+ Returns a provider instance for executing cross-chain transfers.
207
+
208
+ ```typescript
209
+ const provider = core.getProvider("Wormhole");
210
+ ```
211
+
212
+ #### `getWalletUSDCBalance(walletAddress: string, chain: Chain)`
213
+
214
+ Get the USDC balance for a wallet on any supported chain.
215
+
216
+ ```typescript
217
+ const balance = await core.getWalletUSDCBalance(
218
+ "0x1234...", // wallet address
219
+ "Solana" // chain name
220
+ );
221
+ console.log(`Balance: ${balance} USDC`);
222
+ ```
223
+
224
+ ### WormholeProvider
51
225
 
52
- - `transfer` can be used to initiate a transfer of USDC funds from the source chain wallet to Aptos wallet
226
+ #### `getQuote(params)`
227
+
228
+ Calculate transfer costs and get a quote before executing.
229
+
230
+ ```typescript
231
+ const quote = await provider.getQuote({
232
+ // Amount to transfer (as string, e.g., "100.50")
233
+ amount: "100",
234
+ // The external chain involved
235
+ originChain: "Solana",
236
+ // Transaction type:
237
+ // - "transfer": External chain → Aptos
238
+ // - "withdraw": Aptos → External chain
239
+ type: "transfer"
240
+ });
241
+ ```
53
242
 
54
- ```ts
243
+ **Returns**: Quote object containing fees, estimated time, and transfer details.
244
+
245
+ #### `transfer(params)`
246
+
247
+ Transfer USDC from an external chain wallet to an Aptos wallet.
248
+
249
+ ```typescript
55
250
  const { originChainTxnId, destinationChainTxnId } = await provider.transfer({
56
- // The blockchain to transfer from
57
- sourceChain,
58
- // The source chain wallet to sign the transfer transaction
59
- wallet,
60
- // The destination address for the funds to go to
61
- destinationAddress: account?.address?.toString() ?? "",
62
- // The wallet/signer for the destination (Aptos) chain
63
- mainSigner,
64
- // (optional) Account to sponsor gas fees
65
- sponsorAccount,
251
+ // Source blockchain
252
+ sourceChain: "Solana",
253
+ // Source chain wallet adapter (must support signing)
254
+ wallet: solanaWalletAdapter,
255
+ // Destination Aptos address
256
+ destinationAddress: "0x1234...",
257
+ // Aptos account to sign the claim transaction
258
+ mainSigner: aptosAccount,
259
+ // Optional: Amount (if not using a previous quote)
260
+ amount: "100",
261
+ // Optional: Sponsor account for gas fees
262
+ sponsorAccount: feePayerAccount,
66
263
  });
264
+
265
+ console.log(`Source TX: ${originChainTxnId}`);
266
+ console.log(`Destination TX: ${destinationChainTxnId}`);
67
267
  ```
68
268
 
69
- This function returns:
70
- `originChainTxnId`: Transaction hash on the source chain
71
- `destinationChainTxnId`: Transaction hash on the Aptos destination chain
269
+ **Returns**:
270
+ - `originChainTxnId`: Transaction hash on the source chain
271
+ - `destinationChainTxnId`: Transaction hash on the Aptos destination chain
272
+
273
+ #### `withdraw(params)`
72
274
 
73
- - `withdraw` can be used to initiate a transfer of USDC funds an Aptos wallet to the destination chain wallet
275
+ Withdraw USDC from an Aptos wallet to an external chain wallet.
74
276
 
75
- ```ts
277
+ ```typescript
76
278
  const { originChainTxnId, destinationChainTxnId } = await provider.withdraw({
77
- // The blockchain to transfer from
78
- sourceChain,
79
- // The source chain wallet to sign the transfer transaction
80
- wallet,
81
- // The destination address for the funds to go to
82
- destinationAddress: originWalletDetails?.address.toString(),
83
- // (optional) Account to sponsor gas fees
84
- sponsorAccount,
279
+ // Destination blockchain
280
+ sourceChain: "Ethereum",
281
+ // Aptos wallet adapter
282
+ wallet: aptosWalletAdapter,
283
+ // Destination address on the external chain
284
+ destinationAddress: "0xabcd...",
285
+ // Optional: Sponsor account for gas fees
286
+ sponsorAccount: feePayerAccount,
85
287
  });
288
+
289
+ console.log(`Aptos TX: ${originChainTxnId}`);
290
+ console.log(`Destination TX: ${destinationChainTxnId}`);
86
291
  ```
87
292
 
88
- This function returns:
89
- `originChainTxnId`: Transaction hash on the source chain
90
- `destinationChainTxnId`: Transaction hash on the Aptos destination chain
293
+ **Returns**:
294
+ - `originChainTxnId`: Transaction hash on Aptos
295
+ - `destinationChainTxnId`: Transaction hash on the destination chain
296
+
297
+ ## Chain ID Mappings
298
+
299
+ The SDK provides mappings from Ethereum chain IDs to chain configurations:
300
+
301
+ ```typescript
302
+ import {
303
+ EthereumChainIdToTestnetChain,
304
+ EthereumChainIdToMainnetChain
305
+ } from "@aptos-labs/cross-chain-core";
306
+
307
+ // Get chain config from chain ID
308
+ const sepoliaConfig = EthereumChainIdToTestnetChain["11155111"];
309
+ const ethereumConfig = EthereumChainIdToMainnetChain["1"];
310
+ ```
311
+
312
+ ## Error Handling
313
+
314
+ The SDK throws descriptive errors for common issues:
315
+
316
+ ```typescript
317
+ try {
318
+ await provider.transfer({ ... });
319
+ } catch (error) {
320
+ if (error.message.includes("Unsupported chain")) {
321
+ // Handle unsupported chain
322
+ } else if (error.message.includes("User rejected")) {
323
+ // Handle user rejection
324
+ }
325
+ }
326
+ ```
327
+
328
+ ## Requirements
329
+
330
+ - Node.js 18+
331
+ - `@aptos-labs/ts-sdk` ^5.1.1 (peer dependency)
332
+ - `@aptos-labs/wallet-adapter-react` - This package is designed to work with the Aptos Wallet Adapter ecosystem, specifically using the derived wallet adapters (`@aptos-labs/derived-wallet-solana`, `@aptos-labs/derived-wallet-ethereum`, `@aptos-labs/derived-wallet-sui`) that are part of this project
package/dist/index.js CHANGED
@@ -96,7 +96,6 @@ var AptosLocalSigner = class {
96
96
  claimedTransactionHashes() {
97
97
  return this._claimedTransactionHashes;
98
98
  }
99
- /* other methods... */
100
99
  async signAndSend(txs) {
101
100
  const txHashes = [];
102
101
  for (const tx of txs) {
@@ -941,7 +940,7 @@ var testnetChains = {
941
940
  explorerName: "Etherscan",
942
941
  icon: "Ethereum",
943
942
  symbol: "ETH",
944
- defaultRpc: "https://eth-sepolia.public.blastapi.io"
943
+ defaultRpc: "https://ethereum-sepolia-rpc.publicnode.com"
945
944
  },
946
945
  BaseSepolia: {
947
946
  key: "BaseSepolia",
@@ -1278,8 +1277,12 @@ var getSolanaWalletUSDCBalance = async (walletAddress, aptosNetwork, rpc) => {
1278
1277
  const splToken = await connection.getTokenAccountsByOwner(address, {
1279
1278
  mint: new import_web33.PublicKey(tokenAddress)
1280
1279
  });
1281
- const checkAddress = splToken.value.length > 0 ? splToken.value[0].pubkey : address;
1282
- const balance = await connection.getTokenAccountBalance(checkAddress);
1280
+ if (splToken.value.length === 0) {
1281
+ return "0";
1282
+ }
1283
+ const balance = await connection.getTokenAccountBalance(
1284
+ splToken.value[0].pubkey
1285
+ );
1283
1286
  return balance.value.uiAmountString ?? import_ethers2.ethers.formatUnits(BigInt(balance.value.amount), balance.value.decimals);
1284
1287
  };
1285
1288
  var getEthereumWalletUSDCBalance = async (walletAddress, aptosNetwork, chain, rpc) => {