-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from rohitverma007/rewards
Rewards
- Loading branch information
Showing
9 changed files
with
296 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + ' ꜩ' | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |