Skip to content

Commit ce4b6f4

Browse files
committed
refactor: unify deposit constants for DRep and staking functionality
- Replaced hardcoded deposit values with constants from `protocol-deposit-constants` for DRep deregistration in `offchain.ts` and `retire.tsx`. - Updated transaction handling logic in `useTransaction` to account for DRep deposit adjustments. - Refactored `getTxBuilder` to use the new deposit constant for staking. - Removed the deprecated `staking-constants.ts` file to streamline codebase and improve maintainability.
1 parent a4c4bc5 commit ce4b6f4

8 files changed

Lines changed: 65 additions & 23 deletions

File tree

src/components/multisig/proxy/offchain.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from "@meshsdk/core";
88
import type { UTxO, MeshTxBuilder } from "@meshsdk/core";
99
// import { parseDatumCbor } from "@meshsdk/core-cst";
10+
import { DREP_DEPOSIT_STRING } from "@/utils/protocol-deposit-constants";
1011

1112
import { MeshTxInitiator } from "./common";
1213
import type { MeshTxInitiatorInput } from "./common";
@@ -501,7 +502,7 @@ export class MeshProxyContract extends MeshTxInitiator {
501502
anchorDataHash: anchorHash!,
502503
});
503504
} else if (action === "deregister") {
504-
txHex.drepDeregistrationCertificate(drepId, "500000000");
505+
txHex.drepDeregistrationCertificate(drepId, DREP_DEPOSIT_STRING);
505506
} else if (action === "update") {
506507
txHex.drepUpdateCertificate(drepId, {
507508
anchorUrl: anchorUrl!,

src/components/pages/wallet/governance/drep/retire.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { MeshProxyContract } from "@/components/multisig/proxy/offchain";
1414
import { api } from "@/utils/api";
1515
import { useCallback } from "react";
1616
import { useToast } from "@/hooks/use-toast";
17+
import { DREP_DEPOSIT_STRING } from "@/utils/protocol-deposit-constants";
1718

1819
export default function Retire({ appWallet, manualUtxos }: { appWallet: Wallet; manualUtxos: UTxO[] }) {
1920
const network = useSiteStore((state) => state.network);
@@ -250,7 +251,7 @@ export default function Retire({ appWallet, manualUtxos }: { appWallet: Wallet;
250251
txBuilder
251252
.txInScript(scriptCbor)
252253
.changeAddress(changeAddress)
253-
.drepDeregistrationCertificate(dRepId, "500000000");
254+
.drepDeregistrationCertificate(dRepId, DREP_DEPOSIT_STRING);
254255

255256
// Only add certificateScript if it's different from the spending script
256257
// to avoid "extraneous scripts" error

src/components/pages/wallet/staking/StakingActions/stake.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { MultisigWallet } from "@/utils/multisigSDK";
88
import { ToastAction } from "@radix-ui/react-toast";
99
import { toast } from "@/hooks/use-toast";
1010
import { getTxBuilder } from "@/utils/get-tx-builder";
11-
import { STAKE_KEY_DEPOSIT } from "@/utils/staking-constants";
11+
import { STAKE_KEY_DEPOSIT } from "@/utils/protocol-deposit-constants";
1212
import useTransaction from "@/hooks/useTransaction";
1313

1414
type StakingAction = "register" | "deregister" | "delegate" | "withdrawal" | "registerAndDelegate";

src/components/pages/wallet/transactions/all-transactions.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import useTransaction from "@/hooks/useTransaction";
2929
import React, { useEffect, useMemo, useState } from "react";
3030
import ResponsiveTransactionsTable from "./responsive-transactions-table";
3131
import type { LucideIcon } from "lucide-react";
32+
import { DREP_DEPOSIT } from "@/utils/protocol-deposit-constants";
3233

3334
type CertificateInfo = {
3435
type: string;
@@ -290,12 +291,12 @@ function TransactionRow({
290291
<>
291292
{dbTransaction.description == "DRep registration" && (
292293
<div className="text-red-400 font-medium text-sm">
293-
-{lovelaceToAda(500000000)}
294+
-{lovelaceToAda(DREP_DEPOSIT)}
294295
</div>
295296
)}
296297
{dbTransaction.description == "DRep retirement" && (
297298
<div className="text-green-400 font-medium text-sm">
298-
+{lovelaceToAda(500000000)}
299+
+{lovelaceToAda(DREP_DEPOSIT)}
299300
</div>
300301
)}
301302
</>

src/hooks/useTransaction.ts

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,65 @@ import {
1212
submitTxWithScriptRecovery,
1313
} from "@/utils/txSignUtils";
1414
import { getProvider } from "@/utils/get-provider";
15-
import { STAKE_KEY_DEPOSIT_LOVELACE } from "@/utils/staking-constants";
15+
import {
16+
DREP_DEPOSIT_LOVELACE,
17+
STAKE_KEY_DEPOSIT_LOVELACE,
18+
} from "@/utils/protocol-deposit-constants";
19+
20+
function parseCoinToBigInt(value: unknown, fallback: bigint): bigint {
21+
if (typeof value === "bigint") return value;
22+
if (typeof value === "number" && Number.isFinite(value)) {
23+
return BigInt(Math.trunc(value));
24+
}
25+
if (typeof value === "string" && value.trim().length > 0) {
26+
try {
27+
return BigInt(value);
28+
} catch {
29+
return fallback;
30+
}
31+
}
32+
return fallback;
33+
}
1634

17-
function getStakeKeyDepositDelta(txBuilder: MeshTxBuilder): bigint {
35+
function getCertificateCoinDelta(txBuilder: MeshTxBuilder): bigint {
1836
const body = txBuilder.meshTxBuilderBody as {
19-
certificates?: Array<{ certType?: { type?: string } }>;
37+
certificates?: Array<{
38+
certType?: {
39+
type?: string;
40+
coin?: string | number | bigint;
41+
deposit?: string | number | bigint;
42+
};
43+
}>;
2044
};
2145
const certs = body.certificates ?? [];
2246

2347
let delta = 0n;
2448
for (const cert of certs) {
2549
const certType = cert?.certType?.type ?? "";
2650
const normalized = certType.toLowerCase();
27-
const isDeregister =
28-
normalized.includes("deregister") || normalized.includes("unregister");
29-
const isRegister =
30-
!isDeregister &&
31-
(normalized.includes("registerstake") || normalized.includes("registration"));
51+
const certCoin = cert?.certType?.coin ?? cert?.certType?.deposit;
52+
53+
if (normalized.includes("drepregistration")) {
54+
delta -= parseCoinToBigInt(certCoin, DREP_DEPOSIT_LOVELACE);
55+
continue;
56+
}
57+
if (normalized.includes("drepderegistration")) {
58+
delta += parseCoinToBigInt(certCoin, DREP_DEPOSIT_LOVELACE);
59+
continue;
60+
}
61+
62+
const isStakeRegister =
63+
normalized.includes("registerstake") || normalized.includes("stakeregistration");
64+
const isStakeDeregister =
65+
normalized.includes("deregisterstake") ||
66+
normalized.includes("unregisterstake") ||
67+
normalized.includes("stakederegistration");
3268

33-
if (isRegister) {
69+
if (isStakeRegister) {
3470
delta -= STAKE_KEY_DEPOSIT_LOVELACE;
3571
continue;
3672
}
37-
if (isDeregister) {
73+
if (isStakeDeregister) {
3874
delta += STAKE_KEY_DEPOSIT_LOVELACE;
3975
}
4076
}
@@ -190,13 +226,13 @@ export default function useTransaction() {
190226

191227
let unsignedTx = await data.txBuilder.complete();
192228

193-
// Workaround for stake cert txs where builder-produced change may not
194-
// fully account for stake key deposit charge/refund in downstream validation.
195-
const stakeDepositDelta = getStakeKeyDepositDelta(data.txBuilder);
196-
if (stakeDepositDelta !== 0n) {
229+
// Workaround for certificate txs where builder-produced change may not
230+
// fully account for deposit charge/refund in downstream validation.
231+
const certificateCoinDelta = getCertificateCoinDelta(data.txBuilder);
232+
if (certificateCoinDelta !== 0n) {
197233
unsignedTx = adjustTxForStakeKeyDeposit(
198234
unsignedTx,
199-
stakeDepositDelta,
235+
certificateCoinDelta,
200236
appWallet.address,
201237
);
202238
}

src/utils/get-tx-builder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { MeshTxBuilder } from "@meshsdk/core";
22
import { getProvider } from "@/utils/get-provider";
3-
import { STAKE_KEY_DEPOSIT } from "@/utils/staking-constants";
3+
import { STAKE_KEY_DEPOSIT } from "@/utils/protocol-deposit-constants";
44
// import { CSLSerializer } from "@meshsdk/core-csl";
55

66
export function getTxBuilder(network: number) {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const STAKE_KEY_DEPOSIT = 2_000_000;
2+
export const STAKE_KEY_DEPOSIT_LOVELACE = 2_000_000n;
3+
export const DREP_DEPOSIT = 500_000_000;
4+
export const DREP_DEPOSIT_LOVELACE = 500_000_000n;
5+
export const DREP_DEPOSIT_STRING = "500000000";

src/utils/staking-constants.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)