-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
32 additions
and
67 deletions.
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
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
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,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 } | ||
} |