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

Commit

Permalink
feat: wraps db calls in feathers services
Browse files Browse the repository at this point in the history
  • Loading branch information
jurajpiar committed Jun 10, 2020
1 parent f6563dd commit df9e17f
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 74 deletions.
4 changes: 2 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ export const services = {
[SupportedServices.RNS]: rns
}

export function isSupportedServices(value: any): value is SupportedServices {
export function isSupportedServices (value: any): value is SupportedServices {
return Object.values(SupportedServices).includes(value)
}

export async function appFactory(): Promise<Application> {
export async function appFactory (): Promise<Application> {
const app: Application = express(feathers())

const corsOptions: CorsOptionsDelegate = config.get('cors')
Expand Down
4 changes: 2 additions & 2 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import { RnsService } from './services/rns'
import ConfirmationService from './blockchain/confirmation.service'

export enum ServiceAddresses {
RNS_DOMAINS = '/rns/v0/:ownerAddress/domains',
RNS_SOLD = '/rns/v0/:ownerAddress/sold',
RNS_DOMAINS = '/rns/v0/domains',
RNS_SOLD = '/rns/v0/sold',
RNS_OFFERS = '/rns/v0/offers',
STORAGE_OFFERS = '/storage/v0/offers',
XR = '/rates/v0/',
Expand Down
12 changes: 7 additions & 5 deletions src/services/rns/hooks/domain.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export default {
if (context.params.query) {
const { ownerAddress, placed, name } = context.params.query

if (!ownerAddress) { return Promise.reject(new Error('No ownerAddress specified.')) }

context.params.sequelize = {
raw: false,
nest: true,
Expand All @@ -51,7 +53,7 @@ export default {
model: DomainOwner,
attributes: ['address'],
where: {
address: ownerAddress
address: ownerAddress.toLowerCase()
}
},
{
Expand Down Expand Up @@ -86,10 +88,10 @@ export default {
}
],
get: [],
create: disallow(),
update: disallow(),
patch: disallow(),
remove: disallow()
create: disallow('external'),
update: disallow('external'),
patch: disallow('external'),
remove: disallow('external')
},

after: {
Expand Down
13 changes: 9 additions & 4 deletions src/services/rns/hooks/sold-domain.hooks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HookContext } from '@feathersjs/feathers'
import { disallow, discardQuery } from 'feathers-hooks-common'
import { disallow } from 'feathers-hooks-common'
import { Op } from 'sequelize'
import { numberToHex, sha3 } from 'web3-utils'
import Domain from '../models/domain.model'
Expand All @@ -22,16 +22,21 @@ export default {
],
find: [
(context: HookContext) => {
const { params: { query } } = context
const sellerAddress = query?.ownerAddress.toLowerCase()

if (!sellerAddress) { return Promise.reject(new Error('No ownerAddress specified.')) }

context.params.sequelize = {
raw: false,
nest: true,
include: [
{ model: Domain, attributes: ['tokenId', 'name'] },
{
model: Transfer,
attributes: ['sellerAddress', 'newOwnerAddress'],
attributes: ['sellerAddress', 'buyerAddress'],
where: {
sellerAddress: context.params.route?.ownerAddress.toLowerCase()
sellerAddress
}
}
]
Expand All @@ -52,7 +57,7 @@ export default {
}
}

discardQuery('domain')
context.params.query = {}
}
],
get: [],
Expand Down
10 changes: 5 additions & 5 deletions src/services/rns/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export interface RnsServices {
offers: RnsService
}

function fetchEventsForService(eth: Eth, serviceName: string, abi: AbiItem[], dataPusher: (event: EventData) => void): Promise<void> {
function fetchEventsForService (eth: Eth, serviceName: string, abi: AbiItem[], dataPusher: (event: EventData) => void): Promise<void> {
return new Promise<void>((resolve, reject) => {
const eventsEmitter = getEventsEmitterForService(serviceName, eth, abi)
eventsEmitter.on('initFinished', () => {
Expand All @@ -53,7 +53,7 @@ function fetchEventsForService(eth: Eth, serviceName: string, abi: AbiItem[], da
})
}

async function precache(eth?: Eth): Promise<void> {
async function precache (eth?: Eth): Promise<void> {
eth = eth || ethFactory()
const precacheLogger = loggingFactory('rns:precache:processor')
const eventsDataQueue: EventData[] = []
Expand Down Expand Up @@ -90,7 +90,7 @@ async function precache(eth?: Eth): Promise<void> {

const rns: CachedService = {
// eslint-disable-next-line require-await
async initialize(app: Application): Promise<void> {
async initialize (app: Application): Promise<void> {
if (!config.get<boolean>('rns.enabled')) {
logger.info('RNS service: disabled')
return Promise.resolve()
Expand All @@ -112,7 +112,7 @@ const rns: CachedService = {
sold.hooks(soldDomainHooks)
offers.hooks(domainOfferHooks)

app.configure(rnsChannels);
app.configure(rnsChannels)

// Initialize blockchain watcher
const eth = app.get('eth') as Eth
Expand Down Expand Up @@ -145,7 +145,7 @@ const rns: CachedService = {

precache,

async purge(): Promise<void> {
async purge (): Promise<void> {
const transferCount = await Transfer.destroy({ where: {}, truncate: true, cascade: true })
const offersCount = await DomainOffer.destroy({ where: {}, truncate: true, cascade: true })
const soldCount = await SoldDomain.destroy({ where: {}, truncate: true, cascade: true })
Expand Down
4 changes: 2 additions & 2 deletions src/services/rns/models/domain-offer.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ export default class DomainOffer extends Model {
domain!: Domain

@Column(DataType.STRING)
sellerAddress!: string
ownerAddress!: string

@Column(DataType.STRING)
sellerDomain!: string
ownerDomain!: string

@Column(DataType.STRING)
paymentToken!: string
Expand Down
2 changes: 1 addition & 1 deletion src/services/rns/models/transfer.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ export default class Transfer extends Model {
sellerAddress!: string // previous owner

@Column(DataType.STRING)
newOwnerAddress!: string // buyer
buyerAddress!: string // buyer
}
30 changes: 15 additions & 15 deletions src/services/rns/rns.channels.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import '@feathersjs/transport-commons';
import { Application } from '../definitions';
import '@feathersjs/transport-commons'
import { Application, ServiceAddresses } from '../../definitions'

export default function (app: Application) {
if (typeof app.channel !== 'function') {
// If no real-time functionality has been configured just return
return;
}
app.on('connection', (connection: any) => {
app.channel('offers').join(connection)
app.channel('domains').join(connection)
app.channel('sold').join(connection)
})
app.service('/rns/v0/offers').publish(() => app.channel(`offers`));
app.service('/rns/v0/:ownerAddress/sold').publish(() => app.channel(`sold`));
app.service('/rns/v0/:ownerAddress/domains').publish(() => app.channel(`domains`));
};
if (typeof app.channel !== 'function') {
// If no real-time functionality has been configured just return
return
}
app.on('connection', (connection: any) => {
app.channel('offers').join(connection)
app.channel('domains').join(connection)
app.channel('sold').join(connection)
})
app.service(ServiceAddresses.RNS_OFFERS).publish(() => app.channel('offers'))
app.service(ServiceAddresses.RNS_DOMAINS).publish(() => app.channel('domains'))
app.service(ServiceAddresses.RNS_SOLD).publish(() => app.channel('sold'))
}
4 changes: 2 additions & 2 deletions src/services/rns/rns.precache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ abiDecoder.addABI([
}
])

export async function processRskOwner(eth: Eth, logger: Logger, contractAbi: Utils.AbiItem[]) {
export async function processRskOwner (eth: Eth, logger: Logger, contractAbi: Utils.AbiItem[]) {
if (!config.has('rns.fifsAddrRegistrar.contractAddress')) {
logger.warn('RNS FIFS Registrar address is not defined, skipping Auction Registrar precaching!')
return
Expand All @@ -61,7 +61,7 @@ export async function processRskOwner(eth: Eth, logger: Logger, contractAbi: Uti
}
}

export async function processAuctionRegistrar(eth: Eth, logger: Logger, contractAbi: Utils.AbiItem[]) {
export async function processAuctionRegistrar (eth: Eth, logger: Logger, contractAbi: Utils.AbiItem[]) {
if (!config.has('rns.registrar.contractAddress')) {
logger.warn('RNS Registrar address is not defined, skipping Auction Registrar precaching!')
return
Expand Down
Loading

0 comments on commit df9e17f

Please sign in to comment.