Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 27 additions & 18 deletions apps/proxy/src/routes/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,24 +192,33 @@ async function proxyHandler(c: Context) {
console.log('[Proxy Data]', { cleanedTargetUrl, data });

// Insert into ClickHouse (non-blocking, fire-and-forget)
void insertResourceInvocation({
id: randomUUID(),
resource_id: null, // TODO: lookup resourceId by URL if needed
status_code: clonedUpstreamResponse.status,
duration: fetchDuration,
status_text: clonedUpstreamResponse.statusText,
method,
url: targetUrl.toString(),
request_content_type:
clonedRequest.headers.get('content-type') ?? '',
response_content_type:
clonedUpstreamResponse.headers.get('content-type') ?? '',
response_headers: responseHeaders,
response_body: responseBody,
request_headers: requestHeaders,
request_body: requestBody,
created_at: new Date(),
});
// Skip insertion if skip_tracking is enabled (i.e developer/test mode)
const skipTracking = c.req.query('skip_tracking') === 'true';
if (!skipTracking) {
void insertResourceInvocation({
id: randomUUID(),
resource_id: null, // TODO: lookup resourceId by URL if needed
status_code: clonedUpstreamResponse.status,
duration: fetchDuration,
status_text: clonedUpstreamResponse.statusText,
method,
url: targetUrl.toString(),
request_content_type:
clonedRequest.headers.get('content-type') ?? '',
response_content_type:
clonedUpstreamResponse.headers.get('content-type') ?? '',
response_headers: responseHeaders,
response_body: responseBody,
request_headers: requestHeaders,
request_body: requestBody,
created_at: new Date(),
});
} else {
console.log('Proxy Skip Tracking', {
url: targetUrl.toString(),
reason: 'skip_tracking parameter enabled',
});
}
}
} catch (error) {
console.error('[Proxy After Error]', {
Expand Down
3 changes: 3 additions & 0 deletions apps/scan/src/app/_components/resource-fetch/chains/evm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ interface Props<TData = unknown> {
options?: Omit<UseMutationOptions<X402FetchResponse<TData>>, 'mutationFn'>;
isTool?: boolean;
text?: string;
skipTracking?: boolean;
}

export const FetchEvm: React.FC<Props> = ({
Expand All @@ -37,6 +38,7 @@ export const FetchEvm: React.FC<Props> = ({
options,
isTool = false,
text,
skipTracking = false,
}) => {
const { data: walletClient, isLoading: isLoadingWalletClient } =
useWalletClient();
Expand All @@ -49,6 +51,7 @@ export const FetchEvm: React.FC<Props> = ({
init: typeof requestInit === 'function' ? requestInit(chain) : requestInit,
options,
isTool,
skipTracking,
});

const { data: balance, isLoading: isLoadingBalance } = useEvmTokenBalance({
Expand Down
5 changes: 5 additions & 0 deletions apps/scan/src/app/_components/resource-fetch/chains/svm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ interface Props<TData = unknown> {
options?: Omit<UseMutationOptions<X402FetchResponse<TData>>, 'mutationFn'>;
isTool?: boolean;
text?: string;
skipTracking?: boolean;
}

export const FetchSvm: React.FC<Props> = ({
Expand All @@ -37,6 +38,7 @@ export const FetchSvm: React.FC<Props> = ({
options,
isTool = false,
text,
skipTracking = false,
}) => {
const { connectedWallet } = useSolanaWallet();

Expand Down Expand Up @@ -74,6 +76,7 @@ export const FetchSvm: React.FC<Props> = ({
options={options}
isTool={isTool}
text={text}
skipTracking={skipTracking}
/>
);
};
Expand All @@ -92,6 +95,7 @@ const FetchContent: React.FC<FetchContentProps> = ({
options,
isTool = false,
text,
skipTracking = false,
}) => {
const { mutate: execute, isPending } = useSvmX402Fetch({
account,
Expand All @@ -103,6 +107,7 @@ const FetchContent: React.FC<FetchContentProps> = ({
: requestInit,
options,
isTool,
skipTracking,
});
const { isInitialized } = useIsInitialized();

Expand Down
4 changes: 4 additions & 0 deletions apps/scan/src/app/_components/resource-fetch/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface Props<TData = unknown> {
options?: Omit<UseMutationOptions<X402FetchResponse<TData>>, 'mutationFn'>;
isTool?: boolean;
text?: string;
skipTracking?: boolean;
}

export const ResourceFetch: React.FC<Props> = ({
Expand All @@ -29,6 +30,7 @@ export const ResourceFetch: React.FC<Props> = ({
options,
isTool = false,
text,
skipTracking = false,
}) => {
return (
<div
Expand All @@ -48,6 +50,7 @@ export const ResourceFetch: React.FC<Props> = ({
options={options}
isTool={isTool}
text={text}
skipTracking={skipTracking}
/>
) : (
<FetchEvm
Expand All @@ -60,6 +63,7 @@ export const ResourceFetch: React.FC<Props> = ({
options={options}
isTool={isTool}
text={text}
skipTracking={skipTracking}
/>
)
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ interface Props {
maxAmountRequired: bigint;
method: Methods;
resource: string;
skipTracking?: boolean;
}

export function Form({
Expand All @@ -51,6 +52,7 @@ export function Form({
maxAmountRequired,
method,
resource,
skipTracking = false,
}: Props) {
const queryFields = useMemo(
() => getFields(inputSchema.queryParams),
Expand Down Expand Up @@ -230,6 +232,7 @@ export function Form({
onSuccess: data => setData(data),
onError: error => setError(error),
}}
skipTracking={skipTracking}
/>

{error && (
Expand Down
3 changes: 3 additions & 0 deletions apps/scan/src/app/_components/resources/executor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ interface Props {
hideOrigin?: boolean;
defaultOpen?: boolean;
isFlat?: boolean;
skipTracking?: boolean;
}

export const ResourceExecutor: React.FC<Props> = ({
Expand All @@ -40,6 +41,7 @@ export const ResourceExecutor: React.FC<Props> = ({
className,
hideOrigin = false,
isFlat = false,
skipTracking = false,
}) => {
const { data: resourceMetrics } = api.public.resources.getMetrics.useQuery(
{
Expand Down Expand Up @@ -95,6 +97,7 @@ export const ResourceExecutor: React.FC<Props> = ({
maxAmountRequired={maxAmountRequired}
method={bazaarMethod}
resource={resource.resource}
skipTracking={skipTracking}
/>
</AccordionContent>
</Card>
Expand Down
1 change: 1 addition & 0 deletions apps/scan/src/app/_hooks/x402/evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface UseEvmX402FetchParams<TData = unknown> {
init?: RequestInit;
options?: Omit<UseMutationOptions<X402FetchResponse<TData>>, 'mutationFn'>;
isTool?: boolean;
skipTracking?: boolean;
}

export const useEvmX402Fetch = <TData = unknown>({
Expand Down
1 change: 1 addition & 0 deletions apps/scan/src/app/_hooks/x402/svm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface UseSvmX402FetchParams<TData = unknown> {
init?: RequestInit;
options?: Omit<UseMutationOptions<X402FetchResponse<TData>>, 'mutationFn'>;
isTool?: boolean;
skipTracking?: boolean;
}

export const useSvmX402Fetch = <TData = unknown>({
Expand Down
7 changes: 6 additions & 1 deletion apps/scan/src/app/_hooks/x402/use-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ interface UseX402FetchParams<TData = unknown> {
init?: RequestInit;
options?: Omit<UseMutationOptions<X402FetchResponse<TData>>, 'mutationFn'>;
isTool?: boolean;
skipTracking?: boolean;
}

export const useX402Fetch = <TData = unknown>({
Expand All @@ -21,11 +22,15 @@ export const useX402Fetch = <TData = unknown>({
init,
options,
isTool = false,
skipTracking = false,
}: UseX402FetchParams<TData>) => {
return useMutation({
mutationFn: async () => {
const fetchWithPayment = wrapperFn(
isTool ? fetch : fetchWithProxy,
isTool
? fetch
: (url: string | URL | Request, init?: RequestInit) =>
fetchWithProxy(url, init, { skipTracking }),
value
);
const response = await fetchWithPayment(targetUrl, init);
Expand Down
1 change: 1 addition & 0 deletions apps/scan/src/app/developer/_components/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ export const TestEndpointForm = () => {
}
hideOrigin
isFlat
skipTracking={true}
/>
))}
</Accordion>
Expand Down
6 changes: 5 additions & 1 deletion apps/scan/src/lib/x402/proxy-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const PROXY_ENDPOINT = '/api/proxy' as const;

export const fetchWithProxy = async (
input: URL | RequestInfo,
requestInit?: RequestInit
requestInit?: RequestInit,
options?: { skipTracking?: boolean }
) => {
let url: string;
if (input instanceof Request) {
Expand All @@ -15,6 +16,9 @@ export const fetchWithProxy = async (
const proxyUrl = new URL(PROXY_ENDPOINT, env.NEXT_PUBLIC_PROXY_URL);
proxyUrl.searchParams.set('url', encodeURIComponent(url));
proxyUrl.searchParams.set('share_data', 'true');
if (options?.skipTracking) {
proxyUrl.searchParams.set('skip_tracking', 'true');
}

const { method = 'GET', ...restInit } = requestInit ?? {};
const normalizedMethod = method.toString().toUpperCase();
Expand Down
23 changes: 14 additions & 9 deletions apps/scan/src/trpc/routers/developer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { createTRPCRouter, publicProcedure } from '../trpc';

import { getOriginFromUrl } from '@/lib/url';
import { parseX402Response } from '@/lib/x402/schema';
import { fetchWithProxy } from '@/lib/x402/proxy-fetch';
import { scrapeOriginData } from '@/services/scraper';

export const developerRouter = createTRPCRouter({
Expand Down Expand Up @@ -56,15 +57,19 @@ export const developerRouter = createTRPCRouter({
.query(async ({ input }) => {
const { method, url, headers = {} } = input;

const response = await fetch(url, {
method,
headers:
method === 'POST'
? { ...headers, 'Content-Type': 'application/json' }
: headers,
body: method === 'POST' ? '{}' : undefined,
redirect: 'follow',
});
const response = await fetchWithProxy(
url,
{
method,
headers:
method === 'POST'
? { ...headers, 'Content-Type': 'application/json' }
: headers,
body: method === 'POST' ? '{}' : undefined,
redirect: 'follow',
},
{ skipTracking: true }
);

const text = await response.text();
let body: unknown = null;
Expand Down