Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(wallet-dashboard): hook timelocked staking feature #1976

Merged
Merged
Show file tree
Hide file tree
Changes from 128 commits
Commits
Show all changes
143 commits
Select commit Hold shift + click to select a range
aca9c9a
feat(wallet-dashboard): fetch supply increase timelocked portfolio
cpl121 Jul 18, 2024
82cd25c
feat(wallet-dashboard): add new route to vesting
cpl121 Jul 19, 2024
e7206c8
feat(wallet-dashboard): update request timelocked objets to get all
cpl121 Jul 19, 2024
230fc19
feat(wallet-dashboard): improve the way to get timelock objects
cpl121 Jul 19, 2024
08e9357
Merge branch 'develop' into tooling-dashboard/fetch-supply-increases-…
cpl121 Jul 19, 2024
dc1ed20
Merge branch 'tooling-dashboard/fetch-supply-increases-timelocked-por…
cpl121 Jul 19, 2024
922bc68
Merge branch 'develop' into tooling-dashboard/fetch-supply-increases-…
cpl121 Jul 29, 2024
1d99a16
Merge branch 'tooling-dashboard/fetch-supply-increases-timelocked-por…
cpl121 Jul 29, 2024
2c63275
Merge branch 'develop' into tooling-dashboard/fetch-supply-increases-…
begonaalvarezd Jul 29, 2024
292311b
fix(wallet-dashboard): remove testing constants
cpl121 Jul 29, 2024
3649cb7
feat(wallet-dashboard): update filter in get timelocked objects
cpl121 Jul 29, 2024
d06aa2b
Merge branch 'develop' into tooling-dashboard/fetch-supply-increases-…
cpl121 Jul 29, 2024
a734306
Merge branches 'tooling-dashboard/add-a-vesting-page' and 'tooling-da…
cpl121 Jul 29, 2024
eb261a1
fix(wallet-dashboard): remove TESTING_SUPPLY_INCREASE_VESTING_LABEL r…
cpl121 Jul 30, 2024
615bd7a
Merge branch 'develop' into tooling-dashboard/fetch-supply-increases-…
cpl121 Jul 30, 2024
a284613
Merge branch 'tooling-dashboard/fetch-supply-increases-timelocked-por…
cpl121 Jul 30, 2024
8d22b5b
feat(wallet-dashboard): hook collect(unwrap) feature to dashboard WIP
cpl121 Jul 30, 2024
fe13bd1
Merge branch 'tooling-dashboard/add-a-vesting-page' into tooling-dash…
cpl121 Jul 30, 2024
20c1667
Merge remote-tracking branch 'origin/develop' into tooling-dashboard/…
begonaalvarezd Jul 31, 2024
0a5debf
refactor: revert prettier changes coming from last merge
begonaalvarezd Jul 31, 2024
09723be
fix(wallet-dashboard): remove debris
cpl121 Jul 31, 2024
336821c
fix(wallet-dashboard): add current epoch ms to getVestingOverview
cpl121 Jul 31, 2024
56ca42c
feat(wallet-dashboard): add stale time to get current epoch hook
cpl121 Jul 31, 2024
e129911
feat(wallet-dashboard): add timelock stakes to vesting portfolio logic
cpl121 Jul 31, 2024
f2598ea
Merge branches 'tooling-dashboard/add-timelock-stakes-to-vesting-port…
cpl121 Aug 2, 2024
17d0195
Merge branches 'tooling-dashboard/hook-collect-feature' and 'develop'…
cpl121 Aug 2, 2024
5b01bd4
Merge branch 'develop' into tooling-dashboard/hook-collect-feature
cpl121 Aug 5, 2024
6752846
feat(wallet-dashboard): add ptb to unlock all timelocked objects
cpl121 Aug 5, 2024
d675907
Merge branch 'develop' into tooling-dashboard/hook-collect-feature
cpl121 Aug 6, 2024
c29fecd
Merge branch 'develop' into tooling-dashboard/hook-collect-feature
cpl121 Aug 6, 2024
fed7374
Merge branch 'develop' into tooling-dashboard/hook-collect-feature
cpl121 Aug 6, 2024
78e7003
fix(wallet-dashboard): improvements
cpl121 Aug 6, 2024
bc5d115
Merge branch 'develop' into tooling-dashboard/hook-collect-feature
cpl121 Aug 6, 2024
f083429
Merge branch 'develop' into tooling-dashboard/add-timelock-stakes-to-…
cpl121 Aug 6, 2024
91faa9d
fix(wallet-dashboard): improve filters to get timelocked objects
cpl121 Aug 6, 2024
01f748d
feat(wallet-dashboard): map timelocked staked iota response
cpl121 Aug 7, 2024
36dfaea
Merge branch 'develop' into tooling-dashboard/add-timelock-stakes-to-…
cpl121 Aug 7, 2024
3a3afa1
Merge branch 'develop' into tooling-dashboard/hook-collect-feature
cpl121 Aug 7, 2024
3c82818
fix(wallet-dashboard): update Date.now by current epoch
cpl121 Aug 7, 2024
c01a478
fix(wallet-dashboard): improve transaction to unlock timelocked objects
cpl121 Aug 7, 2024
0123b2f
Merge branch 'develop' into tooling-dashboard/add-timelock-stakes-to-…
cpl121 Aug 7, 2024
9ee0710
Merge branch 'develop' into tooling-dashboard/hook-collect-feature
cpl121 Aug 8, 2024
ee95e68
fix(wallet-dashboard): improve unlock timelocked objects transaction
cpl121 Aug 8, 2024
7ed5621
Merge branch 'develop' into tooling-dashboard/hook-collect-feature
cpl121 Aug 8, 2024
3fed0cd
Merge branch 'develop' into tooling-dashboard/add-timelock-stakes-to-…
cpl121 Aug 8, 2024
c8c89fa
Merge branch 'develop' into tooling-dashboard/hook-collect-feature
cpl121 Aug 9, 2024
9f0277e
Merge branch 'develop' into tooling-dashboard/hook-collect-feature
cpl121 Aug 12, 2024
4bbd3c3
fix(wallet-dashboard): improvements and rename variables
cpl121 Aug 12, 2024
4f46fc0
Merge branch 'develop' into tooling-dashboard/add-timelock-stakes-to-…
cpl121 Aug 12, 2024
c47e268
Merge branch 'develop' into tooling-dashboard/hook-collect-feature
cpl121 Aug 12, 2024
6129782
fix(wallet-dashboard): rename hook
cpl121 Aug 12, 2024
9e65030
Merge branch 'develop' into tooling-dashboard/add-timelock-stakes-to-…
cpl121 Aug 12, 2024
c1ab238
feat(wallet-dashboard): use new endpoint to get staked timelocked obj…
cpl121 Aug 12, 2024
7e2b792
fix(wallet-dashboard): modify mapped data with the new response
cpl121 Aug 12, 2024
54a7370
refactor: separate vesting from timelock
begonaalvarezd Aug 12, 2024
03570ce
Merge branch 'develop' into tooling-dashboard/hook-collect-feature
cpl121 Aug 12, 2024
7ac9817
fix(wallet-dashboard): create a util function to check if a timelocke…
cpl121 Aug 12, 2024
8bdf259
fix(wallet-dashboard): remove redundant "all" in unlockedTimelockedOb…
cpl121 Aug 12, 2024
b310c31
Merge branches 'tooling-dashboard/add-timelock-stakes-to-vesting-port…
cpl121 Aug 12, 2024
25cebda
fix: mising files from refactor
begonaalvarezd Aug 12, 2024
17c7dbc
Merge pull request #1750 from iotaledger/tooling-wallet-dashboard/ref…
begonaalvarezd Aug 12, 2024
b2c1a63
Merge branch 'develop' into tooling-dashboard/hook-collect-feature
cpl121 Aug 13, 2024
4f78169
fix(wallet-dashboard): remove unnecessary conversion
cpl121 Aug 13, 2024
3762d67
Merge branch 'develop' into tooling-dashboard/add-timelock-stakes-to-…
cpl121 Aug 13, 2024
f6c4327
refactor: update iota-sdk name
begonaalvarezd Aug 13, 2024
c7511be
Merge branch 'develop' into tooling-dashboard/hook-collect-feature
begonaalvarezd Aug 13, 2024
88963a1
fix(wallet-dashboard): update logic with new DelegatedTimelockedStake…
cpl121 Aug 13, 2024
106aa11
fix(wallet-dashboard): update test logic with new DelegatedTimelocked…
cpl121 Aug 13, 2024
bb73c05
Merge branch 'develop' into tooling-dashboard/add-timelock-stakes-to-…
cpl121 Aug 13, 2024
04079ca
Merge branch 'develop' into tooling-dashboard/hook-collect-feature
begonaalvarezd Aug 13, 2024
2647a14
feat(wallet-dashboard): add hook to get validators info
cpl121 Aug 13, 2024
c8251c5
feat(wallet-dashboard): group and display timelocked stakes
cpl121 Aug 13, 2024
6194c74
feat(wallet-dashboard): hook timelocked unstaking feature
cpl121 Aug 13, 2024
91516f6
Merge remote-tracking branch 'origin/develop' into tooling-dashboard/…
begonaalvarezd Aug 15, 2024
c3786ff
Merge remote-tracking branch 'origin/develop' into tooling-dashboard/…
begonaalvarezd Aug 15, 2024
fb4e48e
fix(wallet-dashboard): invalidate query to fetch staked timelocked ob…
cpl121 Aug 16, 2024
276e16d
Merge branch 'develop' into tooling-dashboard/hook-collect-feature
cpl121 Aug 19, 2024
fb1a8ee
fix(wallet-dashboard): update delegated staked timelocked interface
cpl121 Aug 19, 2024
c3f0c5a
feat: add timlocked staking transaction
brancoder Aug 21, 2024
3246dc9
fix: adjust creation of stake transaction
brancoder Aug 22, 2024
6d11c5b
fix: polish util methods
brancoder Aug 22, 2024
cff0405
Merge branch 'develop' into tooling-dashboard/group-and-display-timel…
cpl121 Aug 23, 2024
2caaf31
Merge branch 'tooling-dashboard/group-and-display-timelocked-stakes' …
cpl121 Aug 23, 2024
1be97f7
fix(wallet-dashboard): invalidate queries after unstaking
cpl121 Aug 23, 2024
dfcc107
Merge branch 'tooling-dashboard/hook-timelocked-unstaking-feature' of…
cpl121 Aug 23, 2024
fbbcabf
Merge branch 'tooling-dashboard/hook-timelocked-unstaking-feature' in…
brancoder Aug 25, 2024
75fcb9e
fix: handling of unstake transaction query invalidation
brancoder Aug 25, 2024
ae58846
fix: resolve conflicts
brancoder Aug 25, 2024
9107ac8
Merge branch 'develop' into tooling-dashboard/group-and-display-timel…
cpl121 Aug 26, 2024
10e8431
fix(wallet-dashboard): rename timelocked staked object and modify the…
cpl121 Aug 26, 2024
3c0bce9
fix(wallet-dashboard): group timelocked staked objects
cpl121 Aug 26, 2024
0a8c2f8
Merge branch 'develop' into tooling-dashboard/hook-collect-feature
cpl121 Aug 26, 2024
d3a4f69
fix(wallet-dashboard): add waitForTransactionBlock to collect function
cpl121 Aug 26, 2024
9b1f9c9
Merge branches 'tooling-dashboard/hook-collect-feature' and 'develop'…
cpl121 Aug 26, 2024
91a3b33
Merge branch 'develop' into tooling-dashboard/group-and-display-timel…
cpl121 Aug 26, 2024
4c1ec00
Merge branch 'tooling-dashboard/hook-collect-feature' into tooling-da…
cpl121 Aug 26, 2024
8e3e6df
Merge branches 'tooling-dashboard/hook-timelocked-unstaking-feature' …
cpl121 Aug 26, 2024
d84b2ec
Merge branch 'develop' into tooling-dashboard/hook-collect-feature
brancoder Aug 26, 2024
3a73633
fix(wallet-dashboard): move groupTimelockedStakedObjects to timelock …
cpl121 Aug 26, 2024
a01b449
Merge branches 'tooling-dashboard/hook-timelocked-unstaking-feature' …
cpl121 Aug 26, 2024
0db4a9f
Merge branch 'develop' into tooling-dashboard/hook-collect-feature
begonaalvarezd Aug 26, 2024
fd50f3c
Merge branches 'tooling-dashboard/hook-collect-feature' and 'develop'…
cpl121 Aug 27, 2024
16ec8ff
fix(wallet-dashboard): rename useUnlockTimelockedObjects hook
cpl121 Aug 27, 2024
3eb87c9
Merge branches 'tooling-dashboard/group-and-display-timelocked-stakes…
cpl121 Aug 27, 2024
5d6d8b9
fix(wallet-dashboard): rename isTimelockedUnlockable function
cpl121 Aug 27, 2024
e40a80d
fix(wallet-dashboard): vesting tests
cpl121 Aug 27, 2024
5dba641
Merge branch 'develop' into tooling-dashboard/hook-collect-feature
cpl121 Aug 27, 2024
0866db0
Merge branch 'tooling-dashboard/hook-collect-feature' of github.com:i…
cpl121 Aug 27, 2024
a3fbcc2
Merge branches 'tooling-dashboard/hook-timelocked-unstaking-feature' …
cpl121 Aug 27, 2024
ba3d700
fix(tooling): linter
cpl121 Aug 27, 2024
adce389
Merge branches 'tooling-dashboard/group-and-display-timelocked-stakes…
cpl121 Aug 27, 2024
cb5710c
fix(wallet-dashboard): bad merge
cpl121 Aug 27, 2024
fa8a60a
Merge branch 'tooling-dashboard/group-and-display-timelocked-stakes' …
cpl121 Aug 27, 2024
461812b
Merge branch 'develop' into tooling-dashboard/group-and-display-timel…
brancoder Aug 27, 2024
832cd97
Merge branch 'tooling-dashboard/group-and-display-timelocked-stakes' …
cpl121 Aug 27, 2024
df1e894
fix(wallet-dashboard): improvement startEpoch value
cpl121 Aug 27, 2024
d4e776c
Merge branch 'tooling-dashboard/group-and-display-timelocked-stakes' …
cpl121 Aug 27, 2024
84e5f55
Merge branch 'tooling-dashboard/group-and-display-timelocked-stakes' …
cpl121 Aug 27, 2024
da3c587
fix(wallet-dashboard): rename startEpoch to stakeRequestEpoch in Time…
cpl121 Aug 27, 2024
a5442b6
Merge branch 'tooling-dashboard/group-and-display-timelocked-stakes' …
cpl121 Aug 27, 2024
cf7b0ed
fix(wallet-dashboard): update merge conflicts
cpl121 Aug 27, 2024
fd838cb
Merge branches 'tooling-dashboard/hook-timelocked-unstaking-feature' …
cpl121 Aug 27, 2024
619c6ac
fix(wallet-dashboard): improve styles
cpl121 Aug 27, 2024
c24779c
fix(wallet-dashboard): rename key to invalidate query
cpl121 Aug 27, 2024
a5e5258
fix: resolve conflicts
brancoder Aug 27, 2024
bd1eada
fix: polish variable naming and creation of timelocked staking tx
brancoder Aug 27, 2024
0d2b818
fix: lint
brancoder Aug 27, 2024
3b22063
fix: lint
brancoder Aug 27, 2024
e7d54c3
fix: resolve conflicts
brancoder Aug 28, 2024
a6d7516
fix: add comments for the extended timelock object
brancoder Aug 28, 2024
1eeb09c
fix: spelling
brancoder Aug 28, 2024
4cc5e9b
Merge branch 'develop' into tooling-dashboard/hook-timelocked-staking…
brancoder Aug 28, 2024
1ce3149
fix: spelling
brancoder Aug 28, 2024
63f707c
fix: resolve comments
brancoder Aug 29, 2024
969ae53
fix: add simple split algorythm
brancoder Aug 30, 2024
f099120
fix: spelling
brancoder Aug 30, 2024
d9b05e6
fix: variable naming
brancoder Aug 31, 2024
a119de2
Merge branch 'develop' into tooling-dashboard/hook-timelocked-staking…
brancoder Aug 31, 2024
4fb8428
fix: add missing break in adjusting split amounts
brancoder Sep 3, 2024
b0bfc5b
Merge branch 'develop' into tooling-dashboard/hook-timelocked-staking…
brancoder Sep 3, 2024
58419e3
fix: variable naming
brancoder Sep 4, 2024
f171540
Merge branch 'develop' into tooling-dashboard/hook-timelocked-staking…
brancoder Sep 7, 2024
e3abe9c
Merge branch 'develop' into tooling-dashboard/hook-timelocked-staking…
begonaalvarezd Sep 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions apps/core/src/utils/stake/createTimelockedStakeTransaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright (c) 2024 IOTA Stiftung
begonaalvarezd marked this conversation as resolved.
Show resolved Hide resolved
// SPDX-License-Identifier: Apache-2.0

