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

chore(staking): refactor a bit to remove isSelf #1896

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
47 changes: 18 additions & 29 deletions apps/staking/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type Data = {
integrityStakingPublishers: {
name: string | undefined;
publicKey: PublicKey;
isSelf: boolean;
stakeAccount: PublicKey | undefined;
selfStake: bigint;
poolCapacity: bigint;
poolUtilization: bigint;
Expand Down Expand Up @@ -184,12 +184,7 @@ const loadDataNoStakeAccount = async (
cooldown2: 0n,
},
unlockSchedule: [],
integrityStakingPublishers: publishers.map(
({ stakeAccount, ...publisher }) => ({
...publisher,
isSelf: false,
}),
),
integrityStakingPublishers: publishers,
};
};

Expand Down Expand Up @@ -242,27 +237,21 @@ const loadDataForStakeAccount = async (
cooldown2: filterGovernancePositions(PositionState.UNLOCKED),
},
unlockSchedule,
integrityStakingPublishers: publishers.map(
({ stakeAccount: publisherStakeAccount, ...publisher }) => ({
...publisher,
isSelf: publisherStakeAccount?.equals(stakeAccount.address) ?? false,
positions: {
warmup: filterOISPositions(
publisher.publicKey,
PositionState.LOCKING,
),
staked: filterOISPositions(publisher.publicKey, PositionState.LOCKED),
cooldown: filterOISPositions(
publisher.publicKey,
PositionState.PREUNLOCKING,
),
cooldown2: filterOISPositions(
publisher.publicKey,
PositionState.UNLOCKED,
),
},
}),
),
integrityStakingPublishers: publishers.map((publisher) => ({
...publisher,
positions: {
warmup: filterOISPositions(publisher.publicKey, PositionState.LOCKING),
staked: filterOISPositions(publisher.publicKey, PositionState.LOCKED),
cooldown: filterOISPositions(
publisher.publicKey,
PositionState.PREUNLOCKING,
),
cooldown2: filterOISPositions(
publisher.publicKey,
PositionState.UNLOCKED,
),
},
})),
};
};

Expand Down Expand Up @@ -312,7 +301,7 @@ const loadPublisherData = async (
publicKey: publisher.pubkey,
qualityRanking: publisherRanking?.rank ?? 0,
selfStake: publisher.selfDelegation,
stakeAccount: publisher.stakeAccount,
stakeAccount: publisher.stakeAccount ?? undefined,
};
});
};
Expand Down
99 changes: 67 additions & 32 deletions apps/staking/src/components/OracleIntegrityStaking/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,20 @@ export const OracleIntegrityStaking = ({
yieldRate,
}: Props) => {
const self = useMemo(
() => publishers.find((publisher) => publisher.isSelf),
[publishers],
() =>
api.type === ApiStateType.Loaded &&
publishers.find((publisher) =>
publisher.stakeAccount?.equals(api.account.address),
),
[publishers, api],
);

const otherPublishers = useMemo(
() =>
publishers.filter(
(publisher) =>
!publisher.isSelf &&
(publisher.poolCapacity > 0n || hasAnyPositions(publisher)),
),
[publishers],
self === undefined
? publishers
: publishers.filter((publisher) => publisher !== self),
[publishers, self],
);

return (
Expand Down Expand Up @@ -697,11 +699,11 @@ type PublisherProps = {
currentEpoch: bigint;
availableToStake: bigint;
totalStaked: bigint;
isSelf?: boolean;
isSelf?: boolean | undefined;
publisher: {
name: string | undefined;
publicKey: PublicKey;
isSelf: boolean;
stakeAccount: PublicKey | undefined;
selfStake: bigint;
poolCapacity: bigint;
poolUtilization: bigint;
Expand Down Expand Up @@ -817,7 +819,7 @@ const Publisher = ({
<PublisherTableCell className="text-center">
<div>
{calculateApy({
isSelf: publisher.isSelf,
isSelf: isSelf ?? false,
selfStake: publisher.selfStake,
poolCapacity: publisher.poolCapacity,
poolUtilization: publisher.poolUtilization,
Expand Down Expand Up @@ -851,6 +853,7 @@ const Publisher = ({
availableToStake={availableToStake}
publisher={publisher}
yieldRate={yieldRate}
isSelf={isSelf ?? false}
/>
</PublisherTableCell>
</tr>
Expand Down Expand Up @@ -950,6 +953,7 @@ type StakeToPublisherButtonProps = {
currentEpoch: bigint;
availableToStake: bigint;
yieldRate: bigint;
isSelf: boolean;
};

const StakeToPublisherButton = ({
Expand All @@ -958,6 +962,7 @@ const StakeToPublisherButton = ({
availableToStake,
publisher,
yieldRate,
isSelf,
}: StakeToPublisherButtonProps) => {
const delegate = useTransferActionForPublisher(
api.type === ApiStateType.Loaded ? api.delegateIntegrityStaking : undefined,
Expand All @@ -981,27 +986,14 @@ const StakeToPublisherButton = ({
<>
<div className="mb-8 flex flex-row items-center justify-between text-sm">
<div>APY after staking</div>
<div className="font-medium">
{publisher.isSelf
? calculateApy({
isSelf: publisher.isSelf,
selfStake:
publisher.selfStake +
(amount.type === AmountType.Valid ? amount.amount : 0n),
poolCapacity: publisher.poolCapacity,
yieldRate,
})
: calculateApy({
isSelf: publisher.isSelf,
selfStake: publisher.selfStake,
poolCapacity: publisher.poolCapacity,
poolUtilization:
publisher.poolUtilization +
(amount.type === AmountType.Valid ? amount.amount : 0n),
yieldRate,
})}
%
</div>
<NewApy
className="font-medium"
isSelf={isSelf}
publisher={publisher}
yieldRate={yieldRate}
>
{amount.type === AmountType.Valid ? amount.amount : 0n}
</NewApy>
</div>
<StakingTimeline currentEpoch={currentEpoch} />
</>
Expand All @@ -1010,6 +1002,49 @@ const StakeToPublisherButton = ({
);
};

type NewApyProps = Omit<HTMLAttributes<HTMLDivElement>, "children"> & {
isSelf: boolean;
publisher: PublisherProps["publisher"];
yieldRate: bigint;
children: bigint;
};

const NewApy = ({
isSelf,
publisher,
yieldRate,
children,
...props
}: NewApyProps) => {
const apy = useMemo(
() =>
calculateApy({
poolCapacity: publisher.poolCapacity,
yieldRate,
...(isSelf
? {
isSelf: true,
selfStake: publisher.selfStake + children,
}
: {
isSelf: false,
selfStake: publisher.selfStake,
poolUtilization: publisher.poolUtilization + children,
}),
}),
[
publisher.poolCapacity,
yieldRate,
isSelf,
publisher.selfStake,
publisher.poolUtilization,
children,
],
);

return <div {...props}>{apy}%</div>;
};

type PublisherNameProps = Omit<HTMLAttributes<HTMLSpanElement>, "children"> & {
children: PublisherProps["publisher"];
fullKey?: boolean | undefined;
Expand Down
Loading