Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: init ark lib + tests #262

Closed
Closed
Prev Previous commit
Next Next commit
feat: add collectionactivity fn
  • Loading branch information
MartianGreed committed Aug 26, 2024
commit d19c8c054875a3cd7853959bd26c4a64c8368763
21 changes: 17 additions & 4 deletions apps/nextjs/src/lib/ark/ark-api.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
import { describe, it, mock, expect } from "bun:test"
import { describe, it, vi, expect, beforeEach } from "vitest"
import { erc721Tokens } from "@/constants";
import { ChainType } from "@/constants/tokens";
import { getCollections } from "./getCollection";
import type { Fetcher } from "./client";
import { ArkClient } from "./client";
import { getCollectionActivity } from "./getCollectionActivity";

describe('ArkApi', () => {
const fetchMock = mock(() => ({ json: () => Promise.resolve({}) }))
const client = new ArkClient(fetchMock);
let fetchMock: Fetcher<any>;
let client: ArkClient;
beforeEach(() => {
fetchMock = vi.fn(() => ({ json: () => Promise.resolve({}) })) as Fetcher<any>;
client = new ArkClient(fetchMock);
});

it('should get collections', async () => {
const collectionAddress = erc721Tokens.beasts.contractAddresses.L2[ChainType.L2.MAIN] as string
const response = await getCollections({ client, collectionAddress })
const _ = await getCollections({ client, collectionAddress })

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

it('should get collections activity', async () => {
const collectionAddress = erc721Tokens.beasts.contractAddresses.L2[ChainType.L2.MAIN] as string
const _ = await getCollectionActivity({ client, collectionAddress, page: 10 })

expect(fetchMock.mock.calls.length).toBe(1)
});
Expand Down
1 change: 1 addition & 0 deletions apps/nextjs/src/lib/ark/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, it, expect } from "vitest"
import { ArkClient } from "./client";

describe('ArkClient', () => {
Expand Down
4 changes: 2 additions & 2 deletions apps/nextjs/src/lib/ark/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
type Fetcher<T> = (url: string, options?: FetcherOpts) => Promise<T>
interface FetcherOpts {
export type Fetcher<T> = (url: string, options?: FetcherOpts) => Promise<T>
export interface FetcherOpts {
headers?: Record<string, string>
};

Expand Down
30 changes: 30 additions & 0 deletions apps/nextjs/src/lib/ark/getCollectionActivity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { ArkClient } from "./client";
import type { CollectionActivity } from "@/types/ark"

export interface CollectionActivityApiResponse {
count: number;
data: CollectionActivity[];
next_page: number;
}

interface GetCollectionActivityParams {
client: ArkClient
collectionAddress: string
page: number
itemsPerPage?: number
}

export async function getCollectionActivity({ client, page, collectionAddress, itemsPerPage = 10 }: GetCollectionActivityParams) {
try {
const queryParams = [`items_per_page=${itemsPerPage}`];

if (page !== undefined) {
queryParams.push(`page=${page}`);
}

return await client.fetch(`/collections/${collectionAddress}/activity?${queryParams.join("&")}`);

} catch (error) {
throw new Error("failed to fetch collection activity : " + error)
}
}
38 changes: 38 additions & 0 deletions apps/nextjs/src/types/ark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,41 @@ export interface Collection {
total_volume: number;
volume_7d_eth: number;
}

export interface TokenMetadataAttribute {
display_type?: string;
trait_type?: string;
value?: string;
}

export interface TokenMetadata {
image: string;
name: string;
animation_key: string | null;
animation_url: string | null;
image_key: string | null;
attributes: TokenMetadataAttribute[];
}

export type CollectionActivityType =
| "LISTING"
| "OFFER"
| "CANCELLED"
| "FULFILL"
| "TRANSFER"
| "EXECUTED"
| "MINT"
| "BURN";

export interface CollectionActivity {
activity_type: CollectionActivityType;
from: string;
is_verified: boolean;
name: string;
price: string;
time_stamp: number;
to: string;
token_id: string;
token_metadata: TokenMetadata;
transaction_hash: string | null;
}