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

Feature/market transactions better handling #58

Merged
merged 3 commits into from
Apr 4, 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
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