Skip to content

Commit

Permalink
prices and feerates
Browse files Browse the repository at this point in the history
  • Loading branch information
xuanyan0x7c7 committed Jul 19, 2019
1 parent 5e3459c commit 7c1dbf6
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 138 deletions.
35 changes: 4 additions & 31 deletions agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ const SocketClient = require('socket.io-client')

module.exports = function(agent) {
let tip = null
let stakeWeight = null
let feeRate = null
let dgpInfo = null

agent.messenger.on('egg-ready', () => {
let io = SocketClient(`http://localhost:${agent.config.qtuminfo.port}`)
Expand Down Expand Up @@ -32,10 +29,6 @@ module.exports = function(agent) {
})
})

async function fetchFeeRate() {
agent.messenger.sendRandom('update-feerate')
}

let lastTipHash = Buffer.alloc(0)
function updateStatistics() {
if (tip && Buffer.compare(lastTipHash, tip.hash) !== 0) {
Expand All @@ -48,42 +41,22 @@ module.exports = function(agent) {
}
}

setInterval(fetchFeeRate, 60 * 1000).unref()
setInterval(updateStatistics, 2 * 60 * 1000).unref()

agent.messenger.on('blockchain-info', () => {
agent.messenger.sendToApp('blockchain-info', {tip, stakeWeight, feeRate, dgpInfo})
})
agent.messenger.on('stakeweight', result => {
stakeWeight = result
agent.messenger.sendToApp('stakeweight', stakeWeight)
agent.messenger.sendRandom('socket/stakeweight', stakeWeight)
})
agent.messenger.on('feerate', result => {
if (result) {
feeRate = result
} else if (feeRate == null) {
feeRate = 0.004
}
agent.messenger.sendToApp('feerate', feeRate)
agent.messenger.sendRandom('socket/feerate', feeRate)
})
agent.messenger.on('dgpinfo', result => {
dgpInfo = result
agent.messenger.sendToApp('dgpinfo', dgpInfo)
agent.messenger.sendRandom('socket/dgpinfo', dgpInfo)
agent.messenger.sendToApp('blockchain-info', {tip})
})

agent.messenger.on('egg-ready', () => {
let interval = setInterval(() => {
if (tip && stakeWeight && feeRate && dgpInfo) {
agent.messenger.sendToApp('blockchain-info', {tip, stakeWeight, feeRate, dgpInfo})
if (tip) {
agent.messenger.sendToApp('blockchain-info', {tip})
clearInterval(interval)
updateStatistics()
}
}, 0)
agent.messenger.sendRandom('update-stakeweight')
fetchFeeRate()
agent.messenger.sendRandom('update-feerate')
agent.messenger.sendRandom('update-dgpinfo')
})
}
34 changes: 6 additions & 28 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
module.exports = app => {
app.blockchainInfo = {
tip: null,
stakeWeight: null,
feeRate: null,
dgpInfo: null
tip: null
}
const namespace = app.io.of('/')

Expand Down Expand Up @@ -42,19 +39,19 @@ module.exports = app => {
app.messenger.on('update-stakeweight', async () => {
let ctx = app.createAnonymousContext()
let stakeWeight = await ctx.service.info.getStakeWeight()
app.messenger.sendToAgent('stakeweight', stakeWeight)
await app.redis.hset(app.name, 'stakeweight', JSON.stringify(stakeWeight))
namespace.to('blockchain').emit('stakeweight', stakeWeight)
})

app.messenger.on('update-feerate', async () => {
let ctx = app.createAnonymousContext()
let feeRate = await ctx.service.info.getFeeRate()
app.messenger.sendToAgent('feerate', feeRate)
await app.runSchedule('update-feerate')
})

app.messenger.on('update-dgpinfo', async () => {
let ctx = app.createAnonymousContext()
let dgpInfo = await ctx.service.info.getDGPInfo()
app.messenger.sendToAgent('dgpinfo', dgpInfo)
await app.redis.hset(app.name, 'dgpinfo', JSON.stringify(dgpInfo))
namespace.to('blockchain').emit('dgpinfo', dgpInfo)
})

app.messenger.on('blockchain-info', info => {
Expand All @@ -69,15 +66,6 @@ module.exports = app => {
app.messenger.on('reorg-to-block', block => {
app.blockchainInfo.tip = block
})
app.messenger.on('stakeweight', stakeWeight => {
app.blockchainInfo.stakeWeight = stakeWeight
})
app.messenger.on('feerate', feeRate => {
app.blockchainInfo.feeRate = feeRate
})
app.messenger.on('dgpinfo', dgpInfo => {
app.blockchainInfo.dgpInfo = dgpInfo
})

app.messenger.on('socket/block-tip', async tip => {
app.blockchainInfo.tip = tip
Expand Down Expand Up @@ -113,14 +101,4 @@ module.exports = app => {
namespace.to(`address/${address}`).emit('address/transaction', {address, id: id.toString('hex')})
}
})

app.messenger.on('socket/stakeweight', stakeWeight => {
namespace.to('blockchain').emit('stakeweight', stakeWeight)
})
app.messenger.on('socket/feerate', feeRate => {
namespace.to('blockchain').emit('feerate', feeRate)
})
app.messenger.on('socket/dgpinfo', dgpInfo => {
namespace.to('blockchain').emit('dgpinfo', dgpInfo)
})
}
6 changes: 5 additions & 1 deletion app/controller/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const {Controller} = require('egg')

class InfoController extends Controller {
async index() {
this.ctx.body = this.ctx.service.info.getInfo()
this.ctx.body = await this.ctx.service.info.getInfo()
}

async supply() {
Expand All @@ -16,6 +16,10 @@ class InfoController extends Controller {
async circulatingSupply() {
this.ctx.body = this.ctx.service.info.getInfo().circulatingSupply
}

async feeRates() {
this.ctx.body = JSON.parse(await this.app.redis.hget(this.app.name, 'feerate')).filter(item => [2, 4, 6, 12, 24].includes(item.blocks))
}
}

module.exports = InfoController
4 changes: 4 additions & 0 deletions app/controller/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class MiscController extends Controller {
}))
}
}

async prices() {
this.ctx.body = JSON.parse(await this.app.redis.hget(this.app.name, 'qtum-price'))
}
}

module.exports = MiscController
2 changes: 2 additions & 0 deletions app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = app => {
router.get('/supply', controller.info.supply)
router.get('/total-max-supply', controller.info.totalMaxSupply)
router.get('/circulating-supply', controller.info.circulatingSupply)
router.get('/feerates', controller.info.feeRates)

router.get('/blocks', controller.block.list)
router.get('/block/:block', controller.block.block)
Expand Down Expand Up @@ -174,6 +175,7 @@ module.exports = app => {
paginationMiddleware,
controller.misc.biggestMiners
)
router.get('/misc/prices', controller.misc.prices)

router.get('/stats/daily-transactions', controller.statistics.dailyTransactions)
router.get('/stats/block-interval', controller.statistics.blockInterval)
Expand Down
21 changes: 21 additions & 0 deletions app/schedule/update-feerate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const {Subscription} = require('egg')

class UpdateFeerateSubscription extends Subscription {
static get schedule() {
return {
interval: '1m',
type: 'worker'
}
}

async subscribe() {
let feeRate = await this.ctx.service.info.getFeeRates()
if (feeRate) {
await this.app.redis.hset(this.app.name, 'feerate', JSON.stringify(feeRate))
this.app.io.of('/').to('blockchain')
.emit('feerate', feeRate)
}
}
}

module.exports = UpdateFeerateSubscription
19 changes: 19 additions & 0 deletions app/schedule/update-price.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const {Subscription} = require('egg')

class UpdatePriceSubscription extends Subscription {
static get schedule() {
return {
interval: '1m',
type: 'worker'
}
}

async subscribe() {
let price = await this.ctx.service.misc.getPrices()
await this.app.redis.hset(this.app.name, 'qtum-price', JSON.stringify(price))
this.app.io.of('/').to('coin')
.emit('qtum-price', price)
}
}

module.exports = UpdatePriceSubscription
32 changes: 21 additions & 11 deletions app/service/info.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
const {Service} = require('egg')

class InfoService extends Service {
getInfo() {
let info = this.app.blockchainInfo
async getInfo() {
let height = this.app.blockchainInfo.tip.height
let stakeWeight = JSON.parse(await this.app.redis.hget(this.app.name, 'stakeweight')) || 0
let feeRate = JSON.parse(await this.app.redis.hget(this.app.name, 'feerate')).find(item => item.blocks === 10).feeRate || 0.004
let dgpInfo = JSON.parse(await this.app.redis.hget(this.app.name, 'dgpinfo')) || {}
return {
height: info.tip.height,
supply: this.getTotalSupply(info.tip.height),
circulatingSupply: this.getCirculatingSupply(info.tip.height),
netStakeWeight: Math.round(info.stakeWeight),
feeRate: info.feeRate,
dgpInfo: info.dgpInfo
height,
supply: this.getTotalSupply(height),
circulatingSupply: this.getCirculatingSupply(height),
netStakeWeight: Math.round(stakeWeight),
feeRate,
dgpInfo
}
}

Expand Down Expand Up @@ -61,10 +64,17 @@ class InfoService extends Service {
return sum * 2 ** 32 * 16 / interval
}

async getFeeRate() {
async getFeeRates() {
let client = new this.app.qtuminfo.rpc(this.app.config.qtuminfo.rpc)
let info = await client.estimatesmartfee(10)
return info.feerate
let results = await Promise.all([2, 4, 6, 10, 12, 24].map(blocks => client.estimatesmartfee(blocks)))
return [
{blocks: 2, feeRate: results[0].feerate},
{blocks: 4, feeRate: results[1].feerate},
{blocks: 6, feeRate: results[2].feerate},
{blocks: 10, feeRate: results[3].feerate},
{blocks: 12, feeRate: results[4].feerate},
{blocks: 24, feeRate: results[5].feerate}
]
}

async getDGPInfo() {
Expand Down
32 changes: 32 additions & 0 deletions app/service/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,38 @@ class MiscService extends Service {

return {}
}

async getPrices() {
const coinId = 1684
let [USDResult, CNYResult] = await Promise.all([
this.ctx.curl('https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest', {
headers: {
'X-CMC_PRO_API_KEY': '00000000-0000-0000-0000-000000000000',
Accept: 'application/json'
},
data: {
id: coinId,
convert: 'USD'
},
dataType: 'json'
}),
this.ctx.curl('https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest', {
headers: {
'X-CMC_PRO_API_KEY': '00000000-0000-0000-0000-000000000000',
Accept: 'application/json'
},
data: {
id: coinId,
convert: 'CNY'
},
dataType: 'json'
})
])
return {
USD: USDResult.data.data[coinId].quote.USD.price,
CNY: CNYResult.data.data[coinId].quote.CNY.price
}
}
}

module.exports = MiscService
Loading

0 comments on commit 7c1dbf6

Please sign in to comment.