Skip to content

Commit

Permalink
Merge pull request #58 from TrustyFund/feature/market-transactions-be…
Browse files Browse the repository at this point in the history
…tter-handling

Feature/market transactions better handling
  • Loading branch information
youaresofunny authored Apr 4, 2018
2 parents 76d844d + 6aaa4f3 commit 0b6afda
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
18 changes: 14 additions & 4 deletions src/actions/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const fetchComissions = async ({ commit }) => {

export const createOrdersFromDistribution = async (store) => {
const { commit, rootGetters, getters } = store;
if (getters.areTransactionsProcessing) return;
const distribution = getters.getPendingDistribution;
if (!distribution) return;
const userId = rootGetters['account/getAccountUserId'];
Expand Down Expand Up @@ -57,7 +58,6 @@ export const createOrdersFromDistribution = async (store) => {
}
});

// const update = calcPortfolioDistributionChange(baseBalances, distribution);

const orders = API.Market.generateOrders({
userId,
Expand All @@ -67,6 +67,8 @@ export const createOrdersFromDistribution = async (store) => {
});
console.log(orders);

// if sell finished, only update buy orders

commit(types.UPDATE_PENDING_ORDERS, { orders });
};

Expand All @@ -81,12 +83,20 @@ export const removePendingDistribution = (store) => {
commit(types.REMOVE_PENDING_DISTRIBUTION);
};


export const handleOrdersError = (store) => {
const { commit } = store;
commit(types.PROCESS_PENDING_ORDERS_ERROR);
createOrdersFromDistribution(store);
};


export const processPendingOrders = async (store) => {
const { getters, commit, rootGetters } = store;
commit(types.PROCESS_PENDING_ORDERS_REQUEST);
const keys = rootGetters['account/getKeys'];
if (!keys) {
commit(types.PROCESS_PENDING_ORDERS_ERROR);
handleOrdersError(store);
return {
success: false,
error: 'Account is locked'
Expand All @@ -98,7 +108,7 @@ export const processPendingOrders = async (store) => {
orders: pendingOrders.sellOrders,
keys });
if (!sellResult.success) {
commit(types.PROCESS_PENDING_ORDERS_ERROR);
handleOrdersError(store);
return {
success: false,
error: sellResult.error
Expand All @@ -113,7 +123,7 @@ export const processPendingOrders = async (store) => {
});
console.log(buyResult);
if (!buyResult.success) {
commit(types.PROCESS_PENDING_ORDERS_ERROR);
handleOrdersError(store);
return {
success: false,
error: buyResult.error
Expand Down
1 change: 1 addition & 0 deletions src/modules/market.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const actions = {
assetsIds.forEach(id => {
console.log('unsubscribing: ', id);
API.Market.unsubscribeFromExchangeRate(id);
API.Market.unsubscribeFromMarkets();
});
},

Expand Down
15 changes: 12 additions & 3 deletions src/modules/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ import * as actions from '../actions/transactions';

const initialState = {
pendingDistributionUpdate: null,
pendingOrders: {},
pendingOrders: {
sellOrders: [],
buyOrders: []
},
pendingTransfer: false,
pending: false,
error: null,
transactionsProcessing: false,
sellOrdersProcessed: false,
fees: {
order: {
fee: 0
Expand All @@ -23,7 +27,8 @@ const initialState = {

const getters = {
getPendingOrders: state => state.pendingOrders,
hasPendingOrders: state => state.pendingOrders.sellOrders || state.pendingOrders.buyOrders,
hasPendingOrders: state => state.pendingOrders.sellOrders.length
|| state.pendingOrders.buyOrders.length,
getPendingDistribution: state => state.pendingDistributionUpdate,
hasPendingTransfer: state => state.pendingTransfer !== false,
areTransactionsProcessing: state => state.transactionsProcessing,
Expand Down Expand Up @@ -52,14 +57,17 @@ const mutations = {
state.transactionsProcessing = false;
},
[types.UPDATE_PENDING_ORDERS](state, { orders }) {
if (state.sellOrdersProcessed) orders.sellOrders = [];
Vue.set(state, 'pendingOrders', orders);
},
[types.SET_PENDING_DISTRIBUTION](state, { distribution }) {
state.pendingDistributionUpdate = distribution;
},
[types.REMOVE_PENDING_DISTRIBUTION](state) {
state.pendingDistributionUpdate = null;
state.pendingOrders = {};
state.pendingOrders.sellOrders = [];
state.pendingOrders.buyOrders = [];
state.sellOrdersProcessed = false;
},
[types.PROCESS_PENDING_ORDERS_REQUEST](state) {
state.transactionsProcessing = true;
Expand All @@ -78,6 +86,7 @@ const mutations = {
},
[types.PROCESS_PENDING_ORDERS_SELL_COMPLETE](state) {
state.pendingOrders.sellOrders = [];
state.sellOrdersProcessed = true;
}
};

Expand Down

0 comments on commit 0b6afda

Please sign in to comment.