-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from daiagi/TokenEntity
- Loading branch information
Showing
8 changed files
with
172 additions
and
21 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
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,44 @@ | ||
import { create, getOptional } from '@kodadot1/metasquid/entity' | ||
import md5 from 'md5' | ||
import { CollectionEntity as CE, NFTEntity as NE, TokenEntity as TE } from '../../model' | ||
import { warn } from '../utils/logger' | ||
import { Context } from '../utils/types' | ||
|
||
const OPERATION = 'TokenEntity' as any | ||
|
||
export async function handleTokenEntity(context: Context, collection: CE, nft: NE): Promise<TE | undefined> { | ||
const nftMedia = nft.image || nft.media | ||
if (!nftMedia || nftMedia === '') { | ||
warn(OPERATION, `MISSING NFT MEDIA ${nft.id}`) | ||
return | ||
} | ||
|
||
const tokenId = `${collection.id}-${md5(nftMedia)}` | ||
let token = await getOptional<TE>(context.store, TE, tokenId) | ||
|
||
if (!token) { | ||
const tokenName = (typeof nft.name === 'string' ? nft.name?.replace(/([#_]\d+$)/g, '').trim() : '') | ||
|
||
token = create(TE, tokenId, { | ||
createdAt: nft.createdAt, | ||
collection, | ||
name: tokenName, | ||
count: 1, | ||
hash: md5(tokenId), | ||
image: nft.image, | ||
media: nft.media, | ||
blockNumber: nft.blockNumber, | ||
updatedAt: nft.updatedAt, | ||
id: tokenId, | ||
}) | ||
} else { | ||
token.count += 1 | ||
} | ||
|
||
token.updatedAt = nft.updatedAt | ||
token.blockNumber = nft.blockNumber | ||
|
||
await context.store.save(token) | ||
|
||
return token | ||
} |
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
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
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,47 @@ | ||
import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_, ManyToOne as ManyToOne_, Index as Index_, OneToMany as OneToMany_} from "typeorm" | ||
import * as marshal from "./marshal" | ||
import {CollectionEntity} from "./collectionEntity.model" | ||
import {NFTEntity} from "./nftEntity.model" | ||
|
||
@Entity_() | ||
export class TokenEntity { | ||
constructor(props?: Partial<TokenEntity>) { | ||
Object.assign(this, props) | ||
} | ||
|
||
@PrimaryColumn_() | ||
id!: string | ||
|
||
@Column_("numeric", {transformer: marshal.bigintTransformer, nullable: true}) | ||
blockNumber!: bigint | undefined | null | ||
|
||
@Index_() | ||
@ManyToOne_(() => CollectionEntity, {nullable: true}) | ||
collection!: CollectionEntity | undefined | null | ||
|
||
@OneToMany_(() => NFTEntity, e => e.token) | ||
nfts!: NFTEntity[] | ||
|
||
@Index_() | ||
@Column_("text", {nullable: false}) | ||
hash!: string | ||
|
||
@Column_("text", {nullable: true}) | ||
image!: string | undefined | null | ||
|
||
@Column_("text", {nullable: true}) | ||
media!: string | undefined | null | ||
|
||
@Index_() | ||
@Column_("text", {nullable: true}) | ||
name!: string | undefined | null | ||
|
||
@Column_("timestamp with time zone", {nullable: false}) | ||
updatedAt!: Date | ||
|
||
@Column_("timestamp with time zone", {nullable: false}) | ||
createdAt!: Date | ||
|
||
@Column_("int4", {nullable: false}) | ||
count!: number | ||
} |