Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 8
feat: Add max negative caps and enforce ledger bounds#618
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
49 changes: 48 additions & 1 deletion
49 platforms/eCurrency-api/src/controllers/CurrencyController.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14 platforms/eCurrency-api/src/database/migrations/1765784749012-migration.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { MigrationInterface, QueryRunner } from "typeorm"; | ||
| export class Migration1765784749012 implements MigrationInterface { | ||
| name = 'Migration1765784749012' | ||
| public async up(queryRunner: QueryRunner): Promise<void> { | ||
| await queryRunner.query(`ALTER TABLE "currencies" ADD "maxNegativeBalance" numeric(18,2)`); | ||
| } | ||
| public async down(queryRunner: QueryRunner): Promise<void> { | ||
| await queryRunner.query(`ALTER TABLE "currencies" DROP COLUMN "maxNegativeBalance"`); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 120 additions & 1 deletion
121 platforms/eCurrency/client/src/pages/currency-detail.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import { useQuery } from "@tanstack/react-query"; | ||
| import { useQuery, useQueryClient } from "@tanstack/react-query"; | ||
| import { useLocation, useRoute } from "wouter"; | ||
| import { apiClient } from "../lib/apiClient"; | ||
| import { useAuth } from "../hooks/useAuth"; | ||
| @@ -15,12 +15,17 @@ export default function CurrencyDetail() { | ||
| const [, params] = useRoute("/currency/:currencyId"); | ||
| const [, setLocation] = useLocation(); | ||
| const { user } = useAuth(); | ||
| const queryClient = useQueryClient(); | ||
| const [transferOpen, setTransferOpen] = useState(false); | ||
| const [mintOpen, setMintOpen] = useState(false); | ||
| const [selectedTransactionId, setSelectedTransactionId] = useState<string | null>(null); | ||
| const [transactionOffset, setTransactionOffset] = useState(0); | ||
| const [allTransactions, setAllTransactions] = useState<any[]>([]); | ||
| const PAGE_SIZE = 10; | ||
| const MAX_NEGATIVE_SLIDER = 1_000_000; | ||
| const [maxNegativeInput, setMaxNegativeInput] = useState<string>(""); | ||
| const [maxNegativeSaving, setMaxNegativeSaving] = useState(false); | ||
| const [maxNegativeError, setMaxNegativeError] = useState<string | null>(null); | ||
| // Load account context from localStorage | ||
| const [accountContext, setAccountContext] = useState<{ type: "user" | "group"; id: string } | null>(() => { | ||
| @@ -48,6 +53,16 @@ export default function CurrencyDetail() { | ||
| enabled: !!currencyId, | ||
| }); | ||
| useEffect(() => { | ||
| if (currency) { | ||
| if (currency.maxNegativeBalance !== null && currency.maxNegativeBalance !== undefined) { | ||
| setMaxNegativeInput(Math.abs(Number(currency.maxNegativeBalance)).toString()); | ||
| } else { | ||
| setMaxNegativeInput(""); | ||
| } | ||
| } | ||
| }, [currency]); | ||
| const { data: accountDetails } = useQuery({ | ||
| queryKey: ["accountDetails", currencyId, accountContext], | ||
| queryFn: async () => { | ||
| @@ -177,6 +192,40 @@ export default function CurrencyDetail() { | ||
| const isAdminOfCurrency = currency && groups?.some((g: any) => g.id === currency.groupId && g.isAdmin); | ||
| const saveMaxNegative = async () => { | ||
| if (!currencyId) return; | ||
| setMaxNegativeError(null); | ||
| setMaxNegativeSaving(true); | ||
| try { | ||
| const trimmed = maxNegativeInput.trim(); | ||
| const isClearing = trimmed === ""; | ||
| let payloadValue: number | null = null; | ||
| if (!isClearing) { | ||
| const magnitude = parseFloat(trimmed); | ||
| if (Number.isNaN(magnitude) || magnitude < 0) { | ||
| setMaxNegativeError("Enter a valid non-negative number."); | ||
| setMaxNegativeSaving(false); | ||
| return; | ||
| } | ||
| // Store as negative (or zero) | ||
| payloadValue = magnitude === 0 ? 0 : -Math.abs(magnitude); | ||
| } | ||
| await apiClient.patch(`/api/currencies/${currencyId}/max-negative`, { | ||
| value: payloadValue, | ||
| }); | ||
| await queryClient.invalidateQueries({ queryKey: ["currency", currencyId] }); | ||
| await queryClient.invalidateQueries({ queryKey: ["accountDetails", currencyId, accountContext] }); | ||
| } catch (error: any) { | ||
| const message = error?.response?.data?.error || error?.message || "Failed to update max negative balance"; | ||
| setMaxNegativeError(message); | ||
| } finally { | ||
| setMaxNegativeSaving(false); | ||
| } | ||
| }; | ||
coodos marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if (!currencyId) { | ||
| return <div>Currency not found</div>; | ||
| } | ||
| @@ -240,6 +289,16 @@ export default function CurrencyDetail() { | ||
| {currency.allowNegative ? "Yes" : "No"} | ||
| </p> | ||
| </div> | ||
| <div> | ||
| <h3 className="text-sm font-medium text-muted-foreground mb-1">Max Negative Balance</h3> | ||
| <p className="text-lg font-medium"> | ||
| {currency.allowNegative | ||
| ? (currency.maxNegativeBalance !== null && currency.maxNegativeBalance !== undefined | ||
| ? Number(currency.maxNegativeBalance).toLocaleString() | ||
| : "No cap") | ||
| : "Not applicable"} | ||
| </p> | ||
| </div> | ||
| <div> | ||
| <h3 className="text-sm font-medium text-muted-foreground mb-1">Total Currency Supply</h3> | ||
| <p className="text-lg font-semibold"> | ||
| @@ -257,6 +316,66 @@ export default function CurrencyDetail() { | ||
| </div> | ||
| )} | ||
| {/* Max Negative Control - only for admins when negatives are allowed */} | ||
| {currency && currency.allowNegative && isAdminOfCurrency && accountContext?.type === "group" && accountContext.id === currency.groupId && ( | ||
| <div className="bg-white border rounded-lg p-6 mb-6"> | ||
| <h3 className="text-lg font-semibold mb-2">Set max negative balance</h3> | ||
| <p className="text-sm text-muted-foreground mb-4"> | ||
| Limit how far any account can go negative for this currency. Leave blank for no cap. | ||
| </p> | ||
| <div className="space-y-4"> | ||
| <input | ||
| type="range" | ||
| min={0} | ||
| max={MAX_NEGATIVE_SLIDER} | ||
| step={0.01} | ||
| value={maxNegativeInput === "" ? 0 : Math.min(MAX_NEGATIVE_SLIDER, Math.max(0, Number(maxNegativeInput) || 0))} | ||
| onChange={(e) => setMaxNegativeInput(e.target.value)} | ||
| className="w-full" | ||
| /> | ||
| <div className="flex flex-col gap-3 md:flex-row md:items-center"> | ||
| <div className="flex-1"> | ||
| <label className="block text-sm font-medium mb-1">Max negative (absolute value)</label> | ||
| <input | ||
| type="number" | ||
| min={0} | ||
| max={MAX_NEGATIVE_SLIDER} | ||
| step={0.01} | ||
| value={maxNegativeInput} | ||
| onChange={(e) => setMaxNegativeInput(e.target.value)} | ||
| placeholder="Leave blank for no cap" | ||
| className="w-full px-4 py-2 border rounded-lg" | ||
| /> | ||
| <div className="text-xs text-muted-foreground mt-1"> | ||
| Saved as negative value: {maxNegativeInput === "" ? "No cap" : `-${Math.abs(Number(maxNegativeInput) || 0).toLocaleString()}`} | ||
| </div> | ||
| </div> | ||
| <div className="flex gap-2"> | ||
| <button | ||
| onClick={saveMaxNegative} | ||
| disabled={maxNegativeSaving} | ||
| className="px-4 py-2 bg-primary text-primary-foreground rounded-lg hover:opacity-90 disabled:opacity-50" | ||
| > | ||
| {maxNegativeSaving ? "Saving..." : "Save"} | ||
| </button> | ||
| <button | ||
| onClick={() => setMaxNegativeInput("")} | ||
| disabled={maxNegativeSaving} | ||
| className="px-4 py-2 border rounded-lg hover:bg-gray-50 disabled:opacity-50" | ||
| > | ||
| Clear cap | ||
| </button> | ||
| </div> | ||
| </div> | ||
| {maxNegativeError && ( | ||
| <div className="bg-red-50 border border-red-200 text-red-700 px-4 py-2 rounded-lg"> | ||
| {maxNegativeError} | ||
| </div> | ||
| )} | ||
| </div> | ||
| </div> | ||
| )} | ||
| {/* Transactions */} | ||
| <div className="mb-6"> | ||
| <div className="flex justify-between items-center mb-4"> | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.