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

Refactor send page state management #10965

Merged
merged 4 commits into from
Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 18 additions & 0 deletions ui/ducks/send/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
hideLoadingIndication,
showConfTxPage,
showLoadingIndication,
updateTokenType,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So while trying to test this I re-found an issue similar to one I had been having while trying to tack a PR on top of the broad send-slice refactor.
Here it is, so entering the send flow the form status is immediately set to valid:
Screen Shot 2021-06-23 at 12 05 52 PM

once, however, I click the dropdown to select another token, the form becomes invalid:
Screen Shot 2021-06-23 at 12 06 22 PM

That is expected, but then I cannot get the form status to be valid again:
Screen Shot 2021-06-23 at 12 06 29 PM

even if I go back and select Eth as the send asset:
Screen Shot 2021-06-23 at 12 11 24 PM

I believe it has something to do with sequencing of actions and the gas loading thunk. The last call to validateSendState sets us to invalid here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you try 3d8ca2d out for a spin? see if the issue persists?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brad-decker That commit seems to have fixed the issue, 🚀

updateTransaction,
} from '../../store/actions';
import {
Expand Down Expand Up @@ -831,6 +832,7 @@ const slice = createSlice({
// 5. State is invalid if the send state is uninitialized
// 6. State is invalid if gas estimates are loading
// 7. State is invalid if gasLimit is less than the minimumGasLimit
// 8. State is invalid if the selected asset is a ERC721
case Boolean(state.amount.error):
case Boolean(state.gas.error):
case state.asset.type === ASSET_TYPES.TOKEN &&
Expand All @@ -843,6 +845,10 @@ const slice = createSlice({
):
state.status = SEND_STATUSES.INVALID;
break;
case state.asset.type === ASSET_TYPES.TOKEN &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not req but wondering if it makes sense to separate this out so that asset has an error field, as we do with amount and gas?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i was thinking that when I was working through erc721 updates, i think its a definite candidate for an improvement but not to be handled here.

state.asset.details.isERC721:
state.state = SEND_STATUSES.INVALID;
break;
default:
state.status = SEND_STATUSES.VALID;
// Recompute the draftTransaction object
Expand Down Expand Up @@ -1078,6 +1084,11 @@ export function updateSendAsset({ type, details }) {
details,
state.send.account.address ?? getSelectedAddress(state),
);
if (details && details.isERC721 === undefined) {
const updatedAssetDetails = await updateTokenType(details.address);
details.isERC721 = updatedAssetDetails.isERC721;
}

await dispatch(hideLoadingIndication());
} else {
// if changing to native currency, get it from the account key in send
Expand Down Expand Up @@ -1385,6 +1396,13 @@ export function getSendAssetAddress(state) {
return getSendAsset(state)?.details?.address;
}

export function getIsAssetSendable(state) {
if (state[name].asset.type === ASSET_TYPES.NATIVE) {
return true;
}
return state[name].asset.details.isERC721 === false;
}

// Amount Selectors
export function getSendAmount(state) {
return state[name].amount.value;
Expand Down
2 changes: 2 additions & 0 deletions ui/ducks/send/send.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ jest.mock('../../store/actions', () => {
return {
...actual,
estimateGas: jest.fn(() => Promise.resolve('0x0')),
updateTokenType: jest.fn(() => Promise.resolve({ isERC721: false })),
};
});

Expand Down Expand Up @@ -1757,6 +1758,7 @@ describe('Send Slice', () => {
address: '0xTokenAddress',
decimals: 18,
symbol: 'SYMB',
isERC721: false,
},
});
expect(actionResult[3].type).toStrictEqual(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ export default class SendAssetRow extends Component {
updateSendAsset: PropTypes.func.isRequired,
nativeCurrency: PropTypes.string,
nativeCurrencyImage: PropTypes.string,
setUnsendableAssetError: PropTypes.func.isRequired,
updateSendErrors: PropTypes.func.isRequired,
updateTokenType: PropTypes.func.isRequired,
};

static contextTypes = {
Expand All @@ -47,28 +44,7 @@ export default class SendAssetRow extends Component {

closeDropdown = () => this.setState({ isShowingDropdown: false });

clearUnsendableAssetError = () => {
this.props.setUnsendableAssetError(false);
this.props.updateSendErrors({
unsendableAssetError: null,
gasLoadingError: null,
});
};

selectToken = async (type, token) => {
if (token && token.isERC721 === undefined) {
const updatedToken = await this.props.updateTokenType(token.address);
if (updatedToken.isERC721) {
this.props.setUnsendableAssetError(true);
this.props.updateSendErrors({
unsendableAssetError: 'unsendableAssetError',
});
}
}

if ((token && token.isERC721 === false) || token === undefined) {
this.clearUnsendableAssetError();
}
selectToken = (type, token) => {
this.setState(
{
isShowingDropdown: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
getNativeCurrencyImage,
getAssetImages,
} from '../../../../selectors';
import { updateTokenType } from '../../../../store/actions';
import { updateSendAsset, getSendAssetAddress } from '../../../../ducks/send';
import SendAssetRow from './send-asset-row.component';

Expand All @@ -23,7 +22,6 @@ function mapStateToProps(state) {

function mapDispatchToProps(dispatch) {
return {
updateTokenType: (tokenAddress) => dispatch(updateTokenType(tokenAddress)),
updateSendAsset: ({ type, details }) =>
dispatch(updateSendAsset({ type, details })),
};
Expand Down
17 changes: 5 additions & 12 deletions ui/pages/send/send-content/send-content.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@ export default class SendContent extends Component {
t: PropTypes.func,
};

state = {
unsendableAssetError: false,
};

static propTypes = {
isAssetSendable: PropTypes.bool,
showAddToAddressBookModal: PropTypes.func,
showHexData: PropTypes.bool,
contact: PropTypes.object,
Expand All @@ -34,19 +31,16 @@ export default class SendContent extends Component {
noGasPrice: PropTypes.bool,
};

setUnsendableAssetError = (unsendableAssetError) =>
this.setState({ unsendableAssetError });

render() {
const {
warning,
error,
gasIsExcessive,
isEthGasPrice,
noGasPrice,
isAssetSendable,
} = this.props;

const { unsendableAssetError } = this.state;
let gasError;
if (gasIsExcessive) gasError = GAS_PRICE_EXCESSIVE_ERROR_KEY;
else if (noGasPrice) gasError = GAS_PRICE_FETCH_FAILURE_ERROR_KEY;
Expand All @@ -56,13 +50,12 @@ export default class SendContent extends Component {
<div className="send-v2__form">
{gasError && this.renderError(gasError)}
{isEthGasPrice && this.renderWarning(ETH_GAS_PRICE_FETCH_WARNING_KEY)}
{unsendableAssetError && this.renderError(UNSENDABLE_ASSET_ERROR_KEY)}
{isAssetSendable === false &&
this.renderError(UNSENDABLE_ASSET_ERROR_KEY)}
{error && this.renderError(error)}
{warning && this.renderWarning()}
{this.maybeRenderAddContact()}
<SendAssetRow
setUnsendableAssetError={this.setUnsendableAssetError}
/>
<SendAssetRow />
<SendAmountRow />
<SendGasRow />
{this.props.showHexData && <SendHexDataRow />}
Expand Down
3 changes: 2 additions & 1 deletion ui/pages/send/send-content/send-content.container.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
getNoGasPriceFetched,
} from '../../../selectors';

import { getSendTo } from '../../../ducks/send';
import { getIsAssetSendable, getSendTo } from '../../../ducks/send';

import * as actions from '../../../store/actions';
import SendContent from './send-content.component';
Expand All @@ -15,6 +15,7 @@ function mapStateToProps(state) {
const ownedAccounts = accountsWithSendEtherInfoSelector(state);
const to = getSendTo(state);
return {
isAssetSendable: getIsAssetSendable(state),
isOwnedAccount: Boolean(
ownedAccounts.find(
({ address }) => address.toLowerCase() === to.toLowerCase(),
Expand Down
25 changes: 10 additions & 15 deletions ui/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1246,21 +1246,6 @@ export function addToken(
};
}

export function updateTokenType(tokenAddress) {
return async (dispatch) => {
let token = {};
dispatch(showLoadingIndication());
try {
token = await promisifiedBackground.updateTokenType(tokenAddress);
} catch (error) {
log.error(error);
} finally {
dispatch(hideLoadingIndication());
}
return token;
};
}

export function removeToken(address) {
return (dispatch) => {
dispatch(showLoadingIndication());
Expand Down Expand Up @@ -2738,6 +2723,16 @@ export function estimateGas(params) {
return promisifiedBackground.estimateGas(params);
}

export async function updateTokenType(tokenAddress) {
let token = {};
try {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guess you felt loading indicator wasn't required here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its handled at the callsite now, the loading indicator is shown anytime we updateAsset to a token asset.

token = await promisifiedBackground.updateTokenType(tokenAddress);
} catch (error) {
log.error(error);
}
return token;
}

// MetaMetrics
/**
* @typedef {import('../../shared/constants/metametrics').MetaMetricsEventPayload} MetaMetricsEventPayload
Expand Down