feat(sdk): design typed asset model for native and issued assets (#164)#243
Merged
Merged
Conversation
6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📝 Title
feat(sdk): design typed asset model for native and issued assets (#164)🟢 Summary
Closes #164.
This PR introduces a stable, discriminated-union typed asset model (
Asset = NativeAsset | IssuedAsset) across the SDK. Previously, asset support focused primarily on native XLM or ad-hoc strings, creating inconsistency for future issued asset (AlphaNum4/AlphaNum12) flows.This update establishes compile-time safety, runtime validation rules, parsing/formatting helpers, and documentation for consumers.
🛠️ Changes Introduced
1. Core Asset Types (
src/types/asset.ts)NativeAsset(type: 'native',code: 'XLM').IssuedAsset(type: 'issued',code: string,issuer: string).Assetdiscriminated union (NativeAsset | IssuedAsset).NATIVE_ASSETconstant.isNativeAssetandisIssuedAssettype guards.validateAssetandassertValidAssetensuring Stellar spec rules (1–12 char alphanumeric codes, valid 56-charG...issuer public keys, and forbidding XLM as an issued asset code).2. Utility & Parsing Helpers (
src/utils/assetHelpers.ts)formatAsset(asset): Formats assets to standard strings ("XLM"or"CODE:ISSUER").parseAssetString(str): Safely converts"XLM"or"CODE:ISSUER"back into typedAssetobjects.areAssetsEqual(a, b): Deep equality checker for native vs. issued assets.3. Package Root Export (
src/index.ts)4. Tests & Docs
test/types/asset.test.ts: Added complete unit test suite covering native/issued assets, edge-case validation failures, type guards, formatting, parsing, and equality.docs/Asset-Model.md: Created developer guide detailing the asset hierarchy, type narrowing, validation rules, formatting utilities, and usage recommendations.✅ Acceptance Criteria Checklist
NativeAsset&NATIVE_ASSET.docs/Asset-Model.md.🧪 Test Coverage
Ran unit tests with
pytest/npm test:NativeAssetandIssuedAssetcorrectly via guards.'XLM')."XLM"and"USD:G..."strings.