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

Rework market module, add manual trading tab #72

Closed
wants to merge 9 commits into from
Closed
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
8 changes: 4 additions & 4 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ 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',
defaultMarkets: [
'1.3.121',
'1.3.861',
'1.3.113',
'1.3.0',
'1.3.103'
]
'1.3.0'
],
defaultTradingBase: '1.3.0'
};

export default config;
7 changes: 4 additions & 3 deletions src/actions/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

Expand All @@ -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;
}
});

Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
2 changes: 0 additions & 2 deletions src/modules/market.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,13 @@ const getters = {
};
},
getMarketHistory: state => state.history,
isFetching: state => state.pending,
isError: state => state.error,
isSubscribed: state => state.subscribed
};

const initialState = {
history: {},
days: 7,
pending: false,
error: false,
baseAssetId: null,
subscribed: false,
Expand Down
188 changes: 188 additions & 0 deletions src/modules/market2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
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 SUBSCRIBE_TO_EXCHANGE_RATE = 'SUBSCRIBE_TO_EXCHANGE_REQUEST';
const UPDATE_EXCHANGE_PRICE = 'UPDATE_MARKET_PRICE';
Copy link
Contributor

Choose a reason for hiding this comment

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

это лучше одинаково называть, во избежании потом путаницы при дебаге через vuex dev tools

Copy link
Contributor Author

Choose a reason for hiding this comment

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

да, проглядел при изменении



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,
history: {},
ordersUpdateFlags: {}
};

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, { baseId, assetsIds, days }) => {
const { commit } = store;
commit(FETCH_ASSETS_HISTORY_REQUEST);

Promise.all(assetsIds.map(async (assetId) => {
const prices = await actions.fetchMarketHistory(store, { baseId, assetId, days });
if (!prices) throw new Error('error market history');
})).then(() => {
commit(FETCH_ASSETS_HISTORY_COMPLETE);
}).catch(() => {
commit(FETCH_ASSETS_HISTORY_ERROR);
});
},
subscribeToExchangeRate: async (store, { baseId, assetId, balance }) => {
const { commit } = store;
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_EXCHANGE_PRICE, { baseId, assetId, price });

// WTF THIS TYT DELAET? @roma219
Copy link
Contributor

Choose a reason for hiding this comment

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

creates pending orders from distributions

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(SUBSCRIBED_TO_BALANCE_MARKETS);
console.log('subscribed to markets successfully');
});
},
subscribeToMarketOrders: async ({ commit }, { baseId, assetId }) => {
commit(SUBSCRIBE_TO_ORDERS_REQUEST, { baseId, assetId });
await API.Market[baseId].subscribeToMarket(assetId, () => {
console.log('UPDATE MARKET ORDERS', assetId);
commit(UPDATE_MARKET_ORDERS, { baseId, assetId });
});
commit(SUBSCRIBE_TO_ORDERS_COMPLETE, { baseId, assetId });
},
unsubscribeFromMarket: ({ commit }, { baseId }) => {
API.Market[baseId].unsubscribeFromMarkets();
commit(UNSUBSCRIBED_FROM_MARKET);
}
};

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;
state.history[baseId][assetId] = prices;
state.history = { ...state.history };
},
[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;
},
[SUBSCRIBED_TO_BALANCE_MARKETS](state) {
state.subscribed = true;
},
[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);
},
[SUBSCRIBE_TO_ORDERS_REQUEST](state, { baseId, assetId }) {
state.pending = true;
state.ordersUpdateFlags[baseId] = {};
state.ordersUpdateFlags[baseId][assetId] = null;
},
[SUBSCRIBE_TO_ORDERS_COMPLETE](state, { baseId, assetId, }) {
state.pending = false;
state.ordersUpdateFlags[baseId][assetId] = new Date();
state.ordersUpdateFlags = { ...state.ordersUpdateFlags };
},
[UPDATE_MARKET_ORDERS](state, { baseId, assetId }) {
state.ordersUpdateFlags[baseId][assetId] = new Date();
state.ordersUpdateFlags = { ...state.ordersUpdateFlags };
},
[UNSUBSCRIBED_FROM_MARKET](state) {
state.pending = false;
}
};

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,
isPending: state => state.pending,
isSubscribed: state => state.subscribed,
getMarketOrders: (state) => (baseId, assetId) => {
return (state.ordersUpdateFlags[baseId][assetId]) ?
API.Market[baseId].getOrderBook(assetId) : {};
}
};

export default {
state: initialState,
actions,
mutations,
getters,
namespaced: true
};
1 change: 1 addition & 0 deletions src/modules/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
8 changes: 4 additions & 4 deletions src/services/api/assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions src/services/api/market.js
Original file line number Diff line number Diff line change
Expand Up @@ -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': {
Expand Down