Skip to content

Commit

Permalink
Fetch candidate list from REST API
Browse files Browse the repository at this point in the history
  • Loading branch information
mappum committed Nov 27, 2017
1 parent 076c3db commit 649df36
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 67 deletions.
14 changes: 7 additions & 7 deletions app/src/renderer/components/staking/CardCandidate.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ transition(name='ts-card-candidate'): div(:class='cssClass')
i.fa.fa-check-square-o(v-if='inCart' @click='rm(candidate)')
i.fa.fa-square-o(v-else @click='add(candidate)')
router-link(:to="{ name: 'candidate', params: { candidate: candidate.id }}")
| {{ candidate.keybaseID }}
| {{ candidate.pubkey.data }}
.value.atoms.num.bar
span {{ num.prettyInt(candidate.atoms) }}
span {{ num.prettyInt(candidate.voting_power) }}
.bar(:style='atomsCss')
.value.atoms.num.bar.delegated(v-if='signedIn')
span {{ num.prettyInt(candidate.computed.delegatedAtoms) }}
span {{ num.prettyInt(candidate.computed.delegatedCoins) }}
.bar(:style='delegatedAtomsCss')
.value.delegators.num
.value.shares.num
span
i.fa.fa-user
| {{ num.prettyInt(candidate.delegators) }}
i.fa.pie-chart
| {{ num.prettyInt(candidate.shares) }}
menu(v-if='signedIn')
btn(theme='cosmos' v-if='inCart'
icon='times' value='Remove' size='sm' @click.native='rm(candidate)')
Expand Down Expand Up @@ -152,7 +152,7 @@ export default {
span
display block
white-space nowrap
text-overflow ellipsis
overflow hidden
Expand Down
12 changes: 2 additions & 10 deletions app/src/renderer/components/staking/PageCandidate.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,9 @@ page(:title='candidate.id')
router-link(v-if='isMe' to='/nominate') Edit
part(title="Description")
text-block(:content="candidate.description")
part(title="Server Details")
text-block(:content="candidate.serverDetails")
part(title="Candidate Details")
list-item(dt='Commission' :dd='candidate.commissionPercent')
list-item(dt='Country' :dd='countryName(candidate.country)')
list-item(dt='Website' :dd='candidate.website')
list-item(dt='IP Address' :dd='candidate.ipAddress')
part(title="Staking Details")
list-item(dt='Atoms' :dd='candidate.atoms')
list-item(dt='Delegators' :dd='candidate.computed.delegators')
list-item(dt='Slashes' :dd='candidate.computed.slashes.length')
list-item(dt='Voting Power' :dd='candidate.voting_power')
list-item(dt='Shares' :dd='candidate.shares')
</template>

<script>
Expand Down
4 changes: 2 additions & 2 deletions app/src/renderer/components/staking/PageCandidates.vue
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ export default {
{ id: 2, title: 'Delegated', value: 'atoms', initial: true }
]
if (this.user.signedIn) {
props.push({ id: 2, title: 'Delegated (Yours)', value: 'computed.delegatedAtoms' })
props.push({ id: 2, title: 'Delegated (Yours)', value: 'voting_power' })
}
props.push({ id: 3, title: 'Delegators', value: 'computed.delegators' })
props.push({ id: 3, title: 'Shares', value: 'shares' })
return {
property: 'atoms',
order: 'desc',
Expand Down
2 changes: 1 addition & 1 deletion app/src/renderer/vuex/getters.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const nodeIP = state => state.node.nodeIP
export const connected = state => state.node.connected
export const lastHeader = state => state.node.lastHeader

export const candidates = state => state.candidates.list
export const candidates = state => state.candidates
export const shoppingCart = state => state.shoppingCart.candidates
export const user = state => state.user
export const config = state => state.config
Expand Down
67 changes: 20 additions & 47 deletions app/src/renderer/vuex/modules/candidates.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,40 @@
import { PubKey } from 'tendermint-crypto'

const CANDIDATE_INTERVAL = 1000 // update every second
function pubkeyToString (pubkey) {
let type = pubkey.type === 'ed25519' ? '01' : '02'
return type + pubkey.data
}

export default ({ commit, node }) => {
const state = {
list: [],
map: {}
}
export default ({ commit, dispatch, node }) => {
const state = []

const mutations = {
updateCandidate (state, candidate) {
let pubkey = PubKey.encode(candidate.validatorPubKey).toString('base64')

// TODO: replace hardcoded defaults with real data
candidate.computed = {
// delegators: 10,
// atoms: candidate.ownCoinsBonded + candidate.coindBonded,
pubkey,
slashes: []
}
candidate.atoms = candidate.ownCoinsBonded + candidate.coindBonded

state.list.splice(state.list.indexOf(
state.list.find(c => c.id === candidate.id)), 1, candidate)

state.map[pubkey] = candidate

// commit('notify', { title: 'Nomination Updated',
// body: 'You have successfuly updated your candidacy.' })
},
addCandidate (state, candidate) {
let pubkey = PubKey.encode(candidate.validatorPubKey).toString('base64')
if (state.map[pubkey] != null) return
candidate.id = pubkeyToString(candidate.pubkey)

candidate.id = pubkey
candidate.computed = {
// delegators: 10,
// atoms: candidate.ownCoinsBonded + candidate.coindBonded,
pubkey,
slashes: []
// return if we already have this candidate
for (let existingCandidate of state) {
if (existingCandidate.id === candidate.id) return
}
candidate.atoms = candidate.ownCoinsBonded + candidate.coinsBonded
state.list.push(candidate)
state.map[pubkey] = candidate

console.log('new candidate', candidate)
state.push(candidate)
}
}

const actions = {
async getCandidates ({ commit }) {
return // TODO: replace with REST-based client
// let candidates = await node.delegationGame.getCandidates()
// candidates.forEach((c) => commit('addCandidate', c))
},
startCandidateInterval ({ dispatch }) {
// TODO: use tx events instead of polling
setInterval(() => dispatch('getCandidates'), CANDIDATE_INTERVAL)
let candidatePubkeys = (await node.candidates()).data
for (let pubkey of candidatePubkeys) {
dispatch('getCandidate', pubkey)
}
},
async nominateCandidate ({ commit }, candidate) {
await node.delegationGame.nominate(candidate, node.wallet)
async getCandidate ({ commit }, pubkey) {
let candidate = (await node.candidate(pubkeyToString(pubkey))).data
commit('addCandidate', candidate)
}
}

setTimeout(() => dispatch('getCandidates'), 1000)

return { state, mutations, actions }
}

0 comments on commit 649df36

Please sign in to comment.