Skip to content

Commit

Permalink
Move staking actions to the separate module, add accept function to a…
Browse files Browse the repository at this point in the history
…ctions
  • Loading branch information
dmytrotkk committed Nov 15, 2024
1 parent a53b441 commit 14379bf
Show file tree
Hide file tree
Showing 11 changed files with 323 additions and 104 deletions.
8 changes: 8 additions & 0 deletions src/App.scss
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,14 @@ body::-webkit-scrollbar {
background: rgb(244 139 54 / 13%);
}

.btnprimary {
background: rgba(147, 184, 236, 0.16);
}

.btnDisabled {
background: #262626;
}

.btnSmLoading {
padding: 0.5em 1.5em 0.5em 3em !important;
}
Expand Down
1 change: 1 addition & 0 deletions src/Router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ export default function Router() {
validator={validator}
isXs={isXs}
delegations={validatorDelegations}
getMainnetSigner={getMainnetSigner}
/>
}
/>
Expand Down
Binary file added src/assets/validators/v10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed src/assets/validators/v10.webp
Binary file not shown.
Binary file added src/assets/validators/v43.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed src/assets/validators/v43.webp
Binary file not shown.
9 changes: 8 additions & 1 deletion src/components/SkBtn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export default function SkBtn(props: {
'btn',
['btnSm', size === 'sm'],
['btnSmLoading', size === 'sm'],
['btnDisabled', props.loading],
props.className
)}
>
Expand All @@ -58,7 +59,13 @@ export default function SkBtn(props: {
<Button
color={props.color}
variant={props.variant}
className={cls('btn', 'btn' + props.color, props.className, ['btnSm', size === 'sm'])}
className={cls(
'btn',
'btn' + props.color,
props.className,
['btnSm', size === 'sm'],
['btnDisabled', props.disabled]
)}
disabled={props.disabled}
onClick={props.onClick}
startIcon={props.startIcon}
Expand Down
14 changes: 13 additions & 1 deletion src/components/delegation/Delegation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,24 @@ export default function Delegation(props: {
icon={<HistoryRoundedIcon className={cls(styles.chainIconxs)} />}
/>
)}
{Number(props.delegation.stateId) === DelegationState.PROPOSED && props.accept ? (
<SkBtn
loading={loading}
text={loading ? 'Accepting delegation' : 'Accept delegation'}
color="primary"
className={cls('fullW', cmn.mtop20)}
onClick={async () => {
props.accept && (await props.accept(delegationInfo))
}}
disabled={props.loading !== false || props.customAddress !== undefined}
/>
) : null}
{Number(props.delegation.stateId) === DelegationState.DELEGATED && props.unstake ? (
<SkBtn
loading={loading}
text={loading ? 'Unstaking tokens' : 'Unstake tokens'}
color="error"
className="fullW"
className={cls('fullW', cmn.mtop20)}
onClick={async () => {
props.unstake && (await props.unstake(delegationInfo))
}}
Expand Down
175 changes: 175 additions & 0 deletions src/core/delegation/stakingActions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/**
* @license
* SKALE portal
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

/**
* @file stakingActions.tsx
* @copyright SKALE Labs 2024-Present
*/

import { type Signer } from 'ethers'
import { sendTransaction } from '@skalenetwork/metaport'
import { type types } from '@/core'

import { initActionContract } from '../contracts'

export type LoadingState = types.staking.IRewardInfo | types.staking.IDelegationInfo | false
export type SetLoadingFn = (state: LoadingState) => void
export type SetErrorFn = (msg: string | undefined) => void
export type PostActionFn = () => Promise<void>

export interface StakingActionProps {
sc: types.staking.ISkaleContractsMap | null
address: types.AddressType | undefined
skaleNetwork: types.SkaleNetwork
getMainnetSigner: () => Promise<Signer>
setLoading: SetLoadingFn
setErrorMsg: SetErrorFn
postAction: PostActionFn
}

async function processTx({
delegationType,
txName,
txArgs,
contractType,
props
}: {
delegationType: types.staking.DelegationType
txName: string
txArgs: any[]
contractType: types.staking.ContractType
props: StakingActionProps
}) {
if (!props.sc || !props.address) return

try {
const signer = await props.getMainnetSigner()
const contract = await initActionContract(
signer,
delegationType,
props.address,
props.skaleNetwork,
contractType
)

const res = await sendTransaction(contract[txName], txArgs)
if (!res.status) {
props.setErrorMsg(res.err?.name)
} else {
props.setErrorMsg(undefined)
await props.postAction()
}
} catch (err: any) {
console.error(err)
props.setErrorMsg(err.message || 'Transaction failed')
} finally {
props.setLoading(false)
}
}

export async function retrieveRewards({
rewardInfo,
rewardAddress,
props
}: {
rewardInfo: types.staking.IRewardInfo
rewardAddress: types.AddressType
props: StakingActionProps
}) {
props.setLoading(rewardInfo)

await processTx({
delegationType: rewardInfo.delegationType,
txName: 'withdrawBounty',
txArgs: [rewardInfo.validatorId, rewardAddress],
contractType: 'distributor',
props
})
}

export async function unstakeDelegation({
delegationInfo,
props
}: {
delegationInfo: types.staking.IDelegationInfo
props: StakingActionProps
}) {
props.setLoading(delegationInfo)

await processTx({
delegationType: delegationInfo.delegationType,
txName: 'requestUndelegation',
txArgs: [delegationInfo.delegationId],
contractType: 'delegation',
props
})
}

export async function cancelDelegationRequest({
delegationInfo,
props
}: {
delegationInfo: types.staking.IDelegationInfo
props: StakingActionProps
}) {
props.setLoading(delegationInfo)

await processTx({
delegationType: delegationInfo.delegationType,
txName: 'cancelPendingDelegation',
txArgs: [delegationInfo.delegationId],
contractType: 'delegation',
props
})
}

export async function retrieveUnlockedTokens({
rewardInfo,
props
}: {
rewardInfo: types.staking.IRewardInfo
props: StakingActionProps
}) {
props.setLoading(rewardInfo)

await processTx({
delegationType: rewardInfo.delegationType,
txName: 'retrieve',
txArgs: [],
contractType: 'distributor',
props
})
}

export async function acceptDelegation({
delegationInfo,
props
}: {
delegationInfo: types.staking.IDelegationInfo
props: StakingActionProps
}) {
props.setLoading(delegationInfo)

await processTx({
delegationType: delegationInfo.delegationType,
txName: 'acceptPendingDelegation',
txArgs: [delegationInfo.delegationId],
contractType: 'delegation',
props
})
}
Loading

0 comments on commit 14379bf

Please sign in to comment.