-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Support for Token metadata (#1403) * Function to poll decimal metadata and save it in redux Store. Notes: - So far we only fetched our backend for almost everything, so the same base url was shared in all calls. Adding a third party endpoint required to slightly refactor the fetch function to be more flexible. - Token metadata is fetched when wallet is opened and only stored in redux's Store (not in persistent storage). We may add this functionality later * Use token metadata from Store in UI, plus some fixes * UI improvements * use cdn for mainnet and minor fix * trigger token metadata update when txs are received/updated * fix: do not clear token info before fetching, plus a minor UI fix * 4.1.4 (#1405)
- Loading branch information
1 parent
f40cf47
commit bd04820
Showing
27 changed files
with
403 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// @flow | ||
import type {Dispatch} from 'redux' | ||
|
||
import walletManager from '../crypto/walletManager' | ||
import {availableAssetsSelector, tokenBalanceSelector} from '../selectors' | ||
import {ObjectValues} from '../utils/flow' | ||
import {Logger} from '../utils/logging' | ||
|
||
import type {State} from '../state' | ||
import type {Token} from '../types/HistoryTransaction' | ||
import type {MultiToken} from '../crypto/MultiToken' | ||
import type {TokenInfoRequest, TokenInfoResponse} from '../api/types' | ||
|
||
const _startFetching = () => ({ | ||
type: 'START_FETCHING_TOKEN_INFO', | ||
path: ['tokenInfo', 'isFetching'], | ||
payload: null, | ||
reducer: (_state, _isFetching) => true, | ||
}) | ||
|
||
const _endFetching = () => ({ | ||
type: 'END_FETCHING_TOKEN_INFO', | ||
path: ['tokenInfo', 'isFetching'], | ||
payload: null, | ||
reducer: (_state, _isFetching) => false, | ||
}) | ||
|
||
const _setTokenInfo = (tokenInfo) => ({ | ||
type: 'SET_TOKEN_INFO', | ||
path: ['tokenInfo', 'tokens'], | ||
payload: tokenInfo, | ||
reducer: (state, value) => value, | ||
}) | ||
|
||
const _setLastError = (error) => ({ | ||
type: 'SET_LAST_FETCHING_ERROR', | ||
path: ['tokenInfo', 'lastFetchingError'], | ||
payload: error, | ||
reducer: (state, error) => error, | ||
}) | ||
|
||
export const fetchTokenInfo = () => async ( | ||
dispatch: Dispatch<any>, | ||
getState: () => State, | ||
) => { | ||
const state = getState() | ||
if (state.tokenInfo.isFetching) { | ||
return | ||
} | ||
dispatch(_setTokenInfo(availableAssetsSelector(state))) | ||
dispatch(_startFetching()) | ||
try { | ||
const availableAssets: Dict<Token> = availableAssetsSelector(state) | ||
const assetsBalance: MultiToken = tokenBalanceSelector(state) | ||
|
||
// subject -> identifier | ||
const subjectDict = ObjectValues(availableAssets) | ||
.filter((asset) => { | ||
const assetValue = assetsBalance.get(asset.identifier) | ||
return assetValue && assetValue.gt(0) | ||
}) | ||
.reduce((acc, curr: Token): Dict<string> => { | ||
if (curr.identifier === '') return acc | ||
acc[`${curr.metadata.policyId}${curr.metadata.assetName}`] = | ||
curr.identifier | ||
return acc | ||
}, ({}: Dict<string>)) | ||
const tokenIds = Object.keys(subjectDict) | ||
const tokenInfo: TokenInfoResponse = await walletManager.fetchTokenInfo( | ||
({ | ||
tokenIds, | ||
}: TokenInfoRequest), | ||
) | ||
const tokens = {...availableAssets} | ||
for (const key of Object.keys(tokenInfo)) { | ||
const _token = tokens[subjectDict[key]] | ||
const newInfo = tokenInfo[key] | ||
if (newInfo == null) continue | ||
tokens[subjectDict[key]] = { | ||
..._token, | ||
metadata: { | ||
..._token.metadata, | ||
longName: newInfo.name || null, | ||
numberOfDecimals: newInfo.decimals || 0, | ||
}, | ||
} | ||
} | ||
Logger.info('saving token info in state....', tokens) | ||
dispatch(_setTokenInfo(tokens)) | ||
dispatch(_setLastError(null)) | ||
} catch (err) { | ||
Logger.warn('actions:tokenInfo::fetchTokenInfo', err) | ||
dispatch(_setLastError(err)) | ||
} finally { | ||
dispatch(_endFetching()) | ||
} | ||
} |
Oops, something went wrong.