Skip to content

Commit

Permalink
allow to export blocks from delegate by public key
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiaslins committed Jun 5, 2018
1 parent bd2ebcf commit e28419e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 18 deletions.
32 changes: 30 additions & 2 deletions index.js
Expand Up @@ -9,7 +9,7 @@ const { router, get } = require('microrouter')
const { Pool } = require('pg')

const config = require('./config')
const sqlQuery = require('./query')
const { transactionsQuery, blocksQuery } = require('./query')

const { getStartOfYear, getEndOfYear } = require('./helper')

Expand All @@ -35,7 +35,7 @@ module.exports = router(
`attachment; filename=lisk_txs_${address}_${year}.csv`
)

const query = new QueryStream(sqlQuery, [
const query = new QueryStream(transactionsQuery, [
address,
getStartOfYear(year),
getEndOfYear(year)
Expand All @@ -52,6 +52,34 @@ module.exports = router(
}
})

const client = await pool.connect()
return client.query(query).pipe(csvStream)
}),
get('/delegate/:publicAddress', async (req, res) => {
const { publicAddress } = req.params
const { delimiter = ';', timezone = 'UTC' } = req.query
if (publicAddress.includes('favicon')) return ''

res.setHeader('Content-Type', 'application/octet-stream')
res.setHeader(
'Content-Disposition',
`attachment; filename=lisk_delegate_blocks.csv`
)

const query = new QueryStream(blocksQuery, [publicAddress])

const csvStream = csv.createWriteStream({
headers: true,
objectMode: true,
delimiter,
transform(row) {
row.timestamp = convertTimestamp(row.timestamp, timezone)
row.totalfee = fromRawLsk(row.totalfee)
row.reward = fromRawLsk(row.reward)
return row
}
})

const client = await pool.connect()
return client.query(query).pipe(csvStream)
})
Expand Down
41 changes: 25 additions & 16 deletions query.js
@@ -1,16 +1,25 @@
module.exports = `SELECT
"t_id" AS id,
"b_height" AS height,
"t_blockId" AS "blockId",
"t_timestamp" AS timestamp,
"t_senderId" AS "senderId",
"t_recipientId" AS "recipientId",
"t_amount" AS amount,
"t_fee" AS fee,
confirmations
FROM
trs_list
WHERE ("t_senderId" = $1
OR "t_recipientId" = $1)
AND t_timestamp >= $2
AND t_timestamp <= $3`
module.exports = {
transactionsQuery: `SELECT
"t_id" AS id,
"b_height" AS height,
"t_blockId" AS "blockId",
"t_timestamp" AS timestamp,
"t_senderId" AS "senderId",
"t_recipientId" AS "recipientId",
"t_amount" AS amount,
"t_fee" AS fee,
confirmations
FROM
trs_list
WHERE ("t_senderId" = $1
OR "t_recipientId" = $1)
AND t_timestamp >= $2
AND t_timestamp <= $3`,

blocksQuery: `SELECT
"b_height" AS height,
"b_timestamp" AS timestamp,
"b_totalFee" AS totalfee,
"b_reward" AS reward
from blocks_list where "b_generatorPublicKey" = $1;`
}

0 comments on commit e28419e

Please sign in to comment.