Skip to content

Commit

Permalink
Merge pull request #245 from kodadot/final-touches
Browse files Browse the repository at this point in the history
Final touches
  • Loading branch information
vikiival authored Apr 15, 2023
2 parents 89f9558 + 87f0960 commit 41d1fb4
Show file tree
Hide file tree
Showing 18 changed files with 191 additions and 77 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"dependencies": {
"@kodadot1/metasquid": "^0.1.5-rc.0",
"@kodadot1/minimark": "0.1.7-rc.2",
"@kodadot1/minimark": "0.1.7-rc.3",
"@kodadot1/minipfs": "^0.3.0-rc.0",
"@subsquid/archive-registry": "2.1.10",
"@subsquid/big-decimal": "^0.0.0",
Expand Down
15 changes: 10 additions & 5 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type NFTEntity @entity {
parent: NFTEntity # = nft_id
pending: Boolean!
price: BigInt!
properties: [Property!] @derivedFrom (field: "nft")
resources: [Resource!] @derivedFrom (field: "nft")
royalty: Float
recipient: String
Expand Down Expand Up @@ -175,11 +176,6 @@ type Part @entity {
z: Int!
}

# type ResourcePart @entity {
# resource: Resource!
# part: Part!
# }


type Theme @entity {
id: ID!
Expand Down Expand Up @@ -220,6 +216,15 @@ enum PartType {
slot
}

type Property @entity {
id: ID!
key: String!
value: String!
type: String!
mutable: Boolean!
nft: NFTEntity!
}

# Cachce and tables
type Series @entity {
id: ID!
Expand Down
2 changes: 1 addition & 1 deletion src/mappings/utils/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@ export async function findParentBaseResouce(store: Store, id: string, baseId: st
}

return result
}
}
7 changes: 0 additions & 7 deletions src/mappings/v1/change.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { isOwnerOrElseError, withMeta } from '../utils/consolidator'
import { getInteraction } from '../utils/getters'
import { error, success } from '../utils/logger'
import { Action, Context, RmrkInteraction } from '../utils/types'
import { calculateCollectionOwnerCountAndDistribution } from '../utils/helper'

const OPERATION = Action.CHANGEISSUER

Expand All @@ -24,12 +23,6 @@ export async function changeIssuer(context: Context) {
plsBe<CollectionEntity>(real, collection)
isOwnerOrElseError(collection, caller)
collection.currentOwner = interaction.value
const { ownerCount, distribution } = await calculateCollectionOwnerCountAndDistribution(
context.store,
collection.id,
)
collection.ownerCount = ownerCount
collection.distribution = distribution

await context.store.save(collection)
success(OPERATION, `${collection.id} from ${caller}`)
Expand Down
7 changes: 3 additions & 4 deletions src/mappings/v2/addTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { getThemeAdd } from './getters'

const OPERATION = Action.THEMEADD

export async function addTheme(context: Context) {
export async function addTheme(context: Context) {
let interaction: Optional<ThemeAdd> = null

try {
Expand All @@ -26,19 +26,18 @@ export async function addTheme(context: Context) {
const id = toThemeId(interaction.base_id, interaction.name)
const final = await createUnlessNotExist(id, Theme, context)


final.name = interaction.name
final.base = base

const keys = ['theme_color_1', 'theme_color_2', 'theme_color_3', 'theme_color_4']

for (const key of keys) {
const value = interaction.value[key]
if (value && key !== 'base') {
const camelKey = camelCase(key) as keyof Omit<Theme, 'base'>
final[camelKey] = value
}
}
}
// logger.success(`[${OPERATION}] NEW PRIORITY ${interaction.value} for ${nft.id} from ${caller}`)

success(OPERATION, `< ${interaction.name}, ${interaction.value} > for ${interaction.base_id} from ${caller}`)
Expand Down
18 changes: 15 additions & 3 deletions src/mappings/v2/base.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Optional } from '@kodadot1/metasquid/types'
import { CreatedBase, toPartId } from '@kodadot1/minimark/v2'
import { CreatedBase, toPartId, toThemeId } from '@kodadot1/minimark/v2'

import { Base, BaseType, Part, PartType } from '../../model'
import { handleMetadata } from '../shared'
Expand Down Expand Up @@ -52,9 +52,21 @@ export async function base(context: Context) {
await context.store.save(part)
}
}

// TODO: themes
// if (base.themes) {}
// if (base.themes) {
// const keys = ['theme_color_1', 'theme_color_2', 'theme_color_3', 'theme_color_4']
// const themes = Object.entries(base.themes)
// for (const [name, theme] of themes) {
// const themeId = toThemeId(id, name)
// for (const key of keys) {
// const value = theme[key]
// if (value && key !== 'base') {
// const camelKey = camelCase(key) as keyof Omit<Theme, 'base'>
// final[camelKey] = value
// }
// }
// }
// }
} catch (e) {
error(e, OPERATION, JSON.stringify(base))
}
Expand Down
12 changes: 11 additions & 1 deletion src/mappings/v2/buy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ export async function buy(context: Context) {
originalOwner
)


if (nft.currentOwner !== caller) {
await createEvent(
nft,
Expand All @@ -86,6 +85,17 @@ export async function buy(context: Context) {
caller
)
}

if (nft.parent !== null && nft.pending === false) {
await createEvent(
nft.parent,
Action.ACCEPT,
{ blockNumber, caller, timestamp, version },
`NFT::${interaction.id}`,
context.store,
originalOwner
)
}
} catch (e) {
error(e, OPERATION, JSON.stringify(interaction))
}
Expand Down
10 changes: 4 additions & 6 deletions src/mappings/v2/change.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { ChangeIssuer } from '@kodadot1/minimark/v2'
import { Base, CollectionEntity } from '../../model'
import { unwrap } from '../utils'
import { isOwnerOrElseError, withMeta } from '../utils/consolidator'
import { isDummyAddress } from '../utils/helper'
import { error, success } from '../utils/logger'
import { Action, Context } from '../utils/types'
import { calculateCollectionOwnerCountAndDistribution } from '../utils/helper'
import { getChangeIssuer } from './getters'

const OPERATION = Action.CHANGEISSUER
Expand All @@ -26,12 +26,10 @@ export async function changeIssuer(context: Context) {
? await get<Base>(context.store, Base, interaction.id)
: await get<CollectionEntity>(context.store, CollectionEntity, interaction.id)
isOwnerOrElseError(entity, caller)
entity.currentOwner = interaction.newissuer
entity.currentOwner = interaction.value

if (entity instanceof CollectionEntity) {
const { ownerCount, distribution } = await calculateCollectionOwnerCountAndDistribution(context.store, entity.id)
entity.ownerCount = ownerCount
entity.distribution = distribution
if (!isDummyAddress(interaction.value)) {
throw new Error(`Cannot change issuer to value that is not address address ${interaction.value}`)
}

await context.store.save(entity)
Expand Down
6 changes: 5 additions & 1 deletion src/mappings/v2/equip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ export async function equip(context: Context) {
try {
const { value: equip, caller, timestamp, blockNumber, version } = unwrap(context, getEquip)
interaction = equip
const nft = await getWith<NFTEntity>(context.store, NFTEntity, interaction.id, { parent: true, equipped: true, collection: true })
const nft = await getWith<NFTEntity>(context.store, NFTEntity, interaction.id, {
parent: true,
equipped: true,
collection: true,
})
plsNotBe(burned, nft)
isOwnerOrElseError(nft, caller)
plsBe(real, nft.parent)
Expand Down
13 changes: 11 additions & 2 deletions src/mappings/v2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import { mintItem } from './mint'
import { send } from './send'
import { setPriority } from './setpriority'
import { addTheme } from './addTheme'
import { equip } from './equip'
import { equippable } from './equippable'
import { setProperty } from './setProperty'

export async function mainFrame(remark: string, context: Context): Promise<void> {
const base = unwrap(context, (_: Context) => ({ value: remark }))
Expand Down Expand Up @@ -67,10 +70,16 @@ export async function mainFrame(remark: string, context: Context): Promise<void>
case Interaction.THEMEADD:
await addTheme(context)
break
case Interaction.DESTROY:
case Interaction.EQUIP:
await equip(context)
break
case Interaction.EQUIPPABLE:
await equippable(context)
break
case Interaction.SETPROPERTY:
case Interaction.EQUIP:
await setProperty(context)
break
case Interaction.DESTROY:
pending(event, `::${base.blockNumber}::${base.value}`)
break
default:
Expand Down
44 changes: 33 additions & 11 deletions src/mappings/v2/mint.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { plsBe, real } from '@kodadot1/metasquid/consolidator'
import { create, getOrFail as get } from '@kodadot1/metasquid/entity'
import { Mint, resolveRoyalty } from '@kodadot1/minimark/v2'
import { Mint, resolveRoyalty, toPropertyId } from '@kodadot1/minimark/v2'
import md5 from 'md5'
import { unwrap } from '../utils'
import { isOwnerOrElseError } from '../utils/consolidator'

import { CollectionEntity, NFTEntity } from '../../model/generated'
import { CollectionEntity, NFTEntity, Property } from '../../model/generated'
import { createEvent } from '../shared/event'
import { handleMetadata } from '../shared/metadata'
import { findRootItemById } from '../utils/entity'
import { isDummyAddress } from '../utils/helper'
import { calculateCollectionOwnerCountAndDistribution, isDummyAddress } from '../utils/helper'
import logger, { error, success } from '../utils/logger'
import { Action, Context, Optional, getNftId } from '../utils/types'
import { calculateCollectionOwnerCountAndDistribution } from '../utils/helper'
import { getCreateToken } from './getters'

const OPERATION = Action.MINT
Expand Down Expand Up @@ -50,13 +49,6 @@ export async function mintItem(context: Context): Promise<void> {
collection.updatedAt = timestamp
collection.nftCount += 1
collection.supply += 1
const { ownerCount, distribution } = await calculateCollectionOwnerCountAndDistribution(
context.store,
collection.id,
final.currentOwner
)
collection.ownerCount = ownerCount
collection.distribution = distribution

if (final.metadata) {
const metadata = await handleMetadata(final.metadata, '', context.store)
Expand Down Expand Up @@ -116,6 +108,36 @@ export async function mintItem(context: Context): Promise<void> {
caller
)
}

if (final.parent !== null && final.pending === false) {
await createEvent(
final.parent,
Action.ACCEPT,
{ blockNumber, caller, timestamp, version },
`NFT::${final.id}`,
context.store
)
}

if (nft.properties) {
const properties = Object.entries(nft.properties)
const saveList: Property[] = []
for (const [key, value] of properties) {
if (!value.value) {
continue
}
const propertyId = toPropertyId(final.id, key, value.value)
const property = create<Property>(Property, propertyId, {})
property.id = propertyId
property.key = key
property.value = value.value
property.nft = final
property.mutable = value._mutation?.allowed || false
saveList.push(property)
}

await context.store.save(saveList)
}
} catch (e) {
if (e instanceof Error) {
logger.trace(e)
Expand Down
11 changes: 11 additions & 0 deletions src/mappings/v2/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ export async function send(context: Context) {
context.store,
originalOwner
)

if (nft.parent !== null && nft.pending === false) {
await createEvent(
nft.parent,
Action.ACCEPT,
{ blockNumber, caller, timestamp, version },
`NFT::${interaction.id}`,
context.store,
originalOwner
)
}
} catch (e) {
error(e, OPERATION, JSON.stringify(interaction))
}
Expand Down
Loading

0 comments on commit 41d1fb4

Please sign in to comment.