Skip to content

Commit 31004d9

Browse files
authored
Merge pull request #10 from 0xbeny/review-ag/app-follows-contracts
Point the app at auto-extend and the two-field lock
2 parents e22025a + 51dfc49 commit 31004d9

35 files changed

Lines changed: 785 additions & 414 deletions
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE "positions" DROP COLUMN IF EXISTS "penalty_cap_bps";

‎apps/indexer/drizzle/meta/_journal.json‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515
"when": 1789100000000,
1616
"tag": "0001_contributions_epoch",
1717
"breakpoints": true
18+
},
19+
{
20+
"idx": 2,
21+
"version": "7",
22+
"when": 1789200000000,
23+
"tag": "0002_drop_penalty_cap",
24+
"breakpoints": true
1825
}
1926
]
2027
}

‎apps/indexer/src/app/api/positions/[address]/route.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ export async function GET(request: Request, { params }: Params) {
3535
owner: p.owner,
3636
amount: p.amount,
3737
unlockTime: Number(p.unlockTime),
38-
penaltyCapBps: p.penaltyCapBps,
3938
closed: p.closed,
4039
firstEligibleEpoch: p.firstEligibleEpoch,
4140
exitEpoch: p.exitEpoch,

‎apps/indexer/src/db/schema.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ export const positions = pgTable(
3232
owner: text("owner").notNull(),
3333
amount: u256("amount").notNull(),
3434
unlockTime: u256("unlock_time").notNull(),
35-
penaltyCapBps: integer("penalty_cap_bps").notNull(),
3635
closed: boolean("closed").notNull().default(false),
3736
firstEligibleEpoch: u256("first_eligible_epoch"),
3837
exitEpoch: u256("exit_epoch"),

‎apps/indexer/src/lib/keeper.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function chunkIds(ids: bigint[], size: number): bigint[][] {
3636

3737
async function optedInTokenIds(
3838
chainId: number,
39-
eventName: "KeepAtMaxLockSet" | "AutoCompoundSet",
39+
eventName: "AutoExtendSet" | "AutoCompoundSet",
4040
): Promise<bigint[]> {
4141
const db = getDb();
4242
const rows = await db
@@ -120,7 +120,7 @@ export async function runKeeper(): Promise<KeeperResult> {
120120

121121
// Pre-boundary extensions: Wednesday 22:00–24:00 UTC for Thursday epochs.
122122
if (inKeepWindow) {
123-
const keepIds = await optedInTokenIds(chainId, "KeepAtMaxLockSet");
123+
const keepIds = await optedInTokenIds(chainId, "AutoExtendSet");
124124
if (keepIds.length > 0) {
125125
const hashes: Hex[] = [];
126126
for (const chunk of chunkIds(keepIds, batchSize)) {

‎apps/indexer/src/lib/sync.ts‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ async function refreshPosition(
180180

181181
const amount = lock.amount.toString(10);
182182
const unlockTime = lock.end.toString(10);
183-
const penaltyCapBps = Number(lock.penaltyCapBps);
184183
const tokenIdStr = tokenId.toString(10);
185184
const blockStr = toBlockString(blockNumber);
186185

@@ -192,7 +191,6 @@ async function refreshPosition(
192191
owner: normalizeAddress(owner),
193192
amount,
194193
unlockTime,
195-
penaltyCapBps,
196194
closed: closedFlag,
197195
firstEligibleEpoch: firstEligible.toString(10),
198196
exitEpoch: exitEp === 0n ? null : exitEp.toString(10),
@@ -205,7 +203,6 @@ async function refreshPosition(
205203
owner: normalizeAddress(owner),
206204
amount,
207205
unlockTime,
208-
penaltyCapBps,
209206
closed: closedFlag,
210207
firstEligibleEpoch: firstEligible.toString(10),
211208
exitEpoch: exitEp === 0n ? null : exitEp.toString(10),
@@ -344,6 +341,19 @@ async function processVotingEscrowLog(
344341
});
345342
break;
346343
}
344+
case "AutoExtendSet": {
345+
const { tokenId } = decoded.args;
346+
await insertEvent(db, {
347+
chainId,
348+
tokenId: tokenId.toString(10),
349+
eventName: "AutoExtendSet",
350+
txHash,
351+
logIndex,
352+
blockNumber: toBlockString(blockNumber),
353+
payload: jsonSafe(decoded.args),
354+
});
355+
break;
356+
}
347357
case "Withdraw": {
348358
const { tokenId } = decoded.args;
349359
// Mature withdraw: weight is already zero after expiry; exitEpoch is not recorded on-chain.
@@ -489,7 +499,6 @@ async function processFeeDistributorLog(
489499
break;
490500
}
491501
case "Compounded":
492-
case "KeepAtMaxLockSet":
493502
case "AutoCompoundSet": {
494503
const { tokenId } = decoded.args;
495504
await insertEvent(db, {
@@ -644,6 +653,7 @@ async function processZapDepositorLog(
644653
const VE_EVENTS = [
645654
"Deposit",
646655
"LockExtended",
656+
"AutoExtendSet",
647657
"Withdraw",
648658
"EmergencyExit",
649659
"ExitRequested",
@@ -656,7 +666,6 @@ const FD_EVENTS = [
656666
"ForfeitureSynced",
657667
"Claimed",
658668
"Compounded",
659-
"KeepAtMaxLockSet",
660669
"AutoCompoundSet",
661670
] as const;
662671

‎apps/web/design.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ Deposit calls `Zap.zapCreateLock{value}(durationSeconds)` where `durationSeconds
244244
One purpose per control group:
245245

246246
1. **Increase** — native XDC via `zap.zapIncreaseAmount{value}(tokenId)`
247-
2. **Extend** — weeks remaining → `escrow.increaseUnlockTime` or `keepAtMaxLock`
247+
2. **Extend** — weeks remaining → `escrow.increaseUnlockTime`. Auto-extend is `escrow.setAutoExtend`.
248248
3. **Claim** — `distributor.claim(tokenId, rewardTokens)`
249249
4. **Claim & lock** — `distributor.claimAndLock(tokenId)`
250250
5. **Exit** — request withdraw / emergency → cooldown countdown → finalize; cancel when pending

‎apps/web/src/components/ManagePosition.tsx‎

Lines changed: 76 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
contractsReady,
1414
getConfiguredChainId,
1515
getContractsState,
16+
MAX_LOCK_SECONDS,
1617
MAX_LOCK_WEEKS,
1718
MIN_LOCK_WEEKS,
1819
weeksToDuration,
@@ -82,6 +83,22 @@ export function ManagePosition({ tokenId, compact = false }: Props) {
8283
query: { enabled: ready },
8384
});
8485

86+
const { data: autoExtend } = useReadContract({
87+
address: escrow,
88+
abi: abis.VotingEscrow,
89+
functionName: "autoExtend",
90+
args: [tokenId],
91+
query: { enabled: ready },
92+
});
93+
94+
const { data: exitQuote } = useReadContract({
95+
address: escrow,
96+
abi: abis.VotingEscrow,
97+
functionName: "previewExit",
98+
args: [tokenId],
99+
query: { enabled: ready && !Boolean(closed) },
100+
});
101+
85102
const { data: weight } = useReadContract({
86103
address: escrow,
87104
abi: abis.VotingEscrow,
@@ -114,6 +131,12 @@ export function ManagePosition({ tokenId, compact = false }: Props) {
114131
const cooldownDone = readyAt > 0 && Date.now() / 1000 >= readyAt;
115132
const unlockEnd = lock ? Number(lock.end) : 0;
116133
const matured = unlockEnd > 0 && Date.now() / 1000 >= unlockEnd;
134+
const nowSec = Math.floor(Date.now() / 1000);
135+
const secondsLeft = unlockEnd > nowSec ? unlockEnd - nowSec : 0;
136+
const earlyExitBlocked = !matured && secondsLeft > 0 && secondsLeft <= cooldownSec;
137+
const emergencyButMature = exitKind === ExitKind.Emergency && matured;
138+
const penaltyNow = exitQuote ? exitQuote[1] : undefined;
139+
const penaltyBpsNow = exitQuote ? Number(exitQuote[2]) : undefined;
117140
const isClosed = Boolean(closed);
118141
const busy = isPending || confirming;
119142
const writesOk = ready && isConnected && onExpectedChain && !!address && !isClosed;
@@ -256,6 +279,25 @@ export function ManagePosition({ tokenId, compact = false }: Props) {
256279
>
257280
Extend to ~{formatDate(newUnlock)}
258281
</button>
282+
<button
283+
type="button"
284+
className={styles.secondary}
285+
disabled={!writesOk || exitPending || busy}
286+
onClick={() => {
287+
const now = Math.floor(Date.now() / 1000);
288+
run(() =>
289+
writeContract({
290+
chainId: writeChainId,
291+
address: escrow,
292+
abi: abis.VotingEscrow,
293+
functionName: "increaseUnlockTime",
294+
args: [tokenId, BigInt(now + MAX_LOCK_SECONDS)],
295+
}),
296+
);
297+
}}
298+
>
299+
Max lock ({MAX_LOCK_WEEKS}w)
300+
</button>
259301
<button
260302
type="button"
261303
className={styles.secondary}
@@ -266,21 +308,25 @@ export function ManagePosition({ tokenId, compact = false }: Props) {
266308
chainId: writeChainId,
267309
address: escrow,
268310
abi: abis.VotingEscrow,
269-
functionName: "keepAtMaxLock",
270-
args: [tokenId],
311+
functionName: "setAutoExtend",
312+
args: [tokenId, !autoExtend],
271313
}),
272314
)
273315
}
274316
>
275-
Max lock ({MAX_LOCK_WEEKS}w)
317+
{autoExtend ? "Turn off auto-extend" : "Turn on auto-extend"}
276318
</button>
277319
</div>
320+
<p className={styles.groupHint}>
321+
Auto-extend lets the keeper push this lock back to 104 weeks, and only in the last 2 hours of an epoch.
322+
</p>
278323
</div>
279324

280325
<div className={styles.group}>
281326
<h3 className={styles.groupTitle}>Claim</h3>
282327
<p className={styles.groupHint}>
283-
Rewards pay the NFT recipient. Claim &amp; lock compounds WXDC into this position.
328+
Rewards pay the NFT recipient. A week&apos;s yield can be claimed 8 weeks after that week.
329+
An early exit forfeits yield that has not finished the wait. Claim &amp; lock compounds WXDC into this position.
284330
</p>
285331
<div className={styles.row}>
286332
<ClaimButton tokenId={tokenId} mode="claim" />
@@ -291,18 +337,33 @@ export function ManagePosition({ tokenId, compact = false }: Props) {
291337
<div className={styles.group}>
292338
<h3 className={styles.groupTitle}>Exit</h3>
293339
<p className={styles.groupHint}>
294-
Request → wait cooldown → finalize. Emergency applies a penalty snapshotted at request.
340+
Request, wait out the cooldown, then finalize.
295341
{cooldownSec > 0
296342
? ` Cooldown ${Math.round(cooldownSec / 3600)}h.`
297343
: " Cooldown is currently 0."}
344+
{!matured && !isClosed && penaltyNow !== undefined && penaltyNow > 0n
345+
? ` An early exit now costs ${formatXdc(penaltyNow)} XDC (${(penaltyBpsNow ?? 0) / 100}%).`
346+
: ""}
347+
{earlyExitBlocked
348+
? " The cooldown would outlast this lock, so an early exit is not available."
349+
: ""}
298350
</p>
299351

300352
{exitPending ? (
301353
<div className={styles.exitPending}>
302354
<p className={styles.pendingLabel}>
303-
{exitKind === ExitKind.Emergency ? "Emergency" : "Withdraw"} exit armed
355+
{emergencyButMature
356+
? "Lock matured during the cooldown"
357+
: exitKind === ExitKind.Emergency
358+
? "Emergency exit armed"
359+
: "Withdraw armed"}
304360
{readyAt > 0 ? ` · ${formatCountdown(readyAt)}` : ""}
305361
</p>
362+
{emergencyButMature && (
363+
<p className={styles.groupHint}>
364+
Finalizing pays the full principal and does not forfeit yield.
365+
</p>
366+
)}
306367
<div className={styles.row}>
307368
{exitKind === ExitKind.Withdraw && (
308369
<button
@@ -341,7 +402,7 @@ export function ManagePosition({ tokenId, compact = false }: Props) {
341402
)
342403
}
343404
>
344-
Finalize emergency exit
405+
Finalize
345406
</button>
346407
)}
347408
<button
@@ -388,8 +449,14 @@ export function ManagePosition({ tokenId, compact = false }: Props) {
388449
<button
389450
type="button"
390451
className={styles.danger}
391-
disabled={!writesOk || busy || matured}
392-
title={matured ? "Lock matured — use withdraw" : undefined}
452+
disabled={!writesOk || busy || matured || earlyExitBlocked}
453+
title={
454+
matured
455+
? "Lock matured — use withdraw"
456+
: earlyExitBlocked
457+
? "The cooldown would outlast this lock"
458+
: undefined
459+
}
393460
onClick={() =>
394461
run(() =>
395462
writeContract({

‎apps/web/src/components/PositionsList.tsx‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ const ExitKind = {
2424
type LockTuple = {
2525
amount: bigint;
2626
end: bigint | number;
27-
penaltyCapBps: bigint | number;
2827
};
2928

3029
type ExitTuple = {

‎apps/web/src/lib/indexer.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ export type IndexerPosition = {
2727
owner: string;
2828
amount: string;
2929
unlockTime: number;
30-
penaltyCapBps: number;
3130
closed: boolean;
3231
};
3332

0 commit comments

Comments
 (0)