Skip to content

Commit

Permalink
Merge pull request #13 from rohitverma007/rewards
Browse files Browse the repository at this point in the history
Rewards
  • Loading branch information
rloomba authored Jan 21, 2019
2 parents f49794f + 1026521 commit 2e8e683
Show file tree
Hide file tree
Showing 9 changed files with 296 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<b-collapse is-nav id="nav_collapse">
<b-navbar-nav>
<b-nav-item to="/config">Config</b-nav-item>
<b-nav-item to="/cycles">All Cycles</b-nav-item>
<b-nav-item to="/cycle_info/head">Cycle Info</b-nav-item>
</b-navbar-nav>
</b-collapse>
</b-navbar>
Expand All @@ -20,6 +22,7 @@ export default {
name: 'app',
created () {
this.$store.dispatch('user/loadFromConfigFile')
this.$store.dispatch('snapshot/loadFromConfigFile')
}
}
</script>
Expand Down
66 changes: 66 additions & 0 deletions src/components/CycleInfo.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<template>
<div class="container pt-3">
<div class="row">
<h1>Cycle {{currentCycle}}</h1>
</div>
<div class="row">
{{loadingText}}
<b-table v-if="loadingText === ''" striped hover :fields="contractsDataFields" :items="contractsData">
<template slot="contractData" slot-scope="data">
{{tezosHelper.formatTezosNumericalData(data.item.contractData.balance)}}
</template>
</b-table>
</div>
</div>
</template>

<script>
import { mapState } from 'vuex'
import TezosRpc from '@/services/rpc/rpc'
import TezosHelper from '@/services/utils/tezos'
export default {
data () {
return {
currentCycle: this.$route.params.cycle,
contractsDataFields: [
{key: 'contractId', label: 'Delegator'},
{key: 'contractData', label: 'Delegator Balance'}
],
contractsData: [],
tezosHelper: new TezosHelper(),
loadingText: 'Loading...'
}
},
computed: mapState([
'user',
'snapshot'
]),
methods: {
compareBalance: function (a, b) {
const balanceA = parseFloat(a.contractData.balance)
const balanceB = parseFloat(b.contractData.balance)
let comparisonValue = 0
if (balanceA < balanceB) {
comparisonValue = 1
} else if (balanceA > balanceB) {
comparisonValue = -1
}
return comparisonValue
}
},
created: async function () {
const tezosRpc = new TezosRpc(this.user.tezos_rpc_address, this.user.baker_tz_address, this.$route.params.cycle)
await tezosRpc.setCycle(this.$route.params.cycle)
tezosRpc.setSnapshotNumber(this.snapshot.data[tezosRpc.cycle])
this.currentCycle = tezosRpc.cycle
const contractIdsArray = await tezosRpc.getSnapshotDelegateContractIds()
this.contractsData = await tezosRpc.getContractsData(contractIdsArray)
this.contractsData.sort(this.compareBalance)
this.loadingText = ''
}
}
</script>
76 changes: 76 additions & 0 deletions src/components/Cycles.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<template>
<div class="container pt-3">
<div class="row">
{{loadingText}}
<b-pagination size="md" :total-rows="cyclesData.length" v-model="currentPage" :per-page="10">
</b-pagination>
<b-table striped hover :items="cyclesData" :per-page="10" :current-page="currentPage">
<template slot="cycle" slot-scope="data">
<b-link :to="{name: 'CycleInfo', params: { cycle: data.value }}">{{data.value}}</b-link>
</template>
<template slot="stakingBalance" slot-scope="data">
{{tezosHelper.formatTezosNumericalData(data.value)}}
</template>
</b-table>
</div>
</div>
</template>

<script>
import { mapState } from 'vuex'
import TezosRpc from '@/services/rpc/rpc'
import TezosHelper from '@/services/utils/tezos'
export default {
data () {
return {
cyclesData: [],
currentPage: 1,
mostRecentCompletedCycle: 0,
mostRecentSnapshottedCycle: 0,
loadingText: 'Loading...',
tezosRpc: null,
tezosHelper: new TezosHelper()
}
},
computed: mapState([
'user',
'snapshot'
]),
methods: {
async getAllCyclesData () {
for (let i = this.mostRecentCompletedCycle + 6; i > 0; i--) {
let status = ''
if (i > this.mostRecentCompletedCycle) {
status = 'Pending'
} else if (i === this.mostRecentCompletedCycle) {
status = 'Currently In Progress'
} else if (i < this.mostRecentCompletedCycle && i > (this.mostRecentCompletedCycle - 5)) {
status = 'Pending Rewards'
} else {
status = 'Delivered Rewards'
}
await this.tezosRpc.setCycle(i)
this.tezosRpc.setSnapshotNumber(this.snapshot.data[this.tezosRpc.cycle])
const cycleData = await this.tezosRpc.getCycleData()
this.cyclesData.push(
{
'cycle': i,
'status': status,
'stakingBalance': cycleData.staking_balance
}
)
}
}
},
created: async function () {
this.tezosRpc = new TezosRpc(this.user.tezos_rpc_address, this.user.baker_tz_address)
this.mostRecentCompletedCycle = await this.tezosRpc.getHeadCycle()
this.mostRecentSnapshottedCycle = this.mostRecentCompletedCycle + 6
await this.getAllCyclesData()
this.loadingText = ''
}
}
</script>
13 changes: 13 additions & 0 deletions src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import Vue from 'vue'
import Router from 'vue-router'
import Hello from '@/components/Hello'
import Config from '@/components/Config'
import Cycles from '@/components/Cycles'
import CycleInfo from '@/components/CycleInfo'

