Skip to content

Commit

Permalink
Explore tx 2 (MystenLabs#1697)
Browse files Browse the repository at this point in the history
Co-authored-by: Chris Li <[email protected]>
  • Loading branch information
Jibz1 and 666lcz authored May 1, 2022
1 parent 2e824e5 commit 8a9fa49
Show file tree
Hide file tree
Showing 20 changed files with 2,510 additions and 1,839 deletions.
2 changes: 1 addition & 1 deletion explorer/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"scripts": {
"start": "react-scripts start",
"start:static": "REACT_APP_DATA=static PORT=8080 react-scripts start",
"test": "npx start-server-and-test 'npm run start:static' 8080 'react-scripts test --detectOpenHandles --watchAll=false'",
"test": "npx start-server-and-test 'yarn start:static' 8080 'react-scripts test --detectOpenHandles --watchAll=false'",
"build": "react-scripts build",
"eslint:check": "eslint --max-warnings=0 .eslintrc.js \"./src/**/*.{js,jsx,ts,tsx}\"",
"eslint:fix": "yarn eslint:check --fix",
Expand Down
Binary file added explorer/client/public/assets/SuiLogo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion explorer/client/src/__tests__/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ describe('End-to-end Tests', () => {
});
});

// TODO - rewrite this test to use the new transaction data and state object
/*
describe('Transaction Results', () => {
//Specific to transaction tests:
const successTransactionID = 'txCreateSuccess';
Expand Down Expand Up @@ -163,7 +165,7 @@ describe('End-to-end Tests', () => {
await expectErrorResult(page);
});
});

*/
describe('Object Results', () => {
const successObjectID = 'CollectionObject';
const problemObjectID = 'ProblemObject';
Expand Down
2 changes: 1 addition & 1 deletion explorer/client/src/components/footer/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function Footer() {
label="Mysten Labs"
/>
<ExternalLink
href="https://devportal-30dd0.web.app/"
href="https://docs.sui.io/"
label="Developer Hub"
/>
</nav>
Expand Down
13 changes: 9 additions & 4 deletions explorer/client/src/components/longtext/Longtext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function Longtext({
text,
category = 'unknown',
isLink = true,
alttext = '',
}: {
text: string;
category:
Expand All @@ -23,6 +24,7 @@ function Longtext({
| 'ethAddress'
| 'unknown';
isLink?: boolean;
alttext?: string;
}) {
const [isCopyIcon, setCopyIcon] = useState(true);
const [pleaseWait, setPleaseWait] = useState(false);
Expand Down Expand Up @@ -58,7 +60,7 @@ function Longtext({
if (category === 'unknown') {
textComponent = (
<span className={styles.longtext} onClick={navigateUnknown}>
{text}
{alttext ? alttext : text}
</span>
);
} else if (category === 'ethAddress') {
Expand All @@ -71,13 +73,16 @@ function Longtext({
);
} else {
textComponent = (
<Link className={styles.longtext} to={`/${category}/${text}`}>
{text}
<Link
className={styles.longtext}
to={`/${category}/${encodeURIComponent(text)}`}
>
{alttext ? alttext : text}
</Link>
);
}
} else {
textComponent = <span>{text}</span>;
textComponent = <span>{alttext ? alttext : text}</span>;
}

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
div.txcardgrid a {
@apply no-underline text-sky-600 hover:text-sky-900 cursor-pointer break-all;
}

div.txcardgrid h3 {
@apply font-normal leading-3 font-mono text-center;
}

div.txcard {
@apply font-mono md:flex grid items-center bg-center shadow-sm pt-5 pb-5 h-auto border-solid border border-gray-100 pl-5 pr-5 bg-white mt-5 mb-5 text-left;
}

div.txcardgridlarge {
@apply font-mono text-left;
}

div.txcardgrid > div {
@apply block pr-5;
}

div.txcardgrid > div:first-child {
@apply w-full md:w-1/3;
}

.txlatestesults {
@apply w-11/12 md:w-10/12 mx-auto mb-28 mt-10 m-auto;

max-width: 1200px;
}

div.txadd {
@apply flex md:flex-col flex-row;
}

.txheader {
@apply bg-offblack text-offwhite rounded-none md:h-10 shadow-none !important;
}

.txtype {
@apply md:w-2/12 max-w-sm;
}

.txgas {
@apply md:w-1/12 max-w-sm;
}

.txsearch {
@apply w-full;
}

.txsearch form {
@apply w-full;
}

.txsearch input {
@apply h-16 shadow-sm mr-0 ml-0 rounded-none pl-2 pr-2 border-solid border border-gray-100 text-base;
}

.txsearch [type='submit'] {
@apply md:w-1/12 w-20 bg-offblack text-offwhite hover:text-offblack hover:bg-sui;
}
196 changes: 196 additions & 0 deletions explorer/client/src/components/transaction-card/RecentTxCard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

import cl from 'classnames';
import { useEffect, useState } from 'react';
import { Link } from 'react-router-dom';

import Longtext from '../../components/longtext/Longtext';
import Search from '../../components/search/Search';
import theme from '../../styles/theme.module.css';
import { DefaultRpcClient as rpc } from '../../utils/api/DefaultRpcClient';
import ErrorResult from '../error-result/ErrorResult';

import styles from './RecentTxCard.module.css';

const initState = {
loadState: 'pending',
lastestTx: [],
};

const getRecentTransactions = async (txNum: number) => {
try {
// Get the lastest transactions
// TODO add batch transaction kind
// TODO sui.js to get the lastest transactions meta data
const transactions = await rpc
.getRecentTransactions(txNum)
.then((res: any) => res);
const txLatest = await Promise.all(
transactions.map(async (tx: any) => {
//
const txData = await rpc
.getTransaction(tx[1])
.then((res: any) => res)
.catch((err: any) => false);

// For tx with errors or not found
// return false and skip the transaction
if (!txData) {
return false;
}
const txKind = Object.keys(
txData.transaction.data.kind.Single
)[0];

return {
block: tx[0],
txId: tx[1],
// success: txData ? true : false,
kind: txKind,
From: txData.transaction.data.sender,
...(txKind === 'Transfer'
? {
To: txData.transaction.data.kind.Single.Transfer
.recipient,
}
: {}),
};
})
);
// Remove failed transactions and sort by block number
return txLatest
.filter((itm) => itm)
.sort((a: any, b: any) => b.block - a.block);
} catch (error) {
throw error;
}
};

function truncate(fullStr: string, strLen: number, separator: string) {
if (fullStr.length <= strLen) return fullStr;

separator = separator || '...';

const sepLen = separator.length,
charsToShow = strLen - sepLen,
frontChars = Math.ceil(charsToShow / 2),
backChars = Math.floor(charsToShow / 2);

return (
fullStr.substr(0, frontChars) +
separator +
fullStr.substr(fullStr.length - backChars)
);
}

function LastestTxCard() {
const [isLoaded, setIsLoaded] = useState(false);
const [results, setResults] = useState(initState);
useEffect(() => {
let isMounted = true;
getRecentTransactions(15)
.then((resp: any) => {
if (isMounted) {
setIsLoaded(true);
}
setResults({
loadState: 'loaded',
lastestTx: resp,
});
})
.catch((err) => {
setResults({
...initState,
});
});

return () => {
isMounted = false;
};
}, []);
if (!isLoaded) {
return (
<div className={theme.textresults}>
<div className={styles.content}>Loading...</div>
</div>
);
}

if (!isLoaded && results.loadState === 'fail') {
return (
<ErrorResult
id="lastestTx"
errorMsg="There was an issue getting the lastest transaction"
/>
);
}

return (
<div className={styles.txlatestesults}>
<div className={styles.txcardgrid}>
<h3>Latest Transaction</h3>
</div>
<div className={styles.txsearch}>{isLoaded && <Search />}</div>
<div className={styles.transactioncard}>
<div>
<div
className={cl(
styles.txcardgrid,
styles.txcard,
styles.txheader
)}
>
<div className={styles.txcardgridlarge}>TxId</div>
<div className={styles.txtype}>Tx Type</div>
<div className={styles.txgas}>Gas</div>
<div className={styles.txadd}>Sender & Reciever</div>
</div>
{results.lastestTx.map((tx: any, index: number) => (
<div
key={index}
className={cl(styles.txcardgrid, styles.txcard)}
>
<div className={styles.txcardgridlarge}>
<div className={styles.txlink}>
<Longtext
text={tx.txId}
category="transactions"
isLink={true}
alttext={truncate(tx.txId, 26, '...')}
/>
</div>
</div>
<div className={styles.txtype}> {tx.kind}</div>
<div className={styles.txgas}> 10</div>
<div className={styles.txadd}>
<div>
From:
<Link
className={styles.txlink}
to={'addresses/' + tx.From}
>
{truncate(tx.From, 25, '...')}
</Link>
</div>
{tx.To && (
<div>
To :
<Link
className={styles.txlink}
to={'addresses/' + tx.To}
>
{truncate(tx.To, 25, '...')}
</Link>
</div>
)}
</div>
</div>
))}
</div>
</div>
</div>
);
}

export default LastestTxCard;
Loading

0 comments on commit 8a9fa49

Please sign in to comment.