Skip to content

Commit

Permalink
add manually sentry logs to validators (#897)
Browse files Browse the repository at this point in the history
* add manually sentry logs to validators

* add error handling to swap & deposit ops

* prettify
  • Loading branch information
Ridel1e authored Aug 23, 2023
1 parent b34fdf7 commit 73b222b
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 55 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"@sentry/react": "^6.16.1",
"@sentry/tracing": "^6.16.1",
"@spectrumlabs/analytics": "^1.1.8",
"@spectrumlabs/cardano-dex-sdk": "^0.1.170",
"@spectrumlabs/cardano-dex-sdk": "^0.1.172",
"@types/file-saver": "^2.0.5",
"@types/lodash": "^4.14.172",
"@types/numeral": "^2.0.2",
Expand Down
28 changes: 25 additions & 3 deletions src/common/services/ErrorLogs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import { Severity } from '@sentry/react';
import { saveAs } from 'file-saver';

const errorLogs: { meta: any; error: any }[] = [];

interface OperationError {
readonly level: Severity;
readonly error: Error;
}

export const toSentryOperationError = (
error: Error | string,
): OperationError => ({
level: Severity.Critical,
error: typeof error === 'string' ? new Error(error as string) : error,
});

export const addErrorLog =
(meta: any) =>
(error: Error): void => {
Expand All @@ -10,9 +23,18 @@ export const addErrorLog =

export const downloadErrorLog = () => {
saveAs(
new Blob([JSON.stringify(errorLogs, null, 2)], {
type: 'text/plain;charset=utf-8',
}),
new Blob(
[
JSON.stringify(
errorLogs,
(_, value) => (typeof value === 'bigint' ? value.toString() : value),
2,
),
],
{
type: 'text/plain;charset=utf-8',
},
),
'logs.txt',
);
};
23 changes: 1 addition & 22 deletions src/components/ConfirmationModal/ConfirmationModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
Button,
CloseCircleOutlined,
Flex,
message,
Modal,
ModalRef,
Typography,
Expand All @@ -14,7 +13,6 @@ import { RequestProps } from '@ergolabs/ui-kit/dist/components/Modal/presets/Req
import { t, Trans } from '@lingui/macro';
import { DateTime } from 'luxon';
import { ReactNode } from 'react';
import CopyToClipboard from 'react-copy-to-clipboard';
import { TimeoutError } from 'rxjs';

import { applicationConfig } from '../../applicationConfig';
Expand All @@ -25,7 +23,6 @@ import { Currency } from '../../common/models/Currency';
import { downloadErrorLog } from '../../common/services/ErrorLogs';
import { exploreTx } from '../../gateway/utils/exploreAddress';
import { getLockingPeriodString } from '../../pages/Liquidity/utils';
import { useErrorEvent } from '../ErrorBoundary/ErrorEventProvider';

export enum Operation {
SWAP,
Expand Down Expand Up @@ -135,8 +132,6 @@ const ErrorModalContent = (
payload: ModalChainingPayload,
withErrorIcon?: boolean,
) => {
const { errorEvent } = useErrorEvent();

return (
<Flex col align="center">
{withErrorIcon && (
Expand All @@ -161,27 +156,11 @@ const ErrorModalContent = (
<Trans>Transaction rejected</Trans>
</Typography.Body>
</Flex.Item>
<Flex.Item marginBottom={errorEvent?.id ? 3 : 1}>
<Flex.Item marginBottom={2}>
<Typography.Body align="center" secondary>
<Trans>Try again later</Trans>
</Typography.Body>
</Flex.Item>
{errorEvent?.id && (
<Flex.Item marginBottom={2}>
<CopyToClipboard
text={errorEvent.id}
onCopy={() => message.success(t`Copied to clipboard!`)}
>
<Typography.Body
align="center"
secondary
style={{ cursor: 'pointer' }}
>
<Trans>Error id:</Trans> <br /> {errorEvent.id}
</Typography.Body>
</CopyToClipboard>
</Flex.Item>
)}
<Button size="large" type="dashed" onClick={downloadErrorLog}>
<Trans>Download Error log</Trans>
</Button>
Expand Down
9 changes: 9 additions & 0 deletions src/components/common/Layout/Header/BurgerMenu/BurgerMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
Button,
DownloadOutlined,
Dropdown,
FileTextOutlined,
GithubOutlined,
Expand All @@ -23,6 +24,7 @@ import {
LOCALE_LABEL,
SUPPORTED_LOCALES,
} from '../../../../../common/constants/locales';
import { downloadErrorLog } from '../../../../../common/services/ErrorLogs';
import { useApplicationSettings } from '../../../../../context';
import { useQuery } from '../../../../../hooks/useQuery';
import { ThemeSwitch } from '../../../../ThemeSwitch/ThemeSwitch';
Expand Down Expand Up @@ -100,6 +102,13 @@ const BurgerMenu = (): JSX.Element => {
setIsMainMenu(false);
},
},
{
title: t`Download Error Log`,
icon: <DownloadOutlined />,
onClick: () => {
downloadErrorLog();
},
},
];

const changeLanguage = (locale: string) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export const ammTxFeeMapping: AmmTxFeeMapping = {
swapExecution: 500000n,
depositExecution: 500000n,
redeemExecution: 500000n,
poolCreation: 2000000n,
};
27 changes: 26 additions & 1 deletion src/network/cardano/api/operations/common/inputSelector.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FullTxIn, InputSelector } from '@spectrumlabs/cardano-dex-sdk';
import { FullTxIn, InputSelector, TxHash } from '@spectrumlabs/cardano-dex-sdk';
import { TxOut } from '@spectrumlabs/cardano-dex-sdk/build/main/cardano/entities/txOut';
import { Value } from '@spectrumlabs/cardano-dex-sdk/build/main/cardano/entities/value';
import { CollateralSelector } from '@spectrumlabs/cardano-dex-sdk/build/main/cardano/wallet/collateralSelector';
import { catchError, filter, first, map, of, switchMap } from 'rxjs';
Expand Down Expand Up @@ -33,6 +34,30 @@ export class DefaultInputSelector implements InputSelector {
)
.toPromise() as Promise<FullTxIn[] | Error>;
}

selectById(txHash: TxHash, index: number): Promise<FullTxIn[] | Error> {
return selectedWallet$
.pipe(
first(),
switchMap((wallet) => {
if (wallet) {
return wallet.getUtxos();
}
throw new Error('insufficient funds');
}),
map((utxos) => {
const txOut: TxOut | undefined = utxos.find(
(utxo) => utxo.index === index && utxo.txHash === txHash,
);

if (!txOut) {
throw new Error('insufficient funds');
}
return [{ txOut }];
}),
)
.toPromise() as Promise<FullTxIn[] | Error>;
}
}

export class DefaultCollateralSelector implements CollateralSelector {
Expand Down
50 changes: 43 additions & 7 deletions src/network/cardano/api/operations/deposit.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
import { Transaction } from '@emurgo/cardano-serialization-lib-nodejs';
import { t } from '@lingui/macro';
import * as Sentry from '@sentry/react';
import { Severity } from '@sentry/react';
import { TxCandidate } from '@spectrumlabs/cardano-dex-sdk';
import { DepositTxInfo } from '@spectrumlabs/cardano-dex-sdk/build/main/amm/interpreters/ammTxBuilder/depositAmmTxBuilder';
import { NetworkParams } from '@spectrumlabs/cardano-dex-sdk/build/main/cardano/entities/env';
import { first, map, Observable, Subject, switchMap, tap, zip } from 'rxjs';
import {
catchError,
first,
map,
Observable,
Subject,
switchMap,
tap,
throwError,
zip,
} from 'rxjs';

import { Balance } from '../../../../common/models/Balance';
import { Currency } from '../../../../common/models/Currency';
import { addErrorLog } from '../../../../common/services/ErrorLogs';
import {
addErrorLog,
toSentryOperationError,
} from '../../../../common/services/ErrorLogs';
import { TxId } from '../../../../common/types';
import {
openConfirmationModal,
Expand Down Expand Up @@ -64,7 +79,10 @@ const toDepositTxCandidate = ({
pk: settings.ph!,
}),
),
map((data: [Transaction | null, TxCandidate, DepositTxInfo]) => data[0]!),
map(
(data: [Transaction | null, TxCandidate, DepositTxInfo, Error | null]) =>
data[0]!,
),
first(),
);
};
Expand All @@ -87,6 +105,7 @@ export const walletDeposit = (
),
switchMap((tx) => submitTx(tx)),
tap({ error: addErrorLog({ op: 'deposit' }) }),
catchError((err) => throwError(toSentryOperationError(err))),
);

export const deposit = (
Expand Down Expand Up @@ -142,10 +161,27 @@ export const useDepositValidators =
pk: settings.ph!,
}),
),
map((data: [Transaction | null, TxCandidate, DepositTxInfo]) =>
data[0]
? undefined
: t`Insufficient ${networkAsset.ticker} balance for fees`,
map(
(
data: [
Transaction | null,
TxCandidate,
DepositTxInfo,
Error | null,
],
) => {
const error = data[3];
if (error && !data[0]) {
Sentry.captureMessage(error?.message || (error as any), {
level: Severity.Critical,
});
addErrorLog({ op: 'depositValidation', ctx: data[2] })(error);
}

return data[0]
? undefined
: t`Insufficient ${networkAsset.ticker} balance for fees`;
},
),
first(),
);
Expand Down
26 changes: 22 additions & 4 deletions src/network/cardano/api/operations/redeem.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
import { Transaction } from '@emurgo/cardano-serialization-lib-nodejs';
import { RedeemTxInfo, TxCandidate } from '@spectrumlabs/cardano-dex-sdk';
import { NetworkParams } from '@spectrumlabs/cardano-dex-sdk/build/main/cardano/entities/env';
import { first, map, Observable, Subject, switchMap, tap, zip } from 'rxjs';
import {
catchError,
first,
map,
Observable,
Subject,
switchMap,
tap,
throwError,
zip,
} from 'rxjs';

