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

Commit

Permalink
Refresh cached tokens based on registry info & random balances (#6818)
Browse files Browse the repository at this point in the history
* Refresh cached tokens based on registry info & random balances

* Don't display errored token images
  • Loading branch information
jacogr authored Oct 19, 2017
1 parent c128881 commit fdbf6bf
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
15 changes: 3 additions & 12 deletions js/src/redux/providers/tokensActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ function loadCachedTokens (tokenRegContract) {
// Check if we have data from the right contract
if (cached.tokenreg === tokenRegContract.address && cached.tokens) {
log.debug('found cached tokens', cached.tokens);
dispatch(_setTokens(cached.tokens));

// Fetch all the tokens images on load
// (it's the only thing that might have changed)
Expand Down Expand Up @@ -105,22 +104,13 @@ export function loadTokens (options = {}) {
};
}

export function loadTokensBasics (_tokenIndexes, options) {
export function loadTokensBasics (tokenIndexes, options) {
const limit = 64;

return (dispatch, getState) => {
const { api, tokens } = getState();
const { api } = getState();
const { tokenReg } = Contracts.get();
const nextTokens = {};
const prevTokensIndexes = Object.values(tokens).map((t) => t.index);

// Only fetch tokens we don't have yet
const tokenIndexes = _tokenIndexes
.filter((tokenIndex) => {
return !prevTokensIndexes.includes(tokenIndex);
})
.sort();

const count = tokenIndexes.length;

log.debug('loading basic tokens', tokenIndexes);
Expand Down Expand Up @@ -240,6 +230,7 @@ function fetchTokensData (tokenRegContract, tokenIndexes) {
log.debug('fetched', { fullResults, partialResults });

return [].concat(fullResults, partialResults)
.filter(({ address }) => !/0x0*$/.test(address))
.reduce((tokens, token) => {
const { id, image, address } = token;

Expand Down
14 changes: 12 additions & 2 deletions js/src/ui/TokenImage/tokenImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,19 @@ class TokenImage extends Component {
}).isRequired
};

state = {
error: false
};

render () {
const { error } = this.state;
const { api } = this.context;
const { image, token } = this.props;

const imageurl = token.image || image;
let imagesrc = unknownImage;

if (imageurl) {
if (imageurl && !error) {
const host = /^(\/)?api/.test(imageurl)
? api.dappsUrl
: '';
Expand All @@ -49,11 +54,16 @@ class TokenImage extends Component {

return (
<img
src={ imagesrc }
alt={ token.name }
onError={ this.handleError }
src={ imagesrc }
/>
);
}

handleError = () => {
this.setState({ error: true });
};
}

function mapStateToProps (iniState) {
Expand Down
20 changes: 18 additions & 2 deletions js/src/util/tokens/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,14 @@ export function fetchTokensBasics (api, tokenReg, start = 0, limit = 100) {
return api.eth
.call({ data: tokenAddressesBytcode + tokenAddressesCallData })
.then((result) => {
const tokenAddresses = decodeArray(api, 'address[]', result);

return decodeArray(api, 'address[]', result);
})
.then((tokenAddresses) => {
return tokenAddresses.map((tokenAddress, index) => {
if (/^0x0*$/.test(tokenAddress)) {
return null;
}

const tokenIndex = start + index;

return {
Expand All @@ -68,6 +73,17 @@ export function fetchTokensBasics (api, tokenReg, start = 0, limit = 100) {
fetched: false
};
});
})
.then((tokens) => tokens.filter((token) => token))
.then((tokens) => {
const randomAddress = sha3(`${Date.now()}`).substr(0, 42);

return fetchTokensBalances(api, tokens, [randomAddress])
.then((_balances) => {
const balances = _balances[randomAddress];

return tokens.filter(({ id }) => balances[id].eq(0));
});
});
}

Expand Down

0 comments on commit fdbf6bf

Please sign in to comment.