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 1174 Use TZKT for getting a delegate if possible #1023

Merged
Show file tree
Hide file tree
Changes from 13 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
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
62 changes: 36 additions & 26 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 {
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
36 changes: 22 additions & 14 deletions src/app/pages/Home/ContentSection.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FC, ReactNode, Suspense, useCallback, useMemo, useRef } from 'react';
import React, { FC, memo, Suspense, useCallback, useMemo, useRef } from 'react';

import clsx from 'clsx';

Expand Down Expand Up @@ -33,7 +33,7 @@ interface TabData {
whileMessageI18nKey?: TID;
}

export const ContentSection: FC<Props> = ({ assetSlug, className }) => {
export const ContentSection = memo<Props>(({ assetSlug, className }) => {
const { fullPage } = useAppEnv();
const tabSlug = useTabSlug();

Expand Down Expand Up @@ -115,25 +115,33 @@ 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>
<ContentContainer
ContentComponent={Component}
whileMessage={whileMessageI18nKey ? t(whileMessageI18nKey) : 'displaying tab'}
/>
</div>
);
};
});

interface SuspenseContainerProps extends PropsWithChildren {
interface ContentContainerProps {
whileMessage: string;
fallback?: ReactNode;
ContentComponent: React.FC | React.ExoticComponent;
}

const SuspenseContainer: FC<SuspenseContainerProps> = ({ whileMessage, fallback = <SpinnerSection />, children }) => (
<ErrorBoundary whileMessage={whileMessage}>
<Suspense fallback={fallback}>{children}</Suspense>
</ErrorBoundary>
);
const ContentContainer = memo<ContentContainerProps>(({ whileMessage, ContentComponent }) => {
const ErrorBoundaryContent = useCallback(
() => (
<Suspense fallback={<SpinnerSection />}>
<ContentComponent />
</Suspense>
),
[ContentComponent]
);

return <ErrorBoundary whileMessage={whileMessage} Content={ErrorBoundaryContent} />;
});
keshan3262 marked this conversation as resolved.
Show resolved Hide resolved

const SpinnerSection: FC = () => (
const SpinnerSection = () => (
<div className="flex justify-center my-12">
<Spinner theme="gray" className="w-20" />
</div>
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