import { TransactionBlock, TransactionObjectArgument } from '@iota/iota-sdk/transactions';
import { IOTA_SYSTEM_STATE_OBJECT_ID, IOTA_TYPE_ARG } from '@iota/iota-sdk/utils';

export interface ExtendedTimelockObject {
objectId: string;
begonaalvarezd marked this conversation as resolved.
Show resolved Hide resolved
expirationTimestamp: string;
totalLockedAmount: bigint;
objectIds: string[];
label?: string;
splitAmount?: bigint;
}

export function createTimelockedStakeTransaction(
timelockedObjects: ExtendedTimelockObject[],
validatorAddress: string,
) {
const tx = new TransactionBlock();
/**
* Create tranasctions to merge timelocked objects that need merging
*/
const mergeObjects = timelockedObjects.filter((obj) => obj.objectIds.length > 1);
begonaalvarezd marked this conversation as resolved.
Show resolved Hide resolved

for (const mergeObject of mergeObjects) {
// create an array of objectIds to be merged without the first element because first element is the principal object and its id is contained in mergeObject.objectId
const mergeObjectIds = mergeObject.objectIds
.slice(1)
begonaalvarezd marked this conversation as resolved.
Show resolved Hide resolved
.map((objectId) => tx.object(objectId));
tx.moveCall({
target: `0x02::timelock::join_vec`,
typeArguments: [`${IOTA_TYPE_ARG}`],
arguments: [
tx.object(mergeObject.objectId),
tx.makeMoveVec({ objects: mergeObjectIds }),
],
});
}
/**
* Create transactions to split timelocked objects that need splitting.
*/
const splitTimelockedObjects: ExtendedTimelockObject[] = timelockedObjects.filter(
begonaalvarezd marked this conversation as resolved.
Show resolved Hide resolved
(obj) => obj.splitAmount !== undefined && obj.splitAmount > 0,
);
const splitTimelockedObjectTransactions: TransactionObjectArgument[] = [];
splitTimelockedObjects.forEach((obj) => {
const [splitTx] = tx.moveCall({
target: `0x02::timelock::split`,
typeArguments: [`${IOTA_TYPE_ARG}`],
arguments: [tx.object(obj.objectId), tx.pure.u64(obj.splitAmount!)],
});
splitTimelockedObjectTransactions.push(tx.object(splitTx));
});
begonaalvarezd marked this conversation as resolved.
Show resolved Hide resolved

/**
* Create transactions to stake the timelocked objects
*/
msarcev marked this conversation as resolved.
Show resolved Hide resolved
const stakingReadyObjects = timelockedObjects
.filter((obj) => obj.splitAmount === undefined || obj.splitAmount === BigInt(0))
.map((obj) => tx.object(obj.objectId));
tx.moveCall({
target: `0x3::timelocked_staking::request_add_stake_mul_bal`,
arguments: [
tx.sharedObjectRef({
objectId: IOTA_SYSTEM_STATE_OBJECT_ID,
initialSharedVersion: 1,
mutable: true,
}),
tx.makeMoveVec({
objects: [...splitTimelockedObjectTransactions, ...stakingReadyObjects],
}),
tx.pure.address(validatorAddress),
],
});

return tx;
}
25 changes: 25 additions & 0 deletions apps/core/src/utils/stake/createTimelockedUnstakeTransaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import { TransactionBlock } from '@iota/iota-sdk/transactions';
import { IOTA_SYSTEM_STATE_OBJECT_ID } from '@iota/iota-sdk/utils';

