| 1 | ## cw_solana |
| 2 | |
| 3 | Solana wallet module for Cake Wallet. Provides native SOL and SPL token support built on `on_chain/solana` with high-throughput RPC usage and safe transaction parsing. |
| 4 | |
| 5 | ### Features |
| 6 | |
| 7 | - Connect to Solana RPC (Ankr/Chainstack/custom) via `SolanaRPC` over HTTP. |
| 8 | - Fetch SOL balances and aggregate SPL token balances across accounts. |
| 9 | - Parse and stream native and SPL token transactions (filters ATA-only and spam-like micro txs). |
| 10 | - Estimate fees per compiled message and enforce rent-exemption checks. |
| 11 | - Create/sign/broadcast SOL and SPL transfers; auto-create recipient ATA when necessary. |
| 12 | - Manage SPL tokens; fetch on-chain metadata (symbol/name) for unknown mints. |
| 13 | - Sign and verify messages. |
| 14 | - Node health checks for SOL and a known SPL token (USDC). |
| 15 | |
| 16 | ### Getting started |
| 17 | |
| 18 | If you use hosted RPC providers, add a secrets file for keys (optional unless using those hosts): |
| 19 | |
| 20 | ```dart |
| 21 | // cw_solana/lib/.secrets.g.dart (DO NOT COMMIT) |
| 22 | const String ankrApiKey = 'YOUR_ANKR_KEY'; |
| 23 | const String chainStackApiKey = 'YOUR_CHAINSTACK_KEY'; |
| 24 | ``` |
| 25 | |
| 26 | Connect and sync: |
| 27 | |
| 28 | ```dart |
| 29 | final service = SolanaWalletService(walletInfoBox, true); |
| 30 | final wallet = await service.create(SolanaNewWalletCredentials(name: 'My SOL', password: 'secret')); |
| 31 | await wallet.connectToNode(node: Node(uriRaw: 'api.mainnet-beta.solana.com', isSSL: true)); |
| 32 | await wallet.startSync(); |
| 33 | final sol = wallet.balance[CryptoCurrency.sol]?.balance; |
| 34 | ``` |
| 35 | |
| 36 | ### Usage |
| 37 | |
| 38 | Send SOL: |
| 39 | |
| 40 | ```dart |
| 41 | final pending = await wallet.createTransaction( |
| 42 | SolanaTransactionCredentials.single( |
| 43 | address: 'SoL...', |
| 44 | cryptoAmount: '0.05', |
| 45 | currency: CryptoCurrency.sol, |
| 46 | ), |
| 47 | ); |
| 48 | final sig = await pending.commit(); |
| 49 | ``` |
| 50 | |
| 51 | Add an SPL token by mint: |
| 52 | |
| 53 | ```dart |
| 54 | final token = await wallet.getSPLToken('EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v'); // USDC |
| 55 | if (token != null) { |
| 56 | await wallet.addSPLToken(token); |
| 57 | } |
| 58 | ``` |
| 59 | |
| 60 | ### Additional information |
| 61 | |
| 62 | - When using `rpc.ankr.com` or `solana-mainnet.core.chainstack.com`, the client reads API keys from `.secrets.g.dart`. |
| 63 | - See `lib/` for APIs: `SolanaWalletClient`, `SolanaWallet`, `SolanaWalletService`, and credential types. |