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

Commit

Permalink
feat: add staking contract to config
Browse files Browse the repository at this point in the history
precache staking contract
  • Loading branch information
nduchak committed Sep 8, 2020
1 parent 69ae4c1 commit 51d07f6
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 30 deletions.
38 changes: 38 additions & 0 deletions config/default.json5
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
// Sets if Storage service should be enabled
enabled: true,

// Storage Manager Contract
storageManager: {
// Topics that will be listened to
topics: [
Expand Down Expand Up @@ -69,12 +70,49 @@
confirmations: 6
},

// Specify behavior of NewBlockEmitter, that detects new blocks on blockchain.
newBlockEmitter: {
// If to use polling strategy, if false then listening is used.
polling: true
}
},

// Staking Contract
staking: {
// Topics that will be listened to
topics: [
'TotalCapacitySet(address,uin64)',
'BillingPlanSet(address,uint64,uint128)',
'MessageEmitted(address,bytes32[])',
'NewAgreement(bytes32,bytes32[],address,address,uint128,uint64,uint128,uint256)',
'AgreementFundsDeposited(bytes32,uint256)',
'AgreementFundsWithdrawn(bytes32,uint256)',
'AgreementFundsPayout(bytes32,uint256)',
'AgreementStopped(bytes32)',
],

// Specify behavior of EventsEmitter, that retrieves events from blockchain and pass them onwards for further processing.
eventsEmitter: {
// If to use polling strategy, if false then listening is used.
polling: true,

// Interval in milliseconds, how often is blockchain checked.
pollingInterval: 5000,

// Starting block that upon first start of the service, will the blockchain be crawled for the past events.
startingBlock: "genesis",

// Number of blocks that will be waited before passing an event for further processing.
confirmations: 6
},

// Specify behavior of NewBlockEmitter, that detects new blocks on blockchain.
newBlockEmitter: {
// If to use polling strategy, if false then listening is used.
polling: true
}
}

},

// Settings for RNS service related function
Expand Down
5 changes: 2 additions & 3 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 @@ -60,7 +60,7 @@
"@oclif/parser": "^3.8.5",
"@oclif/plugin-help": "^3.2.0",
"@rsksmart/rif-marketplace-nfts": "~0.1.2",
"@rsksmart/rif-marketplace-storage": "^0.1.0-dev.1",
"@rsksmart/rif-marketplace-storage": "https://github.com/rsksmart/rif-marketplace-storage.git#feat/staking-multi-currency",
"@rsksmart/rns-auction-registrar": "1.0.2",
"@rsksmart/rns-reverse": "^1.0.2",
"@rsksmart/rns-rskregistrar": "^1.2.1",
Expand Down
35 changes: 29 additions & 6 deletions src/services/storage/handlers/stake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,41 @@ const handlers = {
async Staked (event: EventData, { stakeService }: StorageServices): Promise<void> {
const { user: account, total, token, amount } = event.returnValues

const [, [stake]] = await StakeModel.update({ total }, { where: { account, tokenAddress: token } })
logger.info(`Account ${account} stake amount ${amount}, final balance ${total}`)
const stakeData = {
total,
token,
account
}
await StakeModel.upsert(stakeData)
const stake = await StakeModel.findOne({ where: { token } })

if (!stake) {
throw new Error(`Stake for token ${stakeData.token} was not created`)
}

if (stakeService.emit) stakeService.emit('updated', stake.toJSON())
logger.info(`Account ${stake?.account} stake amount ${amount}, final balance ${stake?.total}`)

if (stakeService.emit) stakeService.emit('updated', stake!.toJSON())
},

async Unstaked (event: EventData, { stakeService }: StorageServices): Promise<void> {
const { user: account, total, token, amount } = event.returnValues

const [, [stake]] = await StakeModel.update({ total }, { where: { account, tokenAddress: token } })
logger.info(`Account ${account} unstack amount ${amount}, final balance ${total}`)
const stakeData = {
total,
token,
account
}
await StakeModel.upsert(stakeData)
const stake = await StakeModel.findOne({ where: { token } })

if (stakeService.emit) stakeService.emit('updated', stake.toJSON())
if (!stake) {
throw new Error(`Stake for token ${stakeData.token} was not created`)
}

logger.info(`Account ${stake?.account} unstack amount ${amount}, final balance ${stake?.total}`)

if (stakeService.emit) stakeService.emit('updated', stake?.toJSON())
}
}

Expand All @@ -41,4 +63,5 @@ const handler: Handler<StorageServices> = {
return handlers[event.event](event, services)
}
}

export default handler
47 changes: 29 additions & 18 deletions src/services/storage/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import storageManagerContract from '@rsksmart/rif-marketplace-storage/build/contracts/StorageManager.json'
import stakingContract from '@rsksmart/rif-marketplace-storage/build/contracts/Staking.json'
import config from 'config'
import { Service } from 'feathers-sequelize'
import { getObject } from 'sequelize-store'
import Eth from 'web3-eth'
import { EventData } from 'web3-eth-contract'
import { AbiItem } from 'web3-utils'
import { EventEmitter } from 'events'

import { ethFactory } from '../../blockchain'
import { getEventsEmitterForService, isServiceInitialized } from '../../blockchain/utils'
Expand Down Expand Up @@ -41,27 +43,18 @@ export interface StorageServices {
}

const SERVICE_NAME = 'storage'
const STORAGE_MANAGER = 'storageManager'
const STAKING = 'staking'

const logger = loggingFactory(SERVICE_NAME)

// TODO add staking to precache
function precache (possibleEth?: Eth): Promise<void> {
function precacheContract (eventEmitter: EventEmitter, services: StorageServices, eth: Eth): Promise<void> {
return new Promise<void>((resolve, reject) => {
const eth = possibleEth || ethFactory()
const eventsEmitter = getEventsEmitterForService(`${SERVICE_NAME}.storageManager`, eth, storageManagerContract.abi as AbiItem[])
// const eventsEmitter = getEventsEmitterForService(`${SERVICE_NAME}.stake`, eth, storageManagerContract.abi as AbiItem[])

const dataQueue: EventData[] = []
const dataQueuePusher = (event: EventData): void => { dataQueue.push(event) }

const services: StorageServices = {
stakeService: new StakeService({ Model: StakeModel }),
offerService: new OfferService({ Model: Offer }),
agreementService: new AgreementService({ Model: Agreement })
}

eventsEmitter.on('initFinished', async () => {
eventsEmitter.off('newEvent', dataQueuePusher)
eventEmitter.on('initFinished', async () => {
eventEmitter.off('newEvent', dataQueuePusher)

// Needs to be sequentially processed
const processor = eventProcessor(services, eth)
Expand All @@ -74,13 +67,31 @@ function precache (possibleEth?: Eth): Promise<void> {
reject(e)
}
})
eventsEmitter.on('newEvent', dataQueuePusher)
eventsEmitter.on('error', (e: Error) => {
eventEmitter.on('newEvent', dataQueuePusher)
eventEmitter.on('error', (e: Error) => {
logger.error(`There was unknown error in Events Emitter! ${e}`)
})
})
}

async function precache (possibleEth?: Eth): Promise<void> {
const eth = possibleEth || ethFactory()
const storageEventsEmitter = getEventsEmitterForService(`${SERVICE_NAME}.${STORAGE_MANAGER}`, eth, storageManagerContract.abi as AbiItem[])
const stakingEventsEmitter = getEventsEmitterForService(`${SERVICE_NAME}.${STAKING}`, eth, stakingContract.abi as AbiItem[])

const services: StorageServices = {
stakeService: new StakeService({ Model: StakeModel }),
offerService: new OfferService({ Model: Offer }),
agreementService: new AgreementService({ Model: Agreement })
}

// Precache Storage Manager
await precacheContract(storageEventsEmitter, services, eth)

// Precache Staking
await precacheContract(stakingEventsEmitter, services, eth)
}

const storage: CachedService = {
async initialize (app: Application): Promise<void> {
if (!config.get<boolean>('storage.enabled')) {
Expand Down Expand Up @@ -119,7 +130,7 @@ const storage: CachedService = {
const confirmationService = app.service(ServiceAddresses.CONFIRMATIONS)

// Storage Manager watcher
const storageManagerEventsEmitter = getEventsEmitterForService(`${SERVICE_NAME}.storageManager`, eth, storageManagerContract.abi as AbiItem[])
const storageManagerEventsEmitter = getEventsEmitterForService(`${SERVICE_NAME}.${STORAGE_MANAGER}`, eth, storageManagerContract.abi as AbiItem[])
storageManagerEventsEmitter.on('newEvent', errorHandler(eventProcessor(services, eth), logger))
storageManagerEventsEmitter.on('error', (e: Error) => {
logger.error(`There was unknown error in Events Emitter! ${e}`)
Expand All @@ -128,7 +139,7 @@ const storage: CachedService = {
storageManagerEventsEmitter.on('invalidConfirmation', (data) => confirmationService.emit('invalidConfirmation', data))

// Staking watcher
const stakingEventsEmitter = getEventsEmitterForService(`${SERVICE_NAME}.staking`, eth, stakingContract.abi as AbiItem[])
const stakingEventsEmitter = getEventsEmitterForService(`${SERVICE_NAME}.${STAKING}`, eth, stakingContract.abi as AbiItem[])
stakingEventsEmitter.on('newEvent', errorHandler(eventProcessor(services, eth), logger))
stakingEventsEmitter.on('error', (e: Error) => {
logger.error(`There was unknown error in Events Emitter! ${e}`)
Expand Down
4 changes: 2 additions & 2 deletions src/services/storage/models/stake.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ export default class StakeModel extends Model {
total!: BigNumber

@Column
tokenName!: string
tokenName?: string

@Column
tokenAddress?: string
token!: string

@Column
account!: string
Expand Down

0 comments on commit 51d07f6

Please sign in to comment.