diff --git a/src/mappings/shared/index.ts b/src/mappings/shared/index.ts index 0f742804..bd9d9124 100644 --- a/src/mappings/shared/index.ts +++ b/src/mappings/shared/index.ts @@ -1,8 +1,4 @@ export * from './burn' -export * from './change' -export * from './create' -export * from './emote' export * from './event' export * from './list' export * from './metadata' -export * from './send' diff --git a/src/mappings/shared/list.ts b/src/mappings/shared/list.ts index 051737cc..04a80c42 100644 --- a/src/mappings/shared/list.ts +++ b/src/mappings/shared/list.ts @@ -14,7 +14,7 @@ import { error, success } from '../utils/logger' import { Action, Context, RmrkInteraction } from '../utils/types' import { createEvent } from './event' -const OPERATION = Action.BUY +const OPERATION = Action.LIST export async function list(context: Context) { let interaction: Optional = null diff --git a/src/mappings/shared/change.ts b/src/mappings/v1/change.ts similarity index 100% rename from src/mappings/shared/change.ts rename to src/mappings/v1/change.ts diff --git a/src/mappings/shared/create.ts b/src/mappings/v1/create.ts similarity index 97% rename from src/mappings/shared/create.ts rename to src/mappings/v1/create.ts index 491dcaee..0603d256 100644 --- a/src/mappings/shared/create.ts +++ b/src/mappings/v1/create.ts @@ -8,7 +8,7 @@ import { unwrap } from '../utils/extract' import { getCreateCollection } from '../utils/getters' import { error, success } from '../utils/logger' import { Action, Collection, Context } from '../utils/types' -import { handleMetadata } from './metadata' +import { handleMetadata } from '../shared/metadata' const OPERATION = Action.CREATE diff --git a/src/mappings/shared/emote.ts b/src/mappings/v1/emote.ts similarity index 100% rename from src/mappings/shared/emote.ts rename to src/mappings/v1/emote.ts diff --git a/src/mappings/v1/index.ts b/src/mappings/v1/index.ts index 3c605bab..f7feb5f4 100644 --- a/src/mappings/v1/index.ts +++ b/src/mappings/v1/index.ts @@ -1,11 +1,15 @@ -import { unwrapRemark, Interaction } from '@kodadot1/minimark/v1' +import { Interaction, unwrapRemark } from '@kodadot1/minimark/v1' import { unwrap } from '../utils' -import { changeIssuer, burn as consume, createCollection, emote, list, send } from '../shared' -import { mintItem } from '../shared/mint' +import { burn as consume, list } from '../shared' import logger, { pending } from '../utils/logger' import { Context, RmrkInteraction } from '../utils/types' import { buy } from './buy' +import { changeIssuer } from './change' +import { createCollection } from './create' +import { emote } from './emote' +import { mintItem } from './mint' +import { send } from './send' export async function mainFrame(remark: string, context: Context): Promise { const base = unwrap(context, (_: Context) => ({ value: remark })) diff --git a/src/mappings/shared/mint.ts b/src/mappings/v1/mint.ts similarity index 96% rename from src/mappings/shared/mint.ts rename to src/mappings/v1/mint.ts index 70861e2a..833acbda 100644 --- a/src/mappings/shared/mint.ts +++ b/src/mappings/v1/mint.ts @@ -8,8 +8,8 @@ import { create } from '../utils/entity' import { getCreateToken } from '../utils/getters' import { error, success } from '../utils/logger' import { Action, Context, getNftId, NFT, Optional } from '../utils/types' -import { createEvent } from './event' -import { handleMetadata } from './metadata' +import { createEvent } from '../shared/event' +import { handleMetadata } from '../shared/metadata' const OPERATION = Action.MINT diff --git a/src/mappings/shared/send.ts b/src/mappings/v1/send.ts similarity index 96% rename from src/mappings/shared/send.ts rename to src/mappings/v1/send.ts index a18d9803..6b81713b 100644 --- a/src/mappings/shared/send.ts +++ b/src/mappings/v1/send.ts @@ -7,7 +7,7 @@ import { isOwnerOrElseError, validateInteraction } from '../utils/consolidator' import { getInteraction } from '../utils/getters' import { error, success } from '../utils/logger' import { Action, Context, RmrkInteraction } from '../utils/types' -import { createEvent } from './event' +import { createEvent } from '../shared/event' const OPERATION = Action.SEND diff --git a/src/mappings/v2/change.ts b/src/mappings/v2/change.ts new file mode 100644 index 00000000..f627a73a --- /dev/null +++ b/src/mappings/v2/change.ts @@ -0,0 +1,32 @@ +import { plsBe, real } from '@kodadot1/metasquid/consolidator' +import { getOrFail as get } from '@kodadot1/metasquid/entity' +import { Optional } from '@kodadot1/metasquid/types' + +import { CollectionEntity } from '../../model' +import { unwrap } from '../utils' +import { isOwnerOrElseError, withMeta } from '../utils/consolidator' +import { getInteraction } from '../utils/getters' +import { error, success } from '../utils/logger' +import { Action, Context, RmrkInteraction } from '../utils/types' + +const OPERATION = Action.CHANGEISSUER + +// TODO: can also change BASE in V2 (not implemented yet) +export async function changeIssuer(context: Context) { + let interaction: Optional = null + + try { + const { value, caller } = unwrap(context, getInteraction) + interaction = value + plsBe(withMeta, interaction) + const collection = await get(context.store, CollectionEntity, interaction.id) + plsBe(real, collection) + isOwnerOrElseError(collection, caller) + collection.currentOwner = interaction.value + + success(OPERATION, `${collection.id} from ${caller}`) + await context.store.save(collection) + } catch (e) { + error(e, OPERATION, JSON.stringify(interaction)) + } +} diff --git a/src/mappings/v2/emote.ts b/src/mappings/v2/emote.ts new file mode 100644 index 00000000..4d7a20ec --- /dev/null +++ b/src/mappings/v2/emote.ts @@ -0,0 +1,53 @@ +import { burned, plsBe, plsNotBe, real } from '@kodadot1/metasquid/consolidator' +import { create, getOrFail as get } from '@kodadot1/metasquid/entity' +import { Optional } from '@kodadot1/metasquid/types' +import { toVersion } from '@kodadot1/minimark/shared' + +import { Emote, NFTEntity } from '../../model' +import { unwrap } from '../utils' +import { withMeta } from '../utils/consolidator' +import { getInteraction } from '../utils/getters' +import { emoteId } from '../utils/helper' +import { error, success } from '../utils/logger' +import { Action, Context, RmrkInteraction } from '../utils/types' + +const OPERATION = Action.EMOTE + +export async function emote(context: Context) { + let interaction: Optional = null + + try { + const { value, caller, timestamp, version } = unwrap(context, getInteraction) + interaction = value + plsBe(withMeta, interaction) + const nft = await get(context.store, NFTEntity, interaction.id) + plsBe(real, nft) + plsNotBe(burned, nft) + const id = emoteId(interaction, caller) + let emote = await get(context.store, Emote, interaction.id) + nft.updatedAt = timestamp + + if (emote) { + nft.emoteCount -= 1 + await context.store.remove(emote) + await context.store.save(nft) + return + } + + emote = create(Emote, id, { + id, + caller, + value: interaction.value, + version: toVersion(version), + }) + + emote.nft = nft + nft.emoteCount += 1 + + success(OPERATION, `${nft.id} from ${caller}`) + await context.store.save(emote) + await context.store.save(nft) + } catch (e) { + error(e, OPERATION, '') + } +} diff --git a/src/mappings/v2/index.ts b/src/mappings/v2/index.ts index a7288e09..82e73d46 100644 --- a/src/mappings/v2/index.ts +++ b/src/mappings/v2/index.ts @@ -1,6 +1,6 @@ import { Interaction, unwrapRemarkV2 as unwrapRemark } from '@kodadot1/minimark/v2' -import { burn, changeIssuer, emote, list } from '../shared' +import { burn, list } from '../shared' import { unwrap } from '../utils/extract' import logger, { pending } from '../utils/logger' import { Context } from '../utils/types' @@ -8,7 +8,9 @@ import { acceptResource } from './accept' import { addResource } from './addResource' import { base as createBase } from './base' import { buy } from './buy' +import { changeIssuer } from './change' import { createCollection } from './create' +import { emote } from './emote' import { lockCollection } from './lock' import { mintItem } from './mint' import { send } from './send'