-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EVM Staking - Adds wallet balance & total staked info container (#1843)
- Loading branch information
1 parent
39c16f3
commit 6913366
Showing
28 changed files
with
1,623 additions
and
1,250 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
apps/tangle-dapp/components/StatsMetricItem/StatsMetricItem.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { SkeletonLoader, Typography } from '@webb-tools/webb-ui-components'; | ||
import type { FC } from 'react'; | ||
import { Suspense } from 'react'; | ||
import { twMerge } from 'tailwind-merge'; | ||
|
||
import { | ||
getRoundedDownNumberWith2Decimals, | ||
splitTokenValueAndSymbol, | ||
} from '../../utils'; | ||
import { InfoIconWithTooltip } from '..'; | ||
import { StatsMetricItemProps } from './types'; | ||
|
||
export const StatsMetricItem: FC<StatsMetricItemProps> = ({ | ||
title, | ||
tooltip, | ||
className, | ||
...restProps | ||
}) => { | ||
return ( | ||
<div className={twMerge('flex flex-col gap-4', className)}> | ||
<Suspense fallback={<SkeletonLoader size="lg" />}> | ||
<StatsMetricItemValue {...restProps} /> | ||
</Suspense> | ||
|
||
<div className="flex items-center gap-0.5"> | ||
<Typography variant="body1" className="text-mono-140 dark:text-mono-40"> | ||
{title} | ||
</Typography> | ||
{tooltip && <InfoIconWithTooltip content={tooltip} />} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
/** @internal */ | ||
const StatsMetricItemValue = async ( | ||
props: Omit<StatsMetricItemProps, 'tooltip' | 'title'> | ||
) => { | ||
const { dataFetcher, address } = props; | ||
|
||
const value = await dataFetcher(address); | ||
|
||
const { value: value_, symbol } = splitTokenValueAndSymbol(String(value)); | ||
|
||
return ( | ||
<div className="flex gap-2 items-center"> | ||
<Typography | ||
variant="h4" | ||
fw="bold" | ||
className="text-mono-200 dark:text-mono-0" | ||
> | ||
{getRoundedDownNumberWith2Decimals(value_)} | ||
</Typography> | ||
|
||
<Typography | ||
variant="label" | ||
fw="medium" | ||
className="text-mono-140 dark:text-mono-40" | ||
> | ||
{symbol ? symbol : 'tTNT'} | ||
</Typography> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './StatsMetricItem'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { StatsMetricReturnType } from '../../types'; | ||
|
||
export interface StatsMetricItemProps { | ||
title: string; | ||
tooltip?: string; | ||
dataFetcher: (address: string) => Promise<StatsMetricReturnType>; | ||
address: string; | ||
className?: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { chainsConfig } from '@webb-tools/dapp-config/chains/chain-config'; | ||
import { PresetTypedChainId } from '@webb-tools/dapp-types/ChainId'; | ||
import { createPublicClient, defineChain, http } from 'viem'; | ||
|
||
const tangleTestnetConfig = chainsConfig[PresetTypedChainId.TangleTestnet]; | ||
delete tangleTestnetConfig.contracts; | ||
|
||
const tangleTestnet = defineChain(tangleTestnetConfig); | ||
|
||
export const evmClient = createPublicClient({ | ||
chain: tangleTestnet, | ||
transport: http(), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './evm'; | ||
export * from './polkadot'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
apps/tangle-dapp/containers/NominatorStatsContainer/NominatorStatsContainer.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
'use client'; | ||
|
||
import { useWebContext } from '@webb-tools/api-provider-environment'; | ||
import { Button, Divider } from '@webb-tools/webb-ui-components'; | ||
import { | ||
SOCIAL_URLS_RECORD, | ||
WEBB_TANGLE_DOCS_STAKING_URL, | ||
} from '@webb-tools/webb-ui-components/constants'; | ||
import cx from 'classnames'; | ||
import Link from 'next/link'; | ||
import { useMemo } from 'react'; | ||
|
||
import { StatsMetricItem } from '../../components'; | ||
import { getTokenWalletBalance, getTotalStakedAmount } from '../../data'; | ||
import { convertEthereumToSubstrateAddress } from '../../utils'; | ||
|
||
export const NominatorStatsContainer = () => { | ||
const { activeAccount } = useWebContext(); | ||
|
||
const walletAddress = useMemo(() => { | ||
if (!activeAccount?.address) return '0x0'; | ||
|
||
return activeAccount.address; | ||
}, [activeAccount?.address]); | ||
|
||
const substrateAddress = useMemo(() => { | ||
if (!activeAccount?.address) return undefined; | ||
|
||
return convertEthereumToSubstrateAddress(activeAccount.address); | ||
}, [activeAccount?.address]); | ||
|
||
return ( | ||
<div className="flex flex-col md:flex-row gap-4 w-full"> | ||
<div | ||
className={cx( | ||
'w-full rounded-2xl overflow-hidden h-[204px] p-4', | ||
'bg-glass dark:bg-glass_dark', | ||
'border-2 border-mono-0 dark:border-mono-160' | ||
)} | ||
> | ||
<StatsMetricItem | ||
title="Available tTNT in Wallet" | ||
dataFetcher={() => getTokenWalletBalance(walletAddress)} | ||
address={walletAddress} | ||
className="" | ||
/> | ||
|
||
<Divider className="my-6 bg-mono-0 dark:bg-mono-160" /> | ||
|
||
<Button variant="utility" className="w-full" isDisabled> | ||
Delegate | ||
</Button> | ||
</div> | ||
|
||
<div | ||
className={cx( | ||
'w-full rounded-2xl overflow-hidden h-[204px] p-4', | ||
'bg-glass dark:bg-glass_dark', | ||
'border-2 border-mono-0 dark:border-mono-160' | ||
)} | ||
> | ||
<StatsMetricItem | ||
title="Total Staked tTNT" | ||
tooltip="Total Staked tTNT." | ||
dataFetcher={() => getTotalStakedAmount(substrateAddress)} | ||
address={substrateAddress ?? ''} | ||
className="" | ||
/> | ||
|
||
<Divider className="my-6 bg-mono-0 dark:bg-mono-160" /> | ||
|
||
<div className="flex items-center gap-2"> | ||
<Link href={WEBB_TANGLE_DOCS_STAKING_URL} target="_blank"> | ||
<Button variant="utility" className="w-full"> | ||
Learn More | ||
</Button> | ||
</Link> | ||
|
||
<Link href={SOCIAL_URLS_RECORD.discord} target="_blank"> | ||
<Button variant="utility" className="w-full"> | ||
Join Community | ||
</Button> | ||
</Link> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './NominatorStatsContainer'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
export * from './HeaderChipsContainer'; | ||
export * from './KeyMetricsTableContainer'; | ||
export { Layout } from './Layout'; | ||
export * from './NominatorStatsContainer'; | ||
export * from './ValidatorTablesContainer'; | ||
export * from './WalletAndChainContainer'; | ||
export * from './WalletModalContainer'; |
27 changes: 27 additions & 0 deletions
27
apps/tangle-dapp/data/NominatorStats/getTokenWalletBalance.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import ensureHex from '@webb-tools/dapp-config/utils/ensureHex'; | ||
import { formatEther } from 'viem'; | ||
|
||
import { evmClient } from '../../constants'; | ||
import { StatsMetricReturnType } from '../../types'; | ||
|
||
export const getTokenWalletBalance = async ( | ||
address: string | ||
): Promise<StatsMetricReturnType> => { | ||
if (!address || address === '0x0') { | ||
return NaN; | ||
} | ||
|
||
try { | ||
const balance = await evmClient.getBalance({ | ||
address: ensureHex(address), | ||
}); | ||
|
||
const walletBalance = formatEther(balance); | ||
|
||
return Number(walletBalance) ?? NaN; | ||
} catch (e) { | ||
console.error(e); | ||
|
||
return NaN; | ||
} | ||
}; |
27 changes: 27 additions & 0 deletions
27
apps/tangle-dapp/data/NominatorStats/getTotalStakedAmount.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { u128 } from '@polkadot/types'; | ||
|
||
import { formatTokenBalance, getPolkadotApiPromise } from '../../constants'; | ||
import { StatsMetricReturnType } from '../../types'; | ||
|
||
export const getTotalStakedAmount = async ( | ||
address?: string | ||
): Promise<StatsMetricReturnType> => { | ||
const api = await getPolkadotApiPromise(); | ||
|
||
if (!api || !address) return NaN; | ||
|
||
try { | ||
const data = await api.query.staking.ledger(address); | ||
const ledger = data.unwrapOrDefault(); | ||
|
||
const totalStaked = new u128(api.registry, ledger.total.toString()); | ||
|
||
const availableTokenBalance = await formatTokenBalance(totalStaked); | ||
|
||
return availableTokenBalance ?? NaN; | ||
} catch (e) { | ||
console.error(e); | ||
|
||
return NaN; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './getTokenWalletBalance'; | ||
export * from './getTotalStakedAmount'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './HeaderChips'; | ||
export * from './NominatorStats'; | ||
export * from './TopLevelStats'; | ||
export * from './ValidatorTables'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.