Skip to content

feat: fetch TON balances #341

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

feat: fetch TON balances #341

wants to merge 4 commits into from

Conversation

fadeev
Copy link
Member

@fadeev fadeev commented May 23, 2025

yarn zetachain query balances --ton 0QDDnID7qCW5YRcUsZSr-xS0t6T5bLqwkDO0o1qzU9PC2ZU5
┌─────────┬────────────┬───────────────┬───────────┬───────┐
│ (index) │ Amount     │ Chain         │ Token     │ Type  │
├─────────┼────────────┼───────────────┼───────────┼───────┤
│ 0       │ '0.011042' │ 'ton_testnet' │ 'TON.TON' │ 'Gas' │
└─────────┴────────────┴───────────────┴───────────┴───────┘

Summary by CodeRabbit

  • New Features
    • Added support for fetching and displaying TON blockchain token balances.
    • Introduced the ability to specify a TON address using a new command-line option (--ton <address>).

Copy link
Contributor

coderabbitai bot commented May 23, 2025

📝 Walkthrough

Walkthrough

The changes introduce support for retrieving token balances from the TON blockchain. This includes updating the balance-fetching logic to accept and process TON addresses, extending the command-line interface to accept a TON address, and implementing a utility function to fetch and convert TON balances from relevant APIs.

Changes

File(s) Change Summary
packages/client/src/getBalances.ts Added TON support: function now accepts an optional tonAddress and fetches TON balances if given.
packages/commands/src/query/balances.ts CLI and schema updated to accept --ton <address> option; passes TON address to getBalances logic.
utils/balances.ts Added TON API endpoints, response interface, and getTonBalances utility for fetching TON balances.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant CLI (balancesCommand)
    participant Client (getBalances)
    participant Utils (getTonBalances)
    participant TON API

    User->>CLI (balancesCommand): Run balances command with --ton <address>
    CLI (balancesCommand)->>Client (getBalances): Call with tonAddress
    alt If tonAddress is provided
        Client (getBalances)->>Utils (getTonBalances): Fetch TON balances
        Utils (getTonBalances)->>TON API: Request balance for each TON token
        TON API-->>Utils (getTonBalances): Return balances
        Utils (getTonBalances)-->>Client (getBalances): Return processed balances
    end
    Client (getBalances)-->>CLI (balancesCommand): Return all balances
    CLI (balancesCommand)-->>User: Output balances
Loading
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions github-actions bot added the feat label May 23, 2025
@fadeev fadeev linked an issue May 23, 2025 that may be closed by this pull request
@fadeev fadeev marked this pull request as ready for review May 25, 2025 15:35
@fadeev fadeev requested review from a team as code owners May 25, 2025 15:35
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🔭 Outside diff range comments (1)
packages/commands/src/query/balances.ts (1)

130-135: 🛠️ Refactor suggestion

Add TON address to the formatted output.

The formatAddresses function should include the TON address to be consistent with other address types.

     const addressesInfo = formatAddresses({
       bitcoin: btcAddress,
       evm: evmAddress,
       solana: solanaAddress,
       sui: suiAddress,
+      ton: tonAddress,
     });
🧹 Nitpick comments (1)
packages/commands/src/query/balances.ts (1)

95-95: Consider adding TON address resolution from account names.

Unlike other address types (EVM, Solana, Bitcoin, Sui) which have resolver functions that can derive addresses from account names, TON addresses can only be provided directly via the --ton option. This creates an inconsistent user experience.

Would you like me to help implement a resolveTonAddress function to maintain consistency with other address types?

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a6b47cd and f624869.

📒 Files selected for processing (3)
  • packages/client/src/getBalances.ts (4 hunks)
  • packages/commands/src/query/balances.ts (4 hunks)
  • utils/balances.ts (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
packages/client/src/getBalances.ts (1)
utils/balances.ts (1)
  • getTonBalances (805-858)
utils/balances.ts (1)
types/balances.types.ts (2)
  • Token (18-27)
  • TokenBalance (5-16)
🔇 Additional comments (3)
utils/balances.ts (1)

21-22: LGTM!

The TON API endpoint constants are correctly defined.

packages/client/src/getBalances.ts (1)

14-14: LGTM!

The TON balance fetching is correctly integrated following the same pattern as other chains.

Also applies to: 27-27, 37-37, 43-43, 147-152

packages/commands/src/query/balances.ts (1)

39-39: LGTM!

The TON address support is correctly added to the CLI command.

Also applies to: 95-95, 97-103, 120-120, 162-162

fadeev and others added 2 commits May 26, 2025 10:41
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
utils/balances.ts (1)

859-859: Optimize axios import for better performance.

Consider importing isAxiosError directly instead of accessing it from the axios object.

Apply this optimization:

-import axios from "axios";
+import axios, { isAxiosError } from "axios";

Then update the usage:

-    if (axios.isAxiosError(error)) {
+    if (isAxiosError(error)) {
🧰 Tools
🪛 GitHub Check: build

[warning] 859-859:
Caution: axios also has a named export isAxiosError. Check if you meant to write import {isAxiosError} from 'axios' instead

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6e9fb01 and 81a35a9.

📒 Files selected for processing (1)
  • utils/balances.ts (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
utils/balances.ts (1)
types/balances.types.ts (2)
  • Token (18-27)
  • TokenBalance (5-16)
🪛 GitHub Check: build
utils/balances.ts

[warning] 859-859:
Caution: axios also has a named export isAxiosError. Check if you meant to write import {isAxiosError} from 'axios' instead

🔇 Additional comments (4)
utils/balances.ts (4)

21-22: LGTM! API endpoints are correctly defined.

The TON mainnet and testnet API endpoints are properly defined and follow the expected pattern for the TON API v2.


798-806: Interface looks comprehensive and well-typed.

The TonApiResponse interface now includes all the essential fields from the TON API v2 accounts endpoint. The balance field is correctly typed as string | number to handle both response formats.


828-828: API endpoint assignment is now correct.

The ternary condition correctly assigns TON_MAINNET_API when network is "mainnet" and TON_TESTNET_API otherwise. This resolves the previously reported issue.


811-872: Excellent implementation with robust error handling.

The getTonBalances function is well-structured with:

  • Proper token filtering for TON Gas tokens
  • Correct API endpoint selection
  • Thorough response validation
  • Accurate balance conversion from nanoTON to TON (9 decimals)
  • Comprehensive error handling for both Axios and general errors

The logic flow is sound and follows the established patterns in the codebase.

🧰 Tools
🪛 GitHub Check: build

[warning] 859-859:
Caution: axios also has a named export isAxiosError. Check if you meant to write import {isAxiosError} from 'axios' instead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Query ton balances inside getBalances
1 participant