Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Peng/123 vuex tests #333

Merged
merged 20 commits into from
Jan 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 0 additions & 51 deletions app/src/renderer/components/monitor/BlockchainSelect.vue

This file was deleted.

57 changes: 0 additions & 57 deletions app/src/renderer/components/monitor/BlockchainSelectModal.vue

This file was deleted.

32 changes: 15 additions & 17 deletions app/src/renderer/components/wallet/PageSend.vue
Original file line number Diff line number Diff line change
Expand Up @@ -105,27 +105,25 @@ export default {
await this.walletSend({
fees: { denom, amount: 0 },
to: address,
amount: [{ denom, amount }],
cb: (err) => {
this.sending = false
if (err) {
this.$store.commit('notifyError', {
title: 'Error Sending',
body: `An error occurred while trying to send: "${err.message}"`
})
return
}
this.$store.commit('notify', {
title: 'Successfully Sent',
body: `Successfully sent ${amount} ${denom.toUpperCase()} to ${address}`
})
amount: [{ denom, amount }]
}).then(() => {
this.sending = false
this.$store.commit('notify', {
title: 'Successfully Sent',
body: `Successfully sent ${amount} ${denom.toUpperCase()} to ${address}`
})

// resets send transaction form
this.resetForm()
this.resetForm()

// refreshes user transaction history
this.$store.dispatch('queryWalletHistory')
}
this.$store.dispatch('queryWalletHistory')
}, err => {
Copy link
Collaborator

@jbibla jbibla Jan 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is err => { ... } the same as .catch()?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but the then(_, onReject) will get called instead of .catchif available. See: https://codepen.io/faboweb/pen/WdmRoO

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool! thanks for the example. still not sure when to use one vs the other...?

this.sending = false
this.$store.commit('notifyError', {
title: 'Error Sending',
body: `An error occurred while trying to send: "${err.message}"`
})
})
},
...mapActions(['walletSend'])
Expand Down
9 changes: 0 additions & 9 deletions app/src/renderer/vuex/actions.js

This file was deleted.

