-
Notifications
You must be signed in to change notification settings - Fork 11
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 #113 from kodadot/main
🔖 Release Stick v5
- Loading branch information
Showing
28 changed files
with
619 additions
and
82 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module.exports = class Data1696751796584 { | ||
name = 'Data1696751796584' | ||
|
||
async up(db) { | ||
await db.query(`ALTER TABLE "token_entity" ADD "metadata" text`) | ||
await db.query(`ALTER TABLE "token_entity" ADD "supply" integer NOT NULL`) | ||
await db.query(`ALTER TABLE "token_entity" ADD "cheapest_id" character varying`) | ||
await db.query(`ALTER TABLE "token_entity" ADD "meta_id" character varying`) | ||
await db.query(`CREATE INDEX "IDX_637db5c040f1d9f935817ae1e8" ON "token_entity" ("cheapest_id") `) | ||
await db.query(`CREATE INDEX "IDX_ae4ff3b28e3fec72aa14124d1e" ON "token_entity" ("meta_id") `) | ||
await db.query(`ALTER TABLE "token_entity" ADD CONSTRAINT "FK_637db5c040f1d9f935817ae1e8a" FOREIGN KEY ("cheapest_id") REFERENCES "nft_entity"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`) | ||
await db.query(`ALTER TABLE "token_entity" ADD CONSTRAINT "FK_ae4ff3b28e3fec72aa14124d1e1" FOREIGN KEY ("meta_id") REFERENCES "metadata_entity"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`) | ||
} | ||
|
||
async down(db) { | ||
await db.query(`ALTER TABLE "token_entity" DROP COLUMN "metadata"`) | ||
await db.query(`ALTER TABLE "token_entity" DROP COLUMN "supply"`) | ||
await db.query(`ALTER TABLE "token_entity" DROP COLUMN "cheapest_id"`) | ||
await db.query(`ALTER TABLE "token_entity" DROP COLUMN "meta_id"`) | ||
await db.query(`DROP INDEX "public"."IDX_637db5c040f1d9f935817ae1e8"`) | ||
await db.query(`DROP INDEX "public"."IDX_ae4ff3b28e3fec72aa14124d1e"`) | ||
await db.query(`ALTER TABLE "token_entity" DROP CONSTRAINT "FK_637db5c040f1d9f935817ae1e8a"`) | ||
await db.query(`ALTER TABLE "token_entity" DROP CONSTRAINT "FK_ae4ff3b28e3fec72aa14124d1e1"`) | ||
} | ||
} |
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
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 was deleted.
Oops, something went wrong.
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,24 @@ | ||
import { getOptional } from '@kodadot1/metasquid/entity' | ||
import { Context } from '../../utils/types' | ||
import { NFTEntity as NE, TokenEntity as TE } from '../../../model' | ||
import { debug } from '../../utils/logger' | ||
import { OPERATION, generateTokenId, mediaOf } from './utils' | ||
|
||
export async function burnHandler(context: Context, nft: NE): Promise<void> { | ||
debug(OPERATION, { handleBurn: `Handle Burn for NFT ${nft.id}` }) | ||
|
||
const nftMedia = mediaOf(nft) | ||
if (!nftMedia) { | ||
return | ||
} | ||
|
||
const token = await getOptional<TE>(context.store, TE, generateTokenId(nft.collection.id, nftMedia)) | ||
|
||
if (!token) { | ||
return | ||
} | ||
|
||
debug(OPERATION, { BURN: `decrement Token's ${token.id} supply` }) | ||
|
||
await context.store.update(TE, token.id, { supply: token.supply - 1, updatedAt: nft.updatedAt }) | ||
} |
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,3 @@ | ||
export * from './mint' | ||
export * from './setMetadata' | ||
export * from './burn' |
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,20 @@ | ||
import { getOptional } from '@kodadot1/metasquid/entity' | ||
import { Context } from '../../utils/types' | ||
import { CollectionEntity as CE, NFTEntity as NE, TokenEntity as TE } from '../../../model' | ||
import { debug } from '../../utils/logger' | ||
import { OPERATION, generateTokenId, mediaOf } from './utils' | ||
import { TokenAPI } from './tokenAPI' | ||
|
||
export async function mintHandler(context: Context, collection: CE, nft: NE): Promise<TE | undefined> { | ||
debug(OPERATION, { mintHandler: `Handle mint for NFT ${nft.id}` }) | ||
|
||
const nftMedia = mediaOf(nft) | ||
if (!nftMedia) { | ||
return | ||
} | ||
|
||
const tokenApi = new TokenAPI(context.store) | ||
|
||
const existingToken = await getOptional<TE>(context.store, TE, generateTokenId(collection.id, nftMedia)) | ||
return await (existingToken ? tokenApi.addNftToToken(nft, existingToken) : tokenApi.create(collection, nft)) | ||
} |
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,32 @@ | ||
import { getOptional, getWith } from '@kodadot1/metasquid/entity' | ||
import { Context } from '../../utils/types' | ||
import { CollectionEntity as CE, NFTEntity as NE, TokenEntity as TE } from '../../../model' | ||
import { debug, warn } from '../../utils/logger' | ||
import { OPERATION, generateTokenId, mediaOf } from './utils' | ||
import { TokenAPI } from './tokenAPI' | ||
|
||
export async function setMetadataHandler(context: Context, collection: CE, nft: NE): Promise<TE | undefined> { | ||
debug(OPERATION, { handleMetadataSet: `Handle set metadata for NFT ${nft.id}` }) | ||
const nftMedia = mediaOf(nft) | ||
if (!nftMedia) { | ||
return | ||
} | ||
|
||
let nftWithToken, existingToken | ||
try { | ||
[nftWithToken, existingToken] = await Promise.all([ | ||
getWith(context.store, NE, nft.id, { token: true }), | ||
getOptional<TE>(context.store, TE, generateTokenId(collection.id, nftMedia)), | ||
]) | ||
} catch (error) { | ||
warn(OPERATION, `ERROR ${error}`) | ||
return | ||
} | ||
|
||
const tokenAPI = new TokenAPI(context.store) | ||
|
||
if (nftWithToken.token) { | ||
await tokenAPI.removeNftFromToken(nft, nftWithToken.token) | ||
} | ||
return await (existingToken ? tokenAPI.addNftToToken(nft, existingToken) : tokenAPI.create(collection, nft)) | ||
} |
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,73 @@ | ||
import { create as createEntity } from '@kodadot1/metasquid/entity' | ||
import md5 from 'md5' | ||
import { Store } from '../../utils/types' | ||
import { CollectionEntity as CE, NFTEntity as NE, TokenEntity as TE } from '../../../model' | ||
import { debug } from '../../utils/logger' | ||
import { OPERATION, generateTokenId, mediaOf, tokenName } from './utils' | ||
|
||
export class TokenAPI { | ||
constructor(private store: Store) {} | ||
|
||
async create(collection: CE, nft: NE): Promise<TE | undefined> { | ||
const nftMedia = mediaOf(nft) | ||
if (!nftMedia) { | ||
return | ||
} | ||
const tokenId = generateTokenId(collection.id, nftMedia) | ||
debug(OPERATION, { createToken: `Create TOKEN ${tokenId} for NFT ${nft.id}` }) | ||
|
||
const token = createEntity(TE, tokenId, { | ||
createdAt: nft.createdAt, | ||
collection, | ||
name: tokenName(nft.name), | ||
count: 1, | ||
supply: 1, | ||
hash: md5(tokenId), | ||
image: nft.image, | ||
media: nft.media, | ||
metadata: nft.metadata, | ||
meta: nft.meta, | ||
blockNumber: nft.blockNumber, | ||
updatedAt: nft.updatedAt, | ||
id: tokenId, | ||
}) | ||
|
||
await this.store.save(token) | ||
await this.store.update(NE, nft.id, { token }) | ||
|
||
return token | ||
} | ||
|
||
async removeNftFromToken(nft: NE, token: TE): Promise<void> { | ||
if (!token) { | ||
return | ||
} | ||
debug(OPERATION, { removeNftFromToken: `Unlink NFT ${nft.id} from TOKEN ${token.id}` }) | ||
|
||
await this.store.update(NE, nft.id, { token: null }) | ||
const updatedCount = token.count - 1 | ||
await this.store.update(TE, token.id, { | ||
supply: token.supply - 1, | ||
count: updatedCount, | ||
updatedAt: nft.updatedAt, | ||
}) | ||
|
||
if (updatedCount === 0) { | ||
debug(OPERATION, { deleteEmptyToken: `delete empty token ${token.id}` }) | ||
|
||
await this.store.delete(TE, token.id) | ||
} | ||
} | ||
|
||
async addNftToToken(nft: NE, token: TE): Promise<TE> { | ||
debug(OPERATION, { updateToken: `Add NFT ${nft.id} to TOKEN ${token.id} for ` }) | ||
token.count += 1 | ||
token.supply += 1 | ||
token.updatedAt = nft.updatedAt | ||
nft.token = token | ||
await this.store.save(token) | ||
await this.store.save(nft) | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import md5 from 'md5' | ||
import { NFTEntity as NE } from '../../../model' | ||
import { warn } from '../../utils/logger' | ||
|
||
export const OPERATION = 'TokenEntity' as any | ||
|
||
export function generateTokenId(collectionId: string, nftMedia: string): string { | ||
return `${collectionId}-${md5(nftMedia)}` | ||
} | ||
|
||
export const mediaOf = (nft: NE): string | undefined => { | ||
const nftMedia = nft.image ?? nft.media | ||
|
||
if (!nftMedia || nftMedia === '') { | ||
warn(OPERATION, `MISSING NFT MEDIA ${nft.id}`) | ||
return undefined | ||
} | ||
|
||
return nftMedia | ||
} | ||
|
||
export const tokenName = (nftName: string | undefined | null): string => | ||
typeof nftName === 'string' ? nftName?.replace(/([#_]\d+$)/g, '').trim() : '' |
Oops, something went wrong.