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

[beta] Token filter balances (throttle) #7742

Merged
merged 5 commits into from
Jan 31, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions js-old/src/redux/providers/balancesActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ export function fetchTokensBalances (updates, skipNotifications = false) {
const tokenIdsToFetch = Object.values(balances)
.reduce((tokenIds, balance) => {
const nextTokenIds = Object.keys(balance)
.filter((tokenId) => balance[tokenId].gt(0));
.filter((tokenId) => balance[tokenId] && balance[tokenId].gt(0));

return tokenIds.concat(nextTokenIds);
}, []);
Expand All @@ -328,7 +328,7 @@ export function fetchTokensBalances (updates, skipNotifications = false) {
dispatch(setBalances(balances, skipNotifications));
})
.catch((error) => {
console.warn('balances::fetchTokensBalances', error);
console.warn('v1: balances::fetchTokensBalances', error);
});
};
}
4 changes: 2 additions & 2 deletions js-old/src/redux/providers/tokensActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export function loadTokens (options = {}) {
}

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

return (dispatch, getState) => {
const { api } = getState();
Expand Down Expand Up @@ -154,7 +154,7 @@ export function loadTokensBasics (tokenIndexes, options) {

export function fetchTokens (_tokenIndexes) {
const tokenIndexes = uniq(_tokenIndexes || []);
const tokenChunks = chunk(tokenIndexes, 64);
const tokenChunks = chunk(tokenIndexes, 128);

return (dispatch, getState) => {
const { tokenReg } = Contracts.get();
Expand Down
88 changes: 57 additions & 31 deletions js-old/src/util/tokens/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

import { range } from 'lodash';
import { chunk, range } from 'lodash';
import BigNumber from 'bignumber.js';

import { hashToImageUrl } from '~/redux/util';
Expand Down Expand Up @@ -58,13 +58,11 @@ export function fetchTokensBasics (api, tokenReg, start = 0, limit = 100) {
return decodeArray(api, 'address[]', result);
})
.then((tokenAddresses) => {
return tokenAddresses.map((tokenAddress, index) => {
return tokenAddresses.map((address, index) => {
const tokenIndex = start + index;

return {
address: /^0x0*$/.test(tokenAddress)
? ''
: tokenAddress,
address,
id: getTokenId(tokenIndex),
index: tokenIndex,
fetched: false
Expand All @@ -80,12 +78,17 @@ export function fetchTokensBasics (api, tokenReg, start = 0, limit = 100) {

return tokens.map((token) => {
if (balances[token.id] && balances[token.id].gt(0)) {
token.address = '';
token.address = null;
}

return token;
});
});
})
.then((tokens) => {
return tokens.filter(({ address }) => {
return address && !/^0x0*$/.test(address);
});
});
}

Expand Down Expand Up @@ -195,19 +198,22 @@ export function fetchAccountsBalances (api, tokens, updates) {
});

const tokenPromise = Object.keys(tokenUpdates)
.reduce((tokenPromise, accountAddress) => {
.reduce((promises, accountAddress) => {
const tokenIds = tokenUpdates[accountAddress];
const updateTokens = tokens
.filter((t) => tokenIds.includes(t.id));

return tokenPromise
.then(() => fetchTokensBalances(api, updateTokens, [ accountAddress ]))
.then((balances) => {
tokensBalances[accountAddress] = balances[accountAddress];
});
}, Promise.resolve());
promises.push(
fetchTokensBalances(api, updateTokens, [ accountAddress ])
.then((balances) => {
tokensBalances[accountAddress] = balances[accountAddress];
})
);

return Promise.all([ ethPromise, tokenPromise ])
return promises;
}, []);

return Promise.all([ ethPromise, Promise.all(tokenPromise) ])
.then(() => {
const balances = Object.assign({}, tokensBalances);

Expand Down Expand Up @@ -243,36 +249,56 @@ function fetchEthBalances (api, accountAddresses) {
});
}

function fetchTokensBalances (api, tokens, accountAddresses) {
const tokenAddresses = tokens.map((t) => t.address);
const tokensBalancesCallData = encode(
api,
[ 'address[]', 'address[]' ],
[ accountAddresses, tokenAddresses ]
);
function fetchTokensBalances (api, _tokens, accountAddresses) {
const promises = chunk(_tokens, 128).map((tokens) => {
const data = tokensBalancesBytecode + encode(
api,
[ 'address[]', 'address[]' ],
[ accountAddresses, tokens.map(({ address }) => address) ]
);

return api.eth
.call({ data: tokensBalancesBytecode + tokensBalancesCallData })
.then((result) => {
const rawBalances = decodeArray(api, 'uint[]', result);
return api.eth.call({ data }).then((result) => {
const balances = {};
const rawBalances = decodeArray(api, 'uint[]', result);

accountAddresses.forEach((accountAddress, accountIndex) => {
const preIndex = accountIndex * tokens.length;
const balance = {};
const preIndex = accountIndex * tokenAddresses.length;

tokenAddresses.forEach((tokenAddress, tokenIndex) => {
const index = preIndex + tokenIndex;
const token = tokens[tokenIndex];

balance[token.id] = rawBalances[index];
tokens.forEach((token, tokenIndex) => {
balance[token.id] = rawBalances[preIndex + tokenIndex];
});

balances[accountAddress] = balance;
});

return balances;
});
});

return Promise.all(promises).then((results) => {
return results.reduce((combined, result) => {
Object
.keys(result)
.forEach((address) => {
if (!combined[address]) {
combined[address] = {};
}

Object
.keys(result[address])
.forEach((token) => {
const value = result[address][token];

if (value && value.gt(0)) {
combined[address][token] = result[address][token];
}
});
});

return combined;
}, {});
});
}

function getTokenId (...args) {
Expand Down
69 changes: 19 additions & 50 deletions js/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
"@parity/plugin-signer-default": "parity-js/plugin-signer-default#9a47bded9d6d70b69bb2f719732bd6f7854d1842",
"@parity/plugin-signer-hardware": "parity-js/plugin-signer-hardware#4320d818a053d4efae890b74a7476e4c8dc6ba10",
"@parity/plugin-signer-qr": "parity-js/plugin-signer-qr#2d1fafad347ba53eaf58c14265d4d07631d6a45c",
"@parity/shared": "2.2.21",
"@parity/shared": "2.2.23",
"@parity/ui": "3.0.22",
"keythereum": "1.0.2",
"lodash.flatten": "4.4.0",
Expand Down