Skip to content

Commit 715af3d

Browse files
committed
Merge branch 'master' of https://github.com/useverto/cace-interface into master
2 parents 6c963b9 + 8c8ae25 commit 715af3d

15 files changed

+304
-9
lines changed

README.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,128 @@ fetchRandomArtworkWithUser().then((result) => {
369369
})
370370
```
371371

372+
### Fetching random communities without full metadata
373+
374+
**Signature**:
375+
`fetchRandomCommunities = async (): Promise<TokenMetadataLookUp>`
376+
377+
**Usage**:
378+
```typescript
379+
import { fetchRandomCommunities } from 'verto-cache-interface';
380+
381+
fetchRandomCommunities().then((result) => {
382+
const communities = result.entities;
383+
communities.forEach((com) => {
384+
console.log(com.contractId);
385+
console.log(com.type);
386+
console.log(com.lister);
387+
console.log(com.id);
388+
})
389+
})
390+
```
391+
392+
### Fetching random communities with full metadata
393+
394+
**Signature**:
395+
`fetchRandomCommunitiesWithMetadata = async (): Promise<Array<RandomCommunities>>`
396+
397+
**Usage**:
398+
```typescript
399+
import { fetchRandomCommunitiesWithMetadata } from 'verto-cache-interface';
400+
401+
fetchRandomCommunitiesWithMetadata().then((result) => {
402+
result.forEach((com) => {
403+
console.log(com.id);
404+
console.log(com.name);
405+
console.log(com.ticker);
406+
console.log(com.logo);
407+
console.log(com.description);
408+
})
409+
})
410+
```
411+
412+
### Fetching top communities with full metadata
413+
414+
**Signature**:
415+
`fetchTopCommunities = async (): Promise<Array<RandomCommunities>>`
416+
417+
**Usage**:
418+
```typescript
419+
import { fetchTopCommunities } from 'verto-cache-interface';
420+
421+
fetchTopCommunities().then((result) => {
422+
result.forEach((com) => {
423+
console.log(com.id);
424+
console.log(com.name);
425+
console.log(com.ticker);
426+
console.log(com.logo);
427+
console.log(com.description);
428+
})
429+
})
430+
```
431+
432+
### Fetching full metadata for given communities
433+
434+
**Signature**:
435+
`fetchCommunitiesMetadata = async (communityContractIds: Array<string> | string): Promise<Array<RandomCommunities>>`
436+
437+
**Usage**:
438+
```typescript
439+
import { fetchCommunitiesMetadata } from 'verto-cache-interface';
440+
441+
fetchCommunitiesMetadata(["MY-community-id1", "MY-community-id2"]).then((result) => {
442+
result.forEach((com) => {
443+
console.log(com.id);
444+
console.log(com.name);
445+
console.log(com.ticker);
446+
console.log(com.logo);
447+
console.log(com.description);
448+
})
449+
})
450+
```
451+
452+
### Fetching artwork metadata
453+
454+
**Signature**:
455+
`fetchArtworkMetadata = async (): Promise<ArtworkMetadata | undefined>`
456+
457+
**Usage**:
458+
```typescript
459+
import { fetchArtworkMetadata } from 'verto-cache-interface';
460+
461+
fetchArtworkMetadata().then((result) => {
462+
console.log(result.id);
463+
console.log(result.name);
464+
console.log(result.lister); // Object (username, name, addresses, bio, links, image)
465+
console.log(result.lister.addresses);
466+
console.log(result.lister.bio);
467+
console.log(result.lister.links);
468+
})
469+
```
470+
471+
### Fetching token by id and optional filtering
472+
473+
**Signature**:
474+
`fetchTokenById = async (tokenId: string, filter?: (val: CommunityContractToken) => boolean): Promise<CommunityContractToken | undefined>`
475+
476+
**Usage**:
477+
```typescript
478+
import { fetchTokenById } from 'verto-cache-interface';
479+
480+
fetchTokenById("ABC").then((result) => {
481+
console.log(result.id);
482+
console.log(result.type);
483+
console.log(result.lister);
484+
});
485+
486+
487+
fetchTokenById("ABC", (filterData) => filterData.type === "community").then((result) => {
488+
console.log(result.id);
489+
console.log(result.type);
490+
console.log(result.lister);
491+
});
492+
```
493+
372494
## Hooks
373495
Hooks are a way to invoke functions and then invoke certain behaviors inside the cache system.
374496

src/calls/fetch-artwork-metadata.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {fetchTokenMetadata} from "./fetch-token-metadata";
2+
import {fetchUserByUsername} from "./fetch-user-by-username";
3+
import { fetchContract } from "./fetch-contract";
4+
import {CollectionState} from "./types/collection-state";
5+
import {ArtworkMetadata} from "./types/artwork-metadata";
6+
7+
/**
8+
* Fetches the metadata for a given artwork
9+
* @param tokenId
10+
*/
11+
export const fetchArtworkMetadata = async (tokenId: string): Promise<ArtworkMetadata | undefined > => {
12+
const fetchMetadata = await fetchTokenMetadata(tokenId, true);
13+
if(!fetchMetadata || fetchMetadata.type.toLowerCase() !== "art") { return undefined; }
14+
15+
const fetchLister = await fetchUserByUsername(fetchMetadata.lister);
16+
if(!fetchLister) { return undefined; }
17+
18+
const tokenContract = await fetchContract<CollectionState>(fetchMetadata.id!, false, true);
19+
if(!tokenContract) { return undefined; }
20+
21+
return {
22+
id: tokenId,
23+
name: tokenContract.state.name,
24+
lister: fetchLister
25+
};
26+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {fetchCommunityMetadata} from "./fetch-random-communities-with-metadata";
2+
3+
/**
4+
* Fetches the metadata for one (string) or more (array) communities.
5+
* This returns an array with (id, name, ticker, logo, description)
6+
* @param communityContractIds Single string or array of string containing the ids of the community as listed in the community contract
7+
*/
8+
export const fetchCommunitiesMetadata = async (communityContractIds: Array<string> | string) => {
9+
const contractIds = Array.isArray(communityContractIds) ? [...communityContractIds] : [communityContractIds];
10+
return (await fetchCommunityMetadata(contractIds));
11+
}

src/calls/fetch-random-artwork.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import {cacheApiBaseRequest} from "./cache-api-base-request";
2-
import {RandomArtworkResult, TokenMetadata} from "./types/token-metadata";
2+
import {TokenMetadataLookUp, TokenMetadata} from "./types/token-metadata";
33

44
/**
55
* Fetches random art work (tokens with type = 'collection' or 'art').
66
* @param limit Limit of results to be returned
77
*/
88
export const fetchRandomArtwork = async (limit: number = 4) => {
9-
return (await cacheApiBaseRequest<RandomArtworkResult>(`token/artwork/random?limit=${limit}`))?.data || {
9+
return (await cacheApiBaseRequest<TokenMetadataLookUp>(`token/artwork/random?limit=${limit}`))?.data || {
1010
entities: [],
1111
resultsStatus: 'NOT_FOUND'
12-
} as RandomArtworkResult;
12+
} as TokenMetadataLookUp;
1313
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import {RandomCommunities} from "./types/community-contract-state";
2+
import {fetchRandomCommunities} from "./fetch-random-communities";
3+
import {fetchContract} from "./fetch-contract";
4+
5+
/**
6+
* Fetches random communities with included metadata such as name, ticker, logo and description.
7+
* @param limit
8+
*/
9+
export const fetchRandomCommunitiesWithMetadata = async (limit?: number): Promise<Array<RandomCommunities>> => {
10+
const randomCommunities = await fetchRandomCommunities(limit || 4);
11+
const lookupEntities = randomCommunities.entities;
12+
13+
if(randomCommunities.resultsStatus === "NOT_FOUND" || lookupEntities?.length <= 0) {
14+
return [];
15+
} else {
16+
const contractIds = lookupEntities.map((item) => item.contractId);
17+
return fetchCommunityMetadata(contractIds);
18+
}
19+
}
20+
21+
export const fetchCommunityMetadata = async (contractIds: Array<string>) => {
22+
const communities: Array<RandomCommunities> = [];
23+
for (const communitiesKey of contractIds) {
24+
const contract = await fetchContract(communitiesKey, false, true);
25+
if(contract) {
26+
const contractState = contract.state;
27+
const settings: Array<string> = (contractState.settings || []).flat();
28+
const logoIndex = settings.findIndex(item => item === "communityLogo");
29+
const descriptionIndex = settings.findIndex(item => item === "communityDescription");
30+
communities.push({
31+
id: communitiesKey,
32+
name: contractState.name,
33+
ticker: contractState.ticker,
34+
logo: settings[logoIndex + 1],
35+
description: settings[descriptionIndex + 1],
36+
})
37+
}
38+
}
39+
return communities;
40+
}

src/calls/fetch-random-communities.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {cacheApiBaseRequest} from "./cache-api-base-request";
2+
import {TokenMetadataLookUp, TokenMetadata} from "./types/token-metadata";
3+
4+
/**
5+
* Fetches random communities (tokens with type = 'community').
6+
* @param limit Limit of results to be returned
7+
*/
8+
export const fetchRandomCommunities = async (limit: number = 4) => {
9+
return (await cacheApiBaseRequest<TokenMetadataLookUp>(`token/communities/random?limit=${limit}`))?.data || {
10+
entities: [],
11+
resultsStatus: 'NOT_FOUND'
12+
} as TokenMetadataLookUp;
13+
}

src/calls/fetch-token-by-id.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {fetchTokens} from "./fetch-tokens";
2+
import {CommunityContractToken} from "./types/community-contract-state";
3+
4+
/**
5+
* Fetch a token given an id an optionally a predicate
6+
* @param tokenId Id for token to look up
7+
* @param filter Predicate for token
8+
*/
9+
export const fetchTokenById = async (tokenId: string, filter?: (val: CommunityContractToken) => boolean): Promise<CommunityContractToken | undefined> => {
10+
const allTokens = await fetchTokens();
11+
return allTokens.find((token) => token.id === tokenId && (filter ? filter(token) : true));
12+
}

src/calls/fetch-token-metadata.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {cacheApiBaseRequest} from "./cache-api-base-request";
22
import {TokenMetadata} from "./types/token-metadata";
3-
import {fetchTokens} from "./fetch-tokens";
3+
import {fetchTokenById} from "./fetch-token-by-id";
44

55
/**
66
* Returns the metadata of a token (contractId, type, lister)
@@ -9,8 +9,7 @@ import {fetchTokens} from "./fetch-tokens";
99
*/
1010
export const fetchTokenMetadata = async (tokenId: string, fromContract?: boolean) => {
1111
if(fromContract) {
12-
const allTokens = await fetchTokens();
13-
return allTokens.find((token) => token.id === tokenId);
12+
return fetchTokenById(tokenId);
1413
} else {
1514
const getTokenMetadata = await cacheApiBaseRequest<TokenMetadata>(`token/metadata/${tokenId}`);
1615

src/calls/fetch-top-communities.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {fetchTokens} from "./fetch-tokens";
2+
import {cacheApiBaseRequest} from "./cache-api-base-request";
3+
import {CommunityBalancesRaw} from "./types/community-contract-state";
4+
import {LookUp} from "./types/lookup";
5+
import {fetchCommunityMetadata} from "./fetch-random-communities-with-metadata";
6+
7+
/**
8+
* Fetches the communities with the top balance amount per contract.
9+
* Returns metadata containing: (id, name, ticker, logo, description)
10+
* @param limit: Max to fetch
11+
*/
12+
export const fetchTopCommunities = async (limit?: number) => {
13+
const cacheRequest = (await (cacheApiBaseRequest<LookUp<CommunityBalancesRaw>>(`token/communities/top?limit=${limit || 4}`)))?.data?.entities || [];
14+
const contractIds = cacheRequest.map((item) => item.contractId);
15+
return (await fetchCommunityMetadata(contractIds));
16+
}

src/calls/types/artwork-metadata.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {CommunityContractPeople} from "./community-contract-state";
2+
3+
export interface ArtworkMetadata {
4+
id: string;
5+
name: string;
6+
lister: CommunityContractPeople;
7+
}

0 commit comments

Comments
 (0)