Skip to content

Commit 8f03c24

Browse files
committed
feat: add collectionactivity fn
1 parent 2c7207e commit 8f03c24

File tree

5 files changed

+88
-6
lines changed

5 files changed

+88
-6
lines changed

apps/nextjs/src/lib/ark/ark-api.test.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
1-
import { describe, it, mock, expect } from "bun:test"
1+
import { describe, it, vi, expect, beforeEach } from "vitest"
22
import { erc721Tokens } from "@/constants";
33
import { ChainType } from "@/constants/tokens";
44
import { getCollections } from "./getCollection";
5+
import type { Fetcher } from "./client";
56
import { ArkClient } from "./client";
7+
import { getCollectionActivity } from "./getCollectionActivity";
68

79
describe('ArkApi', () => {
8-
const fetchMock = mock(() => ({ json: () => Promise.resolve({}) }))
9-
const client = new ArkClient(fetchMock);
10+
let fetchMock: Fetcher<any>;
11+
let client: ArkClient;
12+
beforeEach(() => {
13+
fetchMock = vi.fn(() => ({ json: () => Promise.resolve({}) })) as Fetcher<any>;
14+
client = new ArkClient(fetchMock);
15+
});
1016

1117
it('should get collections', async () => {
1218
const collectionAddress = erc721Tokens.beasts.contractAddresses.L2[ChainType.L2.MAIN] as string
13-
const response = await getCollections({ client, collectionAddress })
19+
const _ = await getCollections({ client, collectionAddress })
20+
21+
expect(fetchMock.mock.calls.length).toBe(1)
22+
});
23+
24+
it('should get collections activity', async () => {
25+
const collectionAddress = erc721Tokens.beasts.contractAddresses.L2[ChainType.L2.MAIN] as string
26+
const _ = await getCollectionActivity({ client, collectionAddress, page: 10 })
1427

1528
expect(fetchMock.mock.calls.length).toBe(1)
1629
});

apps/nextjs/src/lib/ark/client.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { describe, it, expect } from "vitest"
12
import { ArkClient } from "./client";
23

34
describe('ArkClient', () => {

apps/nextjs/src/lib/ark/client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
type Fetcher<T> = (url: string, options?: FetcherOpts) => Promise<T>
2-
interface FetcherOpts {
1+
export type Fetcher<T> = (url: string, options?: FetcherOpts) => Promise<T>
2+
export interface FetcherOpts {
33
headers?: Record<string, string>
44
};
55

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import type { ArkClient } from "./client";
2+
import type { CollectionActivity } from "@/types/ark"
3+
4+
export interface CollectionActivityApiResponse {
5+
count: number;
6+
data: CollectionActivity[];
7+
next_page: number;
8+
}
9+
10+
interface GetCollectionActivityParams {
11+
client: ArkClient
12+
collectionAddress: string
13+
page: number
14+
itemsPerPage?: number
15+
}
16+
17+
export async function getCollectionActivity({ client, page, collectionAddress, itemsPerPage = 10 }: GetCollectionActivityParams) {
18+
try {
19+
const queryParams = [`items_per_page=${itemsPerPage}`];
20+
21+
if (page !== undefined) {
22+
queryParams.push(`page=${page}`);
23+
}
24+
25+
return await client.fetch(`/collections/${collectionAddress}/activity?${queryParams.join("&")}`);
26+
27+
} catch (error) {
28+
throw new Error("failed to fetch collection activity : " + error)
29+
}
30+
}

apps/nextjs/src/types/ark.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,41 @@ export interface Collection {
1313
total_volume: number;
1414
volume_7d_eth: number;
1515
}
16+
17+
export interface TokenMetadataAttribute {
18+
display_type?: string;
19+
trait_type?: string;
20+
value?: string;
21+
}
22+
23+
export interface TokenMetadata {
24+
image: string;
25+
name: string;
26+
animation_key: string | null;
27+
animation_url: string | null;
28+
image_key: string | null;
29+
attributes: TokenMetadataAttribute[];
30+
}
31+
32+
export type CollectionActivityType =
33+
| "LISTING"
34+
| "OFFER"
35+
| "CANCELLED"
36+
| "FULFILL"
37+
| "TRANSFER"
38+
| "EXECUTED"
39+
| "MINT"
40+
| "BURN";
41+
42+
export interface CollectionActivity {
43+
activity_type: CollectionActivityType;
44+
from: string;
45+
is_verified: boolean;
46+
name: string;
47+
price: string;
48+
time_stamp: number;
49+
to: string;
50+
token_id: string;
51+
token_metadata: TokenMetadata;
52+
transaction_hash: string | null;
53+
}

0 commit comments

Comments
 (0)