-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/useverto/cace-interface i…
…nto master
- Loading branch information
Showing
15 changed files
with
304 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import {fetchTokenMetadata} from "./fetch-token-metadata"; | ||
import {fetchUserByUsername} from "./fetch-user-by-username"; | ||
import { fetchContract } from "./fetch-contract"; | ||
import {CollectionState} from "./types/collection-state"; | ||
import {ArtworkMetadata} from "./types/artwork-metadata"; | ||
|
||
/** | ||
* Fetches the metadata for a given artwork | ||
* @param tokenId | ||
*/ | ||
export const fetchArtworkMetadata = async (tokenId: string): Promise<ArtworkMetadata | undefined > => { | ||
const fetchMetadata = await fetchTokenMetadata(tokenId, true); | ||
if(!fetchMetadata || fetchMetadata.type.toLowerCase() !== "art") { return undefined; } | ||
|
||
const fetchLister = await fetchUserByUsername(fetchMetadata.lister); | ||
if(!fetchLister) { return undefined; } | ||
|
||
const tokenContract = await fetchContract<CollectionState>(fetchMetadata.id!, false, true); | ||
if(!tokenContract) { return undefined; } | ||
|
||
return { | ||
id: tokenId, | ||
name: tokenContract.state.name, | ||
lister: fetchLister | ||
}; | ||
} |
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,11 @@ | ||
import {fetchCommunityMetadata} from "./fetch-random-communities-with-metadata"; | ||
|
||
/** | ||
* Fetches the metadata for one (string) or more (array) communities. | ||
* This returns an array with (id, name, ticker, logo, description) | ||
* @param communityContractIds Single string or array of string containing the ids of the community as listed in the community contract | ||
*/ | ||
export const fetchCommunitiesMetadata = async (communityContractIds: Array<string> | string) => { | ||
const contractIds = Array.isArray(communityContractIds) ? [...communityContractIds] : [communityContractIds]; | ||
return (await fetchCommunityMetadata(contractIds)); | ||
} |
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 |
---|---|---|
@@ -1,13 +1,13 @@ | ||
import {cacheApiBaseRequest} from "./cache-api-base-request"; | ||
import {RandomArtworkResult, TokenMetadata} from "./types/token-metadata"; | ||
import {TokenMetadataLookUp, TokenMetadata} from "./types/token-metadata"; | ||
|
||
/** | ||
* Fetches random art work (tokens with type = 'collection' or 'art'). | ||
* @param limit Limit of results to be returned | ||
*/ | ||
export const fetchRandomArtwork = async (limit: number = 4) => { | ||
return (await cacheApiBaseRequest<RandomArtworkResult>(`token/artwork/random?limit=${limit}`))?.data || { | ||
return (await cacheApiBaseRequest<TokenMetadataLookUp>(`token/artwork/random?limit=${limit}`))?.data || { | ||
entities: [], | ||
resultsStatus: 'NOT_FOUND' | ||
} as RandomArtworkResult; | ||
} as TokenMetadataLookUp; | ||
} |
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,40 @@ | ||
import {RandomCommunities} from "./types/community-contract-state"; | ||
import {fetchRandomCommunities} from "./fetch-random-communities"; | ||
import {fetchContract} from "./fetch-contract"; | ||
|
||
/** | ||
* Fetches random communities with included metadata such as name, ticker, logo and description. | ||
* @param limit | ||
*/ | ||
export const fetchRandomCommunitiesWithMetadata = async (limit?: number): Promise<Array<RandomCommunities>> => { | ||
const randomCommunities = await fetchRandomCommunities(limit || 4); | ||
const lookupEntities = randomCommunities.entities; | ||
|
||
if(randomCommunities.resultsStatus === "NOT_FOUND" || lookupEntities?.length <= 0) { | ||
return []; | ||
} else { | ||
const contractIds = lookupEntities.map((item) => item.contractId); | ||
return fetchCommunityMetadata(contractIds); | ||
} | ||
} | ||
|
||
export const fetchCommunityMetadata = async (contractIds: Array<string>) => { | ||
const communities: Array<RandomCommunities> = []; | ||
for (const communitiesKey of contractIds) { | ||
const contract = await fetchContract(communitiesKey, false, true); | ||
if(contract) { | ||
const contractState = contract.state; | ||
const settings: Array<string> = (contractState.settings || []).flat(); | ||
const logoIndex = settings.findIndex(item => item === "communityLogo"); | ||
const descriptionIndex = settings.findIndex(item => item === "communityDescription"); | ||
communities.push({ | ||
id: communitiesKey, | ||
name: contractState.name, | ||
ticker: contractState.ticker, | ||
logo: settings[logoIndex + 1], | ||
description: settings[descriptionIndex + 1], | ||
}) | ||
} | ||
} | ||
return communities; | ||
} |
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,13 @@ | ||
import {cacheApiBaseRequest} from "./cache-api-base-request"; | ||
import {TokenMetadataLookUp, TokenMetadata} from "./types/token-metadata"; | ||
|
||
/** | ||
* Fetches random communities (tokens with type = 'community'). | ||
* @param limit Limit of results to be returned | ||
*/ | ||
export const fetchRandomCommunities = async (limit: number = 4) => { | ||
return (await cacheApiBaseRequest<TokenMetadataLookUp>(`token/communities/random?limit=${limit}`))?.data || { | ||
entities: [], | ||
resultsStatus: 'NOT_FOUND' | ||
} as TokenMetadataLookUp; | ||
} |
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,12 @@ | ||
import {fetchTokens} from "./fetch-tokens"; | ||
import {CommunityContractToken} from "./types/community-contract-state"; | ||
|
||
/** | ||
* Fetch a token given an id an optionally a predicate | ||
* @param tokenId Id for token to look up | ||
* @param filter Predicate for token | ||
*/ | ||
export const fetchTokenById = async (tokenId: string, filter?: (val: CommunityContractToken) => boolean): Promise<CommunityContractToken | undefined> => { | ||
const allTokens = await fetchTokens(); | ||
return allTokens.find((token) => token.id === tokenId && (filter ? filter(token) : true)); | ||
} |
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,16 @@ | ||
import {fetchTokens} from "./fetch-tokens"; | ||
import {cacheApiBaseRequest} from "./cache-api-base-request"; | ||
import {CommunityBalancesRaw} from "./types/community-contract-state"; | ||
import {LookUp} from "./types/lookup"; | ||
import {fetchCommunityMetadata} from "./fetch-random-communities-with-metadata"; | ||
|
||
/** | ||
* Fetches the communities with the top balance amount per contract. | ||
* Returns metadata containing: (id, name, ticker, logo, description) | ||
* @param limit: Max to fetch | ||
*/ | ||
export const fetchTopCommunities = async (limit?: number) => { | ||
const cacheRequest = (await (cacheApiBaseRequest<LookUp<CommunityBalancesRaw>>(`token/communities/top?limit=${limit || 4}`)))?.data?.entities || []; | ||
const contractIds = cacheRequest.map((item) => item.contractId); | ||
return (await fetchCommunityMetadata(contractIds)); | ||
} |
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,7 @@ | ||
import {CommunityContractPeople} from "./community-contract-state"; | ||
|
||
export interface ArtworkMetadata { | ||
id: string; | ||
name: string; | ||
lister: CommunityContractPeople; | ||
} |
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,6 @@ | ||
import {TokenMetadata} from "./token-metadata"; | ||
|
||
export interface LookUp<T = any> { | ||
entities: Array<T>; | ||
resultsStatus: string; | ||
} |
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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import {LookUp} from "./lookup"; | ||
|
||
export interface TokenMetadata { | ||
contractId: string; | ||
type: string; | ||
lister: string; | ||
id?: string; | ||
} | ||
|
||
export interface RandomArtworkResult { | ||
entities: Array<TokenMetadata>; | ||
resultsStatus: string; | ||
export interface TokenMetadataLookUp extends LookUp<TokenMetadata> { | ||
} |
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