import { Currency } from '../../../../common/models/Currency';
import { addErrorLog } from '../../../../common/services/ErrorLogs';
import {
addErrorLog,
toSentryOperationError,
} from '../../../../common/services/ErrorLogs';
import { TxId } from '../../../../common/types';
import {
openConfirmationModal,
Expand Down Expand Up @@ -50,8 +63,12 @@ const toRedeemTxCandidate = ({
}),
),
map(
([transaction]: [Transaction | null, TxCandidate, RedeemTxInfo]) =>
transaction!,
([transaction]: [
Transaction | null,
TxCandidate,
RedeemTxInfo,
Error | null,
]) => transaction!,
),
first(),
);
Expand All @@ -73,6 +90,7 @@ export const walletRedeem = (
),
switchMap((tx) => submitTx(tx)),
tap({ error: addErrorLog({ op: 'redeem' }) }),
catchError((err) => throwError(toSentryOperationError(err))),
);

export const redeem = (
Expand Down
9 changes: 7 additions & 2 deletions src/network/cardano/api/operations/refund.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
import { NetworkParams } from '@spectrumlabs/cardano-dex-sdk/build/main/cardano/entities/env';
import { CardanoWasm } from '@spectrumlabs/cardano-dex-sdk/build/main/utils/rustLoader';
import {
catchError,
combineLatest,
first,
map,
Expand All @@ -30,11 +31,15 @@ import {
Subject,
switchMap,
tap,
throwError,
zip,
} from 'rxjs';

import { Currency } from '../../../../common/models/Currency';
import { addErrorLog } from '../../../../common/services/ErrorLogs';
import {
addErrorLog,
toSentryOperationError,
} from '../../../../common/services/ErrorLogs';
import { TxId } from '../../../../common/types';
import {
openConfirmationModal,
Expand Down Expand Up @@ -121,7 +126,6 @@ const walletRefund = (txId: TxId): Observable<TxId> =>
),
),
),
tap(console.log, console.log),
map(([, tx, error]: [TxCandidate, Transaction | null, Error | null]) => {
if (!tx) {
throw error || new Error('');
Expand All @@ -130,6 +134,7 @@ const walletRefund = (txId: TxId): Observable<TxId> =>
}),
switchMap((tx) => submitTx(tx, true)),
tap({ error: addErrorLog({ txId, op: 'refund' }) }),
catchError((err) => throwError(toSentryOperationError(err))),
);

export const refund = (
Expand Down
Loading

0 comments on commit 73b222b

Please sign in to comment.