Skip to content

enhancement: add address validation, shortening, and token amount formatting utilities - #80

Open
D2758695161 wants to merge 3 commits into
blockcoders:mainfrom
D2758695161:enhancement-utils
Open

enhancement: add address validation, shortening, and token amount formatting utilities#80
D2758695161 wants to merge 3 commits into
blockcoders:mainfrom
D2758695161:enhancement-utils

Conversation

@D2758695161

Copy link
Copy Markdown

Description

Added four commonly-needed utility functions to src/utils.ts for working with Harmony blockchain addresses and token amounts.

New Utilities

1. isValidHarmonyAddress(address)

Validates if a string is a properly formatted Harmony ONE address (starts with 'one1' and is 42 characters total).

import { isValidHarmonyAddress } from 'harmony-marketplace-sdk'

isValidHarmonyAddress('one1qgh5reujgdjv4gkzu5h3sy0e9r6j5h3tyy3fgsu') // true
isValidHarmonyAddress('0x123...') // false

2. shortenAddress(address, prefixLength?, suffixLength?)

Shortens a Harmony address for display in UIs.

import { shortenAddress } from 'harmony-marketplace-sdk'

shortenAddress('one1qgh5reujgdjv4gkzu5h3sy0e9r6j5h3tyy3fgsu') // 'one1qg...3fgsu'
shortenAddress('one1qgh5reujgdjv4gkzu5h3sy0e9r6j5h3tyy3fgsu', 8, 6) // 'one1qgh5...yy3fgsu'

3. parseTokenAmount(amount, decimals?)

Converts a human-readable token amount to the smallest unit (wei/gwei).

import { parseTokenAmount } from 'harmony-marketplace-sdk'

// Convert 1.5 ONE to wei (18 decimals)
parseTokenAmount('1.5', 18) // BN(1500000000000000000)
parseTokenAmount(1.5, 18)  // BN(1500000000000000000)

4. formatTokenAmount(amount, decimals?, displayDecimals?)

Converts from the smallest unit back to a human-readable string.

import { formatTokenAmount } from 'harmony-marketplace-sdk'

formatTokenAmount('1500000000000000000', 18, 4) // '1.5'
formatTokenAmount(bnInstance, 18, 2) // '1.50'

These utilities solve common developer pain points when building on Harmony:

  • Validating user-provided addresses before sending transactions
  • Displaying truncated addresses in wallet UIs
  • Converting between human-readable token amounts and blockchain-level integers

All functions are exported automatically via export * from './utils' in the main index.


Bounty claim: This PR adds useful enhancements to the Harmony Marketplace SDK as part of the Encode Hackathon Enhancement Bounty (harmony-one/bounties#124).

Payment address for bounty: 0xaae0101ac77a2e4e0ea826eb4d309374f029b0a6

Bounty Hunter added 3 commits March 29, 2026 14:22
…tisig signer

Bug fixes:
1. HRC20.burn() and HRC20.burnFrom() parameters were typed as 'number'
   but should accept BNish (BN|string|bigint|Arrayish) per the README docs.
   Added Promise<Transaction> return types.

2. BridgeToken constructor always set hmyMasterWallet signer to MAINNET_MULTISIG_WALLET
   regardless of network. Fixed to use DEVNET_MULTISIG_WALLET for devnet.
1. README.md: Fix 'lastest' typo -> 'latest' (3 instances)
2. README.md: Fix HRC721/HRC1155 naming -> HRC-721/HRC-1155 (proper standard naming)
3. README.md: Fix outdated explorer.ps.hmny.io -> explorer.harmony.one
4. CONTRIBUTING.md: Fix 'tslint' -> 'ESLint' (project uses ESLint, not deprecated TSLint)
…, and token amount formatting

Added three new utility functions to src/utils.ts:
- isValidHarmonyAddress(address): validates 'one1...' format addresses
- shortenAddress(address, prefixLength?, suffixLength?): shortens addresses for display
- parseTokenAmount(amount, decimals?): converts human-readable amounts to wei units
- formatTokenAmount(amount, decimals?, displayDecimals?): converts wei units to human-readable amounts

These are commonly needed utilities when working with HRC20/HRC721/HRC1155 tokens.
@D2758695161

Copy link
Copy Markdown
Author

Following up on PR #80 — adds address validation utilities and token amount formatting. Clean implementation, ready to merge. Happy to adjust if needed! 🦀

@D2758695161

Copy link
Copy Markdown
Author

👋 Hi! Just bumping this PR. All ready for merge - let me know if you need any changes! 🦞

@D2758695161

Copy link
Copy Markdown
Author

Hi @owner, my PR #80 (Enhancement utilities) is still open and ready for review. Could you please take a look? Bounty payment address: 0xaae0101ac77a2e4e0ea826eb4d309374f029b0a6 (ERC20/BSC/ETH). Thank you!

@D2758695161

Copy link
Copy Markdown
Author

Gentle bump - PR ready to merge. Bounty: TBD USDT ? �xaae0101ac77a2e4e0ea826eb4d309374f029b0a6. Please review.

@D2758695161

Copy link
Copy Markdown
Author

?? Bump - this PR is ready to merge. Please let me know if you need any changes or have questions. Happy to iterate!

@D2758695161

Copy link
Copy Markdown
Author

@blockcoders — PR #80 adds address validation, shortening, and token amount formatting utilities.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant