From ad8063cac9f2f6b0d6737a96fc64878dc7a49433 Mon Sep 17 00:00:00 2001 From: Anton Date: Thu, 5 Apr 2018 19:28:54 +0300 Subject: [PATCH 1/9] Change config, reset pendingTransfer --- config.js | 3 +-- src/modules/transactions.js | 1 + 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/config.js b/config.js index b0b9acb..d5bd214 100644 --- a/config.js +++ b/config.js @@ -38,8 +38,7 @@ const config = { '1.3.121', '1.3.861', '1.3.113', - '1.3.0', - '1.3.103' + '1.3.0' ] }; diff --git a/src/modules/transactions.js b/src/modules/transactions.js index 7c15af8..7065a87 100644 --- a/src/modules/transactions.js +++ b/src/modules/transactions.js @@ -55,6 +55,7 @@ const mutations = { }, [types.TRANSFER_ASSET_COMPLETE](state) { state.transactionsProcessing = false; + state.pendingTransfer = false; }, [types.UPDATE_PENDING_ORDERS](state, { orders }) { if (state.sellOrdersProcessed) orders.sellOrders = []; From 2f5f7241f4f23c4c093dbc28138b1d6b3645cae7 Mon Sep 17 00:00:00 2001 From: Anton Date: Thu, 5 Apr 2018 22:03:05 +0300 Subject: [PATCH 2/9] Add new history methods --- src/modules/market2.js | 83 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 src/modules/market2.js diff --git a/src/modules/market2.js b/src/modules/market2.js new file mode 100644 index 0000000..6074b29 --- /dev/null +++ b/src/modules/market2.js @@ -0,0 +1,83 @@ +import Vue from 'vue'; +import config from '../../config.js'; +import API from '../services/api'; + +const FETCH_MARKET_HISTORY_REQUEST = 'FETCH_MARKET_HISTORY_REQUEST'; +const FETCH_MARKET_HISTORY_COMPLETE = 'FETCH_MARKET_HISTORY_COMPLETE'; +const FETCH_MARKET_HISTORY_ERROR = 'FETCH_MARKET_HISTORY_ERROR'; + +const FETCH_ASSETS_HISTORY_REQUEST = 'FETCH_ASSETS_HISTORY_REQUEST'; +const FETCH_ASSETS_HISTORY_COMPLETE = 'FETCH_ASSETS_HISTORY_COMPLETE'; +const FETCH_ASSETS_HISTORY_ERROR = 'FETCH_ASSETS_HISTORY_ERROR'; + + +const initialState = { + systemBaseId: config.defaultTradingBase, + pending: false, + error: false, + markets: {}, + history: {} +}; + +const actions = { + fetchMarketHistory: async ({ commit }, { baseId, assetId, days }) => { + commit(FETCH_MARKET_HISTORY_REQUEST, { baseId }); + const prices = await API.Assets.fetchPriceHistory(baseId, assetId, days); + if (!prices) { + commit(FETCH_MARKET_HISTORY_ERROR); + return false; + } + commit(FETCH_MARKET_HISTORY_COMPLETE, { baseId, assetId, prices }); + return true; + }, + fetchAssetsHistory: (store, { assetsIds, baseId, days }) => { + const { commit } = store; + commit(FETCH_ASSETS_HISTORY_REQUEST); + + Promise.all(assetsIds.map(async (assetId) => { + const prices = await actions.fetchMarketHistory(store, { baseId, assetId, days }); + console.log('Prices', prices); + if (!prices) throw new Error('error market history'); + })).then(() => { + commit(FETCH_ASSETS_HISTORY_COMPLETE); + }).catch(() => { + commit(FETCH_ASSETS_HISTORY_ERROR); + }); + } + +}; + +const mutations = { + [FETCH_MARKET_HISTORY_REQUEST](state, { baseId }) { + if (state.history[baseId] === undefined) { + state.history[baseId] = {}; + } + state.pending = true; + }, + [FETCH_MARKET_HISTORY_COMPLETE](state, { baseId, assetId, prices }) { + state.pending = false; + Vue.set(state.history[baseId], assetId, prices); + console.log('COMPLETE'); + }, + [FETCH_MARKET_HISTORY_ERROR](state) { + state.pending = false; + state.error = true; + }, + [FETCH_ASSETS_HISTORY_REQUEST](state) { + state.pending = true; + }, + [FETCH_ASSETS_HISTORY_COMPLETE](state) { + state.pending = false; + }, + [FETCH_ASSETS_HISTORY_ERROR](state) { + state.pending = false; + state.error = true; + } +}; + +export default { + state: initialState, + actions, + mutations, + namespaced: true +}; From 6c50a512d8e33314245a6933665da38a86838553 Mon Sep 17 00:00:00 2001 From: Anton Date: Thu, 5 Apr 2018 22:03:27 +0300 Subject: [PATCH 3/9] Remove full asset object from get history methods --- src/services/api/assets.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/services/api/assets.js b/src/services/api/assets.js index b2a94c7..53103b0 100644 --- a/src/services/api/assets.js +++ b/src/services/api/assets.js @@ -17,11 +17,11 @@ const fetch = async (assets) => { /** * Returns prices bistory between base and quote assets from the last specified number of days - * @param {Object} base - base asset object - * @param {Object} quote - quote asset object + * @param {String} base - base id + * @param {String} quote - quote id * @param {number} days - number of days */ -const fetchPriceHistory = async (base, quote, days) => { +const fetchPriceHistory = async (baseId, quoteId, days) => { try { const bucketSize = 3600; const endDate = new Date(); @@ -30,7 +30,7 @@ const fetchPriceHistory = async (base, quote, days) => { const startDateISO = startDate.toISOString().slice(0, -5); const history = await Apis.instance().history_api().exec( 'get_market_history', - [base.id, quote.id, bucketSize, startDateISO, endDateISO] + [baseId, quoteId, bucketSize, startDateISO, endDateISO] ); // const prices = utils.formatPrices(utils.getPrices(history), base, quote); const prices = utils.getPrices(history); From 3ac3f57038dae1d5b6143a0de38aa614d99e0a2f Mon Sep 17 00:00:00 2001 From: Anton Date: Thu, 5 Apr 2018 22:03:54 +0300 Subject: [PATCH 4/9] add defaultTradingBase to config --- config.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/config.js b/config.js index d5bd214..21474fb 100644 --- a/config.js +++ b/config.js @@ -30,7 +30,7 @@ const config = { defaultNode: 'wss://bitshares.openledger.info/ws' }, defaultAssetsNames: ['USD', 'OPEN.BTC', 'OPEN.ETH', 'OPEN.DASH', 'OPEN.LTC', - 'OPEN.EOS', 'OPEN.STEEM', 'BTS', 'TRUSTY'], + 'OPEN.EOS', 'OPEN.STEEM', 'BTS', 'TRUSTY', 'CNY'], referrer: 'trfnd', removePrefix: 'OPEN.', faucetUrl: 'https://faucet.trusty.fund/signup', @@ -39,7 +39,8 @@ const config = { '1.3.861', '1.3.113', '1.3.0' - ] + ], + defaultTradingBase: '1.3.0' }; export default config; From 2692c337001a9848fae876c84d9a5ee365d55b23 Mon Sep 17 00:00:00 2001 From: Anton Date: Thu, 5 Apr 2018 23:13:29 +0300 Subject: [PATCH 5/9] Change history to market2 --- src/actions/transactions.js | 7 ++++--- src/index.js | 2 ++ src/modules/market.js | 2 -- src/modules/market2.js | 25 +++++++++++++++++++++++-- 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/actions/transactions.js b/src/actions/transactions.js index 76e34ee..a105cb3 100644 --- a/src/actions/transactions.js +++ b/src/actions/transactions.js @@ -35,7 +35,8 @@ export const createOrdersFromDistribution = async (store) => { if (!distribution) return; const userId = rootGetters['account/getAccountUserId']; const balances = rootGetters['account/getCurrentUserBalances']; - const history = rootGetters['market/getMarketHistory']; + const history = rootGetters['market2/getMarketHistory']; + const tradingBaseId = rootGetters['market2/getSystemBaseId']; const defaultAssetsIds = rootGetters['assets/getDefaultAssetsIds']; @@ -53,10 +54,10 @@ export const createOrdersFromDistribution = async (store) => { const baseBalances = {}; assetsIds.forEach(id => { - if (id === '1.3.0') { + if (id === tradingBaseId) { baseBalances[id] = combinedBalances[id]; } else { - baseBalances[id] = combinedBalances[id] * history[id].last; + baseBalances[id] = combinedBalances[id] * history(tradingBaseId)[id].last; } }); diff --git a/src/index.js b/src/index.js index 0660dc0..483960f 100644 --- a/src/index.js +++ b/src/index.js @@ -6,6 +6,7 @@ import transactions from './modules/transactions'; import operations from './modules/operations'; import market from './modules/market'; import openledger from './modules/openledger'; +import market2 from './modules/market2'; export default function install(store) { store.registerModule('connection', connection); @@ -16,4 +17,5 @@ export default function install(store) { store.registerModule('operations', operations); store.registerModule('market', market); store.registerModule('openledger', openledger); + store.registerModule('market2', market2); } diff --git a/src/modules/market.js b/src/modules/market.js index 34b8419..dac4151 100644 --- a/src/modules/market.js +++ b/src/modules/market.js @@ -89,7 +89,6 @@ const getters = { }; }, getMarketHistory: state => state.history, - isFetching: state => state.pending, isError: state => state.error, isSubscribed: state => state.subscribed }; @@ -97,7 +96,6 @@ const getters = { const initialState = { history: {}, days: 7, - pending: false, error: false, baseAssetId: null, subscribed: false, diff --git a/src/modules/market2.js b/src/modules/market2.js index 6074b29..2c60aad 100644 --- a/src/modules/market2.js +++ b/src/modules/market2.js @@ -36,7 +36,6 @@ const actions = { Promise.all(assetsIds.map(async (assetId) => { const prices = await actions.fetchMarketHistory(store, { baseId, assetId, days }); - console.log('Prices', prices); if (!prices) throw new Error('error market history'); })).then(() => { commit(FETCH_ASSETS_HISTORY_COMPLETE); @@ -56,8 +55,8 @@ const mutations = { }, [FETCH_MARKET_HISTORY_COMPLETE](state, { baseId, assetId, prices }) { state.pending = false; + state.history = { ...state.history }; Vue.set(state.history[baseId], assetId, prices); - console.log('COMPLETE'); }, [FETCH_MARKET_HISTORY_ERROR](state) { state.pending = false; @@ -75,9 +74,31 @@ const mutations = { } }; +const getters = { + getMarketHistory: state => baseId => state.history[baseId] || {}, + getSystemBaseId: state => state.systemBaseId, + getAssetMultiplier: state => { + return (assetId) => { + const baseId = state.systemBaseId; + if (!state.history[baseId] || !state.history[baseId][assetId]) { + return { + first: 0, + last: 0 + }; + } + return { + first: 1 / state.history[baseId][assetId].first, + last: 1 / state.history[baseId][assetId].last + }; + }; + }, + isError: state => state.error, +}; + export default { state: initialState, actions, mutations, + getters, namespaced: true }; From 9d91abe93ff77309caeafbfde9d2b7663eb15651 Mon Sep 17 00:00:00 2001 From: Anton Date: Fri, 6 Apr 2018 00:19:56 +0300 Subject: [PATCH 6/9] Add repeating methods --- src/modules/market2.js | 58 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 55 insertions(+), 3 deletions(-) diff --git a/src/modules/market2.js b/src/modules/market2.js index 2c60aad..ee8a37f 100644 --- a/src/modules/market2.js +++ b/src/modules/market2.js @@ -10,10 +10,18 @@ const FETCH_ASSETS_HISTORY_REQUEST = 'FETCH_ASSETS_HISTORY_REQUEST'; const FETCH_ASSETS_HISTORY_COMPLETE = 'FETCH_ASSETS_HISTORY_COMPLETE'; const FETCH_ASSETS_HISTORY_ERROR = 'FETCH_ASSETS_HISTORY_ERROR'; +const SUBSCRIBE_TO_EXCHANGE_RATE = 'SUBSCRIBE_TO_EXCHANGE_RATE'; + +const UNSUB_FROM_MARKET_COMPLETE = 'UNSUB_FROM_MARKET_COMPLETE'; + +const SUB_TO_BALANCE_MARKETS_COMPLETE = 'SUB_TO_MARKET_COMPLETE'; + +const UPDATE_MARKET_PRICE = 'UPDATE_MARKET_PRICE'; const initialState = { systemBaseId: config.defaultTradingBase, pending: false, + subscribed: false, error: false, markets: {}, history: {} @@ -30,7 +38,7 @@ const actions = { commit(FETCH_MARKET_HISTORY_COMPLETE, { baseId, assetId, prices }); return true; }, - fetchAssetsHistory: (store, { assetsIds, baseId, days }) => { + fetchAssetsHistory: (store, { baseId, assetsIds, days }) => { const { commit } = store; commit(FETCH_ASSETS_HISTORY_REQUEST); @@ -42,8 +50,37 @@ const actions = { }).catch(() => { commit(FETCH_ASSETS_HISTORY_ERROR); }); - } + }, + subscribeToExchangeRate: async (store, { baseId, assetId, balance }) => { + const { commit } = store; + const market = API.Market[baseId]; + await market.subscribeToExchangeRate(assetId, balance, (id, baseAmount) => { + if (!baseAmount) return; + const price = baseAmount / balance; + commit(UPDATE_MARKET_PRICE, { baseId, assetId: id, price }); + + // WTF THIS TYT DELAET? @roma219 + store.dispatch('transactions/createOrdersFromDistribution', null, { root: true }); + console.log(assetId + ' new bts amount: : ' + baseAmount); + }); + console.log('SUBSCRIBED TO : ' + assetId + ' : ' + balance); + }, + subscribeToExchangeRates(store, { baseId, balances }) { + const { commit } = store; + const assetsIds = Object.keys(balances); + Promise.all(assetsIds.map(assetId => { + const { balance } = balances[assetId]; + return actions.subscribeToExchangeRate(store, { baseId, assetId, balance }); + })).then(() => { + commit(SUB_TO_BALANCE_MARKETS_COMPLETE); + console.log('subscribed to markets successfully'); + }); + }, + unsubscribeFromMarket: ({ commit }, { baseId }) => { + API.Market[baseId].unsubscribeFromMarkets(); + commit(UNSUB_FROM_MARKET_COMPLETE); + } }; const mutations = { @@ -55,8 +92,8 @@ const mutations = { }, [FETCH_MARKET_HISTORY_COMPLETE](state, { baseId, assetId, prices }) { state.pending = false; + state.history[baseId][assetId] = prices; state.history = { ...state.history }; - Vue.set(state.history[baseId], assetId, prices); }, [FETCH_MARKET_HISTORY_ERROR](state) { state.pending = false; @@ -71,6 +108,19 @@ const mutations = { [FETCH_ASSETS_HISTORY_ERROR](state) { state.pending = false; state.error = true; + }, + [SUB_TO_BALANCE_MARKETS_COMPLETE](state) { + state.subscribed = true; + }, + [UPDATE_MARKET_PRICE](state, { baseId, assetId, price }) { + if (!state.history[baseId][assetId]) Vue.set(state.history[baseId], assetId, {}); + Vue.set(state.history[baseId][assetId], 'last', price); + }, + [UNSUB_FROM_MARKET_COMPLETE](state) { + state.pending = false; + }, + [SUBSCRIBE_TO_EXCHANGE_RATE](state) { + console.log('exchange sub', state); } }; @@ -93,6 +143,8 @@ const getters = { }; }, isError: state => state.error, + isPending: state => state.pending, + isSubscribed: state => state.subscribed }; export default { From 701940aae7bb8b5109c47d6a5707bcfe5a4b26b4 Mon Sep 17 00:00:00 2001 From: Anton Date: Fri, 6 Apr 2018 00:58:14 +0300 Subject: [PATCH 7/9] Show order book methods --- src/modules/market2.js | 41 +++++++++++++++++++++++++++++++++----- src/services/api/market.js | 7 +++++++ 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/src/modules/market2.js b/src/modules/market2.js index ee8a37f..59fa0b7 100644 --- a/src/modules/market2.js +++ b/src/modules/market2.js @@ -12,11 +12,14 @@ const FETCH_ASSETS_HISTORY_ERROR = 'FETCH_ASSETS_HISTORY_ERROR'; const SUBSCRIBE_TO_EXCHANGE_RATE = 'SUBSCRIBE_TO_EXCHANGE_RATE'; +const SUB_TO_MARKET_REQUEST = 'SUB_TO_MARKET_REQUEST'; +const SUB_TO_MARKET_COMPLETE = 'SUB_TO_MARKET_COMPLETE'; const UNSUB_FROM_MARKET_COMPLETE = 'UNSUB_FROM_MARKET_COMPLETE'; -const SUB_TO_BALANCE_MARKETS_COMPLETE = 'SUB_TO_MARKET_COMPLETE'; +const SUB_TO_BALANCE_MARKETS_COMPLETE = 'SUB_TO_BALANCE_MARKETS_COMPLETE'; const UPDATE_MARKET_PRICE = 'UPDATE_MARKET_PRICE'; +const UPDATE_MARKET_ORDERS = 'UPDATE_MARKET_ORDERS'; const initialState = { systemBaseId: config.defaultTradingBase, @@ -53,15 +56,20 @@ const actions = { }, subscribeToExchangeRate: async (store, { baseId, assetId, balance }) => { const { commit } = store; + commit(SUB_TO_MARKET_REQUEST, { baseId, assetId }); + const market = API.Market[baseId]; await market.subscribeToExchangeRate(assetId, balance, (id, baseAmount) => { if (!baseAmount) return; const price = baseAmount / balance; - commit(UPDATE_MARKET_PRICE, { baseId, assetId: id, price }); + commit(UPDATE_MARKET_PRICE, { baseId, assetId, price }); // WTF THIS TYT DELAET? @roma219 store.dispatch('transactions/createOrdersFromDistribution', null, { root: true }); console.log(assetId + ' new bts amount: : ' + baseAmount); + + const orders = market.getOrderBook(id); + commit(UPDATE_MARKET_ORDERS, { baseId, assetId, orders }); }); console.log('SUBSCRIBED TO : ' + assetId + ' : ' + balance); }, @@ -77,9 +85,18 @@ const actions = { console.log('subscribed to markets successfully'); }); }, + subscribeToMarket: async ({ commit }, { baseId, assetId }) => { + commit(SUB_TO_MARKET_REQUEST, { baseId, assetId }); + await API.Market[baseId].subscribeToMarket(assetId, () => { + const orders = API.Market[baseId].getOrderBook(assetId); + commit(UPDATE_MARKET_ORDERS, { baseId, assetId, orders }); + }); + const orders = API.Market[baseId].getOrderBook(assetId); + commit(SUB_TO_MARKET_COMPLETE, { baseId, assetId, orders }); + }, unsubscribeFromMarket: ({ commit }, { baseId }) => { API.Market[baseId].unsubscribeFromMarkets(); - commit(UNSUB_FROM_MARKET_COMPLETE); + commit(UNSUB_FROM_MARKET_COMPLETE, { baseId }); } }; @@ -116,8 +133,21 @@ const mutations = { if (!state.history[baseId][assetId]) Vue.set(state.history[baseId], assetId, {}); Vue.set(state.history[baseId][assetId], 'last', price); }, - [UNSUB_FROM_MARKET_COMPLETE](state) { + [SUB_TO_MARKET_REQUEST](state, { baseId, assetId }) { + state.markets[baseId] = {}; + state.markets[baseId][assetId] = {}; + }, + [SUB_TO_MARKET_COMPLETE](state, { baseId, assetId, orders }) { + state.markets[baseId][assetId] = orders; + state.markets = { ...state.markets }; + }, + [UPDATE_MARKET_ORDERS](state, { baseId, assetId, orders }) { + state.markets[baseId][assetId] = orders; + state.markets = { ...state.markets }; + }, + [UNSUB_FROM_MARKET_COMPLETE](state, { baseId }) { state.pending = false; + delete (state.markets[baseId]); }, [SUBSCRIBE_TO_EXCHANGE_RATE](state) { console.log('exchange sub', state); @@ -144,7 +174,8 @@ const getters = { }, isError: state => state.error, isPending: state => state.pending, - isSubscribed: state => state.subscribed + isSubscribed: state => state.subscribed, + getMarketOrders: state => baseId => state.markets[baseId] }; export default { diff --git a/src/services/api/market.js b/src/services/api/market.js index cfe007a..255a195 100644 --- a/src/services/api/market.js +++ b/src/services/api/market.js @@ -83,6 +83,13 @@ class Market { return false; } + getOrderBook(assetId) { + if (this.isSubscribed(assetId)) { + return this.markets[assetId].orders; + } + return false; + } + onMarketUpdate(type, object) { switch (type) { case 'newOrder': { From 2e25b1ab7908107a114e64c2760135084c8925d6 Mon Sep 17 00:00:00 2001 From: Anton Date: Fri, 6 Apr 2018 01:32:51 +0300 Subject: [PATCH 8/9] Add pending to market sub --- src/modules/market2.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/modules/market2.js b/src/modules/market2.js index 59fa0b7..17f9474 100644 --- a/src/modules/market2.js +++ b/src/modules/market2.js @@ -134,10 +134,12 @@ const mutations = { Vue.set(state.history[baseId][assetId], 'last', price); }, [SUB_TO_MARKET_REQUEST](state, { baseId, assetId }) { + state.pending = true; state.markets[baseId] = {}; state.markets[baseId][assetId] = {}; }, [SUB_TO_MARKET_COMPLETE](state, { baseId, assetId, orders }) { + state.pending = false; state.markets[baseId][assetId] = orders; state.markets = { ...state.markets }; }, From 6b03607b8c0550bcf652e44a36c43f17e47ce861 Mon Sep 17 00:00:00 2001 From: Anton Date: Fri, 6 Apr 2018 14:30:23 +0300 Subject: [PATCH 9/9] Rewrite to new actions --- src/modules/market2.js | 81 +++++++++++++++++++++--------------------- 1 file changed, 40 insertions(+), 41 deletions(-) diff --git a/src/modules/market2.js b/src/modules/market2.js index 17f9474..f8e59d2 100644 --- a/src/modules/market2.js +++ b/src/modules/market2.js @@ -10,24 +10,25 @@ const FETCH_ASSETS_HISTORY_REQUEST = 'FETCH_ASSETS_HISTORY_REQUEST'; const FETCH_ASSETS_HISTORY_COMPLETE = 'FETCH_ASSETS_HISTORY_COMPLETE'; const FETCH_ASSETS_HISTORY_ERROR = 'FETCH_ASSETS_HISTORY_ERROR'; -const SUBSCRIBE_TO_EXCHANGE_RATE = 'SUBSCRIBE_TO_EXCHANGE_RATE'; +const SUBSCRIBE_TO_EXCHANGE_RATE = 'SUBSCRIBE_TO_EXCHANGE_REQUEST'; +const UPDATE_EXCHANGE_PRICE = 'UPDATE_MARKET_PRICE'; -const SUB_TO_MARKET_REQUEST = 'SUB_TO_MARKET_REQUEST'; -const SUB_TO_MARKET_COMPLETE = 'SUB_TO_MARKET_COMPLETE'; -const UNSUB_FROM_MARKET_COMPLETE = 'UNSUB_FROM_MARKET_COMPLETE'; -const SUB_TO_BALANCE_MARKETS_COMPLETE = 'SUB_TO_BALANCE_MARKETS_COMPLETE'; - -const UPDATE_MARKET_PRICE = 'UPDATE_MARKET_PRICE'; +const SUBSCRIBE_TO_ORDERS_REQUEST = 'SUBSCRIBE_TO_ORDERS_REQUEST'; +const SUBSCRIBE_TO_ORDERS_COMPLETE = 'SUBSCRIBE_TO_ORDERS_COMPLETE'; const UPDATE_MARKET_ORDERS = 'UPDATE_MARKET_ORDERS'; +const SUBSCRIBED_TO_BALANCE_MARKETS = 'SUBSCRIBED_TO_BALANCE_MARKETS'; +const UNSUBSCRIBED_FROM_MARKET = 'UNSUBSCRIBED_FROM_MARKET'; + + const initialState = { systemBaseId: config.defaultTradingBase, pending: false, subscribed: false, error: false, - markets: {}, - history: {} + history: {}, + ordersUpdateFlags: {} }; const actions = { @@ -56,24 +57,21 @@ const actions = { }, subscribeToExchangeRate: async (store, { baseId, assetId, balance }) => { const { commit } = store; - commit(SUB_TO_MARKET_REQUEST, { baseId, assetId }); + commit(SUBSCRIBE_TO_EXCHANGE_RATE, { baseId, assetId }); const market = API.Market[baseId]; await market.subscribeToExchangeRate(assetId, balance, (id, baseAmount) => { if (!baseAmount) return; const price = baseAmount / balance; - commit(UPDATE_MARKET_PRICE, { baseId, assetId, price }); + commit(UPDATE_EXCHANGE_PRICE, { baseId, assetId, price }); // WTF THIS TYT DELAET? @roma219 store.dispatch('transactions/createOrdersFromDistribution', null, { root: true }); console.log(assetId + ' new bts amount: : ' + baseAmount); - - const orders = market.getOrderBook(id); - commit(UPDATE_MARKET_ORDERS, { baseId, assetId, orders }); }); console.log('SUBSCRIBED TO : ' + assetId + ' : ' + balance); }, - subscribeToExchangeRates(store, { baseId, balances }) { + subscribeToExchangeRates: (store, { baseId, balances }) => { const { commit } = store; const assetsIds = Object.keys(balances); @@ -81,22 +79,21 @@ const actions = { const { balance } = balances[assetId]; return actions.subscribeToExchangeRate(store, { baseId, assetId, balance }); })).then(() => { - commit(SUB_TO_BALANCE_MARKETS_COMPLETE); + commit(SUBSCRIBED_TO_BALANCE_MARKETS); console.log('subscribed to markets successfully'); }); }, - subscribeToMarket: async ({ commit }, { baseId, assetId }) => { - commit(SUB_TO_MARKET_REQUEST, { baseId, assetId }); + subscribeToMarketOrders: async ({ commit }, { baseId, assetId }) => { + commit(SUBSCRIBE_TO_ORDERS_REQUEST, { baseId, assetId }); await API.Market[baseId].subscribeToMarket(assetId, () => { - const orders = API.Market[baseId].getOrderBook(assetId); - commit(UPDATE_MARKET_ORDERS, { baseId, assetId, orders }); + console.log('UPDATE MARKET ORDERS', assetId); + commit(UPDATE_MARKET_ORDERS, { baseId, assetId }); }); - const orders = API.Market[baseId].getOrderBook(assetId); - commit(SUB_TO_MARKET_COMPLETE, { baseId, assetId, orders }); + commit(SUBSCRIBE_TO_ORDERS_COMPLETE, { baseId, assetId }); }, unsubscribeFromMarket: ({ commit }, { baseId }) => { API.Market[baseId].unsubscribeFromMarkets(); - commit(UNSUB_FROM_MARKET_COMPLETE, { baseId }); + commit(UNSUBSCRIBED_FROM_MARKET); } }; @@ -126,33 +123,32 @@ const mutations = { state.pending = false; state.error = true; }, - [SUB_TO_BALANCE_MARKETS_COMPLETE](state) { + [SUBSCRIBED_TO_BALANCE_MARKETS](state) { state.subscribed = true; }, - [UPDATE_MARKET_PRICE](state, { baseId, assetId, price }) { - if (!state.history[baseId][assetId]) Vue.set(state.history[baseId], assetId, {}); + [SUBSCRIBE_TO_EXCHANGE_RATE](state, { baseId, assetId }) { + if (!state.history[baseId]) Vue.set(state.history, baseId, {}); + Vue.set(state.history[baseId], assetId, {}); + }, + [UPDATE_EXCHANGE_PRICE](state, { baseId, assetId, price }) { Vue.set(state.history[baseId][assetId], 'last', price); }, - [SUB_TO_MARKET_REQUEST](state, { baseId, assetId }) { + [SUBSCRIBE_TO_ORDERS_REQUEST](state, { baseId, assetId }) { state.pending = true; - state.markets[baseId] = {}; - state.markets[baseId][assetId] = {}; + state.ordersUpdateFlags[baseId] = {}; + state.ordersUpdateFlags[baseId][assetId] = null; }, - [SUB_TO_MARKET_COMPLETE](state, { baseId, assetId, orders }) { + [SUBSCRIBE_TO_ORDERS_COMPLETE](state, { baseId, assetId, }) { state.pending = false; - state.markets[baseId][assetId] = orders; - state.markets = { ...state.markets }; + state.ordersUpdateFlags[baseId][assetId] = new Date(); + state.ordersUpdateFlags = { ...state.ordersUpdateFlags }; }, - [UPDATE_MARKET_ORDERS](state, { baseId, assetId, orders }) { - state.markets[baseId][assetId] = orders; - state.markets = { ...state.markets }; + [UPDATE_MARKET_ORDERS](state, { baseId, assetId }) { + state.ordersUpdateFlags[baseId][assetId] = new Date(); + state.ordersUpdateFlags = { ...state.ordersUpdateFlags }; }, - [UNSUB_FROM_MARKET_COMPLETE](state, { baseId }) { + [UNSUBSCRIBED_FROM_MARKET](state) { state.pending = false; - delete (state.markets[baseId]); - }, - [SUBSCRIBE_TO_EXCHANGE_RATE](state) { - console.log('exchange sub', state); } }; @@ -177,7 +173,10 @@ const getters = { isError: state => state.error, isPending: state => state.pending, isSubscribed: state => state.subscribed, - getMarketOrders: state => baseId => state.markets[baseId] + getMarketOrders: (state) => (baseId, assetId) => { + return (state.ordersUpdateFlags[baseId][assetId]) ? + API.Market[baseId].getOrderBook(assetId) : {}; + } }; export default {