hiero-react 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,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,171 @@
1
+ # Hiero React Utilities (HRU)
2
+
3
+ React hooks and utilities for easy integration with Hiero/Hedera networks.
4
+
5
+ [![CI](https://github.com/imatthew55/hiero-bounty/actions/workflows/ci.yml/badge.svg)](https://github.com/imatthew55/hiero-bounty/actions/workflows/ci.yml)
6
+ [![License: Apache-2.0](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
7
+
8
+ ## Features
9
+
10
+ - **React Hooks** - Simple hooks for accounts, transactions, tokens, and NFTs
11
+ - **TypeScript** - Full type safety and autocompletion
12
+ - **Mirror Node** - Query Hiero data without running a node
13
+ - **Lightweight** - Minimal dependencies
14
+
15
+ ## Installation
16
+ ```bash
17
+ npm install hiero-react
18
+ ```
19
+
20
+ ## Quick Start
21
+
22
+ ### 1. Wrap your app with HieroProvider
23
+ ```tsx
24
+ import { HieroProvider } from 'hiero-react';
25
+
26
+ function App() {
27
+ return (
28
+ <HieroProvider network="testnet">
29
+ <YourApp />
30
+ </HieroProvider>
31
+ );
32
+ }
33
+ ```
34
+
35
+ ### 2. Use hooks in your components
36
+ ```tsx
37
+ import { useAccount } from 'hiero-react';
38
+
39
+ function Wallet({ accountId }: { accountId: string }) {
40
+ const { balance, loading, error } = useAccount(accountId);
41
+
42
+ if (loading) return <p>Loading...</p>;
43
+ if (error) return <p>Error: {error.message}</p>;
44
+
45
+ return <p>Balance: {balance} HBAR</p>;
46
+ }
47
+ ```
48
+
49
+ ## Available Hooks
50
+
51
+ ### useAccount(accountId)
52
+
53
+ Fetch account balance and info.
54
+ ```tsx
55
+ const { accountId, balance, loading, error } = useAccount('0.0.1234');
56
+ ```
57
+
58
+ ### useTransactions(accountId, limit?)
59
+
60
+ Fetch recent transactions for an account.
61
+ ```tsx
62
+ const { transactions, loading, error, refetch } = useTransactions('0.0.1234', 20);
63
+ ```
64
+
65
+ ### useTokens(accountId)
66
+
67
+ Fetch fungible tokens owned by an account.
68
+ ```tsx
69
+ const { tokens, loading, error } = useTokens('0.0.1234');
70
+ ```
71
+
72
+ ### useNFTs(accountId)
73
+
74
+ Fetch NFTs owned by an account.
75
+ ```tsx
76
+ const { nfts, loading, error } = useNFTs('0.0.1234');
77
+ ```
78
+
79
+ ### useHiero()
80
+
81
+ Access the Hiero context directly.
82
+ ```tsx
83
+ const { network, mirrorNodeUrl } = useHiero();
84
+ ```
85
+
86
+ ## Configuration
87
+
88
+ ### Networks
89
+ ```tsx
90
+ <HieroProvider network="testnet"> // Testnet (development)
91
+ <HieroProvider network="mainnet"> // Mainnet (production)
92
+ <HieroProvider network="previewnet"> // Previewnet (experimental)
93
+ ```
94
+
95
+ ### Custom Mirror Node
96
+ ```tsx
97
+ <HieroProvider network="mainnet" mirrorNodeUrl="https://your-mirror-node.com">
98
+ ```
99
+
100
+ ## Example App
101
+ ```tsx
102
+ import React from 'react';
103
+ import { HieroProvider, useAccount, useTransactions } from 'hiero-react';
104
+
105
+ function WalletDashboard() {
106
+ const accountId = '0.0.1234';
107
+ const { balance, loading: balanceLoading } = useAccount(accountId);
108
+ const { transactions, loading: txLoading } = useTransactions(accountId, 5);
109
+
110
+ return (
111
+ <div>
112
+ <h1>My Wallet</h1>
113
+
114
+ <section>
115
+ <h2>Balance</h2>
116
+ {balanceLoading ? <p>Loading...</p> : <p>{balance} HBAR</p>}
117
+ </section>
118
+
119
+ <section>
120
+ <h2>Recent Transactions</h2>
121
+ {txLoading ? (
122
+ <p>Loading...</p>
123
+ ) : (
124
+ <ul>
125
+ {transactions.map((tx) => (
126
+ <li key={tx.transactionId}>
127
+ {tx.type} - {tx.result}
128
+ </li>
129
+ ))}
130
+ </ul>
131
+ )}
132
+ </section>
133
+ </div>
134
+ );
135
+ }
136
+
137
+ export default function App() {
138
+ return (
139
+ <HieroProvider network="testnet">
140
+ <WalletDashboard />
141
+ </HieroProvider>
142
+ );
143
+ }
144
+ ```
145
+
146
+ ## Development
147
+ ```bash
148
+ # Install dependencies
149
+ npm install
150
+
151
+ # Run tests
152
+ npm test
153
+
154
+ # Build the library
155
+ npm run build
156
+
157
+ # Lint code
158
+ npm run lint
159
+ ```
160
+
161
+ ## Contributing
162
+
163
+ See [CONTRIBUTING.md](./CONTRIBUTING.md) for guidelines.
164
+
165
+ All commits must be:
166
+ - GPG signed
167
+ - Include DCO sign-off (`git commit -s`)
168
+
169
+ ## License
170
+
171
+ Apache-2.0 - see [LICENSE](./LICENSE) for details.
package/dist/index.cjs ADDED
@@ -0,0 +1,215 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ HieroContext: () => HieroContext,
24
+ HieroProvider: () => HieroProvider,
25
+ useAccount: () => useAccount,
26
+ useHiero: () => useHiero,
27
+ useNFTs: () => useNFTs,
28
+ useTokens: () => useTokens,
29
+ useTransactions: () => useTransactions
30
+ });
31
+ module.exports = __toCommonJS(index_exports);
32
+
33
+ // src/providers/HieroProvider.tsx
34
+ var import_react = require("react");
35
+ var import_jsx_runtime = require("react/jsx-runtime");
36
+ var HieroContext = (0, import_react.createContext)(null);
37
+ function HieroProvider({ children, network, mirrorNodeUrl }) {
38
+ const defaultMirrorUrl = `https://${network}.mirrornode.hedera.com`;
39
+ const value = {
40
+ network,
41
+ mirrorNodeUrl: mirrorNodeUrl || defaultMirrorUrl
42
+ };
43
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(HieroContext.Provider, { value, children });
44
+ }
45
+
46
+ // src/hooks/useHiero.ts
47
+ var import_react2 = require("react");
48
+ function useHiero() {
49
+ const context = (0, import_react2.useContext)(HieroContext);
50
+ if (!context) {
51
+ throw new Error("useHiero must be used within a HieroProvider");
52
+ }
53
+ return context;
54
+ }
55
+
56
+ // src/hooks/useAccount.ts
57
+ var import_react3 = require("react");
58
+ function useAccount(accountId) {
59
+ const { mirrorNodeUrl } = useHiero();
60
+ const [balance, setBalance] = (0, import_react3.useState)(0);
61
+ const [loading, setLoading] = (0, import_react3.useState)(true);
62
+ const [error, setError] = (0, import_react3.useState)(null);
63
+ (0, import_react3.useEffect)(() => {
64
+ async function fetchAccount() {
65
+ try {
66
+ setLoading(true);
67
+ const response = await fetch(`${mirrorNodeUrl}/api/v1/accounts/${accountId}`);
68
+ if (!response.ok) {
69
+ throw new Error(`Failed to fetch account: ${response.statusText}`);
70
+ }
71
+ const data = await response.json();
72
+ setBalance(data.balance.balance / 1e8);
73
+ setError(null);
74
+ } catch (err) {
75
+ setError(err instanceof Error ? err : new Error("Unknown error"));
76
+ } finally {
77
+ setLoading(false);
78
+ }
79
+ }
80
+ if (accountId) {
81
+ fetchAccount();
82
+ }
83
+ }, [accountId, mirrorNodeUrl]);
84
+ return { accountId, balance, loading, error };
85
+ }
86
+
87
+ // src/hooks/useTransactions.ts
88
+ var import_react4 = require("react");
89
+ function useTransactions(accountId, limit = 10) {
90
+ const { mirrorNodeUrl } = useHiero();
91
+ const [transactions, setTransactions] = (0, import_react4.useState)([]);
92
+ const [loading, setLoading] = (0, import_react4.useState)(true);
93
+ const [error, setError] = (0, import_react4.useState)(null);
94
+ const fetchTransactions = async () => {
95
+ try {
96
+ setLoading(true);
97
+ const response = await fetch(
98
+ `${mirrorNodeUrl}/api/v1/transactions?account.id=${accountId}&limit=${limit}`
99
+ );
100
+ if (!response.ok) {
101
+ throw new Error(`Failed to fetch transactions: ${response.statusText}`);
102
+ }
103
+ const data = await response.json();
104
+ const formatted = data.transactions.map((tx) => ({
105
+ transactionId: tx.transaction_id,
106
+ type: tx.name,
107
+ result: tx.result,
108
+ consensusTimestamp: tx.consensus_timestamp,
109
+ transfers: tx.transfers?.map((t) => ({
110
+ account: t.account,
111
+ amount: t.amount / 1e8
112
+ })) || []
113
+ }));
114
+ setTransactions(formatted);
115
+ setError(null);
116
+ } catch (err) {
117
+ setError(err instanceof Error ? err : new Error("Unknown error"));
118
+ } finally {
119
+ setLoading(false);
120
+ }
121
+ };
122
+ (0, import_react4.useEffect)(() => {
123
+ if (accountId) {
124
+ fetchTransactions();
125
+ }
126
+ }, [accountId, limit, mirrorNodeUrl]);
127
+ return { transactions, loading, error, refetch: fetchTransactions };
128
+ }
129
+
130
+ // src/hooks/useTokens.ts
131
+ var import_react5 = require("react");
132
+ function useTokens(accountId) {
133
+ const { mirrorNodeUrl } = useHiero();
134
+ const [tokens, setTokens] = (0, import_react5.useState)([]);
135
+ const [loading, setLoading] = (0, import_react5.useState)(true);
136
+ const [error, setError] = (0, import_react5.useState)(null);
137
+ (0, import_react5.useEffect)(() => {
138
+ async function fetchTokens() {
139
+ try {
140
+ setLoading(true);
141
+ const response = await fetch(
142
+ `${mirrorNodeUrl}/api/v1/accounts/${accountId}/tokens`
143
+ );
144
+ if (!response.ok) {
145
+ throw new Error(`Failed to fetch tokens: ${response.statusText}`);
146
+ }
147
+ const data = await response.json();
148
+ const formatted = data.tokens.map((t) => ({
149
+ tokenId: t.token_id,
150
+ symbol: t.symbol || "Unknown",
151
+ balance: t.balance,
152
+ decimals: t.decimals || 0
153
+ }));
154
+ setTokens(formatted);
155
+ setError(null);
156
+ } catch (err) {
157
+ setError(err instanceof Error ? err : new Error("Unknown error"));
158
+ } finally {
159
+ setLoading(false);
160
+ }
161
+ }
162
+ if (accountId) {
163
+ fetchTokens();
164
+ }
165
+ }, [accountId, mirrorNodeUrl]);
166
+ return { tokens, loading, error };
167
+ }
168
+
169
+ // src/hooks/useNFTs.ts
170
+ var import_react6 = require("react");
171
+ function useNFTs(accountId) {
172
+ const { mirrorNodeUrl } = useHiero();
173
+ const [nfts, setNfts] = (0, import_react6.useState)([]);
174
+ const [loading, setLoading] = (0, import_react6.useState)(true);
175
+ const [error, setError] = (0, import_react6.useState)(null);
176
+ (0, import_react6.useEffect)(() => {
177
+ async function fetchNFTs() {
178
+ try {
179
+ setLoading(true);
180
+ const response = await fetch(
181
+ `${mirrorNodeUrl}/api/v1/accounts/${accountId}/nfts`
182
+ );
183
+ if (!response.ok) {
184
+ throw new Error(`Failed to fetch NFTs: ${response.statusText}`);
185
+ }
186
+ const data = await response.json();
187
+ const formatted = data.nfts.map((nft) => ({
188
+ tokenId: nft.token_id,
189
+ serialNumber: nft.serial_number,
190
+ metadata: nft.metadata ? atob(nft.metadata) : ""
191
+ }));
192
+ setNfts(formatted);
193
+ setError(null);
194
+ } catch (err) {
195
+ setError(err instanceof Error ? err : new Error("Unknown error"));
196
+ } finally {
197
+ setLoading(false);
198
+ }
199
+ }
200
+ if (accountId) {
201
+ fetchNFTs();
202
+ }
203
+ }, [accountId, mirrorNodeUrl]);
204
+ return { nfts, loading, error };
205
+ }
206
+ // Annotate the CommonJS export names for ESM import in node:
207
+ 0 && (module.exports = {
208
+ HieroContext,
209
+ HieroProvider,
210
+ useAccount,
211
+ useHiero,
212
+ useNFTs,
213
+ useTokens,
214
+ useTransactions
215
+ });
@@ -0,0 +1,75 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import React, { ReactNode } from 'react';
3
+
4
+ type NetworkType = 'mainnet' | 'testnet' | 'previewnet';
5
+ interface AccountInfo {
6
+ accountId: string;
7
+ balance: number;
8
+ loading: boolean;
9
+ error: Error | null;
10
+ }
11
+
12
+ interface HieroConfig {
13
+ network: NetworkType;
14
+ mirrorNodeUrl?: string;
15
+ }
16
+ interface HieroProviderProps {
17
+ children: ReactNode;
18
+ network: NetworkType;
19
+ mirrorNodeUrl?: string;
20
+ }
21
+ interface HieroContextValue {
22
+ network: NetworkType;
23
+ mirrorNodeUrl: string;
24
+ }
25
+ declare const HieroContext: React.Context<HieroContextValue | null>;
26
+ declare function HieroProvider({ children, network, mirrorNodeUrl }: HieroProviderProps): react_jsx_runtime.JSX.Element;
27
+
28
+ declare function useHiero(): HieroContextValue;
29
+
30
+ declare function useAccount(accountId: string): AccountInfo;
31
+
32
+ interface Transaction {
33
+ transactionId: string;
34
+ type: string;
35
+ result: string;
36
+ consensusTimestamp: string;
37
+ transfers: Array<{
38
+ account: string;
39
+ amount: number;
40
+ }>;
41
+ }
42
+ interface UseTransactionsResult {
43
+ transactions: Transaction[];
44
+ loading: boolean;
45
+ error: Error | null;
46
+ refetch: () => void;
47
+ }
48
+ declare function useTransactions(accountId: string, limit?: number): UseTransactionsResult;
49
+
50
+ interface Token {
51
+ tokenId: string;
52
+ symbol: string;
53
+ balance: number;
54
+ decimals: number;
55
+ }
56
+ interface UseTokensResult {
57
+ tokens: Token[];
58
+ loading: boolean;
59
+ error: Error | null;
60
+ }
61
+ declare function useTokens(accountId: string): UseTokensResult;
62
+
63
+ interface NFT {
64
+ tokenId: string;
65
+ serialNumber: number;
66
+ metadata: string;
67
+ }
68
+ interface UseNFTsResult {
69
+ nfts: NFT[];
70
+ loading: boolean;
71
+ error: Error | null;
72
+ }
73
+ declare function useNFTs(accountId: string): UseNFTsResult;
74
+
75
+ export { type AccountInfo, type HieroConfig, HieroContext, type HieroContextValue, HieroProvider, type HieroProviderProps, type NFT, type NetworkType, type Token, type Transaction, type UseNFTsResult, type UseTokensResult, type UseTransactionsResult, useAccount, useHiero, useNFTs, useTokens, useTransactions };
@@ -0,0 +1,75 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import React, { ReactNode } from 'react';
3
+
4
+ type NetworkType = 'mainnet' | 'testnet' | 'previewnet';
5
+ interface AccountInfo {
6
+ accountId: string;
7
+ balance: number;
8
+ loading: boolean;
9
+ error: Error | null;
10
+ }
11
+
12
+ interface HieroConfig {
13
+ network: NetworkType;
14
+ mirrorNodeUrl?: string;
15
+ }
16
+ interface HieroProviderProps {
17
+ children: ReactNode;
18
+ network: NetworkType;
19
+ mirrorNodeUrl?: string;
20
+ }
21
+ interface HieroContextValue {
22
+ network: NetworkType;
23
+ mirrorNodeUrl: string;
24
+ }
25
+ declare const HieroContext: React.Context<HieroContextValue | null>;
26
+ declare function HieroProvider({ children, network, mirrorNodeUrl }: HieroProviderProps): react_jsx_runtime.JSX.Element;
27
+
28
+ declare function useHiero(): HieroContextValue;
29
+
30
+ declare function useAccount(accountId: string): AccountInfo;
31
+
32
+ interface Transaction {
33
+ transactionId: string;
34
+ type: string;
35
+ result: string;
36
+ consensusTimestamp: string;
37
+ transfers: Array<{
38
+ account: string;
39
+ amount: number;
40
+ }>;
41
+ }
42
+ interface UseTransactionsResult {
43
+ transactions: Transaction[];
44
+ loading: boolean;
45
+ error: Error | null;
46
+ refetch: () => void;
47
+ }
48
+ declare function useTransactions(accountId: string, limit?: number): UseTransactionsResult;
49
+
50
+ interface Token {
51
+ tokenId: string;
52
+ symbol: string;
53
+ balance: number;
54
+ decimals: number;
55
+ }
56
+ interface UseTokensResult {
57
+ tokens: Token[];
58
+ loading: boolean;
59
+ error: Error | null;
60
+ }
61
+ declare function useTokens(accountId: string): UseTokensResult;
62
+
63
+ interface NFT {
64
+ tokenId: string;
65
+ serialNumber: number;
66
+ metadata: string;
67
+ }
68
+ interface UseNFTsResult {
69
+ nfts: NFT[];
70
+ loading: boolean;
71
+ error: Error | null;
72
+ }
73
+ declare function useNFTs(accountId: string): UseNFTsResult;
74
+
75
+ export { type AccountInfo, type HieroConfig, HieroContext, type HieroContextValue, HieroProvider, type HieroProviderProps, type NFT, type NetworkType, type Token, type Transaction, type UseNFTsResult, type UseTokensResult, type UseTransactionsResult, useAccount, useHiero, useNFTs, useTokens, useTransactions };
package/dist/index.js ADDED
@@ -0,0 +1,182 @@
1
+ // src/providers/HieroProvider.tsx
2
+ import { createContext } from "react";
3
+ import { jsx } from "react/jsx-runtime";
4
+ var HieroContext = createContext(null);
5
+ function HieroProvider({ children, network, mirrorNodeUrl }) {
6
+ const defaultMirrorUrl = `https://${network}.mirrornode.hedera.com`;
7
+ const value = {
8
+ network,
9
+ mirrorNodeUrl: mirrorNodeUrl || defaultMirrorUrl
10
+ };
11
+ return /* @__PURE__ */ jsx(HieroContext.Provider, { value, children });
12
+ }
13
+
14
+ // src/hooks/useHiero.ts
15
+ import { useContext } from "react";
16
+ function useHiero() {
17
+ const context = useContext(HieroContext);
18
+ if (!context) {
19
+ throw new Error("useHiero must be used within a HieroProvider");
20
+ }
21
+ return context;
22
+ }
23
+
24
+ // src/hooks/useAccount.ts
25
+ import { useState, useEffect } from "react";
26
+ function useAccount(accountId) {
27
+ const { mirrorNodeUrl } = useHiero();
28
+ const [balance, setBalance] = useState(0);
29
+ const [loading, setLoading] = useState(true);
30
+ const [error, setError] = useState(null);
31
+ useEffect(() => {
32
+ async function fetchAccount() {
33
+ try {
34
+ setLoading(true);
35
+ const response = await fetch(`${mirrorNodeUrl}/api/v1/accounts/${accountId}`);
36
+ if (!response.ok) {
37
+ throw new Error(`Failed to fetch account: ${response.statusText}`);
38
+ }
39
+ const data = await response.json();
40
+ setBalance(data.balance.balance / 1e8);
41
+ setError(null);
42
+ } catch (err) {
43
+ setError(err instanceof Error ? err : new Error("Unknown error"));
44
+ } finally {
45
+ setLoading(false);
46
+ }
47
+ }
48
+ if (accountId) {
49
+ fetchAccount();
50
+ }
51
+ }, [accountId, mirrorNodeUrl]);
52
+ return { accountId, balance, loading, error };
53
+ }
54
+
55
+ // src/hooks/useTransactions.ts
56
+ import { useState as useState2, useEffect as useEffect2 } from "react";
57
+ function useTransactions(accountId, limit = 10) {
58
+ const { mirrorNodeUrl } = useHiero();
59
+ const [transactions, setTransactions] = useState2([]);
60
+ const [loading, setLoading] = useState2(true);
61
+ const [error, setError] = useState2(null);
62
+ const fetchTransactions = async () => {
63
+ try {
64
+ setLoading(true);
65
+ const response = await fetch(
66
+ `${mirrorNodeUrl}/api/v1/transactions?account.id=${accountId}&limit=${limit}`
67
+ );
68
+ if (!response.ok) {
69
+ throw new Error(`Failed to fetch transactions: ${response.statusText}`);
70
+ }
71
+ const data = await response.json();
72
+ const formatted = data.transactions.map((tx) => ({
73
+ transactionId: tx.transaction_id,
74
+ type: tx.name,
75
+ result: tx.result,
76
+ consensusTimestamp: tx.consensus_timestamp,
77
+ transfers: tx.transfers?.map((t) => ({
78
+ account: t.account,
79
+ amount: t.amount / 1e8
80
+ })) || []
81
+ }));
82
+ setTransactions(formatted);
83
+ setError(null);
84
+ } catch (err) {
85
+ setError(err instanceof Error ? err : new Error("Unknown error"));
86
+ } finally {
87
+ setLoading(false);
88
+ }
89
+ };
90
+ useEffect2(() => {
91
+ if (accountId) {
92
+ fetchTransactions();
93
+ }
94
+ }, [accountId, limit, mirrorNodeUrl]);
95
+ return { transactions, loading, error, refetch: fetchTransactions };
96
+ }
97
+
98
+ // src/hooks/useTokens.ts
99
+ import { useState as useState3, useEffect as useEffect3 } from "react";
100
+ function useTokens(accountId) {
101
+ const { mirrorNodeUrl } = useHiero();
102
+ const [tokens, setTokens] = useState3([]);
103
+ const [loading, setLoading] = useState3(true);
104
+ const [error, setError] = useState3(null);
105
+ useEffect3(() => {
106
+ async function fetchTokens() {
107
+ try {
108
+ setLoading(true);
109
+ const response = await fetch(
110
+ `${mirrorNodeUrl}/api/v1/accounts/${accountId}/tokens`
111
+ );
112
+ if (!response.ok) {
113
+ throw new Error(`Failed to fetch tokens: ${response.statusText}`);
114
+ }
115
+ const data = await response.json();
116
+ const formatted = data.tokens.map((t) => ({
117
+ tokenId: t.token_id,
118
+ symbol: t.symbol || "Unknown",
119
+ balance: t.balance,
120
+ decimals: t.decimals || 0
121
+ }));
122
+ setTokens(formatted);
123
+ setError(null);
124
+ } catch (err) {
125
+ setError(err instanceof Error ? err : new Error("Unknown error"));
126
+ } finally {
127
+ setLoading(false);
128
+ }
129
+ }
130
+ if (accountId) {
131
+ fetchTokens();
132
+ }
133
+ }, [accountId, mirrorNodeUrl]);
134
+ return { tokens, loading, error };
135
+ }
136
+
137
+ // src/hooks/useNFTs.ts
138
+ import { useState as useState4, useEffect as useEffect4 } from "react";
139
+ function useNFTs(accountId) {
140
+ const { mirrorNodeUrl } = useHiero();
141
+ const [nfts, setNfts] = useState4([]);
142
+ const [loading, setLoading] = useState4(true);
143
+ const [error, setError] = useState4(null);
144
+ useEffect4(() => {
145
+ async function fetchNFTs() {
146
+ try {
147
+ setLoading(true);
148
+ const response = await fetch(
149
+ `${mirrorNodeUrl}/api/v1/accounts/${accountId}/nfts`
150
+ );
151
+ if (!response.ok) {
152
+ throw new Error(`Failed to fetch NFTs: ${response.statusText}`);
153
+ }
154
+ const data = await response.json();
155
+ const formatted = data.nfts.map((nft) => ({
156
+ tokenId: nft.token_id,
157
+ serialNumber: nft.serial_number,
158
+ metadata: nft.metadata ? atob(nft.metadata) : ""
159
+ }));
160
+ setNfts(formatted);
161
+ setError(null);
162
+ } catch (err) {
163
+ setError(err instanceof Error ? err : new Error("Unknown error"));
164
+ } finally {
165
+ setLoading(false);
166
+ }
167
+ }
168
+ if (accountId) {
169
+ fetchNFTs();
170
+ }
171
+ }, [accountId, mirrorNodeUrl]);
172
+ return { nfts, loading, error };
173
+ }
174
+ export {
175
+ HieroContext,
176
+ HieroProvider,
177
+ useAccount,
178
+ useHiero,
179
+ useNFTs,
180
+ useTokens,
181
+ useTransactions
182
+ };
package/package.json ADDED
@@ -0,0 +1,59 @@
1
+ {
2
+ "name": "hiero-react",
3
+ "version": "0.1.0",
4
+ "description": "React hooks and utilities for Hiero network integration",
5
+ "author": "imatthew55",
6
+ "license": "Apache-2.0",
7
+ "type": "module",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/imatthew55/hiero-bounty.git"
11
+ },
12
+ "keywords": [
13
+ "hiero",
14
+ "hedera",
15
+ "react",
16
+ "hooks",
17
+ "web3",
18
+ "blockchain"
19
+ ],
20
+ "main": "dist/index.js",
21
+ "module": "dist/index.mjs",
22
+ "types": "dist/index.d.ts",
23
+ "files": [
24
+ "dist"
25
+ ],
26
+ "exports": {
27
+ ".": {
28
+ "import": "./dist/index.mjs",
29
+ "require": "./dist/index.js",
30
+ "types": "./dist/index.d.ts"
31
+ }
32
+ },
33
+ "scripts": {
34
+ "build": "tsup src/index.ts --format cjs,esm --dts",
35
+ "dev": "tsup src/index.ts --format cjs,esm --dts --watch",
36
+ "test": "vitest run",
37
+ "test:watch": "vitest",
38
+ "lint": "eslint src/",
39
+ "prepublishOnly": "npm run build"
40
+ },
41
+ "peerDependencies": {
42
+ "react": ">=18.0.0"
43
+ },
44
+ "devDependencies": {
45
+ "@eslint/js": "^9.39.3",
46
+ "@testing-library/react": "^16.0.0",
47
+ "@types/eslint__js": "^8.42.3",
48
+ "@types/node": "^20.0.0",
49
+ "@types/react": "^18.0.0",
50
+ "eslint": "^9.39.3",
51
+ "jsdom": "^24.0.0",
52
+ "react": "^18.0.0",
53
+ "react-dom": "^18.0.0",
54
+ "tsup": "^8.0.0",
55
+ "typescript": "^5.0.0",
56
+ "typescript-eslint": "^8.56.0",
57
+ "vitest": "^1.0.0"
58
+ }
59
+ }