-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from TrustyFund/new-portfolio-utils
Separate History and Market modules
- Loading branch information
Showing
8 changed files
with
109 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import Vue from 'vue'; | ||
import * as types from '../mutations'; | ||
import API from '../services/api'; | ||
|
||
const actions = { | ||
fetch: (store, { assetsIds, baseId, days }) => { | ||
const { commit, rootGetters } = store; | ||
const assets = rootGetters['assets/getAssets']; | ||
const baseAsset = assets[baseId]; | ||
|
||
commit(types.FETCH_PRICES_HISTORY_REQUEST, { baseId }); | ||
Promise.all(assetsIds.map(async (assetId) => { | ||
const prices = await API.Assets.fetchPriceHistory(baseAsset, assets[assetId], days); | ||
if (!prices) throw new Error('error market history'); | ||
return { | ||
assetId, | ||
prices | ||
}; | ||
})).then((pricesObjects) => { | ||
const prices = pricesObjects.reduce((result, obj) => { | ||
result[obj.assetId] = obj.prices; | ||
return result; | ||
}, {}); | ||
commit(types.FETCH_PRICES_HISTORY_COMPLETE, { days, prices }); | ||
}).catch((err) => { | ||
commit(types.FETCH_PRICES_HISTORY_ERROR); | ||
console.log(err); | ||
}); | ||
} | ||
}; | ||
|
||
const getters = { | ||
getByDay: state => { | ||
return (days) => { | ||
return state.days[days] || {}; | ||
}; | ||
}, | ||
isFetching: state => state.fetching, | ||
getAssetHistoryByDay: state => { | ||
return (id, day) => { | ||
if (!state.days[day]) return { first: 0, last: 0 }; | ||
return state.days[day][id] || { first: 0, last: 0 }; | ||
}; | ||
}, | ||
getHistoryAssetMultiplier: state => { | ||
return (days, assetId) => { | ||
if (!state.days[days] || !state.days[days][assetId]) { | ||
return { | ||
first: 0, | ||
last: 0 | ||
}; | ||
} | ||
return { | ||
first: 1 / state.days[days][assetId].first, | ||
last: 1 / state.days[days][assetId].last | ||
}; | ||
}; | ||
} | ||
}; | ||
|
||
const initialState = { | ||
days: {}, | ||
fetching: false, | ||
error: false, | ||
baseId: '' | ||
}; | ||
|
||
const mutations = { | ||
[types.FETCH_PRICES_HISTORY_REQUEST](state, { baseId }) { | ||
state.fetching = true; | ||
state.baseAssetId = baseId; | ||
}, | ||
[types.FETCH_PRICES_HISTORY_COMPLETE](state, { prices, days }) { | ||
state.fetching = false; | ||
Vue.set(state.days, days, prices); | ||
}, | ||
[types.FETCH_PRICES_HISTORY_ERROR](state) { | ||
state.fetching = false; | ||
state.error = true; | ||
} | ||
}; | ||
|
||
export default { | ||
state: initialState, | ||
actions, | ||
getters, | ||
mutations, | ||
namespaced: true | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters