Fix slow balances (#6471)
* Update token updates
* Update token info fetching
* Update logger
* Minor fixes to updates and notifications for balances
* Use Pubsub
* Fix timeout.
* Use pubsub for status.
* Fix signer subscription.
* Process tokens in chunks.
* Fix tokens loaded by chunks
* Linting
* Dispatch tokens asap
* Fix chunks processing.
* Better filter options
* Parallel log fetching.
* Fix signer polling.
* Fix initial block query.
* Token balances updates : the right(er) way
* Better tokens info fetching
* Fixes in token data fetching
* Only fetch what's needed (tokens)
* Fix linting issues
* Revert "Transaction permissioning (#6441)"
This reverts commit eed0e8b03a.
* Revert "Revert "Transaction permissioning (#6441)""
This reverts commit 8f96415e58dde652e5828706eb2639d43416f448.
* Update wasm-tests.
* Fixing balances fetching
* Fix requests tracking in UI
* Fix request watching
* Update the Logger
* PR Grumbles Fixes
* PR Grumbles fixes
* Linting...
This commit is contained in:
committed by
Gav Wood
parent
ee14a3fb31
commit
f1a050366f
@@ -1,133 +0,0 @@
|
||||
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Parity.
|
||||
|
||||
// Parity is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// 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 BigNumber from 'bignumber.js';
|
||||
|
||||
import { hashToImageUrl } from '~/redux/util';
|
||||
import { sha3 } from '~/api/util/sha3';
|
||||
import imagesEthereum from '~/../assets/images/contracts/ethereum-black-64x64.png';
|
||||
|
||||
const BALANCEOF_SIGNATURE = sha3('balanceOf(address)');
|
||||
const ADDRESS_PADDING = range(24).map(() => '0').join('');
|
||||
|
||||
export const ETH_TOKEN = {
|
||||
address: '',
|
||||
format: new BigNumber(10).pow(18),
|
||||
id: sha3('eth_native_token').slice(0, 10),
|
||||
image: imagesEthereum,
|
||||
name: 'Ethereum',
|
||||
native: true,
|
||||
tag: 'ETH'
|
||||
};
|
||||
|
||||
export function fetchTokenIds (tokenregInstance) {
|
||||
return tokenregInstance.tokenCount
|
||||
.call()
|
||||
.then((numTokens) => {
|
||||
const tokenIndexes = range(numTokens.toNumber());
|
||||
|
||||
return tokenIndexes;
|
||||
});
|
||||
}
|
||||
|
||||
export function fetchTokenInfo (api, tokenregInstace, tokenIndex) {
|
||||
return Promise
|
||||
.all([
|
||||
tokenregInstace.token.call({}, [tokenIndex]),
|
||||
tokenregInstace.meta.call({}, [tokenIndex, 'IMG'])
|
||||
])
|
||||
.then(([ tokenData, image ]) => {
|
||||
const [ address, tag, format, name ] = tokenData;
|
||||
|
||||
const token = {
|
||||
format: format.toString(),
|
||||
index: tokenIndex,
|
||||
image: hashToImageUrl(image),
|
||||
id: sha3(address + tokenIndex).slice(0, 10),
|
||||
address,
|
||||
name,
|
||||
tag
|
||||
};
|
||||
|
||||
return token;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* `updates` should be in the shape:
|
||||
* {
|
||||
* [ who ]: [ tokenId ] // Array of tokens to updates
|
||||
* }
|
||||
*
|
||||
* Returns a Promise resolved witht the balances in the shape:
|
||||
* {
|
||||
* [ who ]: { [ tokenId ]: BigNumber } // The balances of `who`
|
||||
* }
|
||||
*/
|
||||
export function fetchAccountsBalances (api, tokens, updates) {
|
||||
const addresses = Object.keys(updates);
|
||||
const promises = addresses
|
||||
.map((who) => {
|
||||
const tokensIds = updates[who];
|
||||
const tokensToUpdate = tokensIds.map((tokenId) => tokens.find((t) => t.id === tokenId));
|
||||
|
||||
return fetchAccountBalances(api, tokensToUpdate, who);
|
||||
});
|
||||
|
||||
return Promise.all(promises)
|
||||
.then((results) => {
|
||||
return results.reduce((balances, accountBalances, index) => {
|
||||
balances[addresses[index]] = accountBalances;
|
||||
return balances;
|
||||
}, {});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a Promise resolved with the balances in the shape:
|
||||
* {
|
||||
* [ tokenId ]: BigNumber // Token balance value
|
||||
* }
|
||||
*/
|
||||
export function fetchAccountBalances (api, tokens, who) {
|
||||
const calldata = '0x' + BALANCEOF_SIGNATURE.slice(2, 10) + ADDRESS_PADDING + who.slice(2);
|
||||
const promises = tokens.map((token) => fetchTokenBalance(api, token, { who, calldata }));
|
||||
|
||||
return Promise.all(promises)
|
||||
.then((results) => {
|
||||
return results.reduce((balances, value, index) => {
|
||||
const token = tokens[index];
|
||||
|
||||
balances[token.id] = value;
|
||||
return balances;
|
||||
}, {});
|
||||
});
|
||||
}
|
||||
|
||||
export function fetchTokenBalance (api, token, { who, calldata }) {
|
||||
if (token.native) {
|
||||
return api.eth.getBalance(who);
|
||||
}
|
||||
|
||||
return api.eth
|
||||
.call({ data: calldata, to: token.address })
|
||||
.then((result) => {
|
||||
const cleanResult = result.replace(/^0x/, '');
|
||||
|
||||
return new BigNumber(`0x${cleanResult || 0}`);
|
||||
});
|
||||
}
|
||||
23
js/src/util/tokens/bytecodes.js
Normal file
23
js/src/util/tokens/bytecodes.js
Normal file
@@ -0,0 +1,23 @@
|
||||
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Parity.
|
||||
|
||||
// Parity is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
// build from : https://raw.githubusercontent.com/paritytech/contracts/4c8501e908166aab7ff4d2ebb05db61b5d017024/TokenCalls.sol
|
||||
// metadata (include build version and options):
|
||||
// {"compiler":{"version":"0.4.16+commit.d7661dd9"},"language":"Solidity","output":{"abi":[{"inputs":[{"name":"tokenRegAddress","type":"address"},{"name":"start","type":"uint256"},{"name":"limit","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}],"devdoc":{"methods":{}},"userdoc":{"methods":{}}},"settings":{"compilationTarget":{"":"Tokens"},"libraries":{},"optimizer":{"enabled":true,"runs":200},"remappings":[]},"sources":{"":{"keccak256":"0x4790e490f418d1a5884c27ffe9684914dab2d55bd1d23b99cff7aa2ca289e2d3","urls":["bzzr://bb200beae6849f1f5bb97b36c57cd493be52877ec0b55ee9969fa5f8159cf37b"]}},"version":1}
|
||||
// {"compiler":{"version":"0.4.16+commit.d7661dd9"},"language":"Solidity","output":{"abi":[{"inputs":[{"name":"who","type":"address[]"},{"name":"tokens","type":"address[]"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}],"devdoc":{"methods":{}},"userdoc":{"methods":{}}},"settings":{"compilationTarget":{"":"TokensBalances"},"libraries":{},"optimizer":{"enabled":true,"runs":200},"remappings":[]},"sources":{"":{"keccak256":"0x4790e490f418d1a5884c27ffe9684914dab2d55bd1d23b99cff7aa2ca289e2d3","urls":["bzzr://bb200beae6849f1f5bb97b36c57cd493be52877ec0b55ee9969fa5f8159cf37b"]}},"version":1}
|
||||
|
||||
export const tokenAddresses = '0x6060604052341561000f57600080fd5b6040516060806102528339810160405280805191906020018051919060200180519150505b6000806000806100426101fc565b600088955085600160a060020a0316639f181b5e6000604051602001526040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401602060405180830381600087803b15156100a657600080fd5b6102c65a03f115156100b757600080fd5b50505060405180519550508688018890116100ce57fe5b8785116100de57600093506100f6565b8487890111156100f25787850393506100f6565b8693505b5b83602002602001925060405191508282016040528382528790505b8388018110156101ea5785600160a060020a031663044215c682600060405160a001526040517c010000000000000000000000000000000000000000000000000000000063ffffffff8416028152600481019190915260240160a060405180830381600087803b151561018457600080fd5b6102c65a03f1151561019557600080fd5b50505060405180519060200180519060200180519060200180519060200180515086935050508a84039050815181106101ca57fe5b600160a060020a039092166020928302909101909101525b600101610112565b8282f35b50505050505050505061020e565b60206040519081016040526000815290565b60368061021c6000396000f30060606040525b600080fd00a165627a7a72305820a9a09f013393cf3c6398ce0f8175073fe363b6f594f9bd569261d0bb94aa84d40029';
|
||||
export const tokensBalances = '0x6060604052341561000f57600080fd5b60405161018b38038061018b8339810160405280805182019190602001805190910190505b6000806000610041610135565b60008060008060008060008c518c51029a506020808c020199507f70a0823100000000000000000000000000000000000000000000000000000000985060405197508988016040528a8852604051965060248701604052888752879550866004019450600093505b8c5184101561011f57600092505b8b51831015610113578c84815181106100cc57fe5b9060200190602002015191508b83815181106100e457fe5b90602001906020020151905060208601955081855260208660248960008561fffff1505b6001909201916100b7565b5b6001909301926100a9565b8988f35b50505050505050505050505050610147565b60206040519081016040526000815290565b6036806101556000396000f30060606040525b600080fd00a165627a7a723058203cfc17c394936aa87b7db79e4f082a7cfdcefef54acd3124d17525b56c92e7950029';
|
||||
283
js/src/util/tokens/index.js
Normal file
283
js/src/util/tokens/index.js
Normal file
@@ -0,0 +1,283 @@
|
||||
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Parity.
|
||||
|
||||
// Parity is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// 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 BigNumber from 'bignumber.js';
|
||||
|
||||
import { hashToImageUrl } from '~/redux/util';
|
||||
import { sha3 } from '~/api/util/sha3';
|
||||
import imagesEthereum from '~/../assets/images/contracts/ethereum-black-64x64.png';
|
||||
import {
|
||||
tokenAddresses as tokenAddressesBytcode,
|
||||
tokensBalances as tokensBalancesBytecode
|
||||
} from './bytecodes';
|
||||
|
||||
export const ETH_TOKEN = {
|
||||
address: '',
|
||||
format: new BigNumber(10).pow(18),
|
||||
id: getTokenId('eth_native_token'),
|
||||
image: imagesEthereum,
|
||||
name: 'Ethereum',
|
||||
native: true,
|
||||
tag: 'ETH'
|
||||
};
|
||||
|
||||
export function fetchTokenIds (tokenregInstance) {
|
||||
return tokenregInstance.tokenCount
|
||||
.call()
|
||||
.then((numTokens) => {
|
||||
const tokenIndexes = range(numTokens.toNumber());
|
||||
|
||||
return tokenIndexes;
|
||||
});
|
||||
}
|
||||
|
||||
export function fetchTokensBasics (api, tokenReg, start = 0, limit = 100) {
|
||||
const tokenAddressesCallData = encode(
|
||||
api,
|
||||
[ 'address', 'uint', 'uint' ],
|
||||
[ tokenReg.address, start, limit ]
|
||||
);
|
||||
|
||||
return api.eth
|
||||
.call({ data: tokenAddressesBytcode + tokenAddressesCallData })
|
||||
.then((result) => {
|
||||
const tokenAddresses = decodeArray(api, 'address[]', result);
|
||||
|
||||
return tokenAddresses.map((tokenAddress, index) => {
|
||||
const tokenIndex = start + index;
|
||||
|
||||
return {
|
||||
address: tokenAddress,
|
||||
id: getTokenId(tokenAddress, tokenIndex),
|
||||
index: tokenIndex,
|
||||
|
||||
fetched: false
|
||||
};
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export function fetchTokensInfo (api, tokenReg, tokenIndexes) {
|
||||
const requests = tokenIndexes.map((tokenIndex) => {
|
||||
const tokenCalldata = tokenReg.getCallData(tokenReg.instance.token, {}, [tokenIndex]);
|
||||
|
||||
return { to: tokenReg.address, data: tokenCalldata };
|
||||
});
|
||||
|
||||
const calls = requests.map((req) => api.eth.call(req));
|
||||
const imagesPromise = fetchTokensImages(api, tokenReg, tokenIndexes);
|
||||
|
||||
return Promise.all(calls)
|
||||
.then((results) => {
|
||||
return imagesPromise.then((images) => [ results, images ]);
|
||||
})
|
||||
.then(([ results, images ]) => {
|
||||
return results.map((rawTokenData, index) => {
|
||||
const tokenIndex = tokenIndexes[index];
|
||||
const tokenData = tokenReg.instance.token
|
||||
.decodeOutput(rawTokenData)
|
||||
.map((t) => t.value);
|
||||
|
||||
const [ address, tag, format, name ] = tokenData;
|
||||
const image = images[index];
|
||||
|
||||
const token = {
|
||||
address,
|
||||
id: getTokenId(address, tokenIndex),
|
||||
index: tokenIndex,
|
||||
|
||||
format: format.toString(),
|
||||
image,
|
||||
name,
|
||||
tag,
|
||||
|
||||
fetched: true
|
||||
};
|
||||
|
||||
return token;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export function fetchTokensImages (api, tokenReg, tokenIndexes) {
|
||||
const requests = tokenIndexes.map((tokenIndex) => {
|
||||
const metaCalldata = tokenReg.getCallData(tokenReg.instance.meta, {}, [tokenIndex, 'IMG']);
|
||||
|
||||
return { to: tokenReg.address, data: metaCalldata };
|
||||
});
|
||||
|
||||
const calls = requests.map((req) => api.eth.call(req));
|
||||
|
||||
return Promise.all(calls)
|
||||
.then((results) => {
|
||||
return results.map((rawImage) => {
|
||||
const image = tokenReg.instance.meta.decodeOutput(rawImage)[0].value;
|
||||
|
||||
return hashToImageUrl(image);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* `updates` should be in the shape:
|
||||
* {
|
||||
* [ who ]: [ tokenId ] // Array of tokens to updates
|
||||
* }
|
||||
*
|
||||
* Returns a Promise resolved with the balances in the shape:
|
||||
* {
|
||||
* [ who ]: { [ tokenId ]: BigNumber } // The balances of `who`
|
||||
* }
|
||||
*/
|
||||
export function fetchAccountsBalances (api, tokens, updates) {
|
||||
const accountAddresses = Object.keys(updates);
|
||||
|
||||
// Updates for the ETH balances
|
||||
const ethUpdates = accountAddresses
|
||||
.filter((accountAddress) => {
|
||||
return updates[accountAddress].find((tokenId) => tokenId === ETH_TOKEN.id);
|
||||
})
|
||||
.reduce((nextUpdates, accountAddress) => {
|
||||
nextUpdates[accountAddress] = [ETH_TOKEN.id];
|
||||
return nextUpdates;
|
||||
}, {});
|
||||
|
||||
// Updates for Tokens balances
|
||||
const tokenUpdates = Object.keys(updates)
|
||||
.reduce((nextUpdates, accountAddress) => {
|
||||
const tokenIds = updates[accountAddress].filter((tokenId) => tokenId !== ETH_TOKEN.id);
|
||||
|
||||
if (tokenIds.length > 0) {
|
||||
nextUpdates[accountAddress] = tokenIds;
|
||||
}
|
||||
|
||||
return nextUpdates;
|
||||
}, {});
|
||||
|
||||
let ethBalances = {};
|
||||
let tokensBalances = {};
|
||||
|
||||
const ethPromise = fetchEthBalances(api, Object.keys(ethUpdates))
|
||||
.then((_ethBalances) => {
|
||||
ethBalances = _ethBalances;
|
||||
});
|
||||
|
||||
const tokenPromise = Object.keys(tokenUpdates)
|
||||
.reduce((tokenPromise, 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());
|
||||
|
||||
return Promise.all([ ethPromise, tokenPromise ])
|
||||
.then(() => {
|
||||
const balances = Object.assign({}, tokensBalances);
|
||||
|
||||
Object.keys(ethBalances).forEach((accountAddress) => {
|
||||
if (!balances[accountAddress]) {
|
||||
balances[accountAddress] = {};
|
||||
}
|
||||
|
||||
balances[accountAddress] = Object.assign(
|
||||
{},
|
||||
balances[accountAddress],
|
||||
ethBalances[accountAddress]
|
||||
);
|
||||
});
|
||||
|
||||
return balances;
|
||||
});
|
||||
}
|
||||
|
||||
function fetchEthBalances (api, accountAddresses) {
|
||||
const promises = accountAddresses
|
||||
.map((accountAddress) => api.eth.getBalance(accountAddress));
|
||||
|
||||
return Promise.all(promises)
|
||||
.then((balancesArray) => {
|
||||
return balancesArray.reduce((balances, balance, index) => {
|
||||
balances[accountAddresses[index]] = {
|
||||
[ETH_TOKEN.id]: balance
|
||||
};
|
||||
|
||||
return balances;
|
||||
}, {});
|
||||
});
|
||||
}
|
||||
|
||||
function fetchTokensBalances (api, tokens, accountAddresses) {
|
||||
const tokenAddresses = tokens.map((t) => t.address);
|
||||
const tokensBalancesCallData = encode(
|
||||
api,
|
||||
[ 'address[]', 'address[]' ],
|
||||
[ accountAddresses, tokenAddresses ]
|
||||
);
|
||||
|
||||
return api.eth
|
||||
.call({ data: tokensBalancesBytecode + tokensBalancesCallData })
|
||||
.then((result) => {
|
||||
const rawBalances = decodeArray(api, 'uint[]', result);
|
||||
const balances = {};
|
||||
|
||||
accountAddresses.forEach((accountAddress, accountIndex) => {
|
||||
const balance = {};
|
||||
const preIndex = accountIndex * tokenAddresses.length;
|
||||
|
||||
tokenAddresses.forEach((tokenAddress, tokenIndex) => {
|
||||
const index = preIndex + tokenIndex;
|
||||
const token = tokens[tokenIndex];
|
||||
|
||||
balance[token.id] = rawBalances[index];
|
||||
});
|
||||
|
||||
balances[accountAddress] = balance;
|
||||
});
|
||||
|
||||
return balances;
|
||||
});
|
||||
}
|
||||
|
||||
function getTokenId (...args) {
|
||||
return sha3(args.join('')).slice(0, 10);
|
||||
}
|
||||
|
||||
function encode (api, types, values) {
|
||||
return api.util.abiEncode(
|
||||
null,
|
||||
types,
|
||||
values
|
||||
).replace('0x', '');
|
||||
}
|
||||
|
||||
function decodeArray (api, type, data) {
|
||||
return api.util
|
||||
.abiDecode(
|
||||
[type],
|
||||
[
|
||||
'0x',
|
||||
(32).toString(16).padStart(64, 0),
|
||||
data.replace('0x', '')
|
||||
].join('')
|
||||
)[0]
|
||||
.map((t) => t.value);
|
||||
}
|
||||
Reference in New Issue
Block a user