Skip to content

Commit

Permalink
Merge pull request #19 from rohitverma007/rewards
Browse files Browse the repository at this point in the history
adding hourly script, modifying script a bit
  • Loading branch information
rloomba authored Mar 15, 2019
2 parents 794c9c7 + 96f3985 commit f97cdf2
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 13 deletions.
58 changes: 58 additions & 0 deletions scripts/hourlySaveToJson.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import TezosRpc from '../src/services/rpc/rpc'
import Helper from '../src/services/utils/helper'
const fs = require('fs');
const { promisify } = require("util");
const writeFile = promisify(fs.writeFile);
const { fork } = require('child_process');

(async () => {
const tezosConfig = require('../static/config.json')
const tezosRpc = new TezosRpc(tezosConfig.tezosRpcAddress)
const block = 'head'
const mostRecentCompletedCycle = await tezosRpc.getHeadCycle()
let cycle = mostRecentCompletedCycle + 6
await tezosRpc.setCycle(cycle)
const rawSnapshotData = require('../static/snapshotData.json')
const cycleDataFromJson = require('../static/allCyclesData.json')

// save rights data + snapshot data for latest future cycle
const latestCycleFromSnapshotJson = rawSnapshotData[rawSnapshotData.length-1]['cycleNumber']
if(latestCycleFromSnapshotJson != cycle) {
const snapshotBlockNumber = await tezosRpc.getSnapshotBlockForCycle('head')
const snapshotNumber = tezosRpc.snapshotNumber
rawSnapshotData.push({
'cycleNumber': cycle,
'snapshotBlockNumber': snapshotBlockNumber,
'snapshotNumber': tezosRpc.snapshotNumber
})
await writeFile('../static/snapshotData.json', JSON.stringify(rawSnapshotData));
console.log('cycle: ', cycle, ', snapshotNumber: ', snapshotNumber, ', snapshotBlockNumber: ', snapshotBlockNumber)
console.log('Saved: snapshotData.json');

const process = fork('./saveCycleDataToJson.js');
let snapshotData = {}
snapshotData[cycle] = snapshotBlockNumber
process.send({mostRecentCompletedCycle, block, snapshotData, cycle})
process.on('message', async (data) => {
cycleDataFromJson[cycle] = data[cycle]
await writeFile('../static/allCyclesData.json', JSON.stringify(cycleDataFromJson))
console.log('Saved: allCyclesData.json with latest future cycle');
})
}

// save rewards data for latest baked cycle
const mostRecentCompletedCycleDatFromJson = cycleDataFromJson[mostRecentCompletedCycle]
if (mostRecentCompletedCycleDatFromJson['prediction']) {
cycle = mostRecentCompletedCycle
await tezosRpc.setCycle(mostRecentCompletedCycle)
const process = fork('./saveCycleDataToJson.js');
const snapshotData = new Helper().parseSnapshotData(rawSnapshotData)['snapshotblockNumberData']
process.send({mostRecentCompletedCycle, block, snapshotData, cycle})
process.on('message', async (data) => {
cycleDataFromJson[cycle] = data[cycle]
await writeFile('../static/allCyclesData.json', JSON.stringify(cycleDataFromJson))
console.log('Saved: allCyclesData.json with latest baked cycle');
})
}

})()
18 changes: 18 additions & 0 deletions scripts/mergeAndSaveJsonData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import TezosRpc from '../src/services/rpc/rpc'
const fs = require('fs');
const util = require('util');
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);

(async () => {
const tezosConfig = require('../static/config.json')
const tezosRpc = new TezosRpc(tezosConfig.tezosRpcAddress)
const mostRecentCompletedCycle = await tezosRpc.getHeadCycle()
let allCyclesData = {}
for(let i = 7; i <= mostRecentCompletedCycle + 6; i++){
const cycleData = await readFile(i+'.json')
allCyclesData = { ...allCyclesData, ...JSON.parse(cycleData.toString('utf8'))}
}
await writeFile('../static/allCyclesData.json', JSON.stringify(allCyclesData))
console.log('Saved: allCyclesData.json')
})()
13 changes: 0 additions & 13 deletions scripts/saveBlockchainDataToJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,4 @@ const util = require('util');
});
})
}

const readFile = util.promisify(fs.readFile);
let allCyclesData = {}
for(let i = 7; i <= mostRecentCompletedCycle + 6; i++){
const cycleData = await readFile('../static/'+i+'.json')
allCyclesData = { ...allCyclesData, ...JSON.parse(cycleData.toString('utf8'))}
}
fs.writeFile('../static/allCyclesData.json', JSON.stringify(allCyclesData), function(err) {
if(err) {
return console.log(err);
}
console.log('Saved: ', "allCyclesData.json");
});
})()
4 changes: 4 additions & 0 deletions scripts/saveCycleDataToJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ process.on('message', async ({mostRecentCompletedCycle, block, snapshotData, cyc
tezosRpc.setSnapshotBlockNumber(snapshotData[tezosRpc.cycle])
let endorsingDataForCycle = {}
let bakingDataForCycle = {}
let prediction = false

if (cycle <= mostRecentCompletedCycle) {
// get data from metadata - already baked
Expand All @@ -17,6 +18,7 @@ process.on('message', async ({mostRecentCompletedCycle, block, snapshotData, cyc
// get rights for future cycles
endorsingDataForCycle = await tezosRpc.getEndorsingRightsOfCycle(block, cycle)
bakingDataForCycle = await tezosRpc.getBakingRightsOfCycle(block, cycle)
prediction = true
}

const allDelegatesArray = Array.from(new Set(Object.keys(endorsingDataForCycle).concat(Object.keys(bakingDataForCycle))))
Expand All @@ -30,11 +32,13 @@ process.on('message', async ({mostRecentCompletedCycle, block, snapshotData, cyc
const bakingRewards = bakingDataForCycle[delegate] || 0
tezosRpc.delegateHash = delegate
const cycleData = await tezosRpc.getCycleData()
cycleRewardsData[cycle]['prediction'] = prediction
cycleRewardsData[cycle][delegate] = {
'endorsingRewards': endorsingRewards,
'bakingRewards': bakingRewards,
'stakingBalance': cycleData.staking_balance
}
}
process.send(cycleRewardsData)
process.exit()
});

0 comments on commit f97cdf2

Please sign in to comment.