import { txClient, queryClient, MissingWalletError , registry} from './module' // @ts-ignore import { SpVuexError } from '@starport/vuex' import { Params } from "./module/types/crescent/liquidstaking/v1beta1/liquidstaking" import { WhitelistedValidator } from "./module/types/crescent/liquidstaking/v1beta1/liquidstaking" import { LiquidValidator } from "./module/types/crescent/liquidstaking/v1beta1/liquidstaking" import { LiquidValidatorState } from "./module/types/crescent/liquidstaking/v1beta1/liquidstaking" import { NetAmountState } from "./module/types/crescent/liquidstaking/v1beta1/liquidstaking" import { VotingPower } from "./module/types/crescent/liquidstaking/v1beta1/liquidstaking" export { Params, WhitelistedValidator, LiquidValidator, LiquidValidatorState, NetAmountState, VotingPower }; async function initTxClient(vuexGetters) { return await txClient(vuexGetters['common/wallet/signer'], { addr: vuexGetters['common/env/apiTendermint'] }) } async function initQueryClient(vuexGetters) { return await queryClient({ addr: vuexGetters['common/env/apiCosmos'] }) } function mergeResults(value, next_values) { for (let prop of Object.keys(next_values)) { if (Array.isArray(next_values[prop])) { value[prop]=[...value[prop], ...next_values[prop]] }else{ value[prop]=next_values[prop] } } return value } function getStructure(template) { let structure = { fields: [] } for (const [key, value] of Object.entries(template)) { let field: any = {} field.name = key field.type = typeof value structure.fields.push(field) } return structure } const getDefaultState = () => { return { Params: {}, LiquidValidators: {}, VotingPower: {}, States: {}, _Structure: { Params: getStructure(Params.fromPartial({})), WhitelistedValidator: getStructure(WhitelistedValidator.fromPartial({})), LiquidValidator: getStructure(LiquidValidator.fromPartial({})), LiquidValidatorState: getStructure(LiquidValidatorState.fromPartial({})), NetAmountState: getStructure(NetAmountState.fromPartial({})), VotingPower: getStructure(VotingPower.fromPartial({})), }, _Registry: registry, _Subscriptions: new Set(), } } // initial state const state = getDefaultState() export default { namespaced: true, state, mutations: { RESET_STATE(state) { Object.assign(state, getDefaultState()) }, QUERY(state, { query, key, value }) { state[query][JSON.stringify(key)] = value }, SUBSCRIBE(state, subscription) { state._Subscriptions.add(JSON.stringify(subscription)) }, UNSUBSCRIBE(state, subscription) { state._Subscriptions.delete(JSON.stringify(subscription)) } }, getters: { getParams: (state) => (params = { params: {}}) => { if (!( params).query) { ( params).query=null } return state.Params[JSON.stringify(params)] ?? {} }, getLiquidValidators: (state) => (params = { params: {}}) => { if (!( params).query) { ( params).query=null } return state.LiquidValidators[JSON.stringify(params)] ?? {} }, getVotingPower: (state) => (params = { params: {}}) => { if (!( params).query) { ( params).query=null } return state.VotingPower[JSON.stringify(params)] ?? {} }, getStates: (state) => (params = { params: {}}) => { if (!( params).query) { ( params).query=null } return state.States[JSON.stringify(params)] ?? {} }, getTypeStructure: (state) => (type) => { return state._Structure[type].fields }, getRegistry: (state) => { return state._Registry } }, actions: { init({ dispatch, rootGetters }) { console.log('Vuex module: crescent.liquidstaking.v1beta1 initialized!') if (rootGetters['common/env/client']) { rootGetters['common/env/client'].on('newblock', () => { dispatch('StoreUpdate') }) } }, resetState({ commit }) { commit('RESET_STATE') }, unsubscribe({ commit }, subscription) { commit('UNSUBSCRIBE', subscription) }, async StoreUpdate({ state, dispatch }) { state._Subscriptions.forEach(async (subscription) => { try { const sub=JSON.parse(subscription) await dispatch(sub.action, sub.payload) }catch(e) { throw new SpVuexError('Subscriptions: ' + e.message) } }) }, async QueryParams({ commit, rootGetters, getters }, { options: { subscribe, all} = { subscribe:false, all:false}, params, query=null }) { try { const key = params ?? {}; const queryClient=await initQueryClient(rootGetters) let value= (await queryClient.queryParams()).data commit('QUERY', { query: 'Params', key: { params: {...key}, query}, value }) if (subscribe) commit('SUBSCRIBE', { action: 'QueryParams', payload: { options: { all }, params: {...key},query }}) return getters['getParams']( { params: {...key}, query}) ?? {} } catch (e) { throw new SpVuexError('QueryClient:QueryParams', 'API Node Unavailable. Could not perform query: ' + e.message) } }, async QueryLiquidValidators({ commit, rootGetters, getters }, { options: { subscribe, all} = { subscribe:false, all:false}, params, query=null }) { try { const key = params ?? {}; const queryClient=await initQueryClient(rootGetters) let value= (await queryClient.queryLiquidValidators()).data commit('QUERY', { query: 'LiquidValidators', key: { params: {...key}, query}, value }) if (subscribe) commit('SUBSCRIBE', { action: 'QueryLiquidValidators', payload: { options: { all }, params: {...key},query }}) return getters['getLiquidValidators']( { params: {...key}, query}) ?? {} } catch (e) { throw new SpVuexError('QueryClient:QueryLiquidValidators', 'API Node Unavailable. Could not perform query: ' + e.message) } }, async QueryVotingPower({ commit, rootGetters, getters }, { options: { subscribe, all} = { subscribe:false, all:false}, params, query=null }) { try { const key = params ?? {}; const queryClient=await initQueryClient(rootGetters) let value= (await queryClient.queryVotingPower( key.voter)).data commit('QUERY', { query: 'VotingPower', key: { params: {...key}, query}, value }) if (subscribe) commit('SUBSCRIBE', { action: 'QueryVotingPower', payload: { options: { all }, params: {...key},query }}) return getters['getVotingPower']( { params: {...key}, query}) ?? {} } catch (e) { throw new SpVuexError('QueryClient:QueryVotingPower', 'API Node Unavailable. Could not perform query: ' + e.message) } }, async QueryStates({ commit, rootGetters, getters }, { options: { subscribe, all} = { subscribe:false, all:false}, params, query=null }) { try { const key = params ?? {}; const queryClient=await initQueryClient(rootGetters) let value= (await queryClient.queryStates()).data commit('QUERY', { query: 'States', key: { params: {...key}, query}, value }) if (subscribe) commit('SUBSCRIBE', { action: 'QueryStates', payload: { options: { all }, params: {...key},query }}) return getters['getStates']( { params: {...key}, query}) ?? {} } catch (e) { throw new SpVuexError('QueryClient:QueryStates', 'API Node Unavailable. Could not perform query: ' + e.message) } }, async sendMsgLiquidUnstake({ rootGetters }, { value, fee = [], memo = '' }) { try { const txClient=await initTxClient(rootGetters) const msg = await txClient.msgLiquidUnstake(value) const result = await txClient.signAndBroadcast([msg], {fee: { amount: fee, gas: "200000" }, memo}) return result } catch (e) { if (e == MissingWalletError) { throw new SpVuexError('TxClient:MsgLiquidUnstake:Init', 'Could not initialize signing client. Wallet is required.') }else{ throw new SpVuexError('TxClient:MsgLiquidUnstake:Send', 'Could not broadcast Tx: '+ e.message) } } }, async sendMsgLiquidStake({ rootGetters }, { value, fee = [], memo = '' }) { try { const txClient=await initTxClient(rootGetters) const msg = await txClient.msgLiquidStake(value) const result = await txClient.signAndBroadcast([msg], {fee: { amount: fee, gas: "200000" }, memo}) return result } catch (e) { if (e == MissingWalletError) { throw new SpVuexError('TxClient:MsgLiquidStake:Init', 'Could not initialize signing client. Wallet is required.') }else{ throw new SpVuexError('TxClient:MsgLiquidStake:Send', 'Could not broadcast Tx: '+ e.message) } } }, async MsgLiquidUnstake({ rootGetters }, { value }) { try { const txClient=await initTxClient(rootGetters) const msg = await txClient.msgLiquidUnstake(value) return msg } catch (e) { if (e == MissingWalletError) { throw new SpVuexError('TxClient:MsgLiquidUnstake:Init', 'Could not initialize signing client. Wallet is required.') }else{ throw new SpVuexError('TxClient:MsgLiquidUnstake:Create', 'Could not create message: ' + e.message) } } }, async MsgLiquidStake({ rootGetters }, { value }) { try { const txClient=await initTxClient(rootGetters) const msg = await txClient.msgLiquidStake(value) return msg } catch (e) { if (e == MissingWalletError) { throw new SpVuexError('TxClient:MsgLiquidStake:Init', 'Could not initialize signing client. Wallet is required.') }else{ throw new SpVuexError('TxClient:MsgLiquidStake:Create', 'Could not create message: ' + e.message) } } }, } }