@@ -10,31 +10,36 @@ import { toast } from "@/hooks/use-toast";
1010import { getTxBuilder } from "@/utils/get-tx-builder" ;
1111import { getProvider } from "@/utils/get-provider" ;
1212import 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}
0 commit comments