|
| 1 | +# hedera-diamond-hts-lib |
| 2 | + |
| 3 | +_Seamless Hedera Token Service (HTS) integration for EIP-2535 Diamond Standard smart contracts._ |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +**hedera-diamond-hts-lib** solves the challenge of using Hedera's non-EVM HTS precompiles and abstract contracts (like `IHederaTokenService`) within modular, upgradeable Diamond (EIP-2535) architectures. Instead of inheriting abstract contracts (which is incompatible with delegatecall-based facets), this library provides gas-optimized, composable `library` wrappers for all core HTS operations. |
| 10 | + |
| 11 | +These libraries enable facets in a Diamond proxy to perform mint, burn, transfer, associate, dissociate, freeze, KYC, and other token operations directly, using delegatecall-compatible stateless functions. This unlocks full HTS utility in modular, upgradable dApps. |
| 12 | + |
| 13 | +--- |
| 14 | + |
| 15 | +## Features |
| 16 | + |
| 17 | +- **Modular HTS Operations:** Each HTS function (mint, burn, transfer, etc.) is available as a standalone library function. |
| 18 | +- **Diamond Facet Compatible:** Designed for use inside EIP-2535 facets via delegatecall, with no stateful logic. |
| 19 | +- **Full HTS Coverage:** Supports association, dissociation, KYC, freeze, custom fees, NFT and fungible operations, and more. |
| 20 | +- **Lightweight & Composable:** No inheritance, minimal overhead, easily composed with other libraries and storage patterns. |
| 21 | + |
| 22 | +--- |
| 23 | + |
| 24 | +## Installation |
| 25 | + |
| 26 | +### Prerequisites |
| 27 | +- Foundry or hardhat project |
| 28 | + |
| 29 | +### Add to Your Project |
| 30 | + |
| 31 | +#### Option 1: Manual Copy |
| 32 | +Copy the `.sol` files from `src/libraries/hts/` into your project's libraries directory. |
| 33 | + |
| 34 | +#### Option 2: forge install |
| 35 | +```sh |
| 36 | +forge install dadadave80/chronicle |
| 37 | +``` |
| 38 | + |
| 39 | +--- |
| 40 | + |
| 41 | +## Usage Example |
| 42 | + |
| 43 | +Import and use in a facet contract: |
| 44 | + |
| 45 | +```solidity |
| 46 | +import {LibHederaTokenService} from "@chronicle/libraries/hts/LibHederaTokenService.sol"; |
| 47 | +
|
| 48 | +contract ProductsFacet { |
| 49 | + function mintProduct(address token, int64 amount) external { |
| 50 | + // Calls the Hedera Token Service mint via precompile |
| 51 | + (int256 code, int64 newSupply, int64[] memory serials) = LibHederaTokenService.mintToken(token, amount, new bytes[](0)); |
| 52 | + require(code == 22, "Mint failed"); // 22 = SUCCESS |
| 53 | + } |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +**Best Practices:** |
| 58 | +- Use with [LibDiamond](https://eips.ethereum.org/EIPS/eip-2535) storage patterns—never store state in libraries. |
| 59 | +- Use `using LibHederaTokenService for address;` for more ergonomic syntax. |
| 60 | +- Combine with access control and event logging as needed. |
| 61 | + |
| 62 | +--- |
| 63 | + |
| 64 | +## Project Structure |
| 65 | + |
| 66 | +- `LibHederaTokenService.sol` — Core stateless wrappers for all HTS precompile operations (mint, burn, transfer, associate, etc.) |
| 67 | +- `LibSafeHTS.sol` — Revert-on-failure safe wrappers for all HTS calls (throws on non-SUCCESS response). |
| 68 | +- `LibFeeHelper.sol` — Utilities for constructing custom fee structs for HTS tokens. |
| 69 | +- `LibKeyHelper.sol` — Utilities for managing HTS key types and key assignment. |
| 70 | + |
| 71 | +--- |
| 72 | + |
| 73 | +## Limitations / Considerations |
| 74 | + |
| 75 | +- **Delegatecall Overhead:** Calls from facets via delegatecall are slightly more expensive than direct contract calls. |
| 76 | +- **No Library State:** All logic must be stateless; use Diamond storage patterns for persistent data. |
| 77 | +- **HTS Compatibility:** Only works on Hedera-compatible EVM chains with HTS precompiles available at `0x167`. |
| 78 | +- **Error Handling:** Use `LibSafeHTS` for automatic revert on failure, or handle response codes manually with `LibHederaTokenService`. |
| 79 | + |
| 80 | +--- |
| 81 | + |
| 82 | +## Testing |
| 83 | + |
| 84 | +- **Unit Tests:** |
| 85 | + - Hardhat: `npx hardhat test` |
| 86 | + - Foundry: `forge test` |
| 87 | +- **Coverage:** |
| 88 | + - Hardhat: `npx hardhat coverage` |
| 89 | + - Foundry: `forge coverage` |
| 90 | + |
| 91 | +Tests cover all core HTS operations, including edge cases and error handling. |
| 92 | + |
| 93 | +--- |
| 94 | + |
| 95 | +## Contributing |
| 96 | + |
| 97 | +- Fork and submit PRs for new HTS methods, optimizations, or bug fixes. |
| 98 | +- Adhere to Solidity style guidelines and include unit tests for new features. |
| 99 | +- Open issues for feature requests or HTS compatibility questions. |
| 100 | + |
| 101 | +--- |
| 102 | + |
| 103 | +## License |
| 104 | + |
| 105 | +MIT |
| 106 | + |
| 107 | +--- |
| 108 | + |
| 109 | +## References |
| 110 | + |
| 111 | +- [Hedera Token Service Documentation](https://docs.hedera.com/hedera/smart-contracts/hedera-token-service) |
| 112 | +- [EIP-2535 Diamond Standard](https://eips.ethereum.org/EIPS/eip-2535) |
| 113 | +- [Hedera Token Service Solidity Interfaces](https://github.com/hashgraph/hedera-smart-contracts) |
0 commit comments