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(1165) : collector whale entity + resolver #87

Merged
merged 10 commits into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions db/migrations/1653223854282-Data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = class Data1653223854282 {
name = 'Data1653223854282'

async up(db) {
await db.query(`CREATE TABLE "collector" ("id" character varying NOT NULL, "name" text NOT NULL, "unique" integer NOT NULL, "unique_collectors" integer NOT NULL, "collections" integer NOT NULL, "total" integer NOT NULL, "average" numeric, "volume" numeric, "max" numeric, CONSTRAINT "PK_0d93071208ac3e8dd49506d903f" PRIMARY KEY ("id"))`)
await db.query(`CREATE INDEX "IDX_d2a6b09fab598ea3dad4f89556" ON "collector" ("volume") `)
}

async down(db) {
await db.query(`DROP TABLE "collector"`)
await db.query(`DROP INDEX "public"."IDX_d2a6b09fab598ea3dad4f89556"`)
}
}
12 changes: 12 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,18 @@ type Spotlight @entity {
volume: BigInt
}

type Collector @entity {
id: ID!
name: String!
unique: Int!
uniqueCollectors: Int!
collections: Int!
total: Int!
average: BigInt
volume: BigInt @index
max: BigInt
}

type CacheStatus @entity {
id: ID!
lastBlockTimestamp: DateTime!
Expand Down
17 changes: 14 additions & 3 deletions src/mappings/utils/cache.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logger, { logError } from './logger';
import { Series, Spotlight, CacheStatus } from "../../model/generated";
import { Series, Spotlight, CacheStatus, Collector } from "../../model/generated";
import { Store } from "@subsquid/substrate-processor";
import { EntityConstructor } from "./types";
import { getOrCreate } from './entity';
Expand Down Expand Up @@ -38,7 +38,17 @@ enum Query {
COALESCE(SUM(e.meta::bigint), 0) as volume
FROM nft_entity ne
JOIN event e on e.nft_id = ne.id WHERE e.interaction = 'BUY'
GROUP BY issuer`
GROUP BY issuer`,

collector = `SELECT
issuer as id, COUNT(distinct collection_id) as collections,
COUNT(distinct meta_id) as unique, AVG(price) as average,
COUNT(*) as total, COUNT(distinct ne.current_owner) as unique_collectors,
SUM(CASE WHEN ne.issuer <> ne.current_owner THEN 1 ELSE 0 END) as sold,
COALESCE(SUM(e.meta::bigint), 0) as volume
FROM nft_entity ne
JOIN event e on e.nft_id = ne.id WHERE e.interaction = 'BUY'
GROUP BY issuer`
}

export async function updateCache(timestamp: Date, store: Store): Promise<void> {
Expand All @@ -49,7 +59,8 @@ export async function updateCache(timestamp: Date, store: Store): Promise<void>
try {
await Promise.all([
updateEntityCache(store, Series, Query.series),
updateEntityCache(store, Spotlight, Query.spotlight)
updateEntityCache(store, Spotlight, Query.spotlight),
updateEntityCache(store, Collector, Query.collector)
])
lastUpdate.lastBlockTimestamp = timestamp;
await store.save(lastUpdate)
Expand Down
37 changes: 37 additions & 0 deletions src/model/generated/collector.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_, Index as Index_} from "typeorm"
import * as marshal from "./marshal"

@Entity_()
export class Collector {
constructor(props?: Partial<Collector>) {
Object.assign(this, props)
}

@PrimaryColumn_()
id!: string

@Column_("text", {nullable: false})
name!: string

@Column_("int4", {nullable: false})
unique!: number

@Column_("int4", {nullable: false})
uniqueCollectors!: number

@Column_("int4", {nullable: false})
collections!: number

@Column_("int4", {nullable: false})
total!: number

@Column_("numeric", {transformer: marshal.bigintTransformer, nullable: true})
average!: bigint | undefined | null

@Index_()
@Column_("numeric", {transformer: marshal.bigintTransformer, nullable: true})
volume!: bigint | undefined | null

@Column_("numeric", {transformer: marshal.bigintTransformer, nullable: true})
max!: bigint | undefined | null
}
1 change: 1 addition & 0 deletions src/model/generated/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export * from "./_interaction"
export * from "./emote.model"
export * from "./series.model"
export * from "./spotlight.model"
export * from "./collector.model"
export * from "./cacheStatus.model"
32 changes: 32 additions & 0 deletions src/server-extension/model/collector.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Field, ObjectType } from 'type-graphql';

@ObjectType()
export class CollectorEntity {
@Field(() => String, { nullable: false })
id!: string

@Field(() => Number, { nullable: false })
name!: string

@Field(() => Number, { nullable: false, name: 'uniqueCollectors' })
unique_collectors!: number

@Field(() => Number, { nullable: false })
unique!: number

@Field(() => BigInt, { nullable: true, })
average!: bigint

@Field(() => BigInt, { nullable: true, defaultValue: 0n, })
volume!: bigint

@Field(() => Number, { nullable: true, defaultValue: 0n, })
total!: number

@Field(() => BigInt, { nullable: true, defaultValue: 0n, })
max!: bigint

constructor(props: Partial<CollectorEntity>) {
Object.assign(this, props);
}
}
57 changes: 57 additions & 0 deletions src/server-extension/resolvers/collector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {
Arg,
Query,
Resolver,
} from "type-graphql";
import type { EntityManager } from "typeorm";
import { NFTEntity} from "../../model/generated";
import { CollectorEntity } from "../model/collector.model";
import { makeQuery } from "../utils";

enum OrderBy {
name = "name",
total = "total",
average = "average",
volume = "volume",
max = "max",
}

enum OrderDirection {
DESC = "DESC",
ASC = "ASC",
}

@Resolver((of) => CollectorEntity)
export class CollectorResolver {
constructor(private tx: () => Promise<EntityManager>) {}

// TODO: calculate score sold * (unique / total)
@Query(() => [CollectorEntity])
async collectorTable(
@Arg("limit", { nullable: true, defaultValue: null }) limit: number,
@Arg("offset", { nullable: true, defaultValue: null }) offset: string,
@Arg("orderBy", { nullable: true, defaultValue: "volume" }) orderBy: OrderBy,
@Arg("orderDirection", { nullable: true, defaultValue: "DESC" })
orderDirection: OrderDirection
): Promise<CollectorEntity[]> {

const query = `SELECT
ne.current_owner as id, ne.current_owner as name,
COUNT(distinct collection_id) as collections,
COUNT(distinct meta_id) as unique, AVG(price) as average,
COUNT(*) as total, COUNT(distinct ne.current_owner) as unique_collectors,
SUM(CASE WHEN ne.issuer <> ne.current_owner THEN 1 ELSE 0 END) as total,
COALESCE(SUM(e.meta::bigint), 0) as volume,
COALESCE(MAX(e.meta::bigint), 0) as max
FROM nft_entity ne
JOIN event e on e.nft_id = ne.id WHERE e.interaction = 'BUY'
GROUP BY ne.current_owner
ORDER BY ${orderBy} ${orderDirection}
LIMIT $1 OFFSET $2`

const result: [CollectorEntity] = await makeQuery(this.tx, NFTEntity, query, [limit, offset])

return result;

}
}
2 changes: 2 additions & 0 deletions src/server-extension/resolvers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { SpotlightResolver } from './spotlight'
import { CollectionChartResolver } from './collectionChart'
import { CollectionEventResolver } from './collectionEvent'
import { PassionFeedResolver } from "./passionFeed";
import { CollectorResolver } from "./collector";

@ObjectType()
export class Hello {
Expand Down Expand Up @@ -37,4 +38,5 @@ export {
SpotlightResolver,
CollectionEventResolver,
PassionFeedResolver,
CollectorResolver
}