Skip to content

fix: use multiple SUI coins for PTB gas payment#3877

Merged
ws4charlie merged 9 commits intodevelopfrom
fix-sui-withdrawAndCall-gas-coin-selection
May 19, 2025
Merged

fix: use multiple SUI coins for PTB gas payment#3877
ws4charlie merged 9 commits intodevelopfrom
fix-sui-withdrawAndCall-gas-coin-selection

Conversation

@ws4charlie
Copy link
Contributor

@ws4charlie ws4charlie commented May 14, 2025

Description

One single SUI coin object may not cover PTB transaction gas fee. Using multiple SUI coin objects for gas payment is a reliable way to avoid potential insufficient funds.

Closes: #3874

How Has This Been Tested?

  • Tested CCTX in localnet
  • Tested in development environment
  • Go unit tests
  • Go integration tests
  • Tested via GitHub Actions

Summary by CodeRabbit

  • Bug Fixes

    • Improved selection of SUI coin objects to ensure the latest object with sufficient balance is used for transaction gas fees.
  • Documentation

    • Updated the changelog with details about the fix for SUI coin selection.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented May 14, 2025

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

📝 Walkthrough

Walkthrough

The changes introduce a new parameter to methods responsible for selecting SUI coin objects, ensuring that only coins with a sufficient balance to cover the gas fee are chosen. All relevant interface, implementation, and test signatures have been updated to accommodate this parameter. Error handling and deterministic selection logic are improved.

Changes

File(s) Change Summary
changelog.md Added a fix entry describing the new logic for selecting SUI coin objects with sufficient balance for gas fees.
zetaclient/chains/sui/client/client.go Updated GetSuiCoinObjectRef to accept a wantBalance parameter, enhance error handling, and ensure deterministic coin selection.
zetaclient/chains/sui/signer/signer.go Updated the RPC interface: GetSuiCoinObjectRef now requires a wantBalance parameter.
zetaclient/chains/sui/signer/signer_tx.go Modified call to getWithdrawAndCallObjectRefs to include the gasBudget parameter.
zetaclient/chains/sui/signer/withdraw_and_call.go Updated getWithdrawAndCallObjectRefs to accept and utilize the gasBudget parameter in coin selection logic.
zetaclient/chains/sui/signer/withdraw_and_call_test.go Adjusted mocks and test calls to support the new parameter in getWithdrawAndCallObjectRefs and GetSuiCoinObjectRef.
zetaclient/testutils/mocks/sui_client.go, zetaclient/testutils/mocks/sui_gen.go Updated mock interfaces and implementations to require the wantBalance parameter for GetSuiCoinObjectRef.

Sequence Diagram(s)

sequenceDiagram
    participant Signer
    participant SuiClient
    participant SuiChain

    Signer->>SuiClient: getWithdrawAndCallObjectRefs(ctx, withdrawCapID, onCallObjectIDs, gasBudget)
    SuiClient->>SuiChain: GetSuiCoinObjectRef(ctx, owner, wantBalance=gasBudget)
    SuiChain-->>SuiClient: SUI Coin ObjectRef (with sufficient balance)
    SuiClient-->>Signer: Object references for transaction
Loading

Assessment against linked issues

