Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚧 wip: swap #170

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/mappings/nfts/cancelSwap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { getWith } from '@kodadot1/metasquid/entity'
import { NFTEntity as NE } from '../../model'
import { createEvent } from '../shared/event'
import { unwrap } from '../utils/extract'
import { debug, pending, success } from '../utils/logger'
import { Action, Context } from '../utils/types'
import { getSwapCancelledEvent } from './getters'

const OPERATION = Action.SEND

export async function handleCancelSwap(context: Context): Promise<void> {
pending(OPERATION, `${context.block.height}`)
const event = unwrap(context, getSwapCancelledEvent)
debug(OPERATION, event)

// SwapCancelled {
// offered_collection: T::CollectionId,
// offered_item: T::ItemId,
// desired_collection: T::CollectionId,
// desired_item: Option<T::ItemId>,
// price: Option<PriceWithDirection<ItemPrice<T, I>>>,
// deadline: BlockNumberFor<T>,
// },
}
26 changes: 26 additions & 0 deletions src/mappings/nfts/claimSwap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { getWith } from '@kodadot1/metasquid/entity'
import { NFTEntity as NE } from '../../model'
import { createEvent } from '../shared/event'
import { unwrap } from '../utils/extract'
import { debug, pending, success } from '../utils/logger'
import { Action, Context } from '../utils/types'
import { getSwapClaimedEvent } from './getters'

const OPERATION = Action.SEND

export async function handleClaimSwap(context: Context): Promise<void> {
pending(OPERATION, `${context.block.height}`)
const event = unwrap(context, getSwapClaimedEvent)
debug(OPERATION, event)

// SwapClaimed {
// sent_collection: T::CollectionId,
// sent_item: T::ItemId,
// sent_item_owner: T::AccountId,
// received_collection: T::CollectionId,
// received_item: T::ItemId,
// received_item_owner: T::AccountId,
// price: Option<PriceWithDirection<ItemPrice<T, I>>>,
// deadline: BlockNumberFor<T>,
// },
}
24 changes: 24 additions & 0 deletions src/mappings/nfts/createSwap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { getWith } from '@kodadot1/metasquid/entity'
import { NFTEntity as NE } from '../../model'
import { createEvent } from '../shared/event'
import { unwrap } from '../utils/extract'
import { debug, pending, success } from '../utils/logger'
import { Action, Context } from '../utils/types'
import { getSwapCreatedEvent } from './getters'

const OPERATION = Action.SEND

export async function handleCreateSwap(context: Context): Promise<void> {
pending(OPERATION, `${context.block.height}`)
const event = unwrap(context, getSwapCreatedEvent)
debug(OPERATION, event)

// SwapCreated {
// offered_collection: T::CollectionId,
// offered_item: T::ItemId,
// desired_collection: T::CollectionId,
// desired_item: Option<T::ItemId>,
// price: Option<PriceWithDirection<ItemPrice<T, I>>>,
// deadline: BlockNumberFor<T>,
// },
}
12 changes: 12 additions & 0 deletions src/mappings/nfts/getters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,15 @@ export function getAttributeEvent(ctx: Context): SetAttribute {
export function getChangeTeamEvent(ctx: Context): ChangeCollectionTeam {
return proc.getChangeTeamEvent(ctx);
}

export function getSwapCreatedEvent(ctx: Context) {
return proc.getSwapCreatedEvent(ctx);
}

export function getSwapCancelledEvent(ctx: Context) {
return proc.getSwapCancelledEvent(ctx);
}

export function getSwapClaimedEvent(ctx: Context) {
return proc.getSwapClaimedEvent(ctx);
}
100 changes: 100 additions & 0 deletions src/mappings/nfts/getters/statemine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,3 +336,103 @@ export function getChangeTeamEvent(ctx: Context): ChangeCollectionTeam {
freezer: freezer ? addressOf(freezer) : '',
}
}

export function getSwapCreatedEvent(ctx: Context) {
const event = new events.NftsSwapCreatedEvent(ctx)

if (event.isV9420) {
const { offeredCollection, offeredItem, desiredCollection, desiredItem, price, deadline } = event.asV9420

return {
offeredCollection: offeredCollection.toString(),
offeredItem: offeredItem.toString(),
desiredCollection: desiredCollection.toString(),
desiredItem: desiredItem?.toString(),
price,
deadline
}
}

ctx.log.warn('USING UNSAFE GETTER! PLS UPDATE TYPES!')
const { offeredCollection, offeredItem, desiredCollection, desiredItem, price, deadline } = ctx._chain.decodeEvent(ctx.event)
return {
offeredCollection: offeredCollection.toString(),
offeredItem: offeredItem.toString(),
desiredCollection: desiredCollection.toString(),
desiredItem: desiredItem?.toString(),
price,
deadline,
}
}

export function getSwapCancelledEvent(ctx: Context) {
const event = new events.NftsSwapCancelledEvent(ctx)

if (event.isV9420) {
const { offeredCollection, offeredItem, desiredCollection, desiredItem, price, deadline } = event.asV9420

return {
offeredCollection: offeredCollection.toString(),
offeredItem: offeredItem.toString(),
desiredCollection: desiredCollection.toString(),
desiredItem: desiredItem?.toString(),
price,
deadline,
}
}

ctx.log.warn('USING UNSAFE GETTER! PLS UPDATE TYPES!')
const { offeredCollection, offeredItem, desiredCollection, desiredItem, price, deadline } = ctx._chain.decodeEvent(
ctx.event
)
return {
offeredCollection: offeredCollection.toString(),
offeredItem: offeredItem.toString(),
desiredCollection: desiredCollection.toString(),
desiredItem: desiredItem?.toString(),
price,
deadline,
}
}

export function getSwapClaimedEvent(ctx: Context) {
const event = new events.NftsSwapClaimedEvent(ctx)

if (event.isV9420) {
const { sentCollection, sentItem, sentItemOwner, receivedCollection, receivedItem, receivedItemOwner, price, deadline } = event.asV9420

return {
sentCollection: sentCollection.toString(),
sentItem: sentItem.toString(),
sentItemOwner: sentItemOwner ? addressOf(sentItemOwner) : '',
receivedCollection: receivedCollection.toString(),
receivedItem: receivedItem.toString(),
receivedItemOwner: receivedItemOwner ? addressOf(receivedItemOwner) : '',
price,
deadline,
}
}

ctx.log.warn('USING UNSAFE GETTER! PLS UPDATE TYPES!')
const {
sentCollection,
sentItem,
sentItemOwner,
receivedCollection,
receivedItem,
receivedItemOwner,
price,
deadline,
} = ctx._chain.decodeEvent(ctx.event)

return {
sentCollection: sentCollection.toString(),
sentItem: sentItem.toString(),
sentItemOwner: sentItemOwner ? addressOf(sentItemOwner) : '',
receivedCollection: receivedCollection.toString(),
receivedItem: receivedItem.toString(),
receivedItemOwner: receivedItemOwner ? addressOf(receivedItemOwner) : '',
price,
deadline,
}
}
111 changes: 111 additions & 0 deletions src/mappings/nfts/getters/statemint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,3 +336,114 @@ export function getChangeTeamEvent(ctx: Context): ChangeCollectionTeam {
freezer: freezer ? addressOf(freezer) : '',
}
}

export function getSwapCreatedEvent(ctx: Context) {
const event = new events.NftsSwapCreatedEvent(ctx)

if (event.isV9430) {
const { offeredCollection, offeredItem, desiredCollection, desiredItem, price, deadline } = event.asV9430

return {
offeredCollection: offeredCollection.toString(),
offeredItem: offeredItem.toString(),
desiredCollection: desiredCollection.toString(),
desiredItem: desiredItem?.toString(),
price,
deadline,
}
}

ctx.log.warn('USING UNSAFE GETTER! PLS UPDATE TYPES!')
const { offeredCollection, offeredItem, desiredCollection, desiredItem, price, deadline } = ctx._chain.decodeEvent(
ctx.event
)
return {
offeredCollection: offeredCollection.toString(),
offeredItem: offeredItem.toString(),
desiredCollection: desiredCollection.toString(),
desiredItem: desiredItem?.toString(),
price,
deadline,
}
}

export function getSwapCancelledEvent(ctx: Context) {
const event = new events.NftsSwapCancelledEvent(ctx)

if (event.isV9430) {
const { offeredCollection, offeredItem, desiredCollection, desiredItem, price, deadline } = event.asV9430

return {
offeredCollection: offeredCollection.toString(),
offeredItem: offeredItem.toString(),
desiredCollection: desiredCollection.toString(),
desiredItem: desiredItem?.toString(),
price,
deadline,
}
}

ctx.log.warn('USING UNSAFE GETTER! PLS UPDATE TYPES!')
const { offeredCollection, offeredItem, desiredCollection, desiredItem, price, deadline } = ctx._chain.decodeEvent(
ctx.event
)
return {
offeredCollection: offeredCollection.toString(),
offeredItem: offeredItem.toString(),
desiredCollection: desiredCollection.toString(),
desiredItem: desiredItem?.toString(),
price,
deadline,
}
}

export function getSwapClaimedEvent(ctx: Context) {
const event = new events.NftsSwapClaimedEvent(ctx)

if (event.isV9430) {
const {
sentCollection,
sentItem,
sentItemOwner,
receivedCollection,
receivedItem,
receivedItemOwner,
price,
deadline,
} = event.asV9430

return {
sentCollection: sentCollection.toString(),
sentItem: sentItem.toString(),
sentItemOwner: sentItemOwner ? addressOf(sentItemOwner) : '',
receivedCollection: receivedCollection.toString(),
receivedItem: receivedItem.toString(),
receivedItemOwner: receivedItemOwner ? addressOf(receivedItemOwner) : '',
price,
deadline,
}
}

ctx.log.warn('USING UNSAFE GETTER! PLS UPDATE TYPES!')
const {
sentCollection,
sentItem,
sentItemOwner,
receivedCollection,
receivedItem,
receivedItemOwner,
price,
deadline,
} = ctx._chain.decodeEvent(ctx.event)

return {
sentCollection: sentCollection.toString(),
sentItem: sentItem.toString(),
sentItemOwner: sentItemOwner ? addressOf(sentItemOwner) : '',
receivedCollection: receivedCollection.toString(),
receivedItem: receivedItem.toString(),
receivedItemOwner: receivedItemOwner ? addressOf(receivedItemOwner) : '',
price,
deadline,
}
}
3 changes: 3 additions & 0 deletions src/mappings/nfts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ export * from './mint'
export * from './setAttribute'
export * from './setMetadata'
export * from './transfer'
export * from './createSwap'
export * from './cancelSwap'
export * from './claimSwap'
9 changes: 8 additions & 1 deletion src/processor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SubstrateProcessor } from '@subsquid/substrate-processor'
import { FullTypeormDatabase as Database } from '@subsquid/typeorm-store'
import logger from './mappings/utils/logger'
import { NonFungible, Unique } from './processable'
import { NonFungible, NewNonFungible, Unique } from './processable'

import { CHAIN, getArchiveUrl, getNodeUrl, isProd } from './environment'
import * as n from './mappings/nfts'
Expand Down Expand Up @@ -93,6 +93,13 @@ processor.addEventHandler(NonFungible.changeTeam, n.handleCollectionTeamChange)
// processor.addEventHandler(NonFungible.thaw, dummy);
processor.addEventHandler(NonFungible.transfer, n.handleTokenTransfer)

/**
* Swap Features
*/
processor.addEventHandler(NewNonFungible.createSwap, n.handleCreateSwap)
processor.addEventHandler(NewNonFungible.cancelSwap, n.handleCancelSwap)
processor.addEventHandler(NewNonFungible.claimSwap, n.handleClaimSwap)

/**
* Assets nft pallet
*/
Expand Down