28 changes: 11 additions & 17 deletions app/src/renderer/vuex/modules/delegation.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,23 @@ export default ({ commit }) => {
let bond = (await axios.get('http://localhost:8998/query/stake/delegator/' + address + '/' + pubkey)).data.data
commit('setCommittedDelegation', {candidateId: bond.PubKey.data, value: bond.Shares})
},
async walletDelegate ({ dispatch }, args) {
walletDelegate ({ dispatch }, args) {
args.type = 'buildDelegate'
await dispatch('walletTx', args)
return dispatch('sendTx', args)
},
async walletUnbond ({ dispatch }, args) {
walletUnbond ({ dispatch }, args) {
args.type = 'buildUnbond'
await dispatch('walletTx', args)
return dispatch('sendTx', args)
},
async submitDelegation ({ state, dispatch }, delegation) {
for (let delegate of delegation.delegates) {
submitDelegation ({ state, dispatch }, delegation) {
return Promise.all(delegation.delegates.map(delegate => {
let candidateId = delegate.delegate.pub_key.data
let currentlyDelegated = state.committedDelegates[candidateId] || 0
let amountChange = delegate.atoms - currentlyDelegated
let action = amountChange > 0 ? 'walletDelegate' : 'walletUnbond'

// skip if no change
if (amountChange === 0) continue
if (amountChange === 0) return null

// bonding takes a 'coin' object, unbond just takes a number
let amount
Expand All @@ -84,17 +84,11 @@ export default ({ commit }) => {
amount = Math.abs(amountChange)
}

await new Promise((resolve, reject) => {
dispatch(action, {
amount,
pub_key: delegate.delegate.pub_key,
cb: (err, res) => {
if (err) return reject(err)
resolve(res)
}
})
return dispatch(action, {
amount,
pub_key: delegate.delegate.pub_key
})
}
}).filter(x => x !== null))
}
}

Expand Down
1 change: 1 addition & 0 deletions app/src/renderer/vuex/modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default (opts) => ({
node: require('./node.js').default(opts),
notifications: require('./notifications.js').default(opts),
proposals: require('./proposals.js').default(opts),
send: require('./send.js').default(opts),
user: require('./user.js').default(opts),
validators: require('./validators.js').default(opts),
wallet: require('./wallet.js').default(opts)
Expand Down
10 changes: 9 additions & 1 deletion app/src/renderer/vuex/modules/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default function ({ node }) {

const state = {
nodeIP,
stopConnecting: false,
connected: false,
lastHeader: {
height: 0,
Expand All @@ -16,6 +17,9 @@ export default function ({ node }) {
}

const mutations = {
stopConnecting (state, stop) {
state.stopConnecting = stop
},
setConnected (state, connected) {
state.connected = connected
}
Expand All @@ -27,11 +31,15 @@ export default function ({ node }) {
dispatch('maybeUpdateValidators', header)
},
async reconnect ({commit, dispatch}) {
if (state.stopConnecting) return

commit('setConnected', false)
await node.rpcReconnect()
dispatch('nodeSubscribe')
},
nodeSubscribe ({commit, dispatch}) {
if (state.stopConnecting) return

// the rpc socket can be closed before we can even attach a listener
// so we remember if the connection is open
// we handle the reconnection here so we can attach all these listeners on reconnect
Expand All @@ -56,7 +64,7 @@ export default function ({ node }) {
chain_id: status.node_info.network
})
})
node.rpc.subscribe({ event: 'NewBlockHeader' }, (err, event) => {
node.rpc.subscribe({ query: 'tm.event = \'NewBlockHeader\'' }, (err, event) => {
console.log(err)
// if (err) return console.error('error subscribing to headers', err)
// commit('setConnected', true)
Expand Down
106 changes: 106 additions & 0 deletions app/src/renderer/vuex/modules/send.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
export default ({ commit, node }) => {
let state = {
// account nonce number, used to prevent replay attacks
nonce: 0,
// queue of transactions to be sent
queue: [],
sending: false
}

const mutations = {
queueSend (state, sendReq) {
state.queue.push(sendReq)
},
shiftSendQueue (state) {
state.queue = state.queue.slice(1)
},
setSending (state, sending) {
state.sending = sending
},
setNonce (state, nonce) {
state.nonce = nonce
}
}

let actions = {
// queries for our account's nonce
async queryNonce ({ commit }, address) {
let res = await node.queryNonce(address)
if (!res) return
commit('setNonce', res.data)
},

// builds, signs, and broadcasts a tx of any type
sendTx ({ state, dispatch, commit, rootState }, args) {
// wait until the current send operation is done
if (state.sending) {
args.done = new Promise((resolve, reject) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx for picking up my promise over callback solution

args.resolve = resolve
args.reject = reject
})
commit('queueSend', args)
return args.done
}

return new Promise((resolve, reject) => {
commit('setSending', true)

// once done, do next send in queue
function done (err, res) {
commit('setSending', false)

if (state.queue.length > 0) {
// do next send
let send = state.queue[0]
commit('shiftSendQueue')
dispatch('sendTx', send)
}

if (err) {
reject(err)
if (args.reject) args.reject(err)
} else {
resolve(res)
if (args.resolve) args.resolve(res)
}
}

args.sequence = state.nonce + 1
args.from = {
chain: '',
app: 'sigs',
addr: rootState.wallet.key.address
};

(async function () {
// build tx
let tx = await node[args.type](args)

// sign tx
let signedTx = await node.sign({
name: rootState.user.account,
password: rootState.user.password,
tx
})

// broadcast tx
let res = await node.postTx(signedTx)

// check response code
if (res.check_tx.code || res.deliver_tx.code) {
let message = res.check_tx.log || res.deliver_tx.log
throw new Error('Error sending transaction: ' + message)
}
})().then(() => {
commit('setNonce', state.nonce + 1)
done(null, args)
dispatch('queryWalletBalances')
}, (err) => {
done(err || Error('Error sending transaction'))
})
})
}
}

return { state, mutations, actions }
}
4 changes: 0 additions & 4 deletions app/src/renderer/vuex/modules/validators.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
export default ({ commit, node }) => {
const state = {
blockchainName: '',
validators: {},
validatorHash: null
}

const mutations = {
setValidatorBlockchainName (state, name) {
state.blockchainName = name
},
setValidators (state, validators) {
state.validators = validators
},
Expand Down
Loading