Objective Addressed Explanation
Select SUI coin object with sufficient balance for gas fee in withdrawAndCall to avoid transaction failures (#3874)
Ensure deterministic and correct coin object selection to prevent random failures and version errors (#3874)

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.

@codecov
Copy link

codecov bot commented May 14, 2025

Codecov Report

Attention: Patch coverage is 4.76190% with 60 lines in your changes missing coverage. Please review.

Project coverage is 64.69%. Comparing base (33a70df) to head (91969f6).
Report is 1 commits behind head on develop.

Files with missing lines Patch % Lines
zetaclient/chains/sui/client/client.go 0.00% 57 Missing ⚠️
zetaclient/chains/sui/signer/signer_tx.go 0.00% 2 Missing ⚠️
zetaclient/chains/sui/signer/withdraw_and_call.go 75.00% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #3877      +/-   ##
===========================================
- Coverage    64.73%   64.69%   -0.04%     
===========================================
  Files          469      469              
  Lines        34333    34350      +17     
===========================================
- Hits         22224    22222       -2     
- Misses       11081    11101      +20     
+ Partials      1028     1027       -1     
Files with missing lines Coverage Δ
zetaclient/chains/sui/observer/outbound.go 58.20% <ø> (+0.85%) ⬆️
zetaclient/chains/sui/signer/signer.go 51.56% <ø> (ø)
zetaclient/chains/sui/signer/withdraw_and_call.go 80.00% <75.00%> (-0.16%) ⬇️
zetaclient/chains/sui/signer/signer_tx.go 50.00% <0.00%> (-0.23%) ⬇️
zetaclient/chains/sui/client/client.go 11.53% <0.00%> (-0.92%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ws4charlie ws4charlie added zetaclient Issues related to ZetaClient chain:sui SUI_TESTS Run make start-sui-tests labels May 14, 2025
@ws4charlie ws4charlie marked this pull request as ready for review May 14, 2025 23:00
@ws4charlie ws4charlie requested a review from a team as a code owner May 14, 2025 23:00
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: 1

🔭 Outside diff range comments (1)
zetaclient/chains/sui/client/client.go (1)

255-335: 💡 Verification agent

🧩 Analysis chain

Add unit tests for enhanced logic

The static analysis indicates that the new logic lacks test coverage. Consider adding unit tests that verify:

  1. Coin sorting works correctly
  2. Latest version detection functions properly
  3. Balance-based selection operates as expected
  4. All error paths are covered

🏁 Script executed:

#!/bin/bash
# Check for any existing tests of the GetSuiCoinObjectRef function

echo "Searching for existing tests of GetSuiCoinObjectRef function..."
rg -A 3 "Test.*GetSuiCoinObjectRef" --type go

Length of output: 184


To ensure there truly aren’t any existing tests covering GetSuiCoinObjectRef, let’s search all Go files and explicitly within test files:


🏁 Script executed:

#!/bin/bash
echo "1) Scanning all Go files for ‘GetSuiCoinObjectRef’..."
rg -n "GetSuiCoinObjectRef" --type go

echo "2) Scanning only *_test.go files for ‘GetSuiCoinObjectRef’..."
rg -n "GetSuiCoinObjectRef" --glob "*_test.go"

Length of output: 1837


Add unit tests for Client.GetSuiCoinObjectRef

There are currently no direct tests covering the logic in zetaclient/chains/sui/client/client.go. Please add a new test file (e.g. chains/sui/client/client_test.go) that exercises:

  • Deterministic sorting of coins.Data by CoinObjectId
  • Selection of the highest-version coin
  • Picking a coin whose balance ≥ wantBalance
  • All error paths (no coins returned, invalid coin type, parse‐uint failures, no qualified coin)

Target locations:

  • chains/sui/client/client.go → add tests in chains/sui/client/client_test.go
  • Use testutils/mocks to stub SuiXGetCoinsRequest responses
🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 256-256: zetaclient/chains/sui/client/client.go#L256
Added line #L256 was not covered by tests


[warning] 261-263: zetaclient/chains/sui/client/client.go#L261-L263
Added lines #L261 - L263 were not covered by tests


[warning] 265-266: zetaclient/chains/sui/client/client.go#L265-L266
Added lines #L265 - L266 were not covered by tests


[warning] 270-272: zetaclient/chains/sui/client/client.go#L270-L272
Added lines #L270 - L272 were not covered by tests


[warning] 275-277: zetaclient/chains/sui/client/client.go#L275-L277
Added lines #L275 - L277 were not covered by tests


[warning] 280-280: zetaclient/chains/sui/client/client.go#L280
Added line #L280 was not covered by tests


[warning] 283-283: zetaclient/chains/sui/client/client.go#L283
Added line #L283 was not covered by tests


[warning] 288-288: zetaclient/chains/sui/client/client.go#L288
Added line #L288 was not covered by tests


[warning] 291-291: zetaclient/chains/sui/client/client.go#L291
Added line #L291 was not covered by tests


[warning] 293-294: zetaclient/chains/sui/client/client.go#L293-L294
Added lines #L293 - L294 were not covered by tests


[warning] 298-300: zetaclient/chains/sui/client/client.go#L298-L300
Added lines #L298 - L300 were not covered by tests


[warning] 303-306: zetaclient/chains/sui/client/client.go#L303-L306
Added lines #L303 - L306 were not covered by tests


[warning] 308-310: zetaclient/chains/sui/client/client.go#L308-L310
Added lines #L308 - L310 were not covered by tests


[warning] 316-316: zetaclient/chains/sui/client/client.go#L316
Added line #L316 was not covered by tests

🧹 Nitpick comments (2)
zetaclient/chains/sui/signer/withdraw_and_call_test.go (1)

322-326: Consider adding specific test cases for balance selection

While the test has been updated to include the new parameter, it doesn't explicitly test the core functionality - selecting a coin with sufficient balance. Consider adding test cases that verify:

  1. A case where multiple coins exist but only one has sufficient balance
  2. A case where no coin has sufficient balance
  3. A case where multiple coins have sufficient balance but only the one with latest version is selected
// Example test case structure:
{
    name: "select coin with sufficient balance",
    mockCoins: models.CoinPage{
        Data: []models.CoinData{
            {CoinObjectId: "id1", Version: "1", Balance: "50", Digest: "digest1"},
            {CoinObjectId: "id2", Version: "2", Balance: "150", Digest: "digest2"},
        },
    },
    wantBalance: 100,
    expectedObjectId: "id2",
},
changelog.md (1)

49-49: Improve description clarity for consistency
The Fixes section favors concise phrasing. Consider rewording to align with existing entries:

  • Use “with sufficient balance” instead of “that contains enough balance to pay”
  • Use “cover” instead of “pay” for gas fees

Apply this diff:

-* [3877](https://github.com/zeta-chain/node/pull/3877) - choose latest SUI coin object that contains enough balance to pay PTB transaction gas fee
+* [3877](https://github.com/zeta-chain/node/pull/3877) - choose latest SUI coin object with sufficient balance to cover PTB transaction gas fee
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 33a70df and dc34832.

📒 Files selected for processing (8)
  • changelog.md (1 hunks)
  • zetaclient/chains/sui/client/client.go (2 hunks)
  • zetaclient/chains/sui/signer/signer.go (1 hunks)
  • zetaclient/chains/sui/signer/signer_tx.go (1 hunks)
  • zetaclient/chains/sui/signer/withdraw_and_call.go (2 hunks)
  • zetaclient/chains/sui/signer/withdraw_and_call_test.go (1 hunks)
  • zetaclient/testutils/mocks/sui_client.go (1 hunks)
  • zetaclient/testutils/mocks/sui_gen.go (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
`**/*.go`: Review the Go code, point out issues relative to principles of clean code, expressiveness, and performance.

**/*.go: Review the Go code, point out issues relative to principles of clean code, expressiveness, and performance.

  • zetaclient/chains/sui/signer/signer.go
  • zetaclient/chains/sui/signer/signer_tx.go
  • zetaclient/testutils/mocks/sui_gen.go
  • zetaclient/chains/sui/signer/withdraw_and_call_test.go
  • zetaclient/chains/sui/signer/withdraw_and_call.go
  • zetaclient/testutils/mocks/sui_client.go
  • zetaclient/chains/sui/client/client.go
🪛 GitHub Check: codecov/patch
zetaclient/chains/sui/signer/signer_tx.go

[warning] 154-154: zetaclient/chains/sui/signer/signer_tx.go#L154
Added line #L154 was not covered by tests

zetaclient/chains/sui/client/client.go

[warning] 256-256: zetaclient/chains/sui/client/client.go#L256
Added line #L256 was not covered by tests


[warning] 261-263: zetaclient/chains/sui/client/client.go#L261-L263
Added lines #L261 - L263 were not covered by tests


[warning] 265-266: zetaclient/chains/sui/client/client.go#L265-L266
Added lines #L265 - L266 were not covered by tests


[warning] 270-272: zetaclient/chains/sui/client/client.go#L270-L272
Added lines #L270 - L272 were not covered by tests


[warning] 275-277: zetaclient/chains/sui/client/client.go#L275-L277
Added lines #L275 - L277 were not covered by tests


[warning] 280-280: zetaclient/chains/sui/client/client.go#L280
Added line #L280 was not covered by tests


[warning] 283-283: zetaclient/chains/sui/client/client.go#L283
Added line #L283 was not covered by tests


[warning] 288-288: zetaclient/chains/sui/client/client.go#L288
Added line #L288 was not covered by tests


[warning] 291-291: zetaclient/chains/sui/client/client.go#L291
Added line #L291 was not covered by tests


[warning] 293-294: zetaclient/chains/sui/client/client.go#L293-L294
Added lines #L293 - L294 were not covered by tests


[warning] 298-300: zetaclient/chains/sui/client/client.go#L298-L300
Added lines #L298 - L300 were not covered by tests


[warning] 303-306: zetaclient/chains/sui/client/client.go#L303-L306
Added lines #L303 - L306 were not covered by tests


[warning] 308-310: zetaclient/chains/sui/client/client.go#L308-L310
Added lines #L308 - L310 were not covered by tests


[warning] 316-316: zetaclient/chains/sui/client/client.go#L316
Added line #L316 was not covered by tests

🔇 Additional comments (15)
zetaclient/testutils/mocks/sui_gen.go (1)

24-24: Interface method signature updated to include wantBalance parameter.

The GetSuiCoinObjectRef interface method has been correctly updated to include the new wantBalance parameter, ensuring that generated mocks will match the actual implementation signature. This maintains type compatibility across the codebase.

zetaclient/chains/sui/signer/signer.go (1)

37-37: Updated RPC interface to include wantBalance parameter.

The GetSuiCoinObjectRef method signature in the RPC interface has been updated to include the wantBalance parameter of type uint64. This change correctly propagates the interface contract to implementers, ensuring they select coin objects with sufficient balance for transactions.

zetaclient/chains/sui/client/client.go (6)

254-256: Improved function documentation

The updated function signature and documentation clearly explain the purpose of the added wantBalance parameter. This change aligns with the PR objective to select coins with sufficient balance for gas fees.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 256-256: zetaclient/chains/sui/client/client.go#L256
Added line #L256 was not covered by tests


261-267: Improved error handling with more specific error messages

The switch statement enhances error handling by providing distinct error paths for different failure scenarios, making debugging easier.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 261-263: zetaclient/chains/sui/client/client.go#L261-L263
Added lines #L261 - L263 were not covered by tests


[warning] 265-266: zetaclient/chains/sui/client/client.go#L265-L266
Added lines #L265 - L266 were not covered by tests


269-273: Good deterministic sorting implementation

Sorting coins by object ID ensures deterministic selection across observers, which is critical for consensus systems.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 270-272: zetaclient/chains/sui/client/client.go#L270-L272
Added lines #L270 - L272 were not covered by tests


274-295: Clear separation of coin selection logic

The first loop correctly identifies the latest coin version across all coins, which is a good first step in the selection process.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 275-277: zetaclient/chains/sui/client/client.go#L275-L277
Added lines #L275 - L277 were not covered by tests


[warning] 280-280: zetaclient/chains/sui/client/client.go#L280
Added line #L280 was not covered by tests


[warning] 283-283: zetaclient/chains/sui/client/client.go#L283
Added line #L283 was not covered by tests


[warning] 288-288: zetaclient/chains/sui/client/client.go#L288
Added line #L288 was not covered by tests


[warning] 291-291: zetaclient/chains/sui/client/client.go#L291
Added line #L291 was not covered by tests


[warning] 293-294: zetaclient/chains/sui/client/client.go#L293-L294
Added lines #L293 - L294 were not covered by tests


297-312: Effective implementation of balance-based selection

The second loop properly selects the first coin with the latest version that meets the balance requirement, which fulfills the PR objective.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 298-300: zetaclient/chains/sui/client/client.go#L298-L300
Added lines #L298 - L300 were not covered by tests


[warning] 303-306: zetaclient/chains/sui/client/client.go#L303-L306
Added lines #L303 - L306 were not covered by tests


[warning] 308-310: zetaclient/chains/sui/client/client.go#L308-L310
Added lines #L308 - L310 were not covered by tests


314-317: Clear error messaging for edge case

The error message explains the exact reason for failure when no qualified coin is found, which aids in troubleshooting.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 316-316: zetaclient/chains/sui/client/client.go#L316
Added line #L316 was not covered by tests

zetaclient/chains/sui/signer/withdraw_and_call.go (2)

137-142: Interface update to include gas budget parameter

The function signature has been properly updated to include the gasBudget parameter, which is required for the enhanced coin selection logic.


207-208: Correctly passing gasBudget to GetSuiCoinObjectRef

The call to GetSuiCoinObjectRef now includes the gasBudget parameter, ensuring that a coin with sufficient balance is selected for gas payment.

zetaclient/testutils/mocks/sui_client.go (3)

108-110: Correctly updated mock method signature

The mock function signature has been properly updated to match the interface changes.


118-123: Updated type assertions for the new parameter

All function type assertions have been correctly updated to handle the additional wantBalance parameter.


127-129: Properly updated error function type assertions

The error handling function type assertions have been correctly updated for the new parameter.

zetaclient/chains/sui/signer/withdraw_and_call_test.go (2)

322-323: Updated mock expectations with the new parameter

The mock expectation now correctly includes a matcher for the new wantBalance parameter.


325-326: Test call updated to provide gas budget parameter

The test now passes a value of 100 for the gas budget parameter, ensuring the function is tested with the new parameter.

@ws4charlie ws4charlie changed the title fix: pick latest coin object with enough balance to cover gas budget fix: use multiple SUI coins for PTB gas payment May 16, 2025
@ws4charlie ws4charlie requested review from lumtis and swift1337 May 16, 2025 19:47
@ws4charlie ws4charlie enabled auto-merge May 19, 2025 15:17
@ws4charlie ws4charlie added this pull request to the merge queue May 19, 2025
Merged via the queue into develop with commit f56b59c May 19, 2025
46 of 47 checks passed
@ws4charlie ws4charlie deleted the fix-sui-withdrawAndCall-gas-coin-selection branch May 19, 2025 15:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

chain:sui SUI_TESTS Run make start-sui-tests zetaclient Issues related to ZetaClient

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sui outbound will be blocked if a low-balance coin object is chosen to pay gas fee

3 participants