From 46b5eb31eae47539b870de01b1322356385f8d87 Mon Sep 17 00:00:00 2001 From: Viki Val Date: Tue, 11 Apr 2023 22:54:03 +0200 Subject: [PATCH] :zap: buy handles recipient --- src/mappings/v2/buy.ts | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/src/mappings/v2/buy.ts b/src/mappings/v2/buy.ts index 96e23ea2..3f032f87 100644 --- a/src/mappings/v2/buy.ts +++ b/src/mappings/v2/buy.ts @@ -1,19 +1,21 @@ -import { getWith } from '@kodadot1/metasquid/entity' +import { getOrFail as get, getWith } from '@kodadot1/metasquid/entity' import { Optional } from '@kodadot1/metasquid/types' +import { Buy } from '@kodadot1/minimark/v2' import { NFTEntity } from '../../model' +import { createEvent } from '../shared/event' import { unwrap } from '../utils' import { isBuyLegalOrElseError, isInteractive, isMoreTransferable, isPositiveOrElseError } from '../utils/consolidator' -import { getInteractionWithExtra } from '../utils/getters' +import { findRootItemById } from '../utils/entity' +import { isDummyAddress } from '../utils/helper' import { error, success } from '../utils/logger' -import { Action, Context, RmrkInteraction } from '../utils/types' -import { createEvent } from '../shared/event' +import { Action, Context } from '../utils/types' import { getBuy } from './getters' const OPERATION = Action.BUY export async function buy(context: Context) { - let interaction: Optional = null + let interaction: Optional = null try { const { value, caller, timestamp, blockNumber, extra } = unwrap(context, getBuy) @@ -25,12 +27,29 @@ export async function buy(context: Context) { isBuyLegalOrElseError(nft, extra || []) const originalPrice = nft.price const originalOwner = nft.currentOwner ?? undefined - nft.currentOwner = value.recipient || caller + const recipient = interaction.recipient || caller + + const isRecipientNFT = !isDummyAddress(recipient) + nft.price = BigInt(0) nft.updatedAt = timestamp nft.collection.updatedAt = timestamp + if (isRecipientNFT) { + const parent = await get(context.store, NFTEntity, recipient) + const isCallerTheOwner = parent.currentOwner === caller + const rootRecipientNFT = await findRootItemById(context.store, recipient) + + nft.currentOwner = rootRecipientNFT.currentOwner + nft.pending = !isCallerTheOwner + nft.parent = parent + } else { + nft.currentOwner = recipient + nft.parent = null + nft.pending = false + } + success(OPERATION, `${nft.id} from ${caller}`) await context.store.save(nft) await createEvent(