export function createTimelockedUnstakeTransaction(timelockedStakedObjectIds: string[]) {
const tx = new TransactionBlock();
// TODO: check the max tx limit per ptb
for (const timelockedStakedObjectId of timelockedStakedObjectIds) {
tx.moveCall({
target: `0x3::timelocked_staking::request_withdraw_stake`,
arguments: [
tx.sharedObjectRef({
objectId: IOTA_SYSTEM_STATE_OBJECT_ID,
initialSharedVersion: 1,
mutable: true,
}),
tx.object(timelockedStakedObjectId),
],
});
}

return tx;
}
2 changes: 2 additions & 0 deletions apps/core/src/utils/stake/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
export * from './createUnstakeTransaction';
export * from './formatDelegatedStake';
export * from './createStakeTransaction';
export * from './createTimelockedUnstakeTransaction';
export * from './createTimelockedStakeTransaction';
67 changes: 42 additions & 25 deletions apps/wallet-dashboard/app/dashboard/vesting/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

'use client';

import { Button } from '@/components';
import { useGetCurrentEpochStartTimestamp, useNotifications } from '@/hooks';
import { Button, NewStakePopup, TimelockedUnstakePopup } from '@/components';
import { useGetCurrentEpochStartTimestamp, useNotifications, usePopups } from '@/hooks';
import {
formatDelegatedTimelockedStake,
getVestingOverview,
Expand All @@ -26,14 +26,15 @@ import {
useIotaClient,
useSignAndExecuteTransactionBlock,
} from '@iota/dapp-kit';
import { IotaValidatorSummary } from '@iota/iota-sdk/client';
import { useQueryClient } from '@tanstack/react-query';

function VestingDashboardPage(): JSX.Element {
const account = useCurrentAccount();
const queryClient = useQueryClient();
const iotaClient = useIotaClient();

const { addNotification } = useNotifications();
const { openPopup, closePopup } = usePopups();
const { data: currentEpochMs } = useGetCurrentEpochStartTimestamp();
const { data: activeValidators } = useGetActiveValidatorsInfo();
const { data: timelockedObjects } = useGetAllOwnedObjects(account?.address || '', {
Expand All @@ -53,11 +54,9 @@ function VestingDashboardPage(): JSX.Element {
Number(currentEpochMs),
);

function getValidatorName(validatorAddress: string): string {
return (
activeValidators?.find(
(activeValidator) => activeValidator.iotaAddress === validatorAddress,
)?.name || validatorAddress
function getValidatorByAddress(validatorAddress: string): IotaValidatorSummary | undefined {
return activeValidators?.find(
(activeValidator) => activeValidator.iotaAddress === validatorAddress,
);
}

Expand All @@ -78,7 +77,7 @@ function VestingDashboardPage(): JSX.Element {
})
.then(() => {
queryClient.invalidateQueries({
queryKey: ['get-staked-timelocked-objects', account?.address],
queryKey: ['get-timelocked-staked-objects', account?.address],
});
queryClient.invalidateQueries({
queryKey: [
Expand Down Expand Up @@ -114,13 +113,29 @@ function VestingDashboardPage(): JSX.Element {
addNotification('Collect transaction was not sent', NotificationType.Error);
});
};
const handleStake = () => {
console.log('Stake');
};

function handleUnstake(delegatedTimelockedStake: TimelockedStakedObjectsGrouped): void {
// TODO: handle unstake logic
console.info('delegatedTimelockedStake', delegatedTimelockedStake);
const validatorInfo = getValidatorByAddress(delegatedTimelockedStake.validatorAddress);
if (!account || !validatorInfo) {
addNotification('Cannot create transaction', NotificationType.Error);
return;
}

openPopup(
<TimelockedUnstakePopup
accountAddress={account.address}
delegatedStake={delegatedTimelockedStake}
validatorInfo={validatorInfo}
closePopup={closePopup}
onSuccess={handleOnSuccess}
/>,
);
}

function handleStake(): void {
openPopup(
<NewStakePopup onClose={closePopup} onSuccess={handleOnSuccess} isTimelockedStaking />,
);
}

return (
Expand All @@ -146,6 +161,7 @@ function VestingDashboardPage(): JSX.Element {
<div className="flex flex-col items-center rounded-lg border p-4">
<span>Available Staking</span>
<span>{vestingSchedule.availableStaking}</span>
<Button onClick={() => handleStake()}>Stake</Button>
cpl121 marked this conversation as resolved.
Show resolved Hide resolved
cpl121 marked this conversation as resolved.
Show resolved Hide resolved
</div>
</div>
</div>
Expand Down Expand Up @@ -174,7 +190,8 @@ function VestingDashboardPage(): JSX.Element {
>
<span>
Validator:{' '}
{getValidatorName(timelockedStakedObject.validatorAddress)}
{getValidatorByAddress(timelockedStakedObject.validatorAddress)
?.name || timelockedStakedObject.validatorAddress}
</span>
<span>
Stake Request Epoch: {timelockedStakedObject.stakeRequestEpoch}
Expand All @@ -188,17 +205,17 @@ function VestingDashboardPage(): JSX.Element {
);
})}
</div>
{account?.address && (
<div className="flex flex-row space-x-4">
{vestingSchedule.availableClaiming ? (
<Button onClick={handleCollect}>Collect</Button>
) : null}
{vestingSchedule.availableStaking ? (
<Button onClick={handleStake}>Stake</Button>
) : null}
</div>
)}
</div>
{account?.address && (
<div className="flex flex-row space-x-4">
{vestingSchedule.availableClaiming ? (
<Button onClick={handleCollect}>Collect</Button>
) : null}
{vestingSchedule.availableStaking ? (
<Button onClick={handleStake}>Stake</Button>
) : null}
</div>
)}
</div>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,68 @@

import React, { useState } from 'react';
import { EnterAmountView, SelectValidatorView } from './views';
import { useNotifications, useNewStakeTransaction } from '@/hooks';
import { parseAmount, useCoinMetadata, useGetValidatorsApy } from '@iota/core';
import {
useNotifications,
useNewStakeTransaction,
useGetCurrentEpochStartTimestamp,
} from '@/hooks';
import {
ExtendedTimelockObject,
parseAmount,
TIMELOCK_IOTA_TYPE,
useCoinMetadata,
useGetAllOwnedObjects,
useGetValidatorsApy,
} from '@iota/core';
import { useCurrentAccount, useSignAndExecuteTransactionBlock } from '@iota/dapp-kit';
import { IOTA_TYPE_ARG } from '@iota/iota-sdk/utils';
import { NotificationType } from '@/stores/notificationStore';
import { prepareObjectsForTimelockedStakingTransaction } from '@/lib/utils';

interface NewStakePopupProps {
onClose: () => void;
isTimelockedStaking?: boolean;
onSuccess?: (digest: string) => void;
}

enum Step {
SelectValidator,
EnterAmount,
}

function NewStakePopup({ onClose }: NewStakePopupProps): JSX.Element {
function NewStakePopup({
onClose,
onSuccess,
isTimelockedStaking,
}: NewStakePopupProps): JSX.Element {
const [step, setStep] = useState<Step>(Step.SelectValidator);
const [selectedValidator, setSelectedValidator] = useState<string>('');
const [amount, setAmount] = useState<string>('');
const account = useCurrentAccount();
const senderAddress = account?.address ?? '';

const { data: metadata } = useCoinMetadata(IOTA_TYPE_ARG);
const coinDecimals = metadata?.decimals ?? 0;
const amountWithoutDecimals = parseAmount(amount, coinDecimals);
const { data: currentEpochMs } = useGetCurrentEpochStartTimestamp();
const { data: timelockedObjects } = useGetAllOwnedObjects(senderAddress, {
StructType: TIMELOCK_IOTA_TYPE,
});

let extendedTimelockObjects: ExtendedTimelockObject[] = [];
if (isTimelockedStaking && timelockedObjects && currentEpochMs) {
extendedTimelockObjects = prepareObjectsForTimelockedStakingTransaction(
timelockedObjects,
amountWithoutDecimals,
currentEpochMs,
);
}

const { data: newStakeData } = useNewStakeTransaction(
selectedValidator,
amountWithoutDecimals,
account?.address ?? '',
senderAddress,
extendedTimelockObjects,
);

const { mutateAsync: signAndExecuteTransactionBlock } = useSignAndExecuteTransactionBlock();
Expand All @@ -58,9 +91,18 @@ function NewStakePopup({ onClose }: NewStakePopupProps): JSX.Element {
addNotification('Stake transaction was not created', NotificationType.Error);
return;
}
signAndExecuteTransactionBlock({
transactionBlock: newStakeData?.transaction,
})
signAndExecuteTransactionBlock(
{
transactionBlock: newStakeData?.transaction,
},
{
onSuccess: (tx) => {
if (onSuccess) {
onSuccess(tx.digest);
}
},
},
)
.then(() => {
onClose();
addNotification('Stake transaction has been sent');
Expand Down
Loading
Loading