Skip to content

Commit

Permalink
⚡ buy handles recipient
Browse files Browse the repository at this point in the history
  • Loading branch information
vikiival committed Apr 11, 2023
1 parent a616ee4 commit 46b5eb3
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions src/mappings/v2/buy.ts
Original file line number Diff line number Diff line change
@@ -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<RmrkInteraction> = null
let interaction: Optional<Buy> = null

try {
const { value, caller, timestamp, blockNumber, extra } = unwrap(context, getBuy)
Expand All @@ -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<NFTEntity>(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(
Expand Down

0 comments on commit 46b5eb3

Please sign in to comment.