Skip to content
Merged
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
16 changes: 12 additions & 4 deletions app/api/x402/image/generate/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,17 @@ export async function GET(request: NextRequest) {
);
} catch (error) {
console.error("Error generating image:", error);
return NextResponse.json(error, {
status: 500,
headers: getCorsHeaders(),
});

const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";

return NextResponse.json(
{
error: errorMessage,
},
{
status: 500,
headers: getCorsHeaders(),
},
);
Comment thread
sweetmantech marked this conversation as resolved.
}
}
2 changes: 1 addition & 1 deletion lib/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ export const ACCOUNT_ADDRESS = "0x7AfB9872Ea382B7Eb01c67B6884dD99A744eA64f" as A
export const SMART_ACCOUNT_ADDRESS = "0xbAf31935ED514e8F7da81D0A730AB5362DEEEEb7" as Address;
export const USDC_ADDRESS = "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913" as Address;
export const PAYMASTER_URL = `https://api.developer.coinbase.com/rpc/v1/base/${process.env.PAYMASTER_KEY}`;
export const IMAGE_GENERATE_PRICE = "0.01111";
export const IMAGE_GENERATE_PRICE = "0.15";
12 changes: 10 additions & 2 deletions lib/x402/fetchWithPayment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { toAccount } from "viem/accounts";
import { getAccount } from "@/lib/coinbase/getAccount";
import { deductCredits } from "../credits/deductCredits";
import { loadAccount } from "./loadAccount";
import { getCreditsForPrice } from "./getCreditsForPrice";
import { IMAGE_GENERATE_PRICE } from "@/lib/const";
import { parseUnits } from "viem";

/**
* Fetches a URL with x402 payment handling.
Expand All @@ -13,9 +16,14 @@ import { loadAccount } from "./loadAccount";
*/
export async function fetchWithPayment(url: string, accountId: string): Promise<Response> {
const account = await getAccount(accountId);
await deductCredits({ accountId, creditsToDeduct: 1 });
const creditsToDeduct = getCreditsForPrice(IMAGE_GENERATE_PRICE);
await deductCredits({ accountId, creditsToDeduct });
Comment thread
sweetmantech marked this conversation as resolved.
await loadAccount(account.address);
Comment thread
sweetmantech marked this conversation as resolved.
const fetchWithPaymentWrapper = wrapFetchWithPayment(fetch, toAccount(account));
const fetchWithPaymentWrapper = wrapFetchWithPayment(
fetch,
toAccount(account),
parseUnits(IMAGE_GENERATE_PRICE, 6),
);
Comment thread
sweetmantech marked this conversation as resolved.
return fetchWithPaymentWrapper(url, {
method: "GET",
headers: {
Expand Down
16 changes: 16 additions & 0 deletions lib/x402/getCreditsForPrice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Converts a price string to the number of credits required.
* 1 credit = $0.01, rounded up to the nearest credit.
*
* @param price - The price string (e.g., "$0.01" or "0.01").
* @returns The number of credits required, rounded up.
* @throws Error if the price string is invalid or cannot be parsed.
*/
export function getCreditsForPrice(price: string): number {
const priceNumber = parseFloat(price);
if (isNaN(priceNumber) || priceNumber <= 0) {
throw new Error(`Invalid price string: ${price}`);
}
const credits = Math.ceil(priceNumber / 0.01);
return credits;
}