Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
flip conversion
  • Loading branch information
christian-byrne committed Dec 9, 2025
commit 757510c68dbc5ad92dd39d13b47093d07c38c9d7
12 changes: 6 additions & 6 deletions src/base/credits/comfyCredits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,22 @@ const formatNumber = ({
return new Intl.NumberFormat(locale, merged).format(value)
}

export const COMFY_CREDIT_RATE_CENTS = 210
export const COMFY_CREDIT_RATE_USD = COMFY_CREDIT_RATE_CENTS / 100
export const CREDITS_PER_USD = 210
export const COMFY_CREDIT_RATE_CENTS = CREDITS_PER_USD / 100 // credits per cent

export const usdToCents = (usd: number): number => Math.round(usd * 100)

export const centsToCredits = (cents: number): number =>
cents / COMFY_CREDIT_RATE_CENTS
Math.round(cents * COMFY_CREDIT_RATE_CENTS)

export const creditsToCents = (credits: number): number =>
Math.round(credits * COMFY_CREDIT_RATE_CENTS)
Math.round(credits / COMFY_CREDIT_RATE_CENTS)

export const usdToCredits = (usd: number): number =>
centsToCredits(usdToCents(usd))
Math.round(usd * CREDITS_PER_USD)

export const creditsToUsd = (credits: number): number =>
creditsToCents(credits) / 100
Math.round((credits / CREDITS_PER_USD) * 100) / 100

export type FormatOptions = {
value: number
Expand Down
8 changes: 3 additions & 5 deletions src/components/dialog/content/TopUpCreditsDialogContent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ import Button from 'primevue/button'
import { ref } from 'vue'
import { useI18n } from 'vue-i18n'

import { creditsToUsd } from '@/base/credits/comfyCredits'
import UserCredit from '@/components/common/UserCredit.vue'
import { useFirebaseAuthActions } from '@/composables/auth/useFirebaseAuthActions'
import { useFeatureFlags } from '@/composables/useFeatureFlags'
Expand Down Expand Up @@ -170,14 +171,11 @@ const creditOptions: CreditOption[] = [
const handleBuy = async () => {
if (!selectedCredits.value) return

telemetry?.trackApiCreditTopupButtonPurchaseClicked(selectedCredits.value)

loading.value = true
try {
// Convert credits to USD (this would need to be implemented based on your conversion rate)
const usdAmount = selectedCredits.value / 100 // Example conversion, adjust as needed
const usdAmount = creditsToUsd(selectedCredits.value)
telemetry?.trackApiCreditTopupButtonPurchaseClicked(usdAmount)
await authActions.purchaseCredits(usdAmount)
// Optionally emit success event or close dialog
} catch (error) {
console.error('Purchase failed:', error)
} finally {
Expand Down
18 changes: 9 additions & 9 deletions tests-ui/tests/base/credits/comfyCredits.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { describe, expect, test } from 'vitest'

import {
CREDITS_PER_USD,
COMFY_CREDIT_RATE_CENTS,
COMFY_CREDIT_RATE_USD,
centsToCredits,
creditsToCents,
creditsToUsd,
Expand All @@ -17,8 +17,8 @@ import {

describe('comfyCredits helpers', () => {
test('exposes the fixed conversion rate', () => {
expect(COMFY_CREDIT_RATE_CENTS).toBe(210)
expect(COMFY_CREDIT_RATE_USD).toBeCloseTo(2.1)
expect(CREDITS_PER_USD).toBe(210)
expect(COMFY_CREDIT_RATE_CENTS).toBeCloseTo(2.1) // credits per cent
})

test('converts between USD and cents', () => {
Expand All @@ -27,20 +27,20 @@ describe('comfyCredits helpers', () => {
})

test('converts cents to credits and back', () => {
expect(centsToCredits(210)).toBeCloseTo(1)
expect(creditsToCents(5)).toBe(1050)
expect(centsToCredits(100)).toBe(210) // 100 cents = 210 credits
expect(creditsToCents(210)).toBe(100) // 210 credits = 100 cents
})

test('converts USD to credits and back', () => {
expect(usdToCredits(2.1)).toBeCloseTo(1)
expect(creditsToUsd(3.5)).toBeCloseTo(7.35)
expect(usdToCredits(1)).toBe(210) // 1 USD = 210 credits
expect(creditsToUsd(210)).toBe(1) // 210 credits = 1 USD
})

test('formats credits and USD values using en-US locale', () => {
const locale = 'en-US'
expect(formatCredits({ value: 1234.567, locale })).toBe('1,234.57')
expect(formatCreditsFromCents({ cents: 210, locale })).toBe('1.00')
expect(formatCreditsFromUsd({ usd: 4.2, locale })).toBe('2.00')
expect(formatCreditsFromCents({ cents: 100, locale })).toBe('210.00')
expect(formatCreditsFromUsd({ usd: 1, locale })).toBe('210.00')
expect(formatUsd({ value: 4.2, locale })).toBe('4.20')
})
})