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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/dark-dancers-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Optimize fiat conversion query
19 changes: 1 addition & 18 deletions packages/thirdweb/src/pay/convert/cryptoToFiat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import { TEST_CLIENT } from "~test/test-clients.js";
import { base } from "../../chains/chain-definitions/base.js";
import { ethereum } from "../../chains/chain-definitions/ethereum.js";
import { sepolia } from "../../chains/chain-definitions/sepolia.js";
import {
NATIVE_TOKEN_ADDRESS,
ZERO_ADDRESS,
} from "../../constants/addresses.js";
import { NATIVE_TOKEN_ADDRESS } from "../../constants/addresses.js";
import { convertCryptoToFiat } from "./cryptoToFiat.js";

describe.runIf(process.env.TW_SECRET_KEY)("Pay: crypto-to-fiat", () => {
Expand Down Expand Up @@ -78,18 +75,4 @@ describe.runIf(process.env.TW_SECRET_KEY)("Pay: crypto-to-fiat", () => {
"Invalid fromTokenAddress. Expected a valid EVM contract address",
);
});

it("should throw error if fromTokenAddress is set to a wallet address", async () => {
await expect(
convertCryptoToFiat({
chain: base,
fromTokenAddress: ZERO_ADDRESS,
fromAmount: 1,
to: "USD",
client: TEST_CLIENT,
}),
).rejects.toThrowError(
`Error: ${ZERO_ADDRESS} on chainId: ${base.id} is not a valid contract address.`,
);
});
});
19 changes: 0 additions & 19 deletions packages/thirdweb/src/pay/convert/cryptoToFiat.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import type { Address } from "abitype";
import type { Chain } from "../../chains/types.js";
import type { ThirdwebClient } from "../../client/client.js";
import { NATIVE_TOKEN_ADDRESS } from "../../constants/addresses.js";
import { getBytecode } from "../../contract/actions/get-bytecode.js";
import { getContract } from "../../contract/contract.js";
import { isAddress } from "../../utils/address.js";
import { getTokenPrice } from "./get-token.js";
import type { SupportedFiatCurrency } from "./type.js";
Expand Down Expand Up @@ -76,22 +73,6 @@ export async function convertCryptoToFiat(
"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.`,
);
}
}

const price = await getTokenPrice(client, fromTokenAddress, chain.id);
if (!price) {
throw new Error(
Expand Down
19 changes: 1 addition & 18 deletions packages/thirdweb/src/pay/convert/fiatToCrypto.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import { TEST_CLIENT } from "~test/test-clients.js";
import { base } from "../../chains/chain-definitions/base.js";
import { ethereum } from "../../chains/chain-definitions/ethereum.js";
import { sepolia } from "../../chains/chain-definitions/sepolia.js";
import {
NATIVE_TOKEN_ADDRESS,
ZERO_ADDRESS,
} from "../../constants/addresses.js";
import { NATIVE_TOKEN_ADDRESS } from "../../constants/addresses.js";
import { convertFiatToCrypto } from "./fiatToCrypto.js";

describe.runIf(process.env.TW_SECRET_KEY)("Pay: fiatToCrypto", () => {
Expand Down Expand Up @@ -81,18 +78,4 @@ describe.runIf(process.env.TW_SECRET_KEY)("Pay: fiatToCrypto", () => {
"Invalid `to`. Expected a valid EVM contract address",
);
});

it("should throw error if `to` is set to a wallet address", async () => {
await expect(
convertFiatToCrypto({
chain: base,
to: ZERO_ADDRESS,
fromAmount: 1,
from: "USD",
client: TEST_CLIENT,
}),
).rejects.toThrowError(
`Error: ${ZERO_ADDRESS} on chainId: ${base.id} is not a valid contract address.`,
);
});
});
18 changes: 0 additions & 18 deletions packages/thirdweb/src/pay/convert/fiatToCrypto.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import type { Address } from "abitype";
import type { Chain } from "../../chains/types.js";
import type { ThirdwebClient } from "../../client/client.js";
import { NATIVE_TOKEN_ADDRESS } from "../../constants/addresses.js";
import { getBytecode } from "../../contract/actions/get-bytecode.js";
import { getContract } from "../../contract/contract.js";
import { isAddress } from "../../utils/address.js";
import { getTokenPrice } from "./get-token.js";
import type { SupportedFiatCurrency } from "./type.js";
Expand Down Expand Up @@ -75,21 +72,6 @@ export async function convertFiatToCrypto(
if (!isAddress(to)) {
throw new Error("Invalid `to`. Expected a valid EVM contract address");
}
// Make sure it's either a valid contract or a native token
if (to.toLowerCase() !== NATIVE_TOKEN_ADDRESS.toLowerCase()) {
const bytecode = await getBytecode(
getContract({
address: to,
chain,
client,
}),
).catch(() => undefined);
if (!bytecode || bytecode === "0x") {
throw new Error(
`Error: ${to} on chainId: ${chain.id} is not a valid contract address.`,
);
}
}
const price = await getTokenPrice(client, to, chain.id);
if (!price || price === 0) {
throw new Error(
Expand Down
Loading