Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Fix wrong token handling #4254

Merged
merged 4 commits into from
Jan 20, 2017
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
2 changes: 1 addition & 1 deletion js/src/modals/Transfer/Details/details.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class TokenSelect extends Component {

return (
<MenuItem
key={ token.tag }
key={ `${index}_${token.tag}` }
value={ token.tag }
label={ label }
>
Expand Down
92 changes: 50 additions & 42 deletions js/src/redux/providers/balancesActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ const log = getLogger(LOG_KEYS.Balances);
const ETH = {
name: 'Ethereum',
tag: 'ETH',
image: imagesEthereum
image: imagesEthereum,
native: true
};

function setBalances (_balances, skipNotifications = false) {
return (dispatch, getState) => {
const state = getState();

const currentTokens = Object.values(state.balances.tokens || {});
const currentTags = [ 'eth' ]
.concat(currentTokens.map((token) => token.tag))
.filter((tag) => tag)
.map((tag) => tag.toLowerCase());
const tokensAddresses = currentTokens
.map((token) => token.address)
.filter((address) => address);

const accounts = state.personal.accounts;
const nextBalances = _balances;
Expand All @@ -61,53 +61,59 @@ function setBalances (_balances, skipNotifications = false) {
const prevTokens = balance.tokens.slice();
const nextTokens = [];

currentTags
.forEach((tag) => {
const prevToken = prevTokens.find((tok) => tok.token.tag.toLowerCase() === tag);
const nextToken = tokens.find((tok) => tok.token.tag.toLowerCase() === tag);
const handleToken = (prevToken, nextToken) => {
// If the given token is not in the current tokens, skip
if (!nextToken && !prevToken) {
return false;
}

// No updates
if (!nextToken) {
return nextTokens.push(prevToken);
}

// If the given token is not in the current tokens, skip
if (!nextToken && !prevToken) {
return false;
}
const { token, value } = nextToken;

// If it's a new token, push it
if (!prevToken) {
return nextTokens.push({
token, value
});
}

// No updates
if (!nextToken) {
return nextTokens.push(prevToken);
}
// Otherwise, update the value
const prevValue = prevToken.value;

const { token, value } = nextToken;
// If received a token/eth (old value < new value), notify
if (prevValue.lt(value) && accounts[address] && !skipNotifications) {
const account = accounts[address];
const txValue = value.minus(prevValue);

// If it's a new token, push it
if (!prevToken) {
return nextTokens.push({
token, value
});
}
const redirectToAccount = () => {
const route = `/accounts/${account.address}`;
dispatch(push(route));
};

// Otherwise, update the value
const prevValue = prevToken.value;
notifyTransaction(account, token, txValue, redirectToAccount);
}

// FIXME: Temporary hack to not continuously pop-up notifications until fixed
const FIXME_SKIP = false;
return nextTokens.push({
...prevToken,
value
});
};

// If received a token/eth (old value < new value), notify
if (FIXME_SKIP && prevValue.lt(value) && accounts[address] && !skipNotifications) {
const account = accounts[address];
const txValue = value.minus(prevValue);
const prevEthToken = prevTokens.find((tok) => tok.token.native);
const nextEthToken = tokens.find((tok) => tok.token.native);

const redirectToAccount = () => {
const route = `/accounts/${account.address}`;
dispatch(push(route));
};
handleToken(prevEthToken, nextEthToken);

notifyTransaction(account, token, txValue, redirectToAccount);
}
tokensAddresses
.forEach((address) => {
const prevToken = prevTokens.find((tok) => tok.token.address === address);
const nextToken = tokens.find((tok) => tok.token.address === address);

return nextTokens.push({
...prevToken,
value
});
handleToken(prevToken, nextToken);
});

balances[address] = { txCount: txCount || new BigNumber(0), tokens: nextTokens };
Expand Down Expand Up @@ -179,6 +185,8 @@ export function fetchTokens (_tokenIds, options = {}) {

return Promise
.all(tokenIds.map((id) => fetchTokenInfo(tokenreg, id, api)))
// FIXME ; shouldn't have to filter out tokens...
.then((tokens) => tokens.filter((token) => token.tag && token.tag.toLowerCase() !== 'eth'))
.then((tokens) => {
// dispatch only the changed images
tokens
Expand Down
4 changes: 2 additions & 2 deletions js/src/ui/Balance/balance.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Balance extends Component {

let body = (balance.tokens || [])
.filter((balance) => new BigNumber(balance.value).gt(0))
.map((balance) => {
.map((balance, index) => {
const token = balance.token;

let value;
Expand Down Expand Up @@ -76,7 +76,7 @@ class Balance extends Component {
return (
<div
className={ styles.balance }
key={ token.tag }
key={ `${index}_${token.tag}` }
>
<img
src={ imagesrc }
Expand Down