UNPKG

namillum

Version:
90 lines (71 loc) 2.43 kB
// Copyright (C) 2020 Zilliqa // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see <https:www.gnu.org/licenses/>. const state = { selected: undefined, accounts: [], }; const getters = { selected: (state) => state.selected, list: (state, getters, rootState, rootGetters) => { const network = rootGetters["networks/selected"]; return state.accounts.filter((account) => account.network === network.url); }, }; const actions = { SelectAccount({ commit, state, rootGetters }, payload) { const network = rootGetters["networks/selected"]; if (payload !== undefined) { const { address } = payload; if (network.url === undefined) { throw Error("Network not selected"); } const account = state.accounts.find(function (item) { return item.network === network.url && item.address === address; }); if (account === undefined) { throw Error("Account does not exist on network."); } commit("setAccount", account); } else { commit("setAccount", undefined); } }, AddAccount({ commit, state, rootGetters, dispatch }, account) { const network = rootGetters["networks/selected"]; if (network.url === undefined) { throw Error("Network not selected"); } const exists = state.accounts.find(function (item) { return item.network === network.url && item.address === account.address; }); if (exists !== undefined) { throw Error("Account already exists on network."); } commit("addAccount", { ...account, network: network.url }); dispatch("SelectAccount", account); }, }; const mutations = { setAccount(state, payload) { state.selected = payload; }, addAccount(state, payload) { state.accounts.push(payload); }, }; export default { namespaced: true, state, getters, actions, mutations, };