fix typings

This commit is contained in:
fernandomg 2020-06-26 18:56:08 -03:00
parent 2d8da9ca98
commit cbc8121bd9
5 changed files with 92 additions and 27 deletions

View File

@ -4,15 +4,67 @@ import { ETHEREUM_NETWORK } from 'src/logic/wallets/getWeb3'
import NFTIcon from 'src/routes/safe/components/Balances/assets/nft_icon.png'
import { OPENSEA_API_KEY } from 'src/utils/constants'
export interface OpenSeaAssetContract {
address: string
name: string
image_url: string
symbol: string
}
export interface OpenSeaCollection {
name: string
slug: string
}
export interface OpenSeaAsset {
asset_contract: OpenSeaAssetContract
background_color: string
collection: OpenSeaCollection
description: string
image_thumbnail_url: string
name: string
token_id: string
}
export type OpenSeaAssets = Array<OpenSeaAsset>
export interface NFTAsset {
address: string
assetContract: OpenSeaAssetContract
collection: OpenSeaCollection
description: string
image: string
name: string
numberOfTokens: number
slug: string
symbol: string
}
export type NFTAssets = Record<string, NFTAsset>
export interface NFTToken {
assetAddress: string
color: string
description: string
image: string
name: string
tokenId: number | string
}
export type NFTTokens = Array<NFTToken>
export interface Collectibles {
nftAssets: NFTAssets
nftTokens: NFTTokens
}
class OpenSea {
_rateLimit = async () => {}
_rateLimit = async (): Promise<void> => {}
_endpointsUrls = {
[ETHEREUM_NETWORK.MAINNET]: 'https://api.opensea.io/api/v1',
[ETHEREUM_NETWORK.RINKEBY]: 'https://rinkeby-api.opensea.io/api/v1',
}
_fetch = async (url) => {
_fetch = async (url: string): Promise<Response> => {
// eslint-disable-next-line no-underscore-dangle
return fetch(url, {
headers: { 'X-API-KEY': OPENSEA_API_KEY || '' },
@ -24,12 +76,12 @@ class OpenSea {
* @param {object} options
* @param {number} options.rps - requests per second
*/
constructor(options) {
constructor(options: { rps: number }) {
// eslint-disable-next-line no-underscore-dangle
this._rateLimit = RateLimit(options.rps, { timeUnit: 60 * 1000, uniformDistribution: true })
}
static extractAssets(assets) {
static extractAssets(assets: OpenSeaAssets): NFTAssets {
const extractNFTAsset = (asset) => ({
address: asset.asset_contract.address,
assetContract: asset.asset_contract,
@ -59,7 +111,7 @@ class OpenSea {
}, {})
}
static extractTokens(assets) {
static extractTokens(assets: OpenSeaAssets): NFTTokens {
return assets.map((asset) => ({
assetAddress: asset.asset_contract.address,
color: asset.background_color,
@ -70,7 +122,7 @@ class OpenSea {
}))
}
static extractCollectiblesInfo(assetResponseJson) {
static extractCollectiblesInfo(assetResponseJson: { assets: OpenSeaAssets }): Collectibles {
return {
nftAssets: OpenSea.extractAssets(assetResponseJson.assets),
nftTokens: OpenSea.extractTokens(assetResponseJson.assets),
@ -82,9 +134,9 @@ class OpenSea {
* for the provided Safe Address in the specified Network
* @param {string} safeAddress
* @param {string} network
* @returns {Promise<{ nftAssets: Map<string, NFTAsset>, nftTokens: Array<NFTToken> }>}
* @returns {Promise<Collectibles>}
*/
async fetchAllUserCollectiblesByCategoryAsync(safeAddress, network) {
async fetchAllUserCollectiblesByCategoryAsync(safeAddress: string, network: string): Promise<Collectibles> {
// eslint-disable-next-line no-underscore-dangle
const metadataSourceUrl = this._endpointsUrls[network]
const url = `${metadataSourceUrl}/assets/?owner=${safeAddress}`

View File

@ -1,28 +1,37 @@
import { List } from 'immutable'
import { List, Map } from 'immutable'
import { createSelector } from 'reselect'
import { NFT_ASSETS_REDUCER_ID, NFT_TOKENS_REDUCER_ID } from 'src/logic/collectibles/store/reducer/collectibles'
import { safeActiveAssetsSelector } from 'src/routes/safe/store/selectors'
import { NFTAssets, NFTTokens, NFTAsset } from '../../sources/OpenSea'
export const nftAssetsSelector = (state) => state[NFT_ASSETS_REDUCER_ID]
export const nftTokensSelector = (state) => state[NFT_TOKENS_REDUCER_ID]
export const nftAssetsSelector = (state: Map<string, unknown>): NFTAssets => state[NFT_ASSETS_REDUCER_ID]
export const nftTokensSelector = (state: Map<string, unknown>): NFTTokens => state[NFT_TOKENS_REDUCER_ID]
export const nftAssetsListSelector = createSelector(nftAssetsSelector, (assets) => {
return assets ? List(Object.entries(assets).map((item) => item[1])) : List([])
})
export const nftAssetsListSelector = createSelector(
nftAssetsSelector,
(assets): List<NFTAsset> => {
return assets ? List(Object.values(assets)) : List()
},
)
export const activeNftAssetsListSelector = createSelector(
nftAssetsListSelector,
safeActiveAssetsSelector,
(assets, activeAssetsList) => {
return assets.filter((asset: any) => activeAssetsList.has(asset.address))
(assets, activeAssetsList): List<NFTAsset> => {
return assets.filter(({ address }) => activeAssetsList.has(address))
},
)
export const safeActiveSelectorMap = createSelector(activeNftAssetsListSelector, (activeAssets) => {
const assetsMap = {}
activeAssets.forEach((asset: any) => {
assetsMap[asset.address] = asset
})
return assetsMap
})
export const safeActiveSelectorMap = createSelector(
activeNftAssetsListSelector,
(activeAssets): Record<string, NFTAsset> => {
const assetsMap = {}
activeAssets.forEach((asset) => {
assetsMap[asset.address] = asset
})
return assetsMap
},
)

View File

@ -1,8 +1,10 @@
import { Map } from 'immutable'
import { createSelector } from 'reselect'
import { TOKEN_REDUCER_ID } from 'src/logic/tokens/store/reducer/tokens'
import { Token } from '../model/token'
export const tokensSelector = (state) => state[TOKEN_REDUCER_ID]
export const tokensSelector = (state: Map<string, unknown>): Map<string, Token> => state[TOKEN_REDUCER_ID]
export const tokenListSelector = createSelector(tokensSelector, (tokens) => tokens.toList())

View File

@ -15,7 +15,7 @@ import { TxServiceModel } from 'src/routes/safe/store/actions/transactions/fetch
export const ETH_ADDRESS = '0x000'
export const SAFE_TRANSFER_FROM_WITHOUT_DATA_HASH = '42842e0e'
export const getEthAsToken = (balance: string): Token => {
export const getEthAsToken = (balance: string | number): Token => {
return makeToken({
address: ETH_ADDRESS,
name: 'Ether',
@ -26,7 +26,7 @@ export const getEthAsToken = (balance: string): Token => {
})
}
export const isAddressAToken = async (tokenAddress): Promise<boolean> => {
export const isAddressAToken = async (tokenAddress: string): Promise<boolean> => {
// SECOND APPROACH:
// They both seem to work the same
// const tokenContract = await getStandardTokenContract()

View File

@ -4,8 +4,9 @@ export type SafeRecordProps = {
name: string
address: string
threshold: number
ethBalance: number
ethBalance: number | string
owners: List<{ name: string; address: string }>
modules: Set<string>
activeTokens: Set<string>
activeAssets: Set<string>
blacklistedTokens: Set<string>
@ -25,6 +26,7 @@ const makeSafe = Record<SafeRecordProps>({
threshold: 0,
ethBalance: 0,
owners: List([]),
modules: Set(),
activeTokens: Set(),
activeAssets: Set(),
blacklistedTokens: Set(),