Skip to content

Commit

Permalink
refactor: changes to use the new lib version
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroferreira1 committed Dec 7, 2023
1 parent 0ca4852 commit d164d12
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 37 deletions.
13 changes: 6 additions & 7 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,17 @@ import TokenDetail from './screens/TokenDetail';
import Dag from './screens/Dag';
import Dashboard from './screens/Dashboard';
import VersionError from './screens/VersionError';
import NanoContractDetail from './screens/NanoContractDetail';
import WebSocketHandler from './WebSocketHandler';
import { apiLoadErrorUpdate, dashboardUpdate, isVersionAllowedUpdate } from "./actions/index";
import { connect } from "react-redux";
import versionApi from './api/version';
import helpers from './utils/helpers';
import hathorLib from '@hathor/wallet-lib';
import { axios as hathorLibAxios, config as hathorLibConfig } from '@hathor/wallet-lib';
import { BASE_URL } from './constants';
import createRequestInstance from './api/customAxiosInstance';

const store = new hathorLib.MemoryStore();
hathorLib.storage.setStore(store);
hathorLib.storage.setItem('wallet:server', BASE_URL);

hathorLibConfig.setServerUrl(BASE_URL);

const mapDispatchToProps = dispatch => {
return {
Expand All @@ -57,11 +55,11 @@ class Root extends React.Component {
componentDidMount() {
WebSocketHandler.on('dashboard', this.handleWebsocket);

hathorLib.axios.registerNewCreateRequestInstance(createRequestInstance);
hathorLibAxios.registerNewCreateRequestInstance(createRequestInstance);
this.props.apiLoadErrorUpdate({apiLoadError: false});

versionApi.getVersion().then((data) => {
hathorLib.network.setNetwork(data.network.split('-')[0]);
hathorLibConfig.setNetwork(data.network.split('-')[0]);
this.props.isVersionAllowedUpdate({allowed: helpers.isVersionAllowed(data.version)});
}, (e) => {
// Error in request
Expand Down Expand Up @@ -118,6 +116,7 @@ class Root extends React.Component {
<NavigationRoute exact path="/statistics" component={Dashboard} />
<NavigationRoute exact path="/token_detail/:tokenUID" component={TokenDetail} />
<NavigationRoute exact path="/address/:address" component={AddressDetail} />
<NavigationRoute exact path="/nano_contract/detail/:nc_id" component={NanoContractDetail} />
<NavigationRoute exact path="" component={DashboardTx} />
</Switch>
</Router>
Expand Down
4 changes: 3 additions & 1 deletion src/components/AddressDetailExplorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ class AddressDetailExplorer extends React.Component {
*/
updateAddress = (address) => {
this.setState({ queryParams: this.pagination.obtainQueryParams() }, () => {
if (hathorLib.transaction.isAddressValid(address)) {
const network = hathorLib.config.getNetwork();
const addressObj = new hathorLib.Address(address, { network });
if (addressObj.isValid()) {
this.setState({
address,
loadingTokens: true,
Expand Down
4 changes: 3 additions & 1 deletion src/components/AddressDetailLegacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ class AddressDetailLegacy extends React.Component {
*/
updateAddress = (address) => {
this.setState({ queryParams: this.pagination.obtainQueryParams() }, () => {
if (hathorLib.transaction.isAddressValid(address)) {
const network = hathorLib.config.getNetwork();
const addressObj = new hathorLib.Address(address, { network });
if (addressObj.isValid()) {
this.setState({ address, loadingSummary: true, transactions: [], errorMessage: '' }, () => {
const queryParams = this.pagination.obtainQueryParams();
if (queryParams.token !== null) {
Expand Down
10 changes: 5 additions & 5 deletions src/components/AddressHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ class AddressHistory extends SortableTable {
*/
isAllAuthority = (tx) => {
for (let txin of tx.inputs) {
if (!hathorLib.wallet.isAuthorityOutput(txin) && txin.decoded.address === this.props.address) {
if (!hathorLib.transactionUtils.isAuthorityOutput(txin) && txin.decoded.address === this.props.address) {
return false;
}
}

for (let txout of tx.outputs) {
if (!hathorLib.wallet.isAuthorityOutput(txout) && txout.decoded.address === this.props.address) {
if (!hathorLib.transactionUtils.isAuthorityOutput(txout) && txout.decoded.address === this.props.address) {
return false;
}
}
Expand Down Expand Up @@ -103,12 +103,12 @@ class AddressHistory extends SortableTable {
}
return (
<tr key={tx.tx_id} className={trClass} onClick={(e) => this.props.onRowClicked(tx.tx_id)}>
<td className="d-none d-lg-table-cell pr-3">{hathorLib.helpers.getTxType(tx)}</td>
<td className="d-none d-lg-table-cell pr-3">{hathorLib.helpers.getShortHash(tx.tx_id)}</td>
<td className="d-none d-lg-table-cell pr-3">{hathorLib.transactionUtils.getTxType(tx)}</td>
<td className="d-none d-lg-table-cell pr-3">{hathorLib.helpersUtils.getShortHash(tx.tx_id)}</td>
<td className="d-none d-lg-table-cell pr-3">{dateFormatter.parseTimestamp(tx.timestamp)}</td>
<td className="state">{statusElement}</td>
<td className="value"><span className="">{prettyValue}</span></td>
<td className="d-lg-none d-table-cell pr-3" colSpan="3">{hathorLib.helpers.getTxType(tx)}<br/>{hathorLib.helpers.getShortHash(tx.tx_id)}<br/>{dateFormatter.parseTimestamp(tx.timestamp)}</td>
<td className="d-lg-none d-table-cell pr-3" colSpan="3">{hathorLib.transactionUtils.getTxType(tx)}<br/>{hathorLib.helpersUtils.getShortHash(tx.tx_id)}<br/>{dateFormatter.parseTimestamp(tx.timestamp)}</td>
</tr>
)
});
Expand Down
14 changes: 7 additions & 7 deletions src/components/AddressHistoryLegacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ class AddressHistory extends React.Component {

for (let txin of tx.inputs) {
if (txin.token === token && txin.decoded.address === this.props.address) {
if (!hathorLib.wallet.isAuthorityOutput(txin)) {
if (!hathorLib.transactionUtils.isAuthorityOutput(txin)) {
value -= txin.value;
}
}
}

for (let txout of tx.outputs) {
if (txout.token === token && txout.decoded.address === this.props.address) {
if (!hathorLib.wallet.isAuthorityOutput(txout)) {
if (!hathorLib.transactionUtils.isAuthorityOutput(txout)) {
value += txout.value;
}
}
Expand All @@ -54,13 +54,13 @@ class AddressHistory extends React.Component {
*/
isAllAuthority = (tx) => {
for (let txin of tx.inputs) {
if (!hathorLib.wallet.isAuthorityOutput(txin) && txin.decoded.address === this.props.address) {
if (!hathorLib.transactionUtils.isAuthorityOutput(txin) && txin.decoded.address === this.props.address) {
return false;
}
}

for (let txout of tx.outputs) {
if (!hathorLib.wallet.isAuthorityOutput(txout) && txout.decoded.address === this.props.address) {
if (!hathorLib.transactionUtils.isAuthorityOutput(txout) && txout.decoded.address === this.props.address) {
return false;
}
}
Expand Down Expand Up @@ -165,13 +165,13 @@ class AddressHistory extends React.Component {

return (
<tr key={tx.tx_id} className={trClass} onClick={(e) => this.props.onRowClicked(tx.tx_id)}>
<td className="d-none d-lg-table-cell pr-3">{hathorLib.helpers.getTxType(tx)}</td>
<td className="d-none d-lg-table-cell pr-3">{hathorLib.helpers.getShortHash(tx.tx_id)}</td>
<td className="d-none d-lg-table-cell pr-3">{hathorLib.transactionUtils.getTxType(tx)}</td>
<td className="d-none d-lg-table-cell pr-3">{hathorLib.helpersUtils.getShortHash(tx.tx_id)}</td>
<td className="d-none d-lg-table-cell pr-3">{dateFormatter.parseTimestamp(tx.timestamp)}</td>
<td className={tx.is_voided ? 'voided state' : 'state'}>{statusElement}</td>
<td>{tx.is_voided && <span className="voided-element">Voided</span>}</td>
<td className='value'><span className={tx.is_voided ? 'voided' : ''}>{prettyValue}</span></td>
<td className="d-lg-none d-table-cell pr-3" colSpan="3">{hathorLib.helpers.getTxType(tx)}<br/>{hathorLib.helpers.getShortHash(tx.tx_id)}<br/>{dateFormatter.parseTimestamp(tx.timestamp)}</td>
<td className="d-lg-none d-table-cell pr-3" colSpan="3">{hathorLib.transactionUtils.getTxType(tx)}<br/>{hathorLib.helpersUtils.getShortHash(tx.tx_id)}<br/>{dateFormatter.parseTimestamp(tx.timestamp)}</td>
</tr>
);
});
Expand Down
12 changes: 8 additions & 4 deletions src/components/Navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,15 @@ class Navigation extends React.Component {
if (regex.test(text)) {
// It's a valid hash
this.props.history.push(`/transaction/${text}`);
} else if (hathorLib.transaction.isAddressValid(text)) {
// It's a valid address
this.props.history.push(`/address/${text}`);
} else {
this.showError();
const network = hathorLib.config.getNetwork();
const addressObj = new hathorLib.Address(text, { network });
if (addressObj.isValid()) {
// It's a valid address
this.props.history.push(`/address/${text}`);
} else {
this.showError();
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/token/TokenConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const TokenConfig = (props) => {

const [token, setToken] = useState(props.token);
const [successMessage, setSuccessMessage] = useState('');
const configurationString = hathorLib.tokens.getConfigurationString(token.uid, token.name, token.symbol);
const configurationString = hathorLib.tokensUtils.getConfigurationString(token.uid, token.name, token.symbol);
const alertSuccess = useRef(null);

useEffect(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/token/TokenRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class TokenRow extends React.Component {
render() {
return (
<tr onClick={(e) => this.onRowClicked(this.props.token.uid)}>
<td className="d-lg-table-cell pr-3">{hathorLib.helpers.getShortHash(this.props.token.uid)}</td>
<td className="d-lg-table-cell pr-3">{hathorLib.helpersUtils.getShortHash(this.props.token.uid)}</td>
<td className="d-lg-table-cell pr-3">{this.props.token.name}</td>
<td className="d-lg-table-cell pr-3">{this.props.token.symbol}</td>
<td className="d-lg-table-cell pr-3">{this.props.token.nft ? 'NFT' : 'Custom Token'}</td>
Expand Down
2 changes: 1 addition & 1 deletion src/components/tx/TxRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class TxRow extends React.Component {
<tr onClick={(e) => this.handleClickTr(this.props.tx.tx_id)}>
<td className="d-none d-lg-table-cell pr-3">{this.props.tx.tx_id}</td>
<td className="d-none d-lg-table-cell pr-3">{dateFormatter.parseTimestamp(this.props.tx.timestamp)}</td>
<td className="d-lg-none d-table-cell pr-3" colSpan="2">{hathorLib.helpers.getShortHash(this.props.tx.tx_id)} {dateFormatter.parseTimestamp(this.props.tx.timestamp)}</td>
<td className="d-lg-none d-table-cell pr-3" colSpan="2">{hathorLib.helpersUtils.getShortHash(this.props.tx.tx_id)} {dateFormatter.parseTimestamp(this.props.tx.timestamp)}</td>
</tr>
);
}
Expand Down
11 changes: 6 additions & 5 deletions src/screens/DashboardTx.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

import React from 'react';
import TxRow from '../components/tx/TxRow';
import hathorLib from '@hathor/wallet-lib';
import WebSocketHandler from '../WebSocketHandler';
import { DASHBOARD_TX_COUNT, DASHBOARD_BLOCKS_COUNT } from '../constants';
import txApi from '../api/txApi';
import helpers from '../utils/helpers';


/**
Expand All @@ -26,11 +27,11 @@ class DashboardTx extends React.Component {

componentDidMount = () => {
this.getInitialData();
hathorLib.WebSocketHandler.on('network', this.handleWebsocket);
WebSocketHandler.on('network', this.handleWebsocket);
}

componentWillUnmount = () => {
hathorLib.WebSocketHandler.removeListener('network', this.handleWebsocket);
WebSocketHandler.removeListener('network', this.handleWebsocket);
}

/**
Expand Down Expand Up @@ -61,14 +62,14 @@ class DashboardTx extends React.Component {
if (tx.is_block) {
let blocks = this.state.blocks;

blocks = hathorLib.helpers.updateListWs(blocks, tx, DASHBOARD_BLOCKS_COUNT);
blocks = helpers.updateListWs(blocks, tx, DASHBOARD_BLOCKS_COUNT);

// Finally we update the state again
this.setState({ blocks });
} else {
let transactions = this.state.transactions;

transactions = hathorLib.helpers.updateListWs(transactions, tx, DASHBOARD_TX_COUNT);
transactions = helpers.updateListWs(transactions, tx, DASHBOARD_TX_COUNT);

// Finally we update the state again
this.setState({ transactions });
Expand Down
2 changes: 1 addition & 1 deletion src/screens/TransactionDetail.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class TransactionDetail extends React.Component {
updateTxInfo = (id) => {
txApi.getTransaction(id).then((data) => {
this.txReceived(data);
if (data.success && !hathorLib.helpers.isBlock(data.tx)) {
if (data.success && !hathorLib.transactionUtils.isBlock(data.tx)) {
this.getConfirmationData();
}
}, (e) => {
Expand Down
6 changes: 3 additions & 3 deletions src/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ const helpers = {
* @inner
*/
isMainnet() {
return hathorLib.network.getNetwork().name === 'mainnet';
return hathorLib.config.getNetwork().name === 'mainnet';
},

/**
Expand Down Expand Up @@ -233,9 +233,9 @@ const helpers = {
*/
renderValue(amount, isInteger) {
if (isInteger) {
return hathorLib.helpersUtils.prettyIntegerValue(amount);
return hathorLib.numberUtils.prettyIntegerValue(amount);
} else {
return hathorLib.helpersUtils.prettyValue(amount);
return hathorLib.numberUtils.prettyValue(amount);
}
},

Expand Down

0 comments on commit d164d12

Please sign in to comment.