Skip to content

[SDK] Optimize fiat conversion query #7242

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

Merged
merged 1 commit into from
May 31, 2025

Conversation

joaquim-verges
Copy link
Member

@joaquim-verges joaquim-verges commented May 31, 2025


PR-Codex overview

This PR focuses on optimizing the fiat conversion queries in the thirdweb package by removing unnecessary checks for contract addresses in the convertFiatToCrypto and convertCryptoToFiat functions, and cleaning up test cases.

Detailed summary

  • Updated .changeset/dark-dancers-hammer.md to reflect optimization.
  • Removed unused imports of ZERO_ADDRESS from fiatToCrypto.test.ts and cryptoToFiat.test.ts.
  • Deleted tests for invalid wallet addresses in fiatToCrypto and cryptoToFiat.
  • Simplified address validation logic in convertFiatToCrypto and convertCryptoToFiat functions.

✨ Ask PR-Codex anything about this PR by commenting with /codex {your question}

Summary by CodeRabbit

  • Bug Fixes
    • Improved fiat conversion reliability by removing unnecessary contract address validation checks in crypto-to-fiat and fiat-to-crypto conversions.
  • Tests
    • Updated test suites to reflect the removal of contract deployment validation, ensuring tests align with the new validation logic.
  • Chores
    • Added a changeset to document the optimization in fiat conversion queries.

Copy link

vercel bot commented May 31, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
docs-v2 ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 31, 2025 9:57am
login ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 31, 2025 9:57am
thirdweb_playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 31, 2025 9:57am
thirdweb-www ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 31, 2025 9:57am
wallet-ui ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 31, 2025 9:57am

Copy link

changeset-bot bot commented May 31, 2025

🦋 Changeset detected

Latest commit: 3703fb8

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
thirdweb Patch
@thirdweb-dev/wagmi-adapter Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

coderabbitai bot commented May 31, 2025

Walkthrough

The changes remove the contract bytecode existence validation from both crypto-to-fiat and fiat-to-crypto conversion functions and their associated tests. Imports related to this validation are deleted, and one changeset file documents the optimization. No changes are made to public APIs or function signatures.

Changes

File(s) Change Summary
packages/thirdweb/src/pay/convert/cryptoToFiat.ts
packages/thirdweb/src/pay/convert/fiatToCrypto.ts
Removed contract bytecode existence validation and related imports from conversion functions.
packages/thirdweb/src/pay/convert/cryptoToFiat.test.ts
packages/thirdweb/src/pay/convert/fiatToCrypto.test.ts
Deleted tests for contract existence error cases and removed unused imports.
.changeset/dark-dancers-hammer.md Added changeset describing the patch update for optimization of fiat conversion query.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant ConversionFunction

    Caller->>ConversionFunction: convertCryptoToFiat(params) / convertFiatToCrypto(params)
    ConversionFunction->>ConversionFunction: Validate EVM address
    ConversionFunction->>ConversionFunction: Fetch token price
    alt Price available
        ConversionFunction-->>Caller: Return conversion result
    else Price unavailable
        ConversionFunction-->>Caller: Throw error
    end
Loading

Possibly related PRs

  • thirdweb-dev/js#7220: Updates the same conversion functions to use the Insight API with caching and enhanced error handling.
  • thirdweb-dev/js#7240: Adds a new Bridge.tokens() function and refactors conversion functions to use a new getTokenPrice helper, touching related conversion logic.

Warning

Review ran into problems

🔥 Problems

Errors were encountered while retrieving linked issues.

Errors (1)
  • TEAM-0000: Entity not found: Issue - Could not find referenced Issue.

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 38627d3 and 3703fb8.

📒 Files selected for processing (5)
  • .changeset/dark-dancers-hammer.md (1 hunks)
  • packages/thirdweb/src/pay/convert/cryptoToFiat.test.ts (1 hunks)
  • packages/thirdweb/src/pay/convert/cryptoToFiat.ts (0 hunks)
  • packages/thirdweb/src/pay/convert/fiatToCrypto.test.ts (1 hunks)
  • packages/thirdweb/src/pay/convert/fiatToCrypto.ts (0 hunks)
💤 Files with no reviewable changes (2)
  • packages/thirdweb/src/pay/convert/cryptoToFiat.ts
  • packages/thirdweb/src/pay/convert/fiatToCrypto.ts
⏰ Context from checks skipped due to timeout of 90000ms (8)
  • GitHub Check: E2E Tests (pnpm, esbuild)
  • GitHub Check: Size
  • GitHub Check: E2E Tests (pnpm, webpack)
  • GitHub Check: E2E Tests (pnpm, vite)
  • GitHub Check: Lint Packages
  • GitHub Check: Build Packages
  • GitHub Check: Unit Tests
  • GitHub Check: Analyze (javascript)
🔇 Additional comments (5)
packages/thirdweb/src/pay/convert/cryptoToFiat.test.ts (2)

6-6: LGTM! Import cleanup aligns with removed test case.

The removal of ZERO_ADDRESS import is consistent with the optimization that removed contract bytecode existence validation.


65-77:

❓ Verification inconclusive

Verify behavior change: wallet addresses now accepted.

With the removal of contract existence validation, convertCryptoToFiat may now accept wallet addresses (like ZERO_ADDRESS) that were previously rejected. Please confirm this behavior change is intentional and consider:

  1. Documentation: Update function documentation to reflect the new validation behavior
  2. Breaking change: If this affects existing integrations, consider documenting as a breaking change
  3. Test coverage: Ensure remaining validation (EVM address format) is sufficient for your use cases

🏁 Script executed:

