@sanketsaagar/polygon-kit 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Polygon
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,522 @@
1
+ # PolygonKit
2
+
3
+ > React components and TypeScript utilities for building full-fledged onchain apps on Polygon
4
+
5
+ PolygonKit is a comprehensive React library inspired by OnchainKit, specifically designed for building web3 applications on Polygon. It provides battle-tested components, hooks, and utilities to create seamless onchain experiences.
6
+
7
+ ## Features
8
+
9
+ - **Wallet Management** - Easy wallet connection and account management
10
+ - **Identity Components** - Display user profiles with ENS support
11
+ - **Transaction Handling** - Simplified transaction building and tracking
12
+ - **Token Operations** - Token display, balance tracking, and swaps
13
+ - **TypeScript First** - Full type safety with TypeScript
14
+ - **Customizable** - Fully customizable components with TailwindCSS
15
+ - **Multi-Chain** - Support for Polygon PoS, zkEVM, and testnets
16
+
17
+ ## Installation
18
+
19
+ ### Install from GitHub
20
+
21
+ ```bash
22
+ npm install github:sanketsaagar/polygonKit wagmi viem @tanstack/react-query
23
+ ```
24
+
25
+ or
26
+
27
+ ```bash
28
+ yarn add github:sanketsaagar/polygonKit wagmi viem @tanstack/react-query
29
+ ```
30
+
31
+ or
32
+
33
+ ```bash
34
+ pnpm add github:sanketsaagar/polygonKit wagmi viem @tanstack/react-query
35
+ ```
36
+
37
+ ### Install from NPM (Coming Soon)
38
+
39
+ ```bash
40
+ npm install @polygon/polygon-kit wagmi viem @tanstack/react-query
41
+ ```
42
+
43
+ ## Quick Start
44
+
45
+ ### 1. Wrap your app with PolygonKitProvider
46
+
47
+ ```tsx
48
+ import { PolygonKitProvider } from '@polygon/polygon-kit';
49
+
50
+ function App() {
51
+ return (
52
+ <PolygonKitProvider>
53
+ <YourApp />
54
+ </PolygonKitProvider>
55
+ );
56
+ }
57
+ ```
58
+
59
+ ### 2. Use Wallet Components
60
+
61
+ ```tsx
62
+ import { Wallet, ConnectWallet, WalletDropdown } from '@polygon/polygon-kit';
63
+
64
+ function Header() {
65
+ return (
66
+ <Wallet>
67
+ <ConnectWallet />
68
+ <WalletDropdown />
69
+ </Wallet>
70
+ );
71
+ }
72
+ ```
73
+
74
+ ### 3. Display User Identity
75
+
76
+ ```tsx
77
+ import { Identity } from '@polygon/polygon-kit';
78
+
79
+ function UserProfile({ address }) {
80
+ return (
81
+ <Identity
82
+ address={address}
83
+ showAvatar
84
+ showAddress
85
+ showBalance
86
+ />
87
+ );
88
+ }
89
+ ```
90
+
91
+ ## Component API
92
+
93
+ ### Wallet Components
94
+
95
+ #### `<ConnectWallet />`
96
+
97
+ Button component for connecting/disconnecting wallet.
98
+
99
+ ```tsx
100
+ <ConnectWallet
101
+ onConnect={(address) => console.log('Connected:', address)}
102
+ onDisconnect={() => console.log('Disconnected')}
103
+ className="custom-class"
104
+ />
105
+ ```
106
+
107
+ **Props:**
108
+ - `onConnect?: (address: Address) => void` - Callback when wallet connects
109
+ - `onDisconnect?: () => void` - Callback when wallet disconnects
110
+ - `className?: string` - Custom CSS classes
111
+ - `children?: ReactNode` - Custom render content
112
+
113
+ #### `<WalletDropdown />`
114
+
115
+ Dropdown menu showing account details and network switching.
116
+
117
+ ```tsx
118
+ <WalletDropdown className="custom-class" />
119
+ ```
120
+
121
+ **Props:**
122
+ - `className?: string` - Custom CSS classes
123
+ - `children?: ReactNode` - Custom trigger content
124
+
125
+ ### Identity Components
126
+
127
+ #### `<Identity />`
128
+
129
+ Display user identity with avatar, name, and balance.
130
+
131
+ ```tsx
132
+ <Identity
133
+ address="0x..."
134
+ showAvatar={true}
135
+ showAddress={true}
136
+ showBalance={true}
137
+ className="custom-class"
138
+ />
139
+ ```
140
+
141
+ **Props:**
142
+ - `address: Address` - User wallet address (required)
143
+ - `showAvatar?: boolean` - Show avatar (default: true)
144
+ - `showAddress?: boolean` - Show address/ENS (default: true)
145
+ - `showBalance?: boolean` - Show balance (default: false)
146
+ - `className?: string` - Custom CSS classes
147
+
148
+ #### `<Avatar />`
149
+
150
+ Display user avatar with gradient colors.
151
+
152
+ ```tsx
153
+ <Avatar address="0x..." size={40} />
154
+ ```
155
+
156
+ **Props:**
157
+ - `address: Address` - Wallet address (required)
158
+ - `size?: number` - Avatar size in pixels (default: 40)
159
+ - `className?: string` - Custom CSS classes
160
+
161
+ #### `<Name />`
162
+
163
+ Display ENS name or shortened address.
164
+
165
+ ```tsx
166
+ <Name address="0x..." />
167
+ ```
168
+
169
+ **Props:**
170
+ - `address: Address` - Wallet address (required)
171
+ - `className?: string` - Custom CSS classes
172
+
173
+ ### Transaction Components
174
+
175
+ #### `<Transaction />`
176
+
177
+ Wrapper for transaction-related components.
178
+
179
+ ```tsx
180
+ <Transaction chainId={137}>
181
+ <TransactionButton
182
+ text="Send Transaction"
183
+ calls={[{ to: '0x...', value: BigInt(1000) }]}
184
+ onSuccess={(hash) => console.log('Success:', hash)}
185
+ onError={(error) => console.log('Error:', error)}
186
+ />
187
+ </Transaction>
188
+ ```
189
+
190
+ **Props:**
191
+ - `chainId?: number` - Required chain ID
192
+ - `className?: string` - Custom CSS classes
193
+ - `children?: ReactNode` - Child components
194
+
195
+ #### `<TransactionButton />`
196
+
197
+ Button to execute transactions.
198
+
199
+ ```tsx
200
+ <TransactionButton
201
+ text="Send MATIC"
202
+ calls={[
203
+ {
204
+ to: '0x...',
205
+ value: BigInt(1000000000000000000), // 1 MATIC
206
+ data: '0x...'
207
+ }
208
+ ]}
209
+ onSuccess={(hash) => console.log('Transaction:', hash)}
210
+ onError={(error) => console.error(error)}
211
+ />
212
+ ```
213
+
214
+ **Props:**
215
+ - `text?: string` - Button text (default: 'Send Transaction')
216
+ - `calls?: TransactionCall[]` - Array of transaction calls
217
+ - `onSuccess?: (hash: string) => void` - Success callback
218
+ - `onError?: (error: Error) => void` - Error callback
219
+ - `disabled?: boolean` - Disable button
220
+ - `className?: string` - Custom CSS classes
221
+
222
+ #### `<TransactionStatus />`
223
+
224
+ Display transaction confirmation status.
225
+
226
+ ```tsx
227
+ <TransactionStatus hash="0x..." />
228
+ ```
229
+
230
+ **Props:**
231
+ - `hash: Hash` - Transaction hash (required)
232
+ - `className?: string` - Custom CSS classes
233
+
234
+ ### Token Components
235
+
236
+ #### `<Token />`
237
+
238
+ Display token information.
239
+
240
+ ```tsx
241
+ <Token
242
+ address="0x..."
243
+ symbol="MATIC"
244
+ amount="1.5"
245
+ className="custom-class"
246
+ />
247
+ ```
248
+
249
+ **Props:**
250
+ - `address: Address` - Token address (required)
251
+ - `symbol?: string` - Token symbol
252
+ - `amount?: string` - Token amount
253
+ - `className?: string` - Custom CSS classes
254
+
255
+ #### `<TokenBalance />`
256
+
257
+ Display token balance for an address.
258
+
259
+ ```tsx
260
+ <TokenBalance
261
+ address="0x..."
262
+ token="0x..." // Optional: ERC20 token address
263
+ />
264
+ ```
265
+
266
+ **Props:**
267
+ - `address: Address` - Wallet address (required)
268
+ - `token?: Address` - Token contract address (optional, shows native if not provided)
269
+ - `className?: string` - Custom CSS classes
270
+
271
+ ### Swap Component
272
+
273
+ #### `<Swap />`
274
+
275
+ Token swap interface (integrate with your DEX aggregator).
276
+
277
+ ```tsx
278
+ <Swap
279
+ onSuccess={(hash) => console.log('Swap success:', hash)}
280
+ onError={(error) => console.error('Swap error:', error)}
281
+ className="custom-class"
282
+ />
283
+ ```
284
+
285
+ **Props:**
286
+ - `onSuccess?: (hash: string) => void` - Success callback
287
+ - `onError?: (error: Error) => void` - Error callback
288
+ - `className?: string` - Custom CSS classes
289
+
290
+ ## Hooks
291
+
292
+ ### `usePolygonKit()`
293
+
294
+ Main hook for wallet interactions.
295
+
296
+ ```tsx
297
+ import { usePolygonKit } from '@polygon/polygon-kit';
298
+
299
+ function Component() {
300
+ const { address, isConnected, chain, balance, connect, disconnect } = usePolygonKit();
301
+
302
+ return (
303
+ <div>
304
+ {isConnected ? (
305
+ <button onClick={disconnect}>Disconnect</button>
306
+ ) : (
307
+ <button onClick={connect}>Connect</button>
308
+ )}
309
+ </div>
310
+ );
311
+ }
312
+ ```
313
+
314
+ **Returns:**
315
+ - `address?: Address` - Connected wallet address
316
+ - `isConnected: boolean` - Connection status
317
+ - `chain?: Chain` - Current chain
318
+ - `balance?: { value: bigint, symbol: string }` - Native token balance
319
+ - `connect: () => void` - Connect wallet function
320
+ - `disconnect: () => void` - Disconnect wallet function
321
+ - `connectors: Connector[]` - Available connectors
322
+
323
+ ### `usePolygonBalance()`
324
+
325
+ Get token balance with formatting.
326
+
327
+ ```tsx
328
+ import { usePolygonBalance } from '@polygon/polygon-kit';
329
+
330
+ function Component({ address }) {
331
+ const { balance, formatted, symbol, isLoading } = usePolygonBalance(address);
332
+
333
+ if (isLoading) return <div>Loading...</div>;
334
+
335
+ return <div>{formatted} {symbol}</div>;
336
+ }
337
+ ```
338
+
339
+ **Parameters:**
340
+ - `address?: Address` - Wallet address
341
+ - `token?: Address` - Token contract address (optional)
342
+
343
+ **Returns:**
344
+ - `balance?: bigint` - Raw balance
345
+ - `formatted: string` - Formatted balance
346
+ - `symbol?: string` - Token symbol
347
+ - `decimals?: number` - Token decimals
348
+ - `isLoading: boolean` - Loading state
349
+ - `isError: boolean` - Error state
350
+ - `refetch: () => void` - Refetch function
351
+
352
+ ### `usePolygonTransaction()`
353
+
354
+ Send transactions with status tracking.
355
+
356
+ ```tsx
357
+ import { usePolygonTransaction } from '@polygon/polygon-kit';
358
+
359
+ function Component() {
360
+ const { send, hash, isPending, isConfirming, isSuccess } = usePolygonTransaction();
361
+
362
+ const handleSend = () => {
363
+ send('0x...', BigInt(1000000000000000000)); // Send 1 MATIC
364
+ };
365
+
366
+ return (
367
+ <div>
368
+ <button onClick={handleSend} disabled={isPending}>
369
+ Send
370
+ </button>
371
+ {isConfirming && <div>Confirming...</div>}
372
+ {isSuccess && <div>Success! Hash: {hash}</div>}
373
+ </div>
374
+ );
375
+ }
376
+ ```
377
+
378
+ **Returns:**
379
+ - `send: (to: Address, value?: bigint, data?: Hex) => void` - Send transaction
380
+ - `hash?: Hash` - Transaction hash
381
+ - `isPending: boolean` - Transaction pending
382
+ - `isConfirming: boolean` - Waiting for confirmation
383
+ - `isSuccess: boolean` - Transaction succeeded
384
+ - `isError: boolean` - Transaction failed
385
+ - `error?: Error` - Error details
386
+
387
+ ## Utilities
388
+
389
+ ### Format Utilities
390
+
391
+ ```tsx
392
+ import {
393
+ shortenAddress,
394
+ formatBalance,
395
+ parseTokenAmount
396
+ } from '@polygon/polygon-kit';
397
+
398
+ // Shorten address: 0x1234...5678
399
+ const short = shortenAddress('0x1234567890123456789012345678901234567890');
400
+
401
+ // Format balance: 1.5000
402
+ const formatted = formatBalance(BigInt('1500000000000000000'), 18, 4);
403
+
404
+ // Parse amount: 1500000000000000000n
405
+ const parsed = parseTokenAmount('1.5', 18);
406
+ ```
407
+
408
+ ## Chain Constants
409
+
410
+ ```tsx
411
+ import { polygon, polygonAmoy, polygonZkEVM } from '@polygon/polygon-kit';
412
+
413
+ console.log(polygon.id); // 137
414
+ console.log(polygonAmoy.id); // 80002
415
+ console.log(polygonZkEVM.id); // 1101
416
+ ```
417
+
418
+ ## Styling
419
+
420
+ PolygonKit components use TailwindCSS classes by default. You can:
421
+
422
+ 1. **Override with className**: Pass custom classes to any component
423
+ 2. **Use custom children**: Provide your own UI as children
424
+ 3. **Theme support**: Components respect dark mode via `dark:` classes
425
+
426
+ ## Example: Complete App
427
+
428
+ ```tsx
429
+ import {
430
+ PolygonKitProvider,
431
+ Wallet,
432
+ ConnectWallet,
433
+ WalletDropdown,
434
+ Identity,
435
+ TransactionButton,
436
+ Swap,
437
+ usePolygonKit,
438
+ } from '@polygon/polygon-kit';
439
+
440
+ function Dashboard() {
441
+ const { address, isConnected } = usePolygonKit();
442
+
443
+ if (!isConnected) {
444
+ return (
445
+ <div className="flex items-center justify-center min-h-screen">
446
+ <ConnectWallet />
447
+ </div>
448
+ );
449
+ }
450
+
451
+ return (
452
+ <div className="container mx-auto p-4">
453
+ <header className="flex justify-between items-center mb-8">
454
+ <h1 className="text-2xl font-bold">My Polygon App</h1>
455
+ <Wallet>
456
+ <WalletDropdown />
457
+ </Wallet>
458
+ </header>
459
+
460
+ <div className="grid grid-cols-1 md:grid-cols-2 gap-6">
461
+ <div className="bg-white dark:bg-gray-900 p-6 rounded-xl">
462
+ <Identity
463
+ address={address!}
464
+ showAvatar
465
+ showAddress
466
+ showBalance
467
+ />
468
+ </div>
469
+
470
+ <Swap />
471
+ </div>
472
+ </div>
473
+ );
474
+ }
475
+
476
+ function App() {
477
+ return (
478
+ <PolygonKitProvider>
479
+ <Dashboard />
480
+ </PolygonKitProvider>
481
+ );
482
+ }
483
+
484
+ export default App;
485
+ ```
486
+
487
+ ## Development
488
+
489
+ ```bash
490
+ # Install dependencies
491
+ pnpm install
492
+
493
+ # Build the library
494
+ pnpm build
495
+
496
+ # Watch mode for development
497
+ pnpm dev
498
+
499
+ # Type check
500
+ pnpm type-check
501
+
502
+ # Lint
503
+ pnpm lint
504
+ ```
505
+
506
+ ## Contributing
507
+
508
+ Contributions are welcome! Please read our contributing guidelines and submit pull requests.
509
+
510
+ ## License
511
+
512
+ MIT
513
+
514
+ ## Support
515
+
516
+ - [Documentation](https://polygon-kit.docs.polygon.technology)
517
+ - [GitHub Issues](https://github.com/polygon/polygon-kit/issues)
518
+ - [Discord Community](https://discord.gg/polygon)
519
+
520
+ ---
521
+
522
+ Built with ❤️ for the Polygon ecosystem