|
| 1 | +import type { Hex as ox__Hex } from "ox"; |
| 2 | +import type { ThirdwebClient } from "../client/client.js"; |
| 3 | +import { getThirdwebBaseUrl } from "../utils/domains.js"; |
| 4 | +import { getClientFetch } from "../utils/fetch.js"; |
1 | 5 |
|
| 6 | +/** |
| 7 | + * Retrieves the status of an Onramp session created via {@link Bridge.Onramp.prepare}. The |
| 8 | + * status will include any on-chain transactions that have occurred as a result of the onramp |
| 9 | + * as well as any arbitrary `purchaseData` that was supplied when the onramp was |
| 10 | + * prepared. |
| 11 | + * |
| 12 | + * @example |
| 13 | + * ```typescript |
| 14 | + * import { Bridge } from "thirdweb"; |
| 15 | + * |
| 16 | + * const onrampStatus = await Bridge.Onramp.status({ |
| 17 | + * id: "022218cc-96af-4291-b90c-dadcb47571ec", |
| 18 | + * client: thirdwebClient, |
| 19 | + * }); |
| 20 | + * |
| 21 | + * // Possible results: |
| 22 | + * // { |
| 23 | + * // status: "CREATED", |
| 24 | + * // transactions: [], |
| 25 | + * // purchaseData: { |
| 26 | + * // orderId: "abc-123", |
| 27 | + * // }, |
| 28 | + * // } |
| 29 | + * // |
| 30 | + * // or |
| 31 | + * // { |
| 32 | + * // status: "PENDING", |
| 33 | + * // transactions: [], |
| 34 | + * // purchaseData: { |
| 35 | + * // orderId: "abc-123", |
| 36 | + * // }, |
| 37 | + * // } |
| 38 | + * // |
| 39 | + * // or |
| 40 | + * // { |
| 41 | + * // status: "COMPLETED", |
| 42 | + * // transactions: [ |
| 43 | + * // { |
| 44 | + * // chainId: 1, |
| 45 | + * // transactionHash: |
| 46 | + * // "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef", |
| 47 | + * // }, |
| 48 | + * // ], |
| 49 | + * // purchaseData: { |
| 50 | + * // orderId: "abc-123", |
| 51 | + * // }, |
| 52 | + * // } |
| 53 | + * ``` |
| 54 | + * |
| 55 | + * @param options - The options for fetching the onramp status. |
| 56 | + * @param options.id - The UUID returned from {@link Bridge.Onramp.prepare}. |
| 57 | + * @param options.client - Your thirdweb client instance. |
| 58 | + * |
| 59 | + * @returns A promise that resolves to the status of the onramp session. |
| 60 | + * |
| 61 | + * @throws Will throw an error if there is an issue fetching the status. |
| 62 | + * @bridge Onramp |
| 63 | + * @beta |
| 64 | + */ |
| 65 | +export async function status(options: status.Options): Promise<status.Result> { |
| 66 | + const { id, client } = options; |
| 67 | + |
| 68 | + const clientFetch = getClientFetch(client); |
| 69 | + const url = new URL(`${getThirdwebBaseUrl("bridge")}/v1/onramp/status`); |
| 70 | + url.searchParams.set("id", id); |
| 71 | + |
| 72 | + const response = await clientFetch(url.toString()); |
| 73 | + if (!response.ok) { |
| 74 | + const errorJson = await response.json(); |
| 75 | + throw new Error( |
| 76 | + `${errorJson.code || response.status} | ${errorJson.message || response.statusText} - ${errorJson.correlationId || "N/A"}`, |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + const { data }: { data: status.Result } = await response.json(); |
| 81 | + return data; |
| 82 | +} |
| 83 | + |
| 84 | +export declare namespace status { |
| 85 | + /** |
| 86 | + * Input parameters for {@link Bridge.Onramp.status}. |
| 87 | + */ |
| 88 | + export type Options = { |
| 89 | + /** |
| 90 | + * The Onramp session ID returned by {@link Bridge.Onramp.prepare}. |
| 91 | + */ |
| 92 | + id: string; |
| 93 | + /** Your {@link ThirdwebClient} instance. */ |
| 94 | + client: ThirdwebClient; |
| 95 | + }; |
| 96 | + |
| 97 | + /** |
| 98 | + * The result returned from {@link Bridge.Onramp.status}. |
| 99 | + */ |
| 100 | + export type Result = |
| 101 | + | { |
| 102 | + status: "COMPLETED"; |
| 103 | + transactions: Array<{ |
| 104 | + chainId: number; |
| 105 | + transactionHash: ox__Hex.Hex; |
| 106 | + }>; |
| 107 | + purchaseData?: unknown; |
| 108 | + } |
| 109 | + | { |
| 110 | + status: "PENDING"; |
| 111 | + transactions: Array<{ |
| 112 | + chainId: number; |
| 113 | + transactionHash: ox__Hex.Hex; |
| 114 | + }>; |
| 115 | + purchaseData?: unknown; |
| 116 | + } |
| 117 | + | { |
| 118 | + status: "CREATED"; |
| 119 | + transactions: Array<{ |
| 120 | + chainId: number; |
| 121 | + transactionHash: ox__Hex.Hex; |
| 122 | + }>; |
| 123 | + purchaseData?: unknown; |
| 124 | + } |
| 125 | + | { |
| 126 | + status: "FAILED"; |
| 127 | + transactions: Array<{ |
| 128 | + chainId: number; |
| 129 | + transactionHash: ox__Hex.Hex; |
| 130 | + }>; |
| 131 | + purchaseData?: unknown; |
| 132 | + }; |
| 133 | +} |
0 commit comments