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

Feat/959 pending transaction in tables. #1008

Merged
merged 4 commits into from
May 4, 2021
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
79 changes: 66 additions & 13 deletions src/app/containers/SwapHistory/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useTranslation } from 'react-i18next';
// import ReactPaginate from 'react-paginate';
import iconSuccess from 'assets/images/icon-success.svg';
import iconRejected from 'assets/images/icon-rejected.svg';
import { Sovryn } from 'utils/sovryn';
import iconPending from 'assets/images/icon-pending.svg';
import { bignumber } from 'mathjs';
import { weiToFixed } from 'utils/blockchain/math-helpers';
import { numberToUSD } from 'utils/display-text/format';
Expand All @@ -23,14 +23,19 @@ import { translations } from '../../../locales/i18n';
import { LoadableValue } from '../../components/LoadableValue';
import { useCachedAssetPrice } from '../../hooks/trading/useCachedAssetPrice';
import { AssetRenderer } from '../../components/AssetRenderer';
import { useSelector } from 'react-redux';
import { selectTransactionArray } from 'store/global/transactions-store/selectors';
import { TxStatus, TxType } from 'store/global/transactions-store/types';

export function SwapHistory() {
const transactions = useSelector(selectTransactionArray);
const account = useAccount();
const url = backendUrl[currentChainId];
const [history, setHistory] = useState([]) as any;
const [currentHistory, setCurrentHistory] = useState([]) as any;
const [loading, setLoading] = useState(false);
const { t } = useTranslation();
const assets = AssetsDictionary.list();

let cancelTokenSource;
const getData = () => {
Expand Down Expand Up @@ -77,7 +82,45 @@ export function SwapHistory() {
setCurrentHistory(history.slice(offset, offset + pageLimit));
};

const assets = AssetsDictionary.list();
const onGoingTransactions = useMemo(() => {
return transactions
.filter(
tx =>
tx.type === TxType.CONVERT_BY_PATH &&
[TxStatus.FAILED, TxStatus.PENDING].includes(tx.status),
)
.map(item => {
const { customData } = item;
let assetFrom = [] as any;
let assetTo = [] as any;

assetFrom = assets.find(
currency => currency.asset === customData?.sourceToken,
);
assetTo = assets.find(
currency => currency.asset === customData?.targetToken,
);

const data = {
status: item.status,
timestamp: customData?.date,
transaction_hash: item.transactionHash,
returnVal: {
_fromAmount: customData?.amount,
_toAmount: customData?.minReturn || null,
},
};

return (
<AssetRow
key={item.transactionHash}
data={data}
itemFrom={assetFrom}
itemTo={assetTo}
/>
);
});
}, [assets, transactions]);

return (
<section>
Expand Down Expand Up @@ -116,6 +159,7 @@ export function SwapHistory() {
</td>
</tr>
)}
{onGoingTransactions}
{currentHistory.map(item => {
let assetFrom = [] as any;
let assetTo = [] as any;
Expand Down Expand Up @@ -168,15 +212,10 @@ interface AssetProps {
}

function AssetRow({ data, itemFrom, itemTo }: AssetProps) {
const txStatus = async (hash: string) => {
return await Sovryn.getWeb3().eth.getTransactionReceipt(hash);
};
const tx = txStatus(data.transaction_hash).then(res => {
return res;
});

const { t } = useTranslation();
const dollars = useCachedAssetPrice(itemTo.asset, Asset.USDT);
const dollarValue = useMemo(() => {
if (data.returnVal._toAmount === null) return '';
return bignumber(data.returnVal._toAmount)
.mul(dollars.value)
.div(10 ** itemTo.decimals)
Expand Down Expand Up @@ -219,16 +258,30 @@ function AssetRow({ data, itemFrom, itemTo }: AssetProps) {
<td>
<div className="d-flex align-items-center justify-content-between col-lg-10 col-md-12 p-0">
<div>
{tx && <p className="m-0">Confirmed</p>}
{!tx && <p className="m-0">Failed</p>}
{!data.status && (
<p className="m-0">{t(translations.common.confirmed)}</p>
)}
{data.status === TxStatus.FAILED && (
<p className="m-0">{t(translations.common.failed)}</p>
)}
{data.status === TxStatus.PENDING && (
<p className="m-0">{t(translations.common.pending)}</p>
)}
<LinkToExplorer
txHash={data.transaction_hash}
className="text-gold font-weight-normal text-nowrap"
/>
</div>
<div>
{tx && <img src={iconSuccess} title="Confirmed" alt="Confirmed" />}
{!tx && <img src={iconRejected} title="Failed" alt="Failed" />}
{!data.status && (
<img src={iconSuccess} title="Confirmed" alt="Confirmed" />
)}
{data.status === TxStatus.FAILED && (
<img src={iconRejected} title="Failed" alt="Failed" />
)}
{data.status === TxStatus.PENDING && (
<img src={iconPending} title="Pending" alt="Pending" />
)}
</div>
</div>
</td>
Expand Down
8 changes: 7 additions & 1 deletion src/app/hooks/swap-network/useSwapNetwork_convertByPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,16 @@ export function useSwapNetwork_convertByPath(
nonce,
};
}

return send(args, config, {
type: TxType.CONVERT_BY_PATH,
approveTransactionHash: approveTx,
customData: {
sourceToken,
targetToken,
amount,
date: new Date().getTime() / 1000,
minReturn,
},
});
},
...rest,
Expand Down
4 changes: 2 additions & 2 deletions src/app/hooks/trading/useApproveAndTrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function useApproveAndTrade(
);

return {
trade: async () => {
trade: async (customData?: object) => {
let tx: CheckAndApproveResult = {};
if (collateral !== Asset.RBTC) {
tx = await contractWriter.checkAndApprove(
Expand All @@ -54,7 +54,7 @@ export function useApproveAndTrade(
return;
}
}
await trade(tx?.nonce, tx?.approveTx);
await trade(tx?.nonce, tx?.approveTx, customData);
},
...rest,
};
Expand Down
3 changes: 2 additions & 1 deletion src/app/hooks/trading/useMarginTrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function useMarginTrade(
);

return {
trade: (nonce?: number, approveTx?: string | null) =>
trade: (nonce?: number, approveTx?: string | null, customData?: object) =>
send(
[
loanId,
Expand All @@ -44,6 +44,7 @@ export function useMarginTrade(
{
approveTransactionHash: approveTx,
type: TxType.TRADE,
customData,
},
),
...txState,
Expand Down
2 changes: 2 additions & 0 deletions src/app/hooks/useSendContractTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface TransactionOptions {
approveTransactionHash?: Nullable<string>;
asset?: Asset;
assetAmount?: string;
customData?: { [key: string]: any };
}

export interface SendTxResponse {
Expand Down Expand Up @@ -89,6 +90,7 @@ export function useSendContractTx(
value: (config?.value as string) || '0',
asset: options?.asset || null,
assetAmount: options?.assetAmount || null,
customData: options?.customData || undefined,
};
dispatch(actions.addTransaction(txData));
setTx(txData);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import React from 'react';

import iconRejected from 'assets/images/icon-rejected.svg';
import iconPending from 'assets/images/icon-pending.svg';

import { weiToNumberFormat } from '../../../../../utils/display-text/format';
import { AssetsDictionary } from '../../../../../utils/dictionaries/assets-dictionary';
import { AssetRenderer } from '../../../../components/AssetRenderer';
import { Transaction, TxStatus } from 'store/global/transactions-store/types';
import { LinkToExplorer } from 'app/components/LinkToExplorer';
import { PositionBlock } from './PositionBlock';
import { useTranslation } from 'react-i18next';
import { translations } from '../../../../../locales/i18n';

interface Props {
item: Transaction;
}

export function PendingPositionRow({ item }: Props) {
const { customData } = item;
const { t } = useTranslation();

const collateralAssetDetails = AssetsDictionary.get(
customData?.collateralToken,
);

return (
<>
<tr>
<td>
<PositionBlock
position={customData?.position}
name={customData?.pair.name}
/>
</td>
<td colSpan={6}>
<div className="tw-truncate">
{weiToNumberFormat(customData?.amount, 4)}{' '}
<AssetRenderer asset={collateralAssetDetails.asset} />
</div>
</td>
<td>
<div className="d-flex align-items-center justify-content-between col-lg-10 col-md-12 p-0">
<div>
{item.status === TxStatus.FAILED && (
<p className="m-0">{t(translations.common.failed)}</p>
)}
{item.status === TxStatus.PENDING && (
<p className="m-0">{t(translations.common.pending)}</p>
)}
<LinkToExplorer
txHash={item.transactionHash}
className="text-gold font-weight-normal text-nowrap"
/>
</div>
<div>
{item.status === TxStatus.FAILED && (
<img src={iconRejected} title="Failed" alt="Failed" />
)}
{item.status === TxStatus.PENDING && (
<img src={iconPending} title="Pending" alt="Pending" />
)}
</div>
</div>
</td>
</tr>
</>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@ import { useGetActiveLoans } from 'app/hooks/trading/useGetActiveLoans';
import { useAccount } from 'app/hooks/useAccount';
import { SkeletonRow } from 'app/components/Skeleton/SkeletonRow';
import { OpenPositionRow } from './OpenPositionRow';
import { PendingPositionRow } from './PendingPositionRow';
import { useTranslation } from 'react-i18next';
import { translations } from '../../../../../locales/i18n';
import { Pagination } from '../../../../components/Pagination';

import { useSelector } from 'react-redux';
import { selectTransactionArray } from 'store/global/transactions-store/selectors';
import { TxStatus, TxType } from 'store/global/transactions-store/types';
interface Props {
perPage: number;
}

export function OpenPositionsTable(props: Props) {
const { t } = useTranslation();
const [page, setPage] = useState(1);
const transactions = useSelector(selectTransactionArray);

const { value, loading } = useGetActiveLoans(
useAccount(),
Expand All @@ -30,12 +34,31 @@ export function OpenPositionsTable(props: Props) {
[props.perPage, page, value],
);

const isEmpty = !loading && !items.length;
const isEmpty = !loading && !items.length && !transactions.length;

const onPageChanged = data => {
setPage(data.currentPage);
};

const onGoingTransactions = useMemo(() => {
return (
transactions.length > 0 && (
<>
{transactions
.filter(
tx =>
tx.type === TxType.TRADE &&
[TxStatus.FAILED, TxStatus.PENDING].includes(tx.status),
)
.reverse()
.map(item => (
<PendingPositionRow key={item.transactionHash} item={item} />
))}
</>
)
);
}, [transactions]);

return (
<>
<table className="tw-table">
Expand Down Expand Up @@ -73,6 +96,7 @@ export function OpenPositionsTable(props: Props) {
<td colSpan={99}>{t(translations.openPositionTable.noData)}</td>
</tr>
)}
{onGoingTransactions}

{loading && (
<tr>
Expand Down
10 changes: 9 additions & 1 deletion src/app/pages/MarginTradePage/components/TradeDialog/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,15 @@ export function TradeDialog() {
amount,
);

const submit = () => trade();
const submit = () =>
trade({
pair,
position,
collateralToken,
collateral,
leverage,
amount,
});

const txArgs = [
'0x0000000000000000000000000000000000000000000000000000000000000000', //0 if new loan
Expand Down
1 change: 1 addition & 0 deletions src/store/global/transactions-store/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export interface Transaction {
value: string;
asset: Nullable<Asset>;
assetAmount: Nullable<string>;
customData?: { [key: string]: any };
}

export interface RequestDialogState {
Expand Down