#!/bin/bash
# Description: Verify the current validation behavior in convertCryptoToFiat function
# Expected: Should show the current validation logic without contract existence checks

ast-grep --pattern 'export function convertCryptoToFiat($$$) {
  $$$
}'

Length of output: 73


#!/bin/bash
# Locate the implementation file for convertCryptoToFiat and inspect validation logic

# Step 1: find the file
file=$(fd cryptoToFiat.ts)

echo "Implementation file:"
echo "$file"

# Step 2: show context around the function definition
rg -n "function convertCryptoToFiat" -A20 -C5 "$file"

#!/bin/bash
# Step 1: locate the implementation file
file=$(fd -e ts 'cryptoToFiat.ts$')

echo "Implementation file found:"
echo "$file"
echo

# Step 2: display the function definition and surrounding validation logic
rg -n "export function convertCryptoToFiat" -A20 -C5 "$file"
echo

# Step 3: search specifically for address‐validation routines
rg -nE "is.*Address|validate.*Address|ZERO_ADDRESS" -A5 -C3 "$file"

Verify changed address‐validation behavior in convertCryptoToFiat

The removal of on-chain contract existence checks means that convertCryptoToFiat now accepts any syntactically valid EVM address—including wallet addresses (e.g. ZERO_ADDRESS) or contracts that don’t exist. Please confirm that this behavior change is intentional and consider the following:

  • Update documentation (JSDoc/README) to state that only address format is validated, not on-chain existence.
  • If downstream integrations relied on the old existence check, document this as a breaking change in your changelog.
  • Add or update tests (e.g., using ZERO_ADDRESS or a known non-contract address) to capture the new validation rules—or reintroduce an explicit existence check if needed.
.changeset/dark-dancers-hammer.md (1)

1-6: LGTM! Changeset properly documents the optimization.

The changeset correctly categorizes this as a patch update and clearly describes the optimization of fiat conversion queries.

packages/thirdweb/src/pay/convert/fiatToCrypto.test.ts (2)

6-6: LGTM! Consistent import cleanup.

The removal of ZERO_ADDRESS import is consistent with the optimization applied across both fiat conversion functions.


68-80:

✅ Verification successful

Verify consistent behavior change with cryptoToFiat.

Similar to convertCryptoToFiat, the convertFiatToCrypto function may now accept wallet addresses that were previously rejected. Ensure both functions have consistent validation behavior after the optimization.


🏁 Script executed:

#!/bin/bash
# Description: Compare validation logic between both conversion functions
# Expected: Both functions should have similar validation patterns

echo "=== convertFiatToCrypto validation ==="
ast-grep --pattern 'export function convertFiatToCrypto($$$) {
  $$$
}'

echo "=== convertCryptoToFiat validation ==="
ast-grep --pattern 'export function convertCryptoToFiat($$$) {
  $$$
}'

Length of output: 321


🏁 Script executed:

#!/bin/bash
# Locate and inspect the definitions of both conversion functions

echo "=== convertFiatToCrypto definition ==="
rg -n -C3 "convertFiatToCrypto" packages/thirdweb/src/pay/convert

echo "=== convertCryptoToFiat definition ==="
rg -n -C3 "convertCryptoToFiat" packages/thirdweb/src/pay/convert

Length of output: 11600


#!/bin/bash
# Print the validation logic inside both conversion functions for comparison

echo "=== convertFiatToCrypto (lines 55-120) ==="
sed -n '55,120p' packages/thirdweb/src/pay/convert/fiatToCrypto.ts

echo
echo "=== convertCryptoToFiat (lines 55-120) ==="
sed -n '55,120p' packages/thirdweb/src/pay/convert/cryptoToFiat.ts

#!/bin/bash
# Check how each function validates the address parameter

echo "=== convertFiatToCrypto address validation ==="
rg -nE "isAddress|Invalid.*`to`" -C3 packages/thirdweb/src/pay/convert/fiatToCrypto.ts

echo
echo "=== convertCryptoToFiat address validation ==="
rg -nE "isAddress|Invalid.*`fromTokenAddress`" -C3 packages/thirdweb/src/pay/convert/cryptoToFiat.ts

Validation is already consistent across both conversion functions
Both convertFiatToCrypto and convertCryptoToFiat include identical tests that reject invalid EVM addresses (using to and fromTokenAddress, respectively). No additional changes are needed here.

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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 packages SDK Involves changes to the thirdweb SDK labels May 31, 2025
Copy link
Member Author


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • merge-queue - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

Copy link
Contributor

size-limit report 📦

Path Size Loading time (3g) Running time (snapdragon) Total time
thirdweb (esm) 62.24 KB (+0.01% 🔺) 1.3 s (+0.01% 🔺) 164 ms (+107.16% 🔺) 1.5 s
thirdweb (cjs) 345.34 KB (0%) 7 s (0%) 661 ms (+10.53% 🔺) 7.6 s
thirdweb (minimal + tree-shaking) 5.7 KB (0%) 114 ms (0%) 58 ms (+757.35% 🔺) 172 ms
thirdweb/chains (tree-shaking) 531 B (0%) 11 ms (0%) 30 ms (+1528.68% 🔺) 40 ms
thirdweb/react (minimal + tree-shaking) 19.54 KB (0%) 391 ms (0%) 80 ms (+379.91% 🔺) 471 ms

@joaquim-verges joaquim-verges merged commit f10fbc8 into main May 31, 2025
23 of 24 checks passed
@joaquim-verges joaquim-verges deleted the _SDK_Optimize_fiat_conversion_query branch May 31, 2025 10:14
@joaquim-verges joaquim-verges mentioned this pull request May 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
packages SDK Involves changes to the thirdweb SDK
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant