Skip to content
This repository has been archived by the owner on May 19, 2023. It is now read-only.

Commit

Permalink
fix: new Transfer entity to avoid SoldDomain synchronization problems
Browse files Browse the repository at this point in the history
Refs #91
  • Loading branch information
Leandro Yalet committed May 19, 2020
1 parent f8973f6 commit 31601cc
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 29 deletions.
38 changes: 21 additions & 17 deletions src/rns/hooks/sold-domain.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { disallow } from 'feathers-hooks-common'
import Domain from '../models/domain.model'
import { Op } from 'sequelize'
import { numberToHex, sha3 } from 'web3-utils'
import Transfer from '../models/transfer.model'

export default {
before: {
Expand All @@ -18,38 +19,41 @@ export default {
if (!context.params.query) {
context.params.query = {}
}

if (context.params.route?.ownerAddress) {
context.params.query.sellerAddress = context.params.route.ownerAddress.toLowerCase()
}
}
],
find: [
(context: HookContext) => {
context.params.sequelize = {
raw: false,
nest: true,
include: {
model: Domain
}
}
const { params: { sequelize: { include } } } = context
const { domain } = context.params.query as any
let nameSearch

if (include && domain) {
const { name: { $like } } = domain
include.where = {
if (domain) {
nameSearch = {
[Op.or]: {
name: {
[Op.like]: `%${$like}%`
[Op.like]: `%${domain}%`
},
tokenId: {
[Op.eq]: numberToHex(((sha3($like)) as string))
[Op.eq]: numberToHex(((sha3(domain)) as string))
}
}
}
}

context.params.sequelize = {
raw: false,
nest: true,
include: [
{ model: Domain, attributes: ['tokenId', 'name', 'expirationDate'], where: nameSearch },
{
model: Transfer,
attributes: ['sellerAddress', 'newOwnerAddress'],
where: {
sellerAddress: context.params.route?.ownerAddress
}
}
]
}

delete (context.params.query as any).domain
}
],
Expand Down
10 changes: 5 additions & 5 deletions src/rns/models/sold-domain.model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Table, DataType, Column, Model, Scopes, ForeignKey, BelongsTo } from 'sequelize-typescript'

import Domain from './domain.model'
import Transfer from './transfer.model'

@Scopes(() => ({
active: {
Expand All @@ -24,11 +25,10 @@ export default class SoldDomain extends Model {
@BelongsTo(() => Domain)
domain!: Domain

@Column(DataType.STRING)
sellerAddress!: string // previous owner

@Column(DataType.STRING)
newOwnerAddress!: string // buyer
@BelongsTo(() => Transfer, {
foreignKey: 'id'
})
transfer!: Transfer

@Column(DataType.STRING)
paymentToken!: string // currency
Expand Down
16 changes: 16 additions & 0 deletions src/rns/models/transfer.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Table, DataType, Column, Model } from 'sequelize-typescript'

@Table({ freezeTableName: true, tableName: 'rns_transfer', timestamps: false })
export default class Transfer extends Model {
@Column({ primaryKey: true, type: DataType.STRING })
id!: string

@Column(DataType.STRING)
tokenId!: string

@Column(DataType.STRING)
sellerAddress!: string // previous owner

@Column(DataType.STRING)
newOwnerAddress!: string // buyer
}
18 changes: 11 additions & 7 deletions src/rns/rns.processor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Domain from './models/domain.model'
import SoldDomain from './models/sold-domain.model'
import Transfer from './models/transfer.model'
import DomainOffer from './models/domain-offer.model'

import { EventData } from 'web3-eth-contract'
Expand All @@ -26,15 +27,16 @@ async function transferHandler (logger: Logger, eventData: EventData): Promise<v
logger.info(`Transfer event: Domain ${tokenId} updated`)
const transactionHash = eventData.transactionHash
const from = eventData.returnValues.from.toLowerCase()
const soldDomain = await SoldDomain.create({

const transferDomain = await Transfer.create({
id: transactionHash,
tokenId: tokenId,
tokenId,
sellerAddress: from,
newOwnerAddress: ownerAddress
})

if (soldDomain) {
logger.info(`Transfer event: SoldDomain ${tokenId} created`)
if (transferDomain) {
logger.info(`Transfer event: Transfer ${tokenId} created`)
}
const [affectedRows] = await Domain.update({ ownerAddress }, { where: { tokenId } })

Expand Down Expand Up @@ -151,13 +153,15 @@ async function tokenSoldHandler (logger: Logger, eventData: EventData, eth: Eth)
lastOffer.status = 'SOLD'
lastOffer.save()

const [affectedRows] = await SoldDomain.update({
const soldDomain = await SoldDomain.create({
id: transactionHash,
tokenId: tokenId,
price: lastOffer.price,
paymentToken: lastOffer.paymentToken,
soldDate: await getBlockDate(eth, eventData.blockNumber)
}, { where: { id: transactionHash } })
})

if (affectedRows) {
if (soldDomain) {
logger.info(`TokenSold event: ${tokenId}`)
} else {
logger.info(`TokenSold event: ${tokenId} not updated`)
Expand Down

0 comments on commit 31601cc

Please sign in to comment.