|
| 1 | +import { Static, Type } from "@sinclair/typebox"; |
| 2 | +import { TransactionErrorInfo } from "@thirdweb-dev/sdk"; |
| 3 | +import { UsageEvent } from "@thirdweb-dev/service-utils/cf-worker"; |
| 4 | +import { FastifyInstance } from "fastify"; |
| 5 | +import { contractParamSchema } from "../server/schemas/sharedApiSchemas"; |
| 6 | +import { walletParamSchema } from "../server/schemas/wallet"; |
| 7 | +import { getChainIdFromChain } from "../server/utils/chain"; |
| 8 | +import { deriveClientId } from "./api-keys"; |
| 9 | +import { env } from "./env"; |
| 10 | +import { logger } from "./logger"; |
| 11 | + |
| 12 | +type CreateHeaderForRequestParams = { |
| 13 | + clientId: string; |
| 14 | + backendwalletAddress?: string; |
| 15 | + chainId?: string; |
| 16 | +}; |
| 17 | + |
| 18 | +export interface ReportUsageParams { |
| 19 | + action: UsageEventTxActionEnum; |
| 20 | + input: { |
| 21 | + chainId?: string; |
| 22 | + fromAddress?: string; |
| 23 | + toAddress?: string; |
| 24 | + value?: string; |
| 25 | + transactionHash?: string; |
| 26 | + onChainTxStatus?: number; |
| 27 | + userOpHash?: string; |
| 28 | + functionName?: string; |
| 29 | + extension?: string; |
| 30 | + retryCount?: number; |
| 31 | + provider?: string; |
| 32 | + transactionValue?: string; |
| 33 | + msSinceQueue?: number; |
| 34 | + msSinceSend?: number; |
| 35 | + }; |
| 36 | + error?: TransactionErrorInfo; |
| 37 | +} |
| 38 | + |
| 39 | +const EngineRequestParams = Type.Object({ |
| 40 | + ...contractParamSchema.properties, |
| 41 | + ...walletParamSchema.properties, |
| 42 | +}); |
| 43 | + |
| 44 | +export enum UsageEventTxActionEnum { |
| 45 | + MineTx = "mine_tx", |
| 46 | + NotSendTx = "not_send_tx", |
| 47 | + QueueTx = "queue_tx", |
| 48 | + SendTx = "send_tx", |
| 49 | + CancelTx = "cancel_tx", |
| 50 | + APIRequest = "api_request", |
| 51 | +} |
| 52 | + |
| 53 | +interface UsageEventSchema extends Omit<UsageEvent, "action"> { |
| 54 | + action: UsageEventTxActionEnum; |
| 55 | +} |
| 56 | + |
| 57 | +const createHeaderForRequest = (input: CreateHeaderForRequestParams) => { |
| 58 | + return { |
| 59 | + "Content-Type": "application/json", |
| 60 | + "x-sdk-version": process.env.ENGINE_VERSION, |
| 61 | + "x-product-name": "engine", |
| 62 | + "x-client-id": input.clientId, |
| 63 | + } as HeadersInit; |
| 64 | +}; |
| 65 | + |
| 66 | +export const withServerUsageReporting = (server: FastifyInstance) => { |
| 67 | + server.addHook("onResponse", async (request, reply) => { |
| 68 | + try { |
| 69 | + // If the CLIENT_ANALYTICS_URL is not set, then we don't want to report usage |
| 70 | + if (env.CLIENT_ANALYTICS_URL === "") { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + const derivedClientId = deriveClientId(env.THIRDWEB_API_SECRET_KEY); |
| 75 | + const headers = createHeaderForRequest({ |
| 76 | + clientId: derivedClientId, |
| 77 | + }); |
| 78 | + |
| 79 | + const requestParams = request?.params as Static< |
| 80 | + typeof EngineRequestParams |
| 81 | + >; |
| 82 | + |
| 83 | + const chainId = requestParams?.chain |
| 84 | + ? await getChainIdFromChain(requestParams.chain) |
| 85 | + : ""; |
| 86 | + |
| 87 | + if (reply.request.routerPath === "" || !reply.request.routerPath) { |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + const requestBody: UsageEventSchema = { |
| 92 | + source: "engine", |
| 93 | + action: UsageEventTxActionEnum.APIRequest, |
| 94 | + clientId: derivedClientId, |
| 95 | + pathname: reply.request.routerPath, |
| 96 | + chainId: chainId || undefined, |
| 97 | + walletAddress: requestParams.walletAddress || undefined, |
| 98 | + contractAddress: requestParams.contractAddress || undefined, |
| 99 | + httpStatusCode: reply.statusCode, |
| 100 | + msTotalDuration: Math.ceil(reply.getResponseTime()), |
| 101 | + }; |
| 102 | + |
| 103 | + fetch(env.CLIENT_ANALYTICS_URL, { |
| 104 | + method: "POST", |
| 105 | + headers, |
| 106 | + body: JSON.stringify(requestBody), |
| 107 | + }); |
| 108 | + } catch (e) {} |
| 109 | + }); |
| 110 | +}; |
| 111 | + |
| 112 | +export const reportUsage = (usageParams: ReportUsageParams[]) => { |
| 113 | + try { |
| 114 | + usageParams.map(async (item) => { |
| 115 | + try { |
| 116 | + // If the CLIENT_ANALYTICS_URL is not set, then we don't want to report usage |
| 117 | + if (env.CLIENT_ANALYTICS_URL === "") { |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + const derivedClientId = deriveClientId(env.THIRDWEB_API_SECRET_KEY); |
| 122 | + const headers = createHeaderForRequest({ |
| 123 | + clientId: derivedClientId, |
| 124 | + }); |
| 125 | + |
| 126 | + const chainId = item.input.chainId |
| 127 | + ? parseInt(item.input.chainId) |
| 128 | + : undefined; |
| 129 | + const requestBody: UsageEventSchema = { |
| 130 | + source: "engine", |
| 131 | + action: item.action, |
| 132 | + clientId: derivedClientId, |
| 133 | + chainId, |
| 134 | + walletAddress: item.input.fromAddress || undefined, |
| 135 | + contractAddress: item.input.toAddress || undefined, |
| 136 | + transactionValue: item.input.value || undefined, |
| 137 | + transactionHash: item.input.transactionHash || undefined, |
| 138 | + userOpHash: item.input.userOpHash || undefined, |
| 139 | + errorCode: item.input.onChainTxStatus |
| 140 | + ? item.input.onChainTxStatus === 0 |
| 141 | + ? "EXECUTION_REVERTED" |
| 142 | + : undefined |
| 143 | + : item.error?.reason || undefined, |
| 144 | + functionName: item.input.functionName || undefined, |
| 145 | + extension: item.input.extension || undefined, |
| 146 | + retryCount: item.input.retryCount || undefined, |
| 147 | + provider: item.input.provider || undefined, |
| 148 | + msSinceSend: item.input.msSinceSend || undefined, |
| 149 | + msSinceQueue: item.input.msSinceQueue || undefined, |
| 150 | + }; |
| 151 | + |
| 152 | + fetch(env.CLIENT_ANALYTICS_URL, { |
| 153 | + method: "POST", |
| 154 | + headers, |
| 155 | + body: JSON.stringify(requestBody), |
| 156 | + }); |
| 157 | + } catch (e) {} |
| 158 | + }); |
| 159 | + } catch (error) { |
| 160 | + logger({ |
| 161 | + service: "worker", |
| 162 | + level: "error", |
| 163 | + message: `Error:`, |
| 164 | + error, |
| 165 | + }); |
| 166 | + } |
| 167 | +}; |
0 commit comments