Skip to content

Commit f10fbc8

Browse files
[SDK] Optimize fiat conversion query (#7242)
1 parent 38627d3 commit f10fbc8

File tree

5 files changed

+7
-73
lines changed

5 files changed

+7
-73
lines changed

.changeset/dark-dancers-hammer.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Optimize fiat conversion query

packages/thirdweb/src/pay/convert/cryptoToFiat.test.ts

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ import { TEST_CLIENT } from "~test/test-clients.js";
33
import { base } from "../../chains/chain-definitions/base.js";
44
import { ethereum } from "../../chains/chain-definitions/ethereum.js";
55
import { sepolia } from "../../chains/chain-definitions/sepolia.js";
6-
import {
7-
NATIVE_TOKEN_ADDRESS,
8-
ZERO_ADDRESS,
9-
} from "../../constants/addresses.js";
6+
import { NATIVE_TOKEN_ADDRESS } from "../../constants/addresses.js";
107
import { convertCryptoToFiat } from "./cryptoToFiat.js";
118

129
describe.runIf(process.env.TW_SECRET_KEY)("Pay: crypto-to-fiat", () => {
@@ -78,18 +75,4 @@ describe.runIf(process.env.TW_SECRET_KEY)("Pay: crypto-to-fiat", () => {
7875
"Invalid fromTokenAddress. Expected a valid EVM contract address",
7976
);
8077
});
81-
82-
it("should throw error if fromTokenAddress is set to a wallet address", async () => {
83-
await expect(
84-
convertCryptoToFiat({
85-
chain: base,
86-
fromTokenAddress: ZERO_ADDRESS,
87-
fromAmount: 1,
88-
to: "USD",
89-
client: TEST_CLIENT,
90-
}),
91-
).rejects.toThrowError(
92-
`Error: ${ZERO_ADDRESS} on chainId: ${base.id} is not a valid contract address.`,
93-
);
94-
});
9578
});

packages/thirdweb/src/pay/convert/cryptoToFiat.ts

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import type { Address } from "abitype";
22
import type { Chain } from "../../chains/types.js";
33
import type { ThirdwebClient } from "../../client/client.js";
4-
import { NATIVE_TOKEN_ADDRESS } from "../../constants/addresses.js";
5-
import { getBytecode } from "../../contract/actions/get-bytecode.js";
6-
import { getContract } from "../../contract/contract.js";
74
import { isAddress } from "../../utils/address.js";
85
import { getTokenPrice } from "./get-token.js";
96
import type { SupportedFiatCurrency } from "./type.js";
@@ -76,22 +73,6 @@ export async function convertCryptoToFiat(
7673
"Invalid fromTokenAddress. Expected a valid EVM contract address",
7774
);
7875
}
79-
// Make sure it's either a valid contract or a native token address
80-
if (fromTokenAddress.toLowerCase() !== NATIVE_TOKEN_ADDRESS.toLowerCase()) {
81-
const bytecode = await getBytecode(
82-
getContract({
83-
address: fromTokenAddress,
84-
chain,
85-
client,
86-
}),
87-
).catch(() => undefined);
88-
if (!bytecode || bytecode === "0x") {
89-
throw new Error(
90-
`Error: ${fromTokenAddress} on chainId: ${chain.id} is not a valid contract address.`,
91-
);
92-
}
93-
}
94-
9576
const price = await getTokenPrice(client, fromTokenAddress, chain.id);
9677
if (!price) {
9778
throw new Error(

packages/thirdweb/src/pay/convert/fiatToCrypto.test.ts

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ import { TEST_CLIENT } from "~test/test-clients.js";
33
import { base } from "../../chains/chain-definitions/base.js";
44
import { ethereum } from "../../chains/chain-definitions/ethereum.js";
55
import { sepolia } from "../../chains/chain-definitions/sepolia.js";
6-
import {
7-
NATIVE_TOKEN_ADDRESS,
8-
ZERO_ADDRESS,
9-
} from "../../constants/addresses.js";
6+
import { NATIVE_TOKEN_ADDRESS } from "../../constants/addresses.js";
107
import { convertFiatToCrypto } from "./fiatToCrypto.js";
118

129
describe.runIf(process.env.TW_SECRET_KEY)("Pay: fiatToCrypto", () => {
@@ -81,18 +78,4 @@ describe.runIf(process.env.TW_SECRET_KEY)("Pay: fiatToCrypto", () => {
8178
"Invalid `to`. Expected a valid EVM contract address",
8279
);
8380
});
84-
85-
it("should throw error if `to` is set to a wallet address", async () => {
86-
await expect(
87-
convertFiatToCrypto({
88-
chain: base,
89-
to: ZERO_ADDRESS,
90-
fromAmount: 1,
91-
from: "USD",
92-
client: TEST_CLIENT,
93-
}),
94-
).rejects.toThrowError(
95-
`Error: ${ZERO_ADDRESS} on chainId: ${base.id} is not a valid contract address.`,
96-
);
97-
});
9881
});

packages/thirdweb/src/pay/convert/fiatToCrypto.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import type { Address } from "abitype";
22
import type { Chain } from "../../chains/types.js";
33
import type { ThirdwebClient } from "../../client/client.js";
4-
import { NATIVE_TOKEN_ADDRESS } from "../../constants/addresses.js";
5-
import { getBytecode } from "../../contract/actions/get-bytecode.js";
6-
import { getContract } from "../../contract/contract.js";
74
import { isAddress } from "../../utils/address.js";
85
import { getTokenPrice } from "./get-token.js";
96
import type { SupportedFiatCurrency } from "./type.js";
@@ -75,21 +72,6 @@ export async function convertFiatToCrypto(
7572
if (!isAddress(to)) {
7673
throw new Error("Invalid `to`. Expected a valid EVM contract address");
7774
}
78-
// Make sure it's either a valid contract or a native token
79-
if (to.toLowerCase() !== NATIVE_TOKEN_ADDRESS.toLowerCase()) {
80-
const bytecode = await getBytecode(
81-
getContract({
82-
address: to,
83-
chain,
84-
client,
85-
}),
86-
).catch(() => undefined);
87-
if (!bytecode || bytecode === "0x") {
88-
throw new Error(
89-
`Error: ${to} on chainId: ${chain.id} is not a valid contract address.`,
90-
);
91-
}
92-
}
9375
const price = await getTokenPrice(client, to, chain.id);
9476
if (!price || price === 0) {
9577
throw new Error(

0 commit comments

Comments
 (0)