chain-desktop-wallet
chain-desktop-wallet copied to clipboard
Feature: Portfolio visualization over selected time periods
Background
Currently we only reflect real time token prices & missing a visual chart tool to display price changes over time. Suggest to support these 2 elements in order to bring along the below features accordingly:
Phases:
- Support token prices over selected time periods with a chart: 8H,1D,1W,1M,3M,6M.
- Support Portfolio distribution of all assets with Pie chart
- Support portfolio value changes over selected time period
Study
Cosmos-based / Crypto.org Chain
It is possible to fetch balances over selected time, after v3
upgrade. (e.g. Crypto.org Chain: 7/12/2021)
Protobuf: /cosmos/bank/v1beta1/balances/{address}
curl \
-X 'GET' \
-H 'accept: application/json' \
-H "x-cosmos-block-height: 4000000" \
'https://mainnet.crypto.org:1317/cosmos/bank/v1beta1/balances/cro1w2kvwrzp23aq54n3amwav4yy4a9ahq2kz2wtmj'
EVM Chain
So the following will get the balance of an account at a specific block number: Ethereum balance:
web3.eth.getBalance(address, blockNumber).then(balance => `Balance at block number is ${balance}
ERC20 token balance:
tokenContract.balanceOf(account, {}, blockNumber);
A good way to calculate block number at certain timestamp:
let blockNum = web3.eth.blockNumber;
const historicTimestamp = new Date(historicDate).getTime();
while(true) {
const block = web3.eth.getBlock(blockNum);
if(block.timestamp < historicTimestamp) break;
--blockNum;
}
Due to the fact that token balances have to be fetched by Token Contract Address one by one, and it's impossible to know which token an address is holding at certain timestamp / block height, fetching balance with specific time on all tokens would be extremely slow and unrealistic. We may prepare a whitelist of token contract addresses & only check token balances on them.
Price API
API endpoint to fetch token market prices over timestamp:
https://price-api.crypto.com/price/v2/all/{slug}?convert={currency}
e.g. https://price-api.crypto.com/price/v2/all/ethereum?convert=USD
Visual Charts
- Decided which visual tool to be best fitted