[beta] Token filter balances (throttle) (#7742)
* [beta] Token filter balances (throttle) * Cleanups * Remove unused uniq * Update @parity/shared to 2.2.23 * Remove unused code paths
This commit is contained in:
parent
627d1a4971
commit
73be0fb096
@ -306,7 +306,7 @@ export function fetchTokensBalances (updates, skipNotifications = false) {
|
|||||||
const tokenIdsToFetch = Object.values(balances)
|
const tokenIdsToFetch = Object.values(balances)
|
||||||
.reduce((tokenIds, balance) => {
|
.reduce((tokenIds, balance) => {
|
||||||
const nextTokenIds = Object.keys(balance)
|
const nextTokenIds = Object.keys(balance)
|
||||||
.filter((tokenId) => balance[tokenId].gt(0));
|
.filter((tokenId) => balance[tokenId] && balance[tokenId].gt(0));
|
||||||
|
|
||||||
return tokenIds.concat(nextTokenIds);
|
return tokenIds.concat(nextTokenIds);
|
||||||
}, []);
|
}, []);
|
||||||
@ -328,7 +328,7 @@ export function fetchTokensBalances (updates, skipNotifications = false) {
|
|||||||
dispatch(setBalances(balances, skipNotifications));
|
dispatch(setBalances(balances, skipNotifications));
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.warn('balances::fetchTokensBalances', error);
|
console.warn('v1: balances::fetchTokensBalances', error);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -105,7 +105,7 @@ export function loadTokens (options = {}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function loadTokensBasics (tokenIndexes, options) {
|
export function loadTokensBasics (tokenIndexes, options) {
|
||||||
const limit = 64;
|
const limit = 128;
|
||||||
|
|
||||||
return (dispatch, getState) => {
|
return (dispatch, getState) => {
|
||||||
const { api } = getState();
|
const { api } = getState();
|
||||||
@ -154,7 +154,7 @@ export function loadTokensBasics (tokenIndexes, options) {
|
|||||||
|
|
||||||
export function fetchTokens (_tokenIndexes) {
|
export function fetchTokens (_tokenIndexes) {
|
||||||
const tokenIndexes = uniq(_tokenIndexes || []);
|
const tokenIndexes = uniq(_tokenIndexes || []);
|
||||||
const tokenChunks = chunk(tokenIndexes, 64);
|
const tokenChunks = chunk(tokenIndexes, 128);
|
||||||
|
|
||||||
return (dispatch, getState) => {
|
return (dispatch, getState) => {
|
||||||
const { tokenReg } = Contracts.get();
|
const { tokenReg } = Contracts.get();
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
// 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 BigNumber from 'bignumber.js';
|
||||||
|
|
||||||
import { hashToImageUrl } from '~/redux/util';
|
import { hashToImageUrl } from '~/redux/util';
|
||||||
@ -58,13 +58,11 @@ export function fetchTokensBasics (api, tokenReg, start = 0, limit = 100) {
|
|||||||
return decodeArray(api, 'address[]', result);
|
return decodeArray(api, 'address[]', result);
|
||||||
})
|
})
|
||||||
.then((tokenAddresses) => {
|
.then((tokenAddresses) => {
|
||||||
return tokenAddresses.map((tokenAddress, index) => {
|
return tokenAddresses.map((address, index) => {
|
||||||
const tokenIndex = start + index;
|
const tokenIndex = start + index;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
address: /^0x0*$/.test(tokenAddress)
|
address,
|
||||||
? ''
|
|
||||||
: tokenAddress,
|
|
||||||
id: getTokenId(tokenIndex),
|
id: getTokenId(tokenIndex),
|
||||||
index: tokenIndex,
|
index: tokenIndex,
|
||||||
fetched: false
|
fetched: false
|
||||||
@ -80,12 +78,17 @@ export function fetchTokensBasics (api, tokenReg, start = 0, limit = 100) {
|
|||||||
|
|
||||||
return tokens.map((token) => {
|
return tokens.map((token) => {
|
||||||
if (balances[token.id] && balances[token.id].gt(0)) {
|
if (balances[token.id] && balances[token.id].gt(0)) {
|
||||||
token.address = '';
|
token.address = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return token;
|
return token;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
})
|
||||||
|
.then((tokens) => {
|
||||||
|
return tokens.filter(({ address }) => {
|
||||||
|
return address && !/^0x0*$/.test(address);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -195,19 +198,22 @@ export function fetchAccountsBalances (api, tokens, updates) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const tokenPromise = Object.keys(tokenUpdates)
|
const tokenPromise = Object.keys(tokenUpdates)
|
||||||
.reduce((tokenPromise, accountAddress) => {
|
.reduce((promises, accountAddress) => {
|
||||||
const tokenIds = tokenUpdates[accountAddress];
|
const tokenIds = tokenUpdates[accountAddress];
|
||||||
const updateTokens = tokens
|
const updateTokens = tokens
|
||||||
.filter((t) => tokenIds.includes(t.id));
|
.filter((t) => tokenIds.includes(t.id));
|
||||||
|
|
||||||
return tokenPromise
|
promises.push(
|
||||||
.then(() => fetchTokensBalances(api, updateTokens, [ accountAddress ]))
|
fetchTokensBalances(api, updateTokens, [ accountAddress ])
|
||||||
.then((balances) => {
|
.then((balances) => {
|
||||||
tokensBalances[accountAddress] = balances[accountAddress];
|
tokensBalances[accountAddress] = balances[accountAddress];
|
||||||
});
|
})
|
||||||
}, Promise.resolve());
|
);
|
||||||
|
|
||||||
return Promise.all([ ethPromise, tokenPromise ])
|
return promises;
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return Promise.all([ ethPromise, Promise.all(tokenPromise) ])
|
||||||
.then(() => {
|
.then(() => {
|
||||||
const balances = Object.assign({}, tokensBalances);
|
const balances = Object.assign({}, tokensBalances);
|
||||||
|
|
||||||
@ -243,29 +249,24 @@ function fetchEthBalances (api, accountAddresses) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function fetchTokensBalances (api, tokens, accountAddresses) {
|
function fetchTokensBalances (api, _tokens, accountAddresses) {
|
||||||
const tokenAddresses = tokens.map((t) => t.address);
|
const promises = chunk(_tokens, 128).map((tokens) => {
|
||||||
const tokensBalancesCallData = encode(
|
const data = tokensBalancesBytecode + encode(
|
||||||
api,
|
api,
|
||||||
[ 'address[]', 'address[]' ],
|
[ 'address[]', 'address[]' ],
|
||||||
[ accountAddresses, tokenAddresses ]
|
[ accountAddresses, tokens.map(({ address }) => address) ]
|
||||||
);
|
);
|
||||||
|
|
||||||
return api.eth
|
return api.eth.call({ data }).then((result) => {
|
||||||
.call({ data: tokensBalancesBytecode + tokensBalancesCallData })
|
|
||||||
.then((result) => {
|
|
||||||
const rawBalances = decodeArray(api, 'uint[]', result);
|
|
||||||
const balances = {};
|
const balances = {};
|
||||||
|
const rawBalances = decodeArray(api, 'uint[]', result);
|
||||||
|
|
||||||
accountAddresses.forEach((accountAddress, accountIndex) => {
|
accountAddresses.forEach((accountAddress, accountIndex) => {
|
||||||
|
const preIndex = accountIndex * tokens.length;
|
||||||
const balance = {};
|
const balance = {};
|
||||||
const preIndex = accountIndex * tokenAddresses.length;
|
|
||||||
|
|
||||||
tokenAddresses.forEach((tokenAddress, tokenIndex) => {
|
tokens.forEach((token, tokenIndex) => {
|
||||||
const index = preIndex + tokenIndex;
|
balance[token.id] = rawBalances[preIndex + tokenIndex];
|
||||||
const token = tokens[tokenIndex];
|
|
||||||
|
|
||||||
balance[token.id] = rawBalances[index];
|
|
||||||
});
|
});
|
||||||
|
|
||||||
balances[accountAddress] = balance;
|
balances[accountAddress] = balance;
|
||||||
@ -273,6 +274,31 @@ function fetchTokensBalances (api, tokens, accountAddresses) {
|
|||||||
|
|
||||||
return balances;
|
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) {
|
function getTokenId (...args) {
|
||||||
|
69
js/package-lock.json
generated
69
js/package-lock.json
generated
@ -45,11 +45,11 @@
|
|||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"@parity/dapp-dapp-methods": {
|
"@parity/dapp-dapp-methods": {
|
||||||
"version": "github:js-dist-paritytech/dapp-dapp-methods#b4dce52a09303fddf55172bb0d2e6bb83f12e196",
|
"version": "github:js-dist-paritytech/dapp-dapp-methods#b649fb9056d49bbf4fde719f91a4cfcaf529f9f6",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"@parity/api": "2.1.15",
|
"@parity/api": "2.1.15",
|
||||||
"@parity/mobx": "1.0.6",
|
"@parity/mobx": "1.0.7",
|
||||||
"@parity/ui": "3.0.22",
|
"@parity/ui": "3.0.22",
|
||||||
"mobx": "3.4.1",
|
"mobx": "3.4.1",
|
||||||
"mobx-react": "4.3.5",
|
"mobx-react": "4.3.5",
|
||||||
@ -63,9 +63,9 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@parity/mobx": {
|
"@parity/mobx": {
|
||||||
"version": "1.0.6",
|
"version": "1.0.7",
|
||||||
"resolved": "https://registry.npmjs.org/@parity/mobx/-/mobx-1.0.6.tgz",
|
"resolved": "https://registry.npmjs.org/@parity/mobx/-/mobx-1.0.7.tgz",
|
||||||
"integrity": "sha512-yVTB3r/2Uel20aUBgt/x+dlxXrdNuzFjRyo86uR3vWTSFQq5A0H/LzIhlR5JBMuhkGwT6i+t7r1JFG4CoynJew==",
|
"integrity": "sha512-HC9VFcFnZ+h/YZWSiA2vIJcXK2yhLNFipPxAIMkDMClgNX9sOxrItmjmTfETAlHVM/axO2FIluLCd3VO/Xze8w==",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"@parity/ledger": "2.1.2"
|
"@parity/ledger": "2.1.2"
|
||||||
@ -128,11 +128,11 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"@parity/dapp-dapp-visible": {
|
"@parity/dapp-dapp-visible": {
|
||||||
"version": "github:js-dist-paritytech/dapp-dapp-visible#19f0b7d3de7bcc9859ac5d8059e866e1f5804366",
|
"version": "github:js-dist-paritytech/dapp-dapp-visible#28546f312ea9877ebeea9c52afea1e7ec943cd0d",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"@parity/api": "2.1.15",
|
"@parity/api": "2.1.15",
|
||||||
"@parity/mobx": "1.0.6",
|
"@parity/mobx": "1.0.7",
|
||||||
"@parity/ui": "3.0.22",
|
"@parity/ui": "3.0.22",
|
||||||
"mobx": "3.4.1",
|
"mobx": "3.4.1",
|
||||||
"mobx-react": "4.3.5",
|
"mobx-react": "4.3.5",
|
||||||
@ -146,9 +146,9 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@parity/mobx": {
|
"@parity/mobx": {
|
||||||
"version": "1.0.6",
|
"version": "1.0.7",
|
||||||
"resolved": "https://registry.npmjs.org/@parity/mobx/-/mobx-1.0.6.tgz",
|
"resolved": "https://registry.npmjs.org/@parity/mobx/-/mobx-1.0.7.tgz",
|
||||||
"integrity": "sha512-yVTB3r/2Uel20aUBgt/x+dlxXrdNuzFjRyo86uR3vWTSFQq5A0H/LzIhlR5JBMuhkGwT6i+t7r1JFG4CoynJew==",
|
"integrity": "sha512-HC9VFcFnZ+h/YZWSiA2vIJcXK2yhLNFipPxAIMkDMClgNX9sOxrItmjmTfETAlHVM/axO2FIluLCd3VO/Xze8w==",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"@parity/ledger": "2.1.2"
|
"@parity/ledger": "2.1.2"
|
||||||
@ -231,11 +231,11 @@
|
|||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"@parity/dapp-status": {
|
"@parity/dapp-status": {
|
||||||
"version": "github:js-dist-paritytech/dapp-status#90c6425804800b1d3599f602cd257e8e4cfa6428",
|
"version": "github:js-dist-paritytech/dapp-status#ea6a3c01d64bd57c5fadf2264efa719a61e70a29",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"@parity/api": "2.1.15",
|
"@parity/api": "2.1.15",
|
||||||
"@parity/mobx": "1.0.6",
|
"@parity/mobx": "1.0.7",
|
||||||
"@parity/ui": "3.0.22",
|
"@parity/ui": "3.0.22",
|
||||||
"format-number": "3.0.0",
|
"format-number": "3.0.0",
|
||||||
"mobx": "3.4.1",
|
"mobx": "3.4.1",
|
||||||
@ -251,9 +251,9 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@parity/mobx": {
|
"@parity/mobx": {
|
||||||
"version": "1.0.6",
|
"version": "1.0.7",
|
||||||
"resolved": "https://registry.npmjs.org/@parity/mobx/-/mobx-1.0.6.tgz",
|
"resolved": "https://registry.npmjs.org/@parity/mobx/-/mobx-1.0.7.tgz",
|
||||||
"integrity": "sha512-yVTB3r/2Uel20aUBgt/x+dlxXrdNuzFjRyo86uR3vWTSFQq5A0H/LzIhlR5JBMuhkGwT6i+t7r1JFG4CoynJew==",
|
"integrity": "sha512-HC9VFcFnZ+h/YZWSiA2vIJcXK2yhLNFipPxAIMkDMClgNX9sOxrItmjmTfETAlHVM/axO2FIluLCd3VO/Xze8w==",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"@parity/ledger": "2.1.2"
|
"@parity/ledger": "2.1.2"
|
||||||
@ -361,9 +361,9 @@
|
|||||||
"version": "github:parity-js/plugin-signer-qr#2d1fafad347ba53eaf58c14265d4d07631d6a45c"
|
"version": "github:parity-js/plugin-signer-qr#2d1fafad347ba53eaf58c14265d4d07631d6a45c"
|
||||||
},
|
},
|
||||||
"@parity/shared": {
|
"@parity/shared": {
|
||||||
"version": "2.2.21",
|
"version": "2.2.23",
|
||||||
"resolved": "https://registry.npmjs.org/@parity/shared/-/shared-2.2.21.tgz",
|
"resolved": "https://registry.npmjs.org/@parity/shared/-/shared-2.2.23.tgz",
|
||||||
"integrity": "sha512-8Xuuv/SpS+lviX3xRVvh3UUMcWYrQPQvn+KkSiKofuHlVL/IlhEnLyflk5j5FgGviXwaqBxmOllTSTPQBna4Gw==",
|
"integrity": "sha512-I9/uk2g2V6vm1SvsopGassY5QvW/5DMNvha5isoCRdFWnAq2EKOCeSH+pbZXx0ZzHCIgSDlFXPTaaWDbvngBzA==",
|
||||||
"requires": {
|
"requires": {
|
||||||
"@parity/ledger": "2.1.2",
|
"@parity/ledger": "2.1.2",
|
||||||
"eventemitter3": "2.0.3",
|
"eventemitter3": "2.0.3",
|
||||||
@ -415,37 +415,6 @@
|
|||||||
"loose-envify": "1.3.1",
|
"loose-envify": "1.3.1",
|
||||||
"symbol-observable": "1.1.0"
|
"symbol-observable": "1.1.0"
|
||||||
}
|
}
|
||||||
},
|
|
||||||
"solc": {
|
|
||||||
"version": "github:ngotchac/solc-js#04eb38cc3003fba8cb3656653a7941ed36408818",
|
|
||||||
"requires": {
|
|
||||||
"memorystream": "0.3.1",
|
|
||||||
"require-from-string": "1.2.1",
|
|
||||||
"yargs": "4.8.1"
|
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"yargs": {
|
|
||||||
"version": "4.8.1",
|
|
||||||
"resolved": "https://registry.npmjs.org/yargs/-/yargs-4.8.1.tgz",
|
|
||||||
"integrity": "sha1-wMQpJMpKqmsObaFznfshZDn53cA=",
|
|
||||||
"requires": {
|
|
||||||
"cliui": "3.2.0",
|
|
||||||
"decamelize": "1.2.0",
|
|
||||||
"get-caller-file": "1.0.2",
|
|
||||||
"lodash.assign": "4.2.0",
|
|
||||||
"os-locale": "1.4.0",
|
|
||||||
"read-pkg-up": "1.0.1",
|
|
||||||
"require-directory": "2.1.1",
|
|
||||||
"require-main-filename": "1.0.1",
|
|
||||||
"set-blocking": "2.0.0",
|
|
||||||
"string-width": "1.0.2",
|
|
||||||
"which-module": "1.0.0",
|
|
||||||
"window-size": "0.2.0",
|
|
||||||
"y18n": "3.2.1",
|
|
||||||
"yargs-parser": "2.4.1"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -457,7 +426,7 @@
|
|||||||
"@parity/api": "2.1.15",
|
"@parity/api": "2.1.15",
|
||||||
"@parity/etherscan": "2.1.3",
|
"@parity/etherscan": "2.1.3",
|
||||||
"@parity/mobx": "1.0.4",
|
"@parity/mobx": "1.0.4",
|
||||||
"@parity/shared": "2.2.21",
|
"@parity/shared": "2.2.23",
|
||||||
"babel-runtime": "6.26.0",
|
"babel-runtime": "6.26.0",
|
||||||
"bignumber.js": "4.1.0",
|
"bignumber.js": "4.1.0",
|
||||||
"brace": "0.11.0",
|
"brace": "0.11.0",
|
||||||
|
@ -146,7 +146,7 @@
|
|||||||
"@parity/plugin-signer-default": "parity-js/plugin-signer-default#9a47bded9d6d70b69bb2f719732bd6f7854d1842",
|
"@parity/plugin-signer-default": "parity-js/plugin-signer-default#9a47bded9d6d70b69bb2f719732bd6f7854d1842",
|
||||||
"@parity/plugin-signer-hardware": "parity-js/plugin-signer-hardware#4320d818a053d4efae890b74a7476e4c8dc6ba10",
|
"@parity/plugin-signer-hardware": "parity-js/plugin-signer-hardware#4320d818a053d4efae890b74a7476e4c8dc6ba10",
|
||||||
"@parity/plugin-signer-qr": "parity-js/plugin-signer-qr#2d1fafad347ba53eaf58c14265d4d07631d6a45c",
|
"@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",
|
"@parity/ui": "3.0.22",
|
||||||
"keythereum": "1.0.2",
|
"keythereum": "1.0.2",
|
||||||
"lodash.flatten": "4.4.0",
|
"lodash.flatten": "4.4.0",
|
||||||
|
Loading…
Reference in New Issue
Block a user