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

TW-737 Use TZKT for getting a delegate if possible #1014

6 changes: 6 additions & 0 deletions public/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,12 @@
"tryAgain": {
"message": "Try again"
},
"errorGettingBakerAddressMessageOnline": {
"message": "Failed to get baker's address. Please, reload the page and try again."
},
"errorGettingBakerAddressMessage": {
"message": "Failed to get baker's address. Please, check your internet connection and try again."
},
"tezosMainnet": {
"message": "Tezos Mainnet",
"description": "Mainnet = main network"
Expand Down
64 changes: 37 additions & 27 deletions src/app/ErrorBoundary.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import React, { Component, ErrorInfo } from 'react';
import React, { Component, ErrorInfo, FC } from 'react';

import classNames from 'clsx';

import { ReactComponent as DangerIcon } from 'app/icons/danger.svg';
import { T } from 'lib/i18n';
import { t, T } from 'lib/i18n';
import { getOnlineStatus } from 'lib/temple/front';

interface ErrorBoundaryProps extends React.PropsWithChildren {
export interface ErrorBoundaryProps extends React.PropsWithChildren {
className?: string;
whileMessage?: string;
Content?: FC<{}>;
}

export class BoundaryError extends Error {
constructor(public readonly message: string, public readonly beforeTryAgain: EmptyFn) {
super(message);
}
}

type ErrorBoundaryState = {
Expand All @@ -33,37 +41,43 @@ export default class ErrorBoundary extends Component<ErrorBoundaryProps> {
});
}

tryAgain() {
async tryAgain() {
const { error } = this.state;
if (error instanceof BoundaryError) {
error.beforeTryAgain();
}
this.setState({ error: null });
}

getDefaultErrorMessage() {
const { whileMessage } = this.props;
const online = getOnlineStatus();
const firstPart = whileMessage ? t('smthWentWrongWhile', [whileMessage]) : t('smthWentWrong');

return online ? firstPart : [firstPart, t('mayHappenBecauseYouAreOffline')].join('. ');
}

componentDidUpdate(prevProps: ErrorBoundaryProps) {
if (prevProps.Content !== this.props.Content) {
this.setState({ error: null });
}
}

render() {
if (this.state.error) {
const online = getOnlineStatus();
const { className, Content, children: childrenFromProps } = this.props;
const { error } = this.state;
const children = Content ? <Content /> : childrenFromProps;

if (error) {
return (
<div className={classNames('w-full', 'flex items-center justify-center', this.props.className)}>
<div className={classNames('w-full', 'flex items-center justify-center', className)}>
<div className={classNames('max-w-xs', 'p-4', 'flex flex-col items-center', 'text-red-600')}>
<DangerIcon className="h-16 w-auto stroke-current" />

<T id="oops">{message => <h2 className="mb-1 text-2xl">{message}</h2>}</T>

<p className="mb-4 text-sm opacity-90 text-center font-light">
{this.props.whileMessage ? (
<T id="smthWentWrongWhile" substitutions={this.props.whileMessage} />
) : (
<T id="smthWentWrong" />
)}
{!online && (
<T id="mayHappenBecauseYouAreOffline">
{message => (
<>
{'. '}
{message}
</>
)}
</T>
)}
{error instanceof BoundaryError ? error.message : this.getDefaultErrorMessage()}
</p>

<T id="tryAgain">
Expand Down Expand Up @@ -92,10 +106,6 @@ export default class ErrorBoundary extends Component<ErrorBoundaryProps> {
);
}

return this.props.children;
return children;
}
}

function getOnlineStatus() {
return typeof navigator !== 'undefined' && typeof navigator.onLine === 'boolean' ? navigator.onLine : true;
}
3 changes: 2 additions & 1 deletion src/app/pages/AddAsset/AddAsset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { T, t } from 'lib/i18n';
import type { TokenMetadata } from 'lib/metadata';
import { fetchOneTokenMetadata } from 'lib/metadata/fetch';
import { TokenMetadataNotFoundError } from 'lib/metadata/on-chain';
import { getCacheKey } from 'lib/swr';
import { loadContract } from 'lib/temple/contract';
import {
useTezos,
Expand Down Expand Up @@ -228,7 +229,7 @@ const Form: FC = () => {
Repo.toAccountTokenKey(chainId, accountPkh, tokenSlug)
);

swrCache.delete(getBalanceSWRKey(tezos, tokenSlug, accountPkh));
swrCache.delete(getCacheKey(getBalanceSWRKey(tezos, tokenSlug, accountPkh)));

formAnalytics.trackSubmitSuccess();

Expand Down
15 changes: 8 additions & 7 deletions src/app/pages/Home/ContentSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import clsx from 'clsx';
import Spinner from 'app/atoms/Spinner/Spinner';
import { useTabSlug } from 'app/atoms/useTabSlug';
import { useAppEnv } from 'app/env';
import ErrorBoundary from 'app/ErrorBoundary';
import ErrorBoundary, { ErrorBoundaryProps } from 'app/ErrorBoundary';
import { ToolbarElement } from 'app/layouts/PageLayout';
import { ActivityComponent } from 'app/templates/activity/Activity';
import AssetInfo from 'app/templates/AssetInfo';
Expand Down Expand Up @@ -115,20 +115,21 @@ export const ContentSection: FC<Props> = ({ assetSlug, className }) => {
<div className={clsx('-mx-4 shadow-top-light', fullPage && 'rounded-t-md', className)}>
<TabsBar ref={tabBarElemRef} tabs={tabs} activeTabName={name} />

<SuspenseContainer whileMessage={whileMessageI18nKey ? t(whileMessageI18nKey) : 'displaying tab'}>
{Component && <Component />}
</SuspenseContainer>
<SuspenseContainer
whileMessage={whileMessageI18nKey ? t(whileMessageI18nKey) : 'displaying tab'}
Content={Component}
/>
</div>
);
};

interface SuspenseContainerProps extends PropsWithChildren {
interface SuspenseContainerProps extends Omit<ErrorBoundaryProps, 'className'> {
whileMessage: string;
fallback?: ReactNode;
}

const SuspenseContainer: FC<SuspenseContainerProps> = ({ whileMessage, fallback = <SpinnerSection />, children }) => (
<ErrorBoundary whileMessage={whileMessage}>
const SuspenseContainer: FC<SuspenseContainerProps> = ({ fallback = <SpinnerSection />, children, ...restProps }) => (
<ErrorBoundary {...restProps}>
<Suspense fallback={fallback}>{children}</Suspense>
</ErrorBoundary>
);
Expand Down
2 changes: 1 addition & 1 deletion src/app/pages/Home/OtherComponents/BakingSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const links = [

const BakingSection = memo(() => {
const acc = useAccount();
const { data: myBakerPkh } = useDelegate(acc.publicKeyHash);
const { data: myBakerPkh } = useDelegate(acc.publicKeyHash, true, false);
const canDelegate = acc.type !== TempleAccountType.WatchOnly;
const chainId = useChainId(true);
const { isDcpNetwork } = useGasToken();
Expand Down
12 changes: 8 additions & 4 deletions src/lib/apis/tzkt/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ import {
allInt32ParameterKeys,
TzktGetRewardsParams,
TzktGetRewardsResponse,
TzktRelatedContract
TzktRelatedContract,
TzktAccount
} from './types';

const TZKT_API_BASE_URLS = {
[TempleChainId.Mainnet]: 'https://api.tzkt.io/v1',
[TempleChainId.Jakartanet]: 'https://api.jakartanet.tzkt.io/v1',
[TempleChainId.Limanet]: 'https://api.limanet.tzkt.io/v1',
[TempleChainId.Mumbai]: 'https://api.mumbainet.tzkt.io/v1',
[TempleChainId.Nairobi]: 'https://api.nairobinet.tzkt.io/v1',
[TempleChainId.Ghostnet]: 'https://api.ghostnet.tzkt.io/v1',
[TempleChainId.Dcp]: 'https://explorer-api.tlnt.net/v1',
[TempleChainId.DcpTest]: 'https://explorer.tlnt.net:8009/v1'
[TempleChainId.DcpTest]: 'https://explorer-api.test.tlnt.net/v1'
};

export type TzktApiChainId = keyof typeof TZKT_API_BASE_URLS;
Expand Down Expand Up @@ -189,3 +190,6 @@ export const fetchAllTokensBalancesFromTzkt = async (selectedRpcUrl: string, acc

return balances;
};

export const getAccountStatsFromTzkt = async (account: string, chainId: TzktApiChainId) =>
fetchGet<TzktAccount>(chainId, `/accounts/${account}`);
3 changes: 3 additions & 0 deletions src/lib/apis/tzkt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ export type {
TzktAccountToken
} from './types';

export { TzktAccountType } from './types';

export type { TzktApiChainId } from './api';
export {
isKnownChainId,
getAccountStatsFromTzkt,
getDelegatorRewards,
getOneUserContracts,
fetchTzktTokens,
Expand Down
Loading
Loading