-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
636fcd7
commit 86d2fd2
Showing
8 changed files
with
387 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
packages/subgraph/src/entities/metadata/bundle/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import { JSONValue, TypedMap, BigInt, log } from "@graphprotocol/graph-ts"; | ||
import { BundleMetadataEntity, Offer } from "../../../../generated/schema"; | ||
import { | ||
convertToString, | ||
convertToObject, | ||
convertToObjectArray | ||
} from "../../../utils/json"; | ||
import { saveAnimationMetadata } from "../animationMetadata"; | ||
import { saveProductV1Seller } from "../product-v1/seller"; | ||
import { getMetadataEntityId, saveMetadataAttributes } from "../utils"; | ||
import { getIpfsMetadataObject, parseIpfsHash } from "../../../utils/ipfs"; | ||
import { saveNftItemMetadata } from "../nft-item"; | ||
import { saveProductV1ItemMetadata } from "../product-v1"; | ||
|
||
export function saveBundleMetadata( | ||
offer: Offer, | ||
metadataObj: TypedMap<string, JSONValue>, | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
timestamp: BigInt | ||
): string { | ||
const offerId = offer.id.toString(); | ||
const metadataId = getMetadataEntityId(offerId); | ||
|
||
const name = convertToString(metadataObj.get("name")); | ||
const description = convertToString(metadataObj.get("description")); | ||
const externalUrl = convertToString(metadataObj.get("externalUrl")); | ||
const animationUrl = convertToString(metadataObj.get("animationUrl")); | ||
const animationMetadata = convertToObject( | ||
metadataObj.get("animationMetadata") | ||
); | ||
const licenseUrl = convertToString(metadataObj.get("licenseUrl")); | ||
const condition = convertToString(metadataObj.get("condition")); | ||
const schemaUrl = convertToString(metadataObj.get("schemaUrl")); | ||
const image = convertToString(metadataObj.get("image")); | ||
const attributes = convertToObjectArray(metadataObj.get("attributes")); | ||
const items = convertToObjectArray(metadataObj.get("items")); | ||
|
||
const savedAnimationMetadataId = saveAnimationMetadata(animationMetadata); | ||
const savedMetadataAttributeIds = saveMetadataAttributes(attributes); | ||
const savedProductV1SellerId = saveProductV1Seller( | ||
convertToObject(metadataObj.get("seller")), | ||
offer.sellerId.toString() | ||
); | ||
|
||
if (savedProductV1SellerId === null) { | ||
return metadataId; | ||
} | ||
|
||
let bundleMetadataEntity = BundleMetadataEntity.load(metadataId); | ||
|
||
if (!bundleMetadataEntity) { | ||
bundleMetadataEntity = new BundleMetadataEntity(metadataId); | ||
} | ||
|
||
bundleMetadataEntity.name = name; | ||
bundleMetadataEntity.description = description; | ||
bundleMetadataEntity.externalUrl = externalUrl; | ||
bundleMetadataEntity.animationUrl = animationUrl; | ||
bundleMetadataEntity.animationMetadata = savedAnimationMetadataId; | ||
bundleMetadataEntity.licenseUrl = licenseUrl; | ||
bundleMetadataEntity.schemaUrl = schemaUrl; | ||
bundleMetadataEntity.condition = condition; | ||
bundleMetadataEntity.type = "BUNDLE"; | ||
bundleMetadataEntity.image = image; | ||
bundleMetadataEntity.attributes = savedMetadataAttributeIds; | ||
|
||
bundleMetadataEntity.offer = offerId; | ||
bundleMetadataEntity.seller = offer.sellerId.toString(); | ||
bundleMetadataEntity.exchangeToken = offer.exchangeToken; | ||
|
||
bundleMetadataEntity.createdAt = timestamp; | ||
bundleMetadataEntity.voided = offer.voided; | ||
bundleMetadataEntity.validFromDate = offer.validFromDate; | ||
bundleMetadataEntity.validUntilDate = offer.validUntilDate; | ||
bundleMetadataEntity.quantityAvailable = offer.quantityAvailable; | ||
bundleMetadataEntity.numberOfCommits = offer.numberOfCommits; | ||
bundleMetadataEntity.numberOfRedemptions = offer.numberOfRedemptions; | ||
|
||
bundleMetadataEntity.productV1Seller = savedProductV1SellerId; | ||
|
||
bundleMetadataEntity.save(); | ||
for (let i = 0; i < items.length; i++) { | ||
const itemObj = items[i]; | ||
const itemMetadataUri = convertToString(itemObj.get("url")); | ||
saveItemMetadata( | ||
itemMetadataUri, | ||
offer, | ||
i.toString(), | ||
metadataId, | ||
savedProductV1SellerId | ||
); | ||
} | ||
return metadataId; | ||
} | ||
|
||
function saveItemMetadata( | ||
itemMetadataUri: string, | ||
offer: Offer, | ||
index: string, | ||
bundleId: string, | ||
productV1SellerId: string | ||
): string | null { | ||
const ipfsHash = parseIpfsHash(itemMetadataUri); | ||
|
||
if (ipfsHash === null) { | ||
log.warning("Metadata URI does not contain supported CID: {}", [ | ||
itemMetadataUri | ||
]); | ||
return null; | ||
} | ||
|
||
const metadataObj = getIpfsMetadataObject(ipfsHash); | ||
|
||
if (metadataObj === null) { | ||
log.warning("Could not load metadata with ipfsHash: {}", [ipfsHash]); | ||
return null; | ||
} | ||
|
||
const metadataType = convertToString(metadataObj.get("type")); | ||
|
||
if (metadataType == "ITEM_PRODUCT_V1") { | ||
return saveProductV1ItemMetadata( | ||
offer, | ||
metadataObj, | ||
index, | ||
bundleId, | ||
productV1SellerId | ||
); | ||
} | ||
|
||
if (metadataType == "ITEM_NFT") { | ||
return saveNftItemMetadata(offer, metadataObj, index, bundleId); | ||
} | ||
|
||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { JSONValue, TypedMap } from "@graphprotocol/graph-ts"; | ||
import { getItemMetadataEntityId, saveMetadataAttributes } from "../utils"; | ||
import { | ||
convertToInt, | ||
convertToObjectArray, | ||
convertToString | ||
} from "../../../utils/json"; | ||
import { NftItemMetadataEntity, Offer } from "../../../../generated/schema"; | ||
|
||
export function saveNftItemMetadata( | ||
offer: Offer, | ||
metadataObj: TypedMap<string, JSONValue>, | ||
index: string, | ||
bundleId: string | ||
): string { | ||
const offerId = offer.id.toString(); | ||
const metadataId = getItemMetadataEntityId(offerId, index); | ||
|
||
const schemaUrl = convertToString(metadataObj.get("schemaUrl")); | ||
const name = convertToString(metadataObj.get("name")); | ||
const description = convertToString(metadataObj.get("description")); | ||
const image = convertToString(metadataObj.get("image")); | ||
const externalUrl = convertToString(metadataObj.get("externalUrl")); | ||
const animationUrl = convertToString(metadataObj.get("animationUrl")); | ||
const chainId = convertToInt(metadataObj.get("chainId")); | ||
const contract = convertToString(metadataObj.get("contract")); | ||
const tokenId = convertToString(metadataObj.get("tokenId")); | ||
const quantity = convertToInt(metadataObj.get("quantity")); | ||
|
||
const attributes = convertToObjectArray(metadataObj.get("attributes")); | ||
const savedMetadataAttributeIds = saveMetadataAttributes(attributes); | ||
|
||
let nftItemMetadataEntity = NftItemMetadataEntity.load(metadataId); | ||
if (!nftItemMetadataEntity) { | ||
nftItemMetadataEntity = new NftItemMetadataEntity(metadataId); | ||
} | ||
|
||
nftItemMetadataEntity.type = "ITEM_NFT"; | ||
nftItemMetadataEntity.schemaUrl = schemaUrl; | ||
nftItemMetadataEntity.name = name; | ||
nftItemMetadataEntity.description = description; | ||
nftItemMetadataEntity.image = image; | ||
nftItemMetadataEntity.externalUrl = externalUrl; | ||
nftItemMetadataEntity.animationUrl = animationUrl; | ||
nftItemMetadataEntity.chainId = chainId; | ||
nftItemMetadataEntity.contract = contract; | ||
nftItemMetadataEntity.tokenId = tokenId; | ||
nftItemMetadataEntity.quantity = quantity; | ||
nftItemMetadataEntity.attributes = savedMetadataAttributeIds; | ||
nftItemMetadataEntity.bundle = bundleId; | ||
|
||
nftItemMetadataEntity.save(); | ||
return metadataId; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.