Skip to content

Commit 718f60d

Browse files
committed
refactor staking actions: consolidate buttons into a single StakeButton component and update staking info fetching logic
1 parent c4467bd commit 718f60d

File tree

7 files changed

+96
-119
lines changed

7 files changed

+96
-119
lines changed

src/components/common/overall-layout/menus/multisig-wallet.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
import { Banknote, Info, List, Signature, UserRoundPen } from "lucide-react";
1+
import { Banknote, Info, List, Signature, UserRoundPen, ChartNoAxesColumnIncreasing } from "lucide-react";
22
import { useRouter } from "next/router";
33
import MenuLink from "./menu-link";
44
import usePendingTransactions from "@/hooks/usePendingTransactions";
55
import useUserWallets from "@/hooks/useUserWallets";
66
import { Badge } from "@/components/ui/badge";
77
import { ChatBubbleIcon } from "@radix-ui/react-icons";
88
import usePendingSignables from "@/hooks/usePendingSignables";
9+
import useMultisigWallet from "@/hooks/useMultisigWallet";
910

1011
export default function MenuWallet() {
1112
const router = useRouter();
1213
const baseUrl = `/wallets/${router.query.wallet as string | undefined}/`;
1314
const { wallets } = useUserWallets();
1415
const { transactions } = usePendingTransactions();
1516
const { signables } = usePendingSignables();
17+
const { multisigWallet } = useMultisigWallet();
1618
if (!wallets) return;
1719
return (
1820
<nav className="grid h-full items-start px-2 text-sm font-medium lg:px-4">
@@ -51,6 +53,15 @@ export default function MenuWallet() {
5153
)}
5254
</div>
5355
</MenuLink>
56+
{multisigWallet && multisigWallet.stakingEnabled() && <MenuLink
57+
href={`${baseUrl}staking`}
58+
className={
59+
router.pathname == "/wallets/[wallet]/staking" ? "text-white" : ""
60+
}
61+
>
62+
<ChartNoAxesColumnIncreasing className="h-6 w-6" />
63+
Staking
64+
</MenuLink>}
5465
<MenuLink
5566
href={`${baseUrl}assets`}
5667
className={

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

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

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

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

src/components/pages/wallet/staking/StakingActions/register.tsx renamed to src/components/pages/wallet/staking/StakingActions/stake.tsx

Lines changed: 60 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,36 @@ import { toast } from "@/hooks/use-toast";
1010
import { getTxBuilder } from "@/utils/get-tx-builder";
1111
import { getProvider } from "@/utils/get-provider";
1212
import useTransaction from "@/hooks/useTransaction";
13-
export default function RegisterButton({
13+
export default function StakeButton({
1414
stakingInfo,
1515
appWallet,
1616
mWallet,
1717
utxos,
1818
network,
1919
poolHex,
20+
action,
2021
}: {
2122
stakingInfo: StakingInfo;
2223
appWallet: Wallet;
2324
mWallet: MultisigWallet;
2425
utxos: UTxO[];
2526
network: number;
2627
poolHex: string;
28+
action: "register" | "deregister" | "delegate" | "withdrawal" | "registerAndDelegate";
2729
}) {
2830
const { newTransaction } = useTransaction();
2931
const [loading, setLoading] = useState(false);
3032

31-
async function register() {
33+
async function Stake() {
3234
setLoading(true);
3335
try {
3436
if (!mWallet) throw new Error("Multisig Wallet could not be built.");
37+
3538
const rewardAddress = mWallet.getStakeAddress();
36-
if (!rewardAddress)
37-
throw new Error("Reward Address could not be built.");
39+
if (!rewardAddress) throw new Error("Reward Address could not be built.");
40+
41+
const stakingScript = mWallet.getStakingScript();
42+
if (!stakingScript) throw new Error("Staking Script could not be built.");
3843

3944
const txBuilder = getTxBuilder(network);
4045
const selectedUtxos = utxos;
@@ -49,42 +54,73 @@ export default function RegisterButton({
4954
)
5055
.txInScript(appWallet.scriptCbor);
5156
}
52-
txBuilder
53-
.selectUtxosFrom(utxos)
54-
.changeAddress(appWallet.address)
55-
//.registerStakeCertificate(rewardAddress)
56-
.delegateStakeCertificate(rewardAddress, poolHex);
5757

58+
const actionsMap = {
59+
register: {
60+
execute: () => txBuilder.registerStakeCertificate(rewardAddress),
61+
description: "Register stake.",
62+
successTitle: "Stake Registered",
63+
successMessage: "Your stake address has been registered.",
64+
},
65+
deregister: {
66+
execute: () => txBuilder.deregisterStakeCertificate(rewardAddress),
67+
description: "Deregister stake.",
68+
successTitle: "Stake Deregistered",
69+
successMessage: "Your stake address has been deregistered.",
70+
},
71+
delegate: {
72+
execute: () => txBuilder.delegateStakeCertificate(rewardAddress, poolHex),
73+
description: "Delegate stake.",
74+
successTitle: "Stake Delegated",
75+
successMessage: "Your stake has been delegated.",
76+
},
77+
withdrawal: {
78+
execute: () => txBuilder.withdrawal(rewardAddress, stakingInfo.rewards),
79+
description: "Withdraw rewards.",
80+
successTitle: "Rewards Withdrawn",
81+
successMessage: "Your staking rewards have been withdrawn.",
82+
},
83+
registerAndDelegate: {
84+
execute: () => {
85+
txBuilder.registerStakeCertificate(rewardAddress);
86+
txBuilder.delegateStakeCertificate(rewardAddress, poolHex);
87+
},
88+
description: "Register & delegate stake.",
89+
successTitle: "Stake Registered & Delegated",
90+
successMessage: "Your stake address has been registered and delegated.",
91+
},
92+
};
5893

59-
const paymentKeys = mWallet.getKeysByRole(0) ?? [];
60-
for (const key of paymentKeys) {
61-
txBuilder.requiredSignerHash(key.keyHash);
94+
const actionConfig = actionsMap[action];
95+
if (!actionConfig) {
96+
throw new Error("Invalid staking action.");
6297
}
6398

64-
const stakingKeys = mWallet.getKeysByRole(2) ?? [];
65-
for (const key of stakingKeys) {
66-
txBuilder.requiredSignerHash(key.keyHash);
67-
}
99+
actionConfig.execute();
100+
101+
txBuilder
102+
.selectUtxosFrom(utxos)
103+
.changeAddress(appWallet.address)
104+
.certificateScript(stakingScript);
68105

69106
await newTransaction({
70107
txBuilder,
71-
description: `Register stake.`,
108+
description: actionConfig.description,
72109
});
73110

74111
toast({
75-
title: "Transaction Successful",
76-
description: `Your Registration has been recorded.`,
112+
title: actionConfig.successTitle,
113+
description: actionConfig.successMessage,
77114
duration: 5000,
78115
});
79-
80116
} catch (error) {
81117
if (
82118
error instanceof Error &&
83119
error.message.includes("User rejected transaction")
84120
) {
85121
toast({
86122
title: "Transaction Aborted",
87-
description: "You canceled the registration transaction.",
123+
description: "You canceled the transaction.",
88124
duration: 5000,
89125
});
90126
} else {
@@ -117,11 +153,9 @@ export default function RegisterButton({
117153
}
118154

119155
return (
120-
<Button variant="outline" onClick={register} disabled={loading}>
121-
{loading ? (
122-
<Loader className="mr-2 h-4 w-4 animate-spin" />
123-
) : null}
124-
Register
156+
<Button variant="outline" onClick={Stake} disabled={loading}>
157+
{loading ? <Loader className="mr-2 h-4 w-4 animate-spin" /> : null}
158+
{action.charAt(0).toUpperCase() + action.slice(1)}
125159
</Button>
126160
);
127161
}

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

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ import CardUI from "@/components/ui/card-content";
22
import UTxOSelector from "@/components/pages/wallet/new-transaction/utxoSelector";
33
import { StakingInfo } from "../stakingInfoCard";
44
import { Wallet } from "@/types/wallet";
5-
import DelegateButton from "./delegate";
6-
import RegisterButton from "./register";
7-
import DeregisterButton from "./deregister";
8-
import WithdrawalButton from "./withdrawal";
5+
import StakeButton from "./stake";
96
import { UTxO } from "@meshsdk/core";
107
import { useState } from "react";
118
import { MultisigWallet } from "@/utils/multisigSDK";
@@ -29,40 +26,48 @@ export default function StakingActionCard({
2926
return (
3027
<CardUI title="Staking Actions">
3128
<div className="flex flex-wrap gap-2">
32-
{stakingInfo.active && (
33-
<RegisterButton
29+
{!stakingInfo.active && (
30+
<StakeButton
3431
stakingInfo={stakingInfo}
3532
appWallet={appWallet}
3633
mWallet={mWallet}
3734
utxos={selectedUtxos}
3835
network={network}
3936
poolHex={poolHex}
37+
action="registerAndDelegate"
4038
/>
4139
)}
42-
4340
{stakingInfo.active && (
4441
<>
45-
<DelegateButton
42+
<StakeButton
4643
stakingInfo={stakingInfo}
4744
appWallet={appWallet}
45+
mWallet={mWallet}
4846
utxos={selectedUtxos}
49-
manualSelected={manualSelected}
47+
network={network}
48+
poolHex={poolHex}
49+
action="delegate"
5050
/>
51-
<DeregisterButton
51+
<StakeButton
5252
stakingInfo={stakingInfo}
5353
appWallet={appWallet}
54+
mWallet={mWallet}
5455
utxos={selectedUtxos}
55-
manualSelected={manualSelected}
56+
network={network}
57+
poolHex={poolHex}
58+
action="deregister"
5659
/>
5760
</>
5861
)}
59-
6062
{Number(stakingInfo.rewards) > 0 && (
61-
<WithdrawalButton
63+
<StakeButton
6264
stakingInfo={stakingInfo}
6365
appWallet={appWallet}
66+
mWallet={mWallet}
6467
utxos={selectedUtxos}
65-
manualSelected={manualSelected}
68+
network={network}
69+
poolHex={poolHex}
70+
action="withdrawal"
6671
/>
6772
)}
6873
</div>

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

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

src/components/pages/wallet/staking/index.tsx

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ export default function PageStaking() {
2424
useEffect(() => {
2525
if (!address) return;
2626
blockchainProvider
27-
.fetchAccountInfo(address)
27+
.get(`/accounts/${address}`)
2828
.then((data) => {
2929
setStakingInfo({
30-
poolId: data.poolId,
30+
poolId: data.pool_id,
3131
active: data.active,
32-
balance: data.balance,
33-
rewards: data.rewards,
34-
withdrawals: data.withdrawals,
32+
balance: data.controlled_amount,
33+
rewards: data.rewards_sum,
34+
withdrawals: data.withdrawals_sum,
3535
});
3636
})
3737
.catch((err) => {
@@ -44,13 +44,6 @@ export default function PageStaking() {
4444
withdrawals: "NA",
4545
});
4646
});
47-
blockchainProvider
48-
.get(`/accounts/${address}/registrations`)
49-
.then((data)=>{
50-
})
51-
.catch((err)=>{
52-
console.error("failed:", err)
53-
})
5447
}, [address, blockchainProvider]);
5548

5649
if (!stakingInfo) return <p>Loading staking info...</p>;

0 commit comments

Comments
 (0)