Skip to content

Commit

Permalink
feat: add terms to nftitem metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
albertfolch-redeemeum committed Mar 5, 2024
1 parent 7c8f885 commit d5415a7
Show file tree
Hide file tree
Showing 9 changed files with 529 additions and 157 deletions.
7 changes: 5 additions & 2 deletions packages/core-sdk/src/metadata/bundle.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,11 @@ fragment BaseBundleMetadataEntityFields on BundleMetadataEntity {
min
max
}
transferMethod
transferDelay
terms {
key
value
displayType
}
quantity
attributes {
traitType
Expand Down
7 changes: 5 additions & 2 deletions packages/core-sdk/src/offers/queries.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,11 @@ fragment BaseOfferFields on Offer {
min
max
}
transferMethod
transferDelay
terms {
key
value
displayType
}
quantity
attributes {
traitType
Expand Down
567 changes: 427 additions & 140 deletions packages/core-sdk/src/subgraph.ts

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion packages/metadata/src/nftItem/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,15 @@ export type NftItem = Omit<IItemMetadata, "type"> & {
};
quantity?: number;
transferMethod?: string;
transferDelay?: string;
terms?:
| {
key: string;
value: string;
displayType?: string;
}[]
| {
key: string;
value: string;
display_type?: string;
}[];
};
15 changes: 13 additions & 2 deletions packages/metadata/src/nftItem/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,19 @@
"max": { "type": "string" }
}
},
"transferMethod": { "type": "string" },
"transferDelay": { "type": "string" },
"terms":{
"type": "array",
"items": {
"type": "object",
"properties": {
"key": { "type": "string"},
"value": { "type": "string"},
"displayType": { "type": "string"},
"display_type": { "type": "string"}
},
"required": ["value", "key"]
}
},
"quantity": { "type": "number", "moreThan": 0 }
},
"required": ["schemaUrl", "type", "name"]
Expand Down
12 changes: 10 additions & 2 deletions packages/metadata/tests/nft-item/valid/full.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@
"max": "115792089237316195423570985008687907853269984665640564039457584007913129639935"
},
"quantity": 18,
"transferMethod": "safeTransferFrom(address,address,uint256)",
"transferDelay": "15 days"
"terms": [
{
"key": "transferMethod",
"value": "safeTransferFrom(address,address,uint256)"
},
{
"key": "transferDelay",
"value": "15 days"
}
]
}
11 changes: 9 additions & 2 deletions packages/subgraph/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,14 @@ type TokenIdRange @entity {
max: String!
}

type Term @entity {
id: ID!
key: String!
value: String!
displayType: String!
}


type NftItemMetadataEntity implements ItemMetadataInterface @entity {
id: ID!
schemaUrl: String! # JSON schema URL
Expand All @@ -301,10 +309,9 @@ type NftItemMetadataEntity implements ItemMetadataInterface @entity {
contract: String
tokenId: String
tokenIdRange: TokenIdRange
transferMethod: String
transferDelay: String
quantity: Int
attributes: [MetadataAttribute!]
terms: [Term!]
bundle: BundleMetadataEntity!
metadataUri: String!
}
Expand Down
14 changes: 9 additions & 5 deletions packages/subgraph/src/entities/metadata/nft-item/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { JSONValue, TypedMap } from "@graphprotocol/graph-ts";
import { getItemMetadataEntityId, saveMetadataAttributes } from "../utils";
import {
getItemMetadataEntityId,
saveMetadataAttributes,
saveTerms
} from "../utils";
import {
convertToInt,
convertToObject,
Expand Down Expand Up @@ -49,13 +53,14 @@ export function saveNftItemMetadata(
const contract = convertToString(metadataObj.get("contract"));
const tokenId = convertToString(metadataObj.get("tokenId"));
const tokenIdRangeObj = convertToObject(metadataObj.get("tokenIdRange"));
const transferMethod = convertToString(metadataObj.get("transferMethod"));
const transferDelay = convertToString(metadataObj.get("transferDelay"));
const quantity = convertToInt(metadataObj.get("quantity"));

const attributes = convertToObjectArray(metadataObj.get("attributes"));
const savedMetadataAttributeIds = saveMetadataAttributes(attributes);

const terms = convertToObjectArray(metadataObj.get("terms"));
const savedTermsIds = saveTerms(terms);

let nftItemMetadataEntity = NftItemMetadataEntity.load(metadataId);
if (!nftItemMetadataEntity) {
nftItemMetadataEntity = new NftItemMetadataEntity(metadataId);
Expand All @@ -75,8 +80,7 @@ export function saveNftItemMetadata(
if (tokenIdRangeObj) {
nftItemMetadataEntity.tokenIdRange = saveTokenIdRange(tokenIdRangeObj);
}
nftItemMetadataEntity.transferMethod = transferMethod;
nftItemMetadataEntity.transferDelay = transferDelay;
nftItemMetadataEntity.terms = savedTermsIds;
nftItemMetadataEntity.quantity = quantity;
nftItemMetadataEntity.attributes = savedMetadataAttributeIds;
nftItemMetadataEntity.bundle = bundleId;
Expand Down
41 changes: 40 additions & 1 deletion packages/subgraph/src/entities/metadata/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { JSONValue, TypedMap } from "@graphprotocol/graph-ts";

import { MetadataAttribute } from "../../../generated/schema";
import { MetadataAttribute, Term } from "../../../generated/schema";
import { convertToString } from "../../utils/json";

export function getMetadataEntityId(id: string): string {
Expand Down Expand Up @@ -52,3 +52,42 @@ export function saveMetadataAttributes(

return savedMetadataAttributeIds;
}

export function getTermId(
key: string,
value: string,
displayType: string
): string {
return `${key}-${value}-${displayType}`;
}

export function saveTerms(
termsArray: Array<TypedMap<string, JSONValue>>
): Array<string> {
const savedTermsIds: string[] = [];

for (let i = 0; i < termsArray.length; i++) {
const termsObj = termsArray[i];
const key = convertToString(termsObj.get("key"));
const value = convertToString(termsObj.get("value"));
let displayType = convertToString(termsObj.get("displayType"));
if (!displayType) {
displayType = convertToString(termsObj.get("display_type"));
}
const termId = getTermId(key, value, displayType);

let term = Term.load(termId);

if (!term) {
term = new Term(termId);
}

term.key = key;
term.value = value;
term.displayType = displayType;
term.save();
savedTermsIds.push(termId);
}

return savedTermsIds;
}

0 comments on commit d5415a7

Please sign in to comment.