Skip to content

Commit

Permalink
test deploy attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
RedBeardEth committed Jan 11, 2024
1 parent cd69f17 commit aa4d154
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 65 deletions.
2 changes: 1 addition & 1 deletion apps/nextjs/src/app/collection/[id]/(list)/Trade.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { TokenTable } from "@/app/collection/TokenTable";
import { TradeFilters } from "@/app/collection/TradeFilters";
import { getAttributes } from "@/lib/reservoir/getAttributes";
import { getToken } from "@/lib/reservoir/getToken";
import { api } from "@/trpc/server";
//import { api } from "@/trpc/server";
import { getTokenContractAddresses } from "@/utils/utils";

const L2ERC721Table = dynamic(() => import("./L2ERC721Table"));
Expand Down
181 changes: 127 additions & 54 deletions apps/nextjs/src/inngest/fetch_metadata.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { NextResponse } from "next/server";
import { hash, shortString, uint256 } from "starknet";

import { and, db, eq, schema, sql } from "@realms-world/db";
import type { SQL } from "@realms-world/db";
import { and, db, eq, inArray, schema, sql } from "@realms-world/db";

import { getTokenContractAddresses } from "../utils/utils";
//import { Client } from "https://esm.sh/ts-postgres";
Expand Down Expand Up @@ -106,15 +107,18 @@ export const fetchMetadata = inngest.createFunction(
)
.returning({ updatedId: schema.erc721Tokens.id });

let attributeKeyQuery: { updatedId: number | null }[] = [];
let attributesQuery: { updatedId: number | null }[] = [];
let tokenAttributeQuery: { updatedId: string | null }[] = [];
const attributeKeys = [];

const attributeKeyResult = await db
let attributeKeyResult: { updatedId: number | null }[] = [];
let attributesResult: { id: number | null }[] = [];
let tokenAttributeResult: { key: string | null }[] = [];
let attributeKeysCountUpdate: { updatedId: number | null }[] = [];
const attributesCountMap: Record<number, { tokenCount: number }> = {};
const addedTokenAttributes = [];
const tokenAttributeCounter = [];
const attributeKeyQuery = await db
.select({
id: schema.erc721AttributeKeys.id,
key: schema.erc721AttributeKeys.key,
attributeCount: schema.erc721AttributeKeys.attributeCount,
})
.from(schema.erc721AttributeKeys)
.where(
Expand All @@ -124,14 +128,25 @@ export const fetchMetadata = inngest.createFunction(
),
);

const attributeKeysIdsMap = attributeKeyResult.map((a) => ({
/*const attributeKeysIdsMap = attributeKeyQuery.map((a) => ({
[a.key ?? "-"]: { id: a.id },
}));
}));*/

const attributeKeysIdsMap = attributeKeyQuery.reduce(
(acc, a) => {
acc[a.key] = { id: a.id, attributeCount: a.attributeCount };
return acc;
},
{} as Record<string, { id: number; attributeCount: number }>,
);

if (parsedJson.attributes) {
for (const attribute of parsedJson.attributes) {
// fetch attribute key fo collection if not exists insert
if (!attributeKeysIdsMap.includes(attribute.trait_type)) {
attributeKeyQuery = await db
//Insert new AttributeKey
if (
!Object.keys(attributeKeysIdsMap).includes(attribute.trait_type)
) {
attributeKeyResult = await db
.insert(schema.erc721AttributeKeys)
.values({
key: attribute.trait_type,
Expand All @@ -140,62 +155,120 @@ export const fetchMetadata = inngest.createFunction(
})
.onConflictDoNothing()
.returning({ updatedId: schema.erc721AttributeKeys.id });
if (attributeKeyQuery[0]?.updatedId) {
attributeKeysIdsMap.push({
[attribute.trait_type]: {
id: attributeKeyQuery[0]?.updatedId,
},
});

//Add new Attribute Keys to IdMap
if (attributeKeyResult[0]?.updatedId) {
attributeKeysIdsMap[attribute.trait_type] = {
id: attributeKeyResult[0]?.updatedId,
attributeCount: 0,
};
}
}
const attributesResult = await db
.select({
id: schema.erc721Attributes.id,
})
.from(schema.erc721Attributes)
.where(
and(
eq(
schema.erc721Attributes.attributeKeyId,
attributeKeysIdsMap.find(
(a) => attribute.trait_type in a,
)?.[attribute.trait_type]?.id ?? 0,
const attributeKey = attributeKeysIdsMap[attribute.trait_type];
if (attributeKey?.id) {
//Fetch Existing Attribute for attribute Key + value
const attributesQuery = await db
.select({
id: schema.erc721Attributes.id,
tokenCount: schema.erc721Attributes.tokenCount,
})
.from(schema.erc721Attributes)
.where(
and(
eq(schema.erc721Attributes.attributeKeyId, attributeKey.id),
eq(schema.erc721Attributes.value, attribute.value),
),
eq(schema.erc721Attributes.value, attribute.value),
),
);
if (!attributesResult[0]?.id) {
attributesQuery = await db
.insert(schema.erc721Attributes)
);

// Add queried Atttributes to counts map
if (attributesQuery[0]?.id) {
attributesCountMap[attributesQuery[0].id] = {
tokenCount: attributesQuery[0].tokenCount ?? 0,
};
}

// Insert Attribute if doesnt Exist
if (!attributesQuery[0]?.id) {
attributesResult = await db
.insert(schema.erc721Attributes)
.values({
kind: typeof attribute.value,
key: attribute.trait_type,
value: attribute.value,
collectionId: event.data.contract_address,
attributeKeyId: attributeKey.id, //
})
.onConflictDoNothing()
.returning({ id: schema.erc721Attributes.id });

//Update count of attribute keys
attributeKeysCountUpdate = await db
.update(schema.erc721AttributeKeys)
.set({
attributeCount: attributeKey?.attributeCount + 1,
})
.where(eq(schema.erc721AttributeKeys.id, attributeKey.id))
.returning({ updatedId: schema.erc721AttributeKeys.id });

// Add to attributes count map

if (attributesResult[0]?.id) {
attributesCountMap[attributesResult[0]?.id] = {
tokenCount: 0,
};
}
}
// Insert Token Attributes
tokenAttributeResult = await db
.insert(schema.erc721TokenAttributes)
.values({
kind: typeof attribute.value,
token_key: tokenKey,
key: attribute.trait_type,
value: attribute.value,
collectionId: event.data.contract_address,
attributeKeyId: attributeKeysIdsMap.find(
(a) => attribute.trait_type in a,
)?.[attribute.trait_type]?.id, //
attributeId:
attributesResult[0]?.id ?? attributesQuery[0]?.id,
})
.onConflictDoNothing()
.returning({ updatedId: schema.erc721Attributes.id });
.returning({
key: schema.erc721TokenAttributes.key,
value: schema.erc721TokenAttributes.value,
attributeId: schema.erc721TokenAttributes.attributeId,
});

if (tokenAttributeResult[0]?.key && attributesResult[0]?.id) {
tokenAttributeCounter.push({
id: attributesResult[0].id,
count: 1,
});
}
}
}
if (Object.keys(attributesCountMap)) {
const sqlChunks: SQL[] = [];
const ids: number[] = [];
sqlChunks.push(sql`(case`);
for (const key in attributesCountMap) {
sqlChunks.push(
sql.raw(
`when id = ${key} then ${
(attributesCountMap[key]?.tokenCount ?? 0) + 1
}`,
),
);
ids.push(parseInt(key));
}
tokenAttributeQuery = await db
.insert(schema.erc721TokenAttributes)
.values({
token_key: tokenKey,
key: attribute.trait_type,
value: attribute.value,
collectionId: event.data.contract_address,
attributeId:
attributesQuery[0]?.updatedId ?? attributesResult[0]?.id,
})
.onConflictDoNothing()
.returning({ updatedId: schema.erc721TokenAttributes.key });
sqlChunks.push(sql`end)`);
const finalSql: SQL = sql.join(sqlChunks, sql.raw(" "));
const res = await db
.update(schema.erc721Attributes)
.set({ tokenCount: finalSql })
.where(inArray(schema.erc721Attributes.id, ids));
}
}
return {
token: query[0]?.updatedId,
attributeKey: attributeKeyQuery[0]?.updatedId,
attributeKey: attributeKeyResult[0]?.updatedId,
//tokenAttribute: tokenAttributeQuery[0]?.updatedId,
};
},
Expand Down
25 changes: 18 additions & 7 deletions packages/db/src/schema/erc721_attribute_keys.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
import { relations } from "drizzle-orm";
import { integer, primaryKey, serial, text } from "drizzle-orm/pg-core";
import {
index,
integer,
jsonb,
primaryKey,
serial,
text,
uniqueIndex,
} from "drizzle-orm/pg-core";

import { int8range } from "../int8range";
import { pgSqlTable } from "./_table";
import { erc721Collections } from "./erc721_collections";

export const erc721AttributeKeys = pgSqlTable(
"erc721_attribute_keys",
{
id: serial("id"),
key: text("key"),
kind: text("kind"),
collectionId: text("collection_id"),
attributeCount: integer("attribute_count"),
id: serial("id").notNull(),
key: text("key").notNull(),
kind: text("kind").notNull(),
collectionId: text("collection_id").notNull(),
attributeCount: integer("attribute_count").notNull().default(0),
info: jsonb("info"),
},
(table) => {
return {
pk: primaryKey({ columns: [table.collectionId, table.key] }),
attributeKeysCollectionIdKeyIndex: uniqueIndex(
"attribute_keys_collection_id_key_index",
).on(table.collectionId, table.key),
};
},
);
Expand Down
14 changes: 12 additions & 2 deletions packages/db/src/schema/erc721_attributes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { relations } from "drizzle-orm";
import { integer, primaryKey, serial, text, unique } from "drizzle-orm/pg-core";
import {
index,
integer,
primaryKey,
serial,
text,
unique,
} from "drizzle-orm/pg-core";

import { int8range } from "../int8range";
import { pgSqlTable } from "./_table";
Expand All @@ -14,7 +21,7 @@ export const erc721Attributes = pgSqlTable(
kind: text("kind"),
collectionId: text("collection_id"),
attributeKeyId: integer("attribute_key_id"),
token_count: integer("token_count"),
tokenCount: integer("token_count").default(0),
/*on_sale_count: number;
floor_sell_value: number;
top_buy_value: number;
Expand All @@ -23,6 +30,9 @@ export const erc721Attributes = pgSqlTable(
},
(t) => ({
pk: primaryKey({ columns: [t.collectionId, t.key, t.value] }),
attributesAttributeKeyIdValueIndex: index(
"attributes_attribute_key_id_value_index",
).on(t.attributeKeyId, t.value),
}),
);
/*
Expand Down
14 changes: 13 additions & 1 deletion packages/db/src/schema/erc721_token_attributes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { relations } from "drizzle-orm";
import { integer, primaryKey, text } from "drizzle-orm/pg-core";
import {
index,
integer,
primaryKey,
text,
uniqueIndex,
} from "drizzle-orm/pg-core";

import { int8range } from "../int8range";
import { pgSqlTable } from "./_table";
Expand All @@ -17,6 +23,12 @@ export const erc721TokenAttributes = pgSqlTable(
(table) => {
return {
pk: primaryKey({ columns: [table.token_key, table.key, table.value] }),
tokenAttributesTokenKeyKeyValueIndex: index(
"token_attributes_token_key_key_value_index",
).on(table.token_key, table.key, table.value),
tokenAttributesAttributeIdTokenKeyUniqueIndex: uniqueIndex(
"token_attributes_attribute_id_token_key_unique_index",
).on(table.attributeId, table.token_key),
};
},
);
Expand Down

0 comments on commit aa4d154

Please sign in to comment.