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: show balances of tokens of a nano contract #287

Merged
merged 4 commits into from
Aug 15, 2024
Merged
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
42 changes: 37 additions & 5 deletions src/screens/nano/NanoContractDetail.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import TxRow from '../../components/tx/TxRow';
import hathorLib from '@hathor/wallet-lib';
import nanoApi from '../../api/nanoApi';
import txApi from '../../api/txApi';
import { Link } from 'react-router-dom'
import { Link } from 'react-router-dom';
import { useSelector } from 'react-redux';


/**
Expand All @@ -37,6 +38,11 @@ function NanoContractDetail(props) {
// errorMessage {string | null} Error message in case a request to get nano contract data fails
const [errorMessage, setErrorMessage] = useState(null);


const { decimalPlaces } = useSelector((state) => {
return { decimalPlaces: state.serverInfo.decimal_places }
});

useEffect(() => {
let ignore = false;

Expand All @@ -56,8 +62,7 @@ function NanoContractDetail(props) {
}

const blueprintInformation = await nanoApi.getBlueprintInformation(transactionData.tx.nc_blueprint_id);
// TODO get all balances after hathor-core supports it
const dataState = await nanoApi.getState(ncId, Object.keys(blueprintInformation.attributes), [], []);
const dataState = await nanoApi.getState(ncId, Object.keys(blueprintInformation.attributes), ['__all__'], []);
if (ignore) {
// This is to prevent setting a state after the componenet has been already cleaned
return;
Expand Down Expand Up @@ -144,6 +149,33 @@ function NanoContractDetail(props) {
});
}

const renderBalances = () => {
return Object.entries(ncState.balances).map(([tokenUid, data]) => (
<tr key={tokenUid}>
<td>{tokenUid === hathorLib.constants.NATIVE_TOKEN_UID ? tokenUid : <Link to={`/token_detail/${tokenUid}`}>{tokenUid}</Link>}</td>
<td>{hathorLib.numberUtils.prettyValue(data.value, decimalPlaces)}</td>
</tr>
));
}

const renderNCBalances = () => {
return (
<div className="table-responsive">
<table className="table table-striped table-bordered" id="attributes-table">
<thead>
<tr>
<th className="d-lg-table-cell">Token</th>
<th className="d-lg-table-cell">Amount</th>
</tr>
</thead>
<tbody>
{renderBalances()}
r4mmer marked this conversation as resolved.
Show resolved Hide resolved
</tbody>
</table>
</div>
);
}

const renderNCAttributes = () => {
return (
<div className="table-responsive">
Expand Down Expand Up @@ -181,8 +213,6 @@ function NanoContractDetail(props) {
);
});
}

// TODO identify that attribute is a token and show as NFT, in case it is.
return (
<div className="content-wrapper">
<h3 className="mt-4">Nano Contract Detail</h3>
Expand All @@ -191,6 +221,8 @@ function NanoContractDetail(props) {
<p><strong>Blueprint: </strong>{ncState.blueprint_name} (<Link to={`/blueprint/detail/${txData.nc_blueprint_id}`}>{txData.nc_blueprint_id}</Link>)</p>
<h4 className="mt-5 mb-4">Attributes</h4>
{ renderNCAttributes() }
<h4 className="mt-3 mb-4">Balances</h4>
{ renderNCBalances() }
<hr />
<h3 className="mt-4">History</h3>
{history && loadTable()}
Expand Down