|
| 1 | +import { |
| 2 | + type TransactionsFilterNested, |
| 3 | + type TransactionsFilterValue, |
| 4 | + searchTransactions as engineSearchTransactions, |
| 5 | +} from "@thirdweb-dev/engine"; |
| 6 | +import type { ThirdwebClient } from "../client/client.js"; |
| 7 | +import { getThirdwebBaseUrl } from "../utils/domains.js"; |
| 8 | +import { getClientFetch } from "../utils/fetch.js"; |
| 9 | +import { stringify } from "../utils/json.js"; |
| 10 | + |
| 11 | +export type SearchTransactionsArgs = { |
| 12 | + client: ThirdwebClient; |
| 13 | + filters?: (TransactionsFilterValue | TransactionsFilterNested)[]; |
| 14 | + pageSize?: number; |
| 15 | + page?: number; |
| 16 | +}; |
| 17 | + |
| 18 | +/** |
| 19 | + * Search for transactions by their ids. |
| 20 | + * @param args - The arguments for the search. |
| 21 | + * @param args.client - The thirdweb client to use. |
| 22 | + * @param args.transactionIds - The ids of the transactions to search for. |
| 23 | + * @engine |
| 24 | + * @example |
| 25 | + * ## Search for transactions by their ids |
| 26 | + * |
| 27 | + * ```ts |
| 28 | + * import { Engine } from "thirdweb"; |
| 29 | + * |
| 30 | + * const transactions = await Engine.searchTransactions({ |
| 31 | + * client, |
| 32 | + * filters: [ |
| 33 | + * { |
| 34 | + * field: "id", |
| 35 | + * values: ["1", "2", "3"], |
| 36 | + * operation: "OR", |
| 37 | + * }, |
| 38 | + * ], |
| 39 | + * }); |
| 40 | + * console.log(transactions); |
| 41 | + * ``` |
| 42 | + * |
| 43 | + * ## Search for transactions by chain id |
| 44 | + * |
| 45 | + * ```ts |
| 46 | + * import { Engine } from "thirdweb"; |
| 47 | + * |
| 48 | + * const transactions = await Engine.searchTransactions({ |
| 49 | + * client, |
| 50 | + * filters: [ |
| 51 | + * { |
| 52 | + * field: "chainId", |
| 53 | + * values: ["1", "137"], |
| 54 | + * operation: "OR", |
| 55 | + * }, |
| 56 | + * ], |
| 57 | + * }); |
| 58 | + * console.log(transactions); |
| 59 | + * ``` |
| 60 | + * |
| 61 | + * ## Search for transactions by sender wallet address |
| 62 | + * |
| 63 | + * ```ts |
| 64 | + * import { Engine } from "thirdweb"; |
| 65 | + * |
| 66 | + * const transactions = await Engine.searchTransactions({ |
| 67 | + * client, |
| 68 | + * filters: [ |
| 69 | + * { |
| 70 | + * field: "from", |
| 71 | + * values: ["0x1234567890123456789012345678901234567890"], |
| 72 | + * operation: "OR", |
| 73 | + * }, |
| 74 | + * ], |
| 75 | + * }); |
| 76 | + * console.log(transactions); |
| 77 | + * ``` |
| 78 | + * |
| 79 | + * ## Combined search |
| 80 | + * |
| 81 | + * ```ts |
| 82 | + * import { Engine } from "thirdweb"; |
| 83 | + * |
| 84 | + * const transactions = await Engine.searchTransactions({ |
| 85 | + * client, |
| 86 | + * filters: [ |
| 87 | + * { |
| 88 | + * filters: [ |
| 89 | + * { |
| 90 | + * field: "from", |
| 91 | + * values: ["0x1234567890123456789012345678901234567890"], |
| 92 | + * operation: "OR", |
| 93 | + * }, |
| 94 | + * { |
| 95 | + * field: "chainId", |
| 96 | + * values: ["8453"], |
| 97 | + * operation: "OR", |
| 98 | + * }, |
| 99 | + * ], |
| 100 | + * operation: "AND", |
| 101 | + * }, |
| 102 | + * ], |
| 103 | + * pageSize: 100, |
| 104 | + * page: 0, |
| 105 | + * }); |
| 106 | + * console.log(transactions); |
| 107 | + * ``` |
| 108 | + */ |
| 109 | +export async function searchTransactions(args: SearchTransactionsArgs) { |
| 110 | + const { client, filters, pageSize = 100, page = 1 } = args; |
| 111 | + const searchResult = await engineSearchTransactions({ |
| 112 | + baseUrl: getThirdwebBaseUrl("engineCloud"), |
| 113 | + bodySerializer: stringify, |
| 114 | + fetch: getClientFetch(client), |
| 115 | + body: { |
| 116 | + filters, |
| 117 | + limit: pageSize, |
| 118 | + page, |
| 119 | + }, |
| 120 | + }); |
| 121 | + |
| 122 | + if (searchResult.error) { |
| 123 | + throw new Error( |
| 124 | + `Error searching for transaction with filters ${stringify(filters)}: ${stringify( |
| 125 | + searchResult.error, |
| 126 | + )}`, |
| 127 | + ); |
| 128 | + } |
| 129 | + |
| 130 | + const data = searchResult.data?.result; |
| 131 | + |
| 132 | + if (!data) { |
| 133 | + throw new Error(`No transactions found with filters ${stringify(filters)}`); |
| 134 | + } |
| 135 | + |
| 136 | + return data; |
| 137 | +} |
0 commit comments