Skip to content
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

[SDK] Add 2 new Pay functions: convertFiatToCrypto and convertCryptoToFiat #5457

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

kien-ngo
Copy link
Contributor

@kien-ngo kien-ngo commented Nov 19, 2024

CNCT-2369

Problem solved

Short description of the bug fixed or feature added


PR-Codex overview

This PR introduces two new payment functions: convertFiatToCrypto and convertCryptoToFiat, along with their respective tests. These functions facilitate the conversion between fiat currency and cryptocurrency.

Detailed summary

  • Added convertFiatToCrypto function in fiatToCrypto.ts.
  • Added convertCryptoToFiat function in cryptoToFiat.ts.
  • Implemented endpoint functions for conversions in definitions.ts.
  • Added tests for convertFiatToCrypto in fiatToCrypto.test.ts.
  • Added tests for convertCryptoToFiat in cryptoToFiat.test.ts.
  • Updated utility functions for fetching conversion endpoints.

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

Copy link

vercel bot commented Nov 19, 2024

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 Nov 28, 2024 1:19pm
thirdweb_playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 28, 2024 1:19pm
thirdweb-www ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 28, 2024 1:19pm
wallet-ui ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 28, 2024 1:19pm

Copy link

changeset-bot bot commented Nov 19, 2024

🦋 Changeset detected

Latest commit: 07afcee

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

This PR includes changesets to release 1 package
Name Type
thirdweb Minor

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

graphite-app bot commented Nov 19, 2024

Your org has enabled the Graphite merge queue for merging into main

Add the label “merge-queue” to the PR and Graphite will automatically add it to the merge queue when it’s ready to merge. Or use the label “hotfix” to add to the merge queue as a hot fix.

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

@github-actions github-actions bot added packages SDK Involves changes to the thirdweb SDK labels Nov 19, 2024
Copy link
Contributor Author

kien-ngo commented Nov 19, 2024

@kien-ngo kien-ngo changed the title update [SDK] Add 2 new Pay functions: convertFiatToCrypto and convertCryptoToFiat Nov 19, 2024
Copy link
Contributor

github-actions bot commented Nov 19, 2024

size-limit report 📦

Path Size Loading time (3g) Running time (snapdragon) Total time
thirdweb (esm) 47.92 KB (0%) 959 ms (0%) 3.1 s (-28.04% 🔽) 4.1 s
thirdweb (cjs) 107.43 KB (+0.15% 🔺) 2.2 s (+0.15% 🔺) 7.2 s (+26.36% 🔺) 9.4 s
thirdweb (minimal + tree-shaking) 5.58 KB (0%) 112 ms (0%) 623 ms (+359.47% 🔺) 735 ms
thirdweb/chains (tree-shaking) 506 B (0%) 10 ms (0%) 41 ms (-17.55% 🔽) 51 ms
thirdweb/react (minimal + tree-shaking) 18.28 KB (0%) 366 ms (0%) 696 ms (+77.14% 🔺) 1.1 s

* The fiat symbol. e.g "usd"
* Only USD is supported at the moment.
*/
to: "usd";
Copy link
Member

Choose a reason for hiding this comment

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

is it usd lowecase or USD uppercase? I see different casing used in the comments / tests

for a currency I think it should be uppercase everywhere

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated to uppercase (although it's not case sensitive in the backend)

Comment on lines +71 to +90
// so we should do some basic input validations before sending the request

// Make sure it's a valid EVM address
if (!isAddress(fromTokenAddress)) {
throw new Error(
"Invalid fromTokenAddress. Expected a valid EVM contract address",
);
}
// Make sure it's either a valid contract or a native token address
if (fromTokenAddress.toLowerCase() !== NATIVE_TOKEN_ADDRESS.toLowerCase()) {
const bytecode = await getBytecode(
getContract({
address: fromTokenAddress,
chain,
client,
}),
).catch(() => undefined);
if (!bytecode || bytecode === "0x") {
throw new Error(
`Error: ${fromTokenAddress} on chainId: ${chain.id} is not a valid contract address.`,
Copy link

Choose a reason for hiding this comment

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

The contract validation logic could be extracted to a shared utility function used by both conversion files to reduce duplication

Spotted by Graphite Reviewer (based on CI logs)

Is this helpful? React 👍 or 👎 to let us know.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hmm maybe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DO NOT MERGE This pull request is still in progress and is not ready to be merged. packages SDK Involves changes to the thirdweb SDK
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants