Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 10
Sweetmantech/myc 3555 apiimagegenerate use credits#7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import { selectCreditsUsage } from "@/lib/supabase/credits_usage/selectCreditsUsage"; | ||
| import { updateCreditsUsage } from "@/lib/supabase/credits_usage/updateCreditsUsage"; | ||
| interface DeductCreditsParams { | ||
| accountId: string; | ||
| creditsToDeduct: number; | ||
| } | ||
| interface DeductCreditsResult { | ||
| success: boolean; | ||
| newBalance?: number; | ||
| message?: string; | ||
| } | ||
| /** | ||
| * Deducts credits from an account's credit balance. | ||
| * | ||
| * @param params - The parameters for deducting credits. | ||
| * @param params.accountId - The account ID to deduct credits from. | ||
| * @param params.creditsToDeduct - The number of credits to deduct. | ||
| * @returns Result object indicating success/failure and new balance. | ||
| */ | ||
| export const deductCredits = async ({ | ||
| accountId, | ||
| creditsToDeduct, | ||
| }: DeductCreditsParams): Promise<DeductCreditsResult> => { | ||
| try { | ||
| const response = await selectCreditsUsage({ account_id: accountId }); | ||
| if (!response || response.length === 0) { | ||
| return { | ||
| success: false, | ||
| message: "No credits usage found for this account", | ||
| }; | ||
| } | ||
| const currentCredits = response[0]; | ||
| const newBalance = currentCredits.remaining_credits - creditsToDeduct; | ||
| await updateCreditsUsage({ | ||
| account_id: accountId, | ||
| updates: { | ||
| remaining_credits: newBalance, | ||
| }, | ||
| }); | ||
sweetmantech marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| return { | ||
| success: true, | ||
| newBalance, | ||
| }; | ||
| } catch (error) { | ||
| console.error("Failed to deduct credits:", error); | ||
| return { | ||
| success: false, | ||
| message: error instanceof Error ? error.message : "Unknown error occurred", | ||
| }; | ||
| } | ||
sweetmantech marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import serverClient from "../serverClient"; | ||
| import { Tables } from "@/types/database.types"; | ||
| export type CreditsUsage = Tables<"credits_usage">; | ||
| interface SelectCreditsUsageParams { | ||
| account_id?: string; | ||
| } | ||
| export const selectCreditsUsage = async ( | ||
| params?: SelectCreditsUsageParams, | ||
| ): Promise<CreditsUsage[]> => { | ||
| let query = serverClient.from("credits_usage").select(); | ||
| if (params?.account_id) { | ||
| query = query.eq("account_id", params.account_id); | ||
| } | ||
| const { data, error } = await query; | ||
| if (error) { | ||
| console.error("Error selecting credits usage:", error); | ||
| throw error; | ||
| } | ||
| return data; | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import serverClient from "../serverClient"; | ||
| import { CreditsUsage } from "./selectCreditsUsage"; | ||
| interface UpdateCreditsUsageParams { | ||
| account_id: string; | ||
| updates: Partial<Omit<CreditsUsage, "account_id">>; | ||
| } | ||
| export const updateCreditsUsage = async ({ | ||
| account_id, | ||
| updates, | ||
| }: UpdateCreditsUsageParams): Promise<CreditsUsage> => { | ||
| const { data, error } = await serverClient | ||
| .from("credits_usage") | ||
| .update(updates) | ||
| .eq("account_id", account_id) | ||
| .select() | ||
| .single(); | ||
| if (error) { | ||
| console.error("Error updating credits usage:", error); | ||
| throw error; | ||
| } | ||
| if (!data) { | ||
| throw new Error(`No credits usage found for account_id: ${account_id}`); | ||
| } | ||
| return data; | ||
| }; | ||
sweetmantech marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import { createClient } from "@supabase/supabase-js"; | ||
| const SUPABASE_URL = process.env.SUPABASE_URL as string; | ||
| const SUPABASE_KEY = process.env.SUPABASE_KEY as string; | ||
| if (!SUPABASE_URL || !SUPABASE_KEY) { | ||
| throw new Error("SUPABASE_URL and SUPABASE_KEY must be set"); | ||
| } | ||
| const supabase = createClient(SUPABASE_URL, SUPABASE_KEY); | ||
| export default supabase; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,20 @@ | ||
| import { wrapFetchWithPayment } from "x402-fetch"; | ||
| import { toAccount } from "viem/accounts"; | ||
| import { getAccount } from "@/lib/coinbase/getAccount"; | ||
| import { deductCredits } from "../credits/deductCredits"; | ||
| const ACCOUNT_ADDRESS = "0x7AfB9872Ea382B7Eb01c67B6884dD99A744eA64f" as const; | ||
| /** | ||
| * Fetches a URL with x402 payment handling. | ||
| * | ||
| * @param url - The URL to fetch. | ||
| * @param accountId - The account ID. | ||
| * @returns Promise resolving to the Response. | ||
| */ | ||
| export async function fetchWithPayment(url: string): Promise<Response> { | ||
| export async function fetchWithPayment(url: string, accountId: string): Promise<Response> { | ||
| const account = await getAccount(ACCOUNT_ADDRESS); | ||
| await deductCredits({ accountId, creditsToDeduct: 1 }); | ||
sweetmantech marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| const fetchWithPaymentWrapper = wrapFetchWithPayment(fetch, toAccount(account)); | ||
sweetmantech marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| return fetchWithPaymentWrapper(url, { | ||
| method: "GET", | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.