Vue.use(Router)

export default new Router({
mode: 'history',
routes: [
{
path: '/',
Expand All @@ -16,6 +19,16 @@ export default new Router({
path: '/config',
name: 'Config',
component: Config
},
{
path: '/cycles',
name: 'Cycles',
component: Cycles
},
{
path: '/cycle_info/:cycle',
name: 'CycleInfo',
component: CycleInfo
}
]
})
83 changes: 83 additions & 0 deletions src/services/rpc/rpc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
export default class {
constructor (url, delegateHash) {
this.url = url
this.delegateHash = delegateHash
}
setSnapshotNumber (snapshotNumber) {
this.snapshotNumber = snapshotNumber
}
async setCycle (cycle) {
if (cycle === 'head') {
await this.setCycleToHead()
} else {
this.cycle = cycle
}
}
async getHeadCycle () {
return this.getCycleFromFirstBlock(await this.getFirstBlockOfCycle('head'))
}
async setCycleToHead () {
this.cycle = await this.getHeadCycle()
}
async sendRequest (endpoint) {
const response = await fetch(this.url + endpoint)
return response.json()
}
async getFirstBlockOfCycle (cycle) {
const levelsInCurrentCycle = await this.sendRequest('/chains/main/blocks/' + cycle + '/helpers/levels_in_current_cycle')
return levelsInCurrentCycle.first
}
async getCycleData () {
return this.sendRequest('/chains/main/blocks/' + await this.snapshotBlockNumber() + '/context/delegates/' + this.delegateHash + '/')
}
async getSnapshotDelegateContractIds () {
const delegateData = await this.getCycleData()
return delegateData.delegated_contracts
}
async getContractsData (contractIds) {
let contractsData = []
for (let i = 0; i < contractIds.length; i++) {
const contractId = contractIds[i]
contractsData.push({
contractId: contractId,
contractData: await this.getContractData(contractId)
})
}
return contractsData
}
async getContractData (contractId) {
return this.sendRequest('/chains/main/blocks/' + await this.snapshotBlockNumber() + '/context/contracts/' + contractId)
}
getDelegationCycle () {
// Delegation Snapshot is taken 7 cycles ago.
return parseFloat(this.cycle - 7)
}

/*
The rpc returns the snapshot number of the delegated cycle.
For example if the 6th snapshot of the cycle is used, '6' is returned by the rpc.
To get the snapshot block, calculate the total number of snapshots taken until the delegated cycle
and then add the snpashot number of the delegated cycle to that number then multiply by 256 to get the block number.
Example:
For baking cycle 65, the snapshot used is taken in cycle 58.
The rpc returns the snapshot of the cycle that was used: 6.
First block of Cycle 58: 4096*58 = 237568
Total snapshots until first block of cycle 58: 237568 / 256 = 928
Add the snapshot number (+1 to include very first snapshot) from the rpc to the total snapshots until start of cycle: (928 + 6) + 1 = 935
935th snapshot is used for cycle 58, get the block number: 935*256 = 239360
Snapshotted Block Number = 23960
*/
async snapshotBlockNumber () {
const snapshotNumberOfCycle = parseFloat(this.snapshotNumber)
const firstBlockOfSnapshotCycle = parseFloat(this.getDelegationCycle()) * 4096
const totalNumberOfSnapshots = firstBlockOfSnapshotCycle / 256
const totalSnapshotNumber = totalNumberOfSnapshots + snapshotNumberOfCycle + 1
const snapshotBlockNumber = parseFloat(totalSnapshotNumber) * 256
return snapshotBlockNumber
}
getCycleFromFirstBlock (blockNumber) {
// current cycle = most recent completed cycle
// - 1 to get the last completed cycle
return parseFloat(Math.floor(blockNumber / 4096) - 1)
}
}
12 changes: 12 additions & 0 deletions src/services/utils/tezos.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default class {
formatTezosNumericalData (data) {
let formattedValue = (parseFloat(parseFloat(data) / 1000000))
if (formattedValue > 1000000) {
return (formattedValue / 1000000) + 'Mꜩ'
} else if (formattedValue > 1000) {
return (formattedValue / 1000).toFixed(2) + ' Kꜩ'
} else {
return formattedValue + ' ꜩ'
}
}
}
4 changes: 3 additions & 1 deletion src/store/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import Vue from 'vue'
import Vuex from 'vuex'
import user from './modules/user'
import snapshot from './modules/snapshot'

Vue.use(Vuex)

export default new Vuex.Store({
modules: {
user
user,
snapshot
}
})
25 changes: 25 additions & 0 deletions src/store/modules/snapshot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// initial state
const state = {
data: null
}

// actions
const actions = {
loadFromConfigFile ({ commit }) {
commit('setSnapshotData', require('../../../static/snapshots.json'))
}
}

// mutations
const mutations = {
setSnapshotData (state, data) {
state.data = data
}
}

export default {
namespaced: true,
state,
actions,
mutations
}
15 changes: 15 additions & 0 deletions static/snapshots.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"60": 7,
"61": 7,
"62": 12,
"63": 7,
"64": 10,
"65": 6,
"66": 7,
"67": 11,
"68": 11,
"69": 13,
"70": 7,
"71": 1,
"72": 12
}

0 comments on commit 2e8e683

Please sign in to comment.