-
Notifications
You must be signed in to change notification settings - Fork 387
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
195 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"thirdweb": minor | ||
--- | ||
|
||
Add 2 new Pay functions: convertFiatToCrypto and convertCryptoToFiat |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { TEST_CLIENT } from "~test/test-clients.js"; | ||
import { base } from "../../chains/chain-definitions/base.js"; | ||
import { NATIVE_TOKEN_ADDRESS } from "../../constants/addresses.js"; | ||
import { convertCryptoToFiat } from "./cryptoToFiat.js"; | ||
|
||
describe.runIf(process.env.TW_SECRET_KEY)("Pay: fiatToCrypto", () => { | ||
it("should convert ETH price to USD on Ethereum mainnet", async () => { | ||
const result = await convertCryptoToFiat({ | ||
chainId: 1, | ||
fromTokenAddress: NATIVE_TOKEN_ADDRESS, | ||
fromAmount: 2, | ||
to: "usd", | ||
client: TEST_CLIENT, | ||
}); | ||
expect(result).toBeDefined(); | ||
// Should be a number | ||
expect(!Number.isNaN(Number(result))).toBe(true); | ||
// Since eth is around US$3000, we can add a test to check if the price is greater than $1500 (as a safe margin) | ||
// let's hope that scenario does not happen :( | ||
expect(Number(result) > 1500).toBe(true); | ||
}); | ||
|
||
it("should convert ETH price to USD on Base mainnet", async () => { | ||
const result = await convertCryptoToFiat({ | ||
chainId: 1, | ||
fromTokenAddress: NATIVE_TOKEN_ADDRESS, | ||
fromAmount: base.id, | ||
to: "usd", | ||
client: TEST_CLIENT, | ||
}); | ||
expect(result).toBeDefined(); | ||
// Should be a number | ||
expect(!Number.isNaN(Number(result))).toBe(true); | ||
// Since eth is around US$3000, we can add a test to check if the price is greater than $1500 (as a safe margin) | ||
// let's hope that scenario does not happen :( | ||
expect(Number(result) > 1500).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import type { Address } from "abitype"; | ||
import type { ThirdwebClient } from "../../client/client.js"; | ||
import { getClientFetch } from "../../utils/fetch.js"; | ||
import { getPayConvertCryptoToFiatEndpoint } from "../utils/definitions.js"; | ||
|
||
export type ConvertCryptoToFiatParams = { | ||
client: ThirdwebClient; | ||
/** | ||
* The contract address of the token | ||
* For native token, use NATIVE_TOKEN_ADDRESS | ||
*/ | ||
fromTokenAddress: Address; | ||
fromAmount: number; | ||
/** | ||
* The chainId that the token is deployed to | ||
*/ | ||
chainId: number; | ||
/** | ||
* The fiat symbol. e.g "usd" | ||
*/ | ||
to: string; | ||
}; | ||
|
||
export async function convertCryptoToFiat(options: ConvertCryptoToFiatParams) { | ||
const { client, fromTokenAddress, to, chainId, fromAmount } = options; | ||
try { | ||
const queryString = new URLSearchParams({ | ||
fromTokenAddress, | ||
to, | ||
chainId: String(chainId), | ||
fromAmount: String(fromAmount), | ||
}).toString(); | ||
const url = `${getPayConvertCryptoToFiatEndpoint()}?${queryString}`; | ||
const response = await getClientFetch(client)(url); | ||
// Assuming the response directly matches the BuyWithCryptoStatus interface | ||
if (!response.ok) { | ||
response.body?.cancel(); | ||
throw new Error(`HTTP error! status: ${response.status}`); | ||
} | ||
|
||
const data: string = (await response.json()).result; | ||
return data; | ||
} catch (error) { | ||
console.error("Fetch error:", error); | ||
throw new Error(`Fetch failed: ${error}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { TEST_CLIENT } from "~test/test-clients.js"; | ||
import { base } from "../../chains/chain-definitions/base.js"; | ||
import { NATIVE_TOKEN_ADDRESS } from "../../constants/addresses.js"; | ||
import { convertFiatToCrypto } from "./fiatToCrypto.js"; | ||
|
||
describe.runIf(process.env.TW_SECRET_KEY)("Pay: fiatToCrypto", () => { | ||
it("should convert fiat price to token on Ethereum mainnet", async () => { | ||
const result = await convertFiatToCrypto({ | ||
chainId: 1, | ||
from: "usd", | ||
fromAmount: 1, | ||
to: NATIVE_TOKEN_ADDRESS, | ||
client: TEST_CLIENT, | ||
}); | ||
expect(result).toBeDefined(); | ||
// Should be a number | ||
expect(!Number.isNaN(Number(result))).toBe(true); | ||
// Since eth is around US$3000, 1 USD should be around 0.0003 | ||
// we give it some safe margin so the test won't be flaky | ||
expect(Number(result) < 0.001).toBe(true); | ||
}); | ||
|
||
it("should convert fiat price to token on Base mainnet", async () => { | ||
const result = await convertFiatToCrypto({ | ||
chainId: base.id, | ||
from: "usd", | ||
fromAmount: 1, | ||
to: NATIVE_TOKEN_ADDRESS, | ||
client: TEST_CLIENT, | ||
}); | ||
|
||
expect(result).toBeDefined(); | ||
// Should be a number | ||
expect(!Number.isNaN(Number(result))).toBe(true); | ||
// Since eth is around US$3000, 1 USD should be around 0.0003 | ||
// we give it some safe margin so the test won't be flaky | ||
expect(Number(result) < 0.001).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import type { Address } from "abitype"; | ||
import type { ThirdwebClient } from "../../client/client.js"; | ||
import { getClientFetch } from "../../utils/fetch.js"; | ||
import { getPayConvertFiatToCryptoEndpoint } from "../utils/definitions.js"; | ||
|
||
export type ConvertFiatToCryptoParams = { | ||
client: ThirdwebClient; | ||
/** | ||
* The fiat symbol. e.g: "usd" | ||
*/ | ||
from: string; | ||
fromAmount: number; | ||
/** | ||
* The token address | ||
* For native token, use NATIVE_TOKEN_ADDRESS | ||
*/ | ||
to: Address; | ||
/** | ||
* The chainId that the token is deployed to | ||
*/ | ||
chainId: number; | ||
}; | ||
|
||
export async function convertFiatToCrypto(options: ConvertFiatToCryptoParams) { | ||
const { client, from, to, chainId, fromAmount } = options; | ||
try { | ||
const queryString = new URLSearchParams({ | ||
from, | ||
to, | ||
chainId: String(chainId), | ||
fromAmount: String(fromAmount), | ||
}).toString(); | ||
const url = `${getPayConvertFiatToCryptoEndpoint()}?${queryString}`; | ||
const response = await getClientFetch(client)(url); | ||
// Assuming the response directly matches the BuyWithCryptoStatus interface | ||
if (!response.ok) { | ||
response.body?.cancel(); | ||
throw new Error(`HTTP error! status: ${response.status}`); | ||
} | ||
|
||
const data: string = (await response.json()).result; | ||
return data; | ||
} catch (error) { | ||
console.error("Fetch error:", error); | ||
throw new Error(`Fetch failed: ${error}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters