From cbc8121bd9a68f0997601e1626fe47e6638fbbe5 Mon Sep 17 00:00:00 2001 From: fernandomg Date: Fri, 26 Jun 2020 18:56:08 -0300 Subject: [PATCH] fix typings --- src/logic/collectibles/sources/OpenSea.ts | 68 ++++++++++++++++--- .../collectibles/store/selectors/index.ts | 39 +++++++---- src/logic/tokens/store/selectors/index.ts | 4 +- src/logic/tokens/utils/tokenHelpers.ts | 4 +- src/routes/safe/store/models/safe.ts | 4 +- 5 files changed, 92 insertions(+), 27 deletions(-) diff --git a/src/logic/collectibles/sources/OpenSea.ts b/src/logic/collectibles/sources/OpenSea.ts index 2fc74225..d626e825 100644 --- a/src/logic/collectibles/sources/OpenSea.ts +++ b/src/logic/collectibles/sources/OpenSea.ts @@ -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 + +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 + +export interface NFTToken { + assetAddress: string + color: string + description: string + image: string + name: string + tokenId: number | string +} +export type NFTTokens = Array + +export interface Collectibles { + nftAssets: NFTAssets + nftTokens: NFTTokens +} + class OpenSea { - _rateLimit = async () => {} + _rateLimit = async (): Promise => {} _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 => { // 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, nftTokens: Array }>} + * @returns {Promise} */ - async fetchAllUserCollectiblesByCategoryAsync(safeAddress, network) { + async fetchAllUserCollectiblesByCategoryAsync(safeAddress: string, network: string): Promise { // eslint-disable-next-line no-underscore-dangle const metadataSourceUrl = this._endpointsUrls[network] const url = `${metadataSourceUrl}/assets/?owner=${safeAddress}` diff --git a/src/logic/collectibles/store/selectors/index.ts b/src/logic/collectibles/store/selectors/index.ts index c9a80c14..333467ff 100644 --- a/src/logic/collectibles/store/selectors/index.ts +++ b/src/logic/collectibles/store/selectors/index.ts @@ -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): NFTAssets => state[NFT_ASSETS_REDUCER_ID] +export const nftTokensSelector = (state: Map): 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 => { + 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 => { + 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 => { + const assetsMap = {} + + activeAssets.forEach((asset) => { + assetsMap[asset.address] = asset + }) + + return assetsMap + }, +) diff --git a/src/logic/tokens/store/selectors/index.ts b/src/logic/tokens/store/selectors/index.ts index 41d5edb1..9da88f7f 100644 --- a/src/logic/tokens/store/selectors/index.ts +++ b/src/logic/tokens/store/selectors/index.ts @@ -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): Map => state[TOKEN_REDUCER_ID] export const tokenListSelector = createSelector(tokensSelector, (tokens) => tokens.toList()) diff --git a/src/logic/tokens/utils/tokenHelpers.ts b/src/logic/tokens/utils/tokenHelpers.ts index eab9ec7e..f3f59a81 100644 --- a/src/logic/tokens/utils/tokenHelpers.ts +++ b/src/logic/tokens/utils/tokenHelpers.ts @@ -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 => { +export const isAddressAToken = async (tokenAddress: string): Promise => { // SECOND APPROACH: // They both seem to work the same // const tokenContract = await getStandardTokenContract() diff --git a/src/routes/safe/store/models/safe.ts b/src/routes/safe/store/models/safe.ts index 40eb1621..22f4c918 100644 --- a/src/routes/safe/store/models/safe.ts +++ b/src/routes/safe/store/models/safe.ts @@ -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 activeTokens: Set activeAssets: Set blacklistedTokens: Set @@ -25,6 +26,7 @@ const makeSafe = Record({ threshold: 0, ethBalance: 0, owners: List([]), + modules: Set(), activeTokens: Set(), activeAssets: Set(), blacklistedTokens: Set(),