import { txClient, queryClient, MissingWalletError , registry} from './module' // @ts-ignore import { SpVuexError } from '@starport/vuex' import { Params } from "./module/types/crescent/farming/v1beta1/farming" import { BasePlan } from "./module/types/crescent/farming/v1beta1/farming" import { FixedAmountPlan } from "./module/types/crescent/farming/v1beta1/farming" import { RatioPlan } from "./module/types/crescent/farming/v1beta1/farming" import { Staking } from "./module/types/crescent/farming/v1beta1/farming" import { QueuedStaking } from "./module/types/crescent/farming/v1beta1/farming" import { TotalStakings } from "./module/types/crescent/farming/v1beta1/farming" import { HistoricalRewards } from "./module/types/crescent/farming/v1beta1/farming" import { OutstandingRewards } from "./module/types/crescent/farming/v1beta1/farming" import { PlanRecord } from "./module/types/crescent/farming/v1beta1/genesis" import { StakingRecord } from "./module/types/crescent/farming/v1beta1/genesis" import { QueuedStakingRecord } from "./module/types/crescent/farming/v1beta1/genesis" import { TotalStakingsRecord } from "./module/types/crescent/farming/v1beta1/genesis" import { HistoricalRewardsRecord } from "./module/types/crescent/farming/v1beta1/genesis" import { OutstandingRewardsRecord } from "./module/types/crescent/farming/v1beta1/genesis" import { CurrentEpochRecord } from "./module/types/crescent/farming/v1beta1/genesis" import { PublicPlanProposal } from "./module/types/crescent/farming/v1beta1/proposal" import { AddPlanRequest } from "./module/types/crescent/farming/v1beta1/proposal" import { ModifyPlanRequest } from "./module/types/crescent/farming/v1beta1/proposal" import { DeletePlanRequest } from "./module/types/crescent/farming/v1beta1/proposal" export { Params, BasePlan, FixedAmountPlan, RatioPlan, Staking, QueuedStaking, TotalStakings, HistoricalRewards, OutstandingRewards, PlanRecord, StakingRecord, QueuedStakingRecord, TotalStakingsRecord, HistoricalRewardsRecord, OutstandingRewardsRecord, CurrentEpochRecord, PublicPlanProposal, AddPlanRequest, ModifyPlanRequest, DeletePlanRequest }; 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: {}, Plans: {}, Plan: {}, Stakings: {}, TotalStakings: {}, Rewards: {}, CurrentEpochDays: {}, _Structure: { Params: getStructure(Params.fromPartial({})), BasePlan: getStructure(BasePlan.fromPartial({})), FixedAmountPlan: getStructure(FixedAmountPlan.fromPartial({})), RatioPlan: getStructure(RatioPlan.fromPartial({})), Staking: getStructure(Staking.fromPartial({})), QueuedStaking: getStructure(QueuedStaking.fromPartial({})), TotalStakings: getStructure(TotalStakings.fromPartial({})), HistoricalRewards: getStructure(HistoricalRewards.fromPartial({})), OutstandingRewards: getStructure(OutstandingRewards.fromPartial({})), PlanRecord: getStructure(PlanRecord.fromPartial({})), StakingRecord: getStructure(StakingRecord.fromPartial({})), QueuedStakingRecord: getStructure(QueuedStakingRecord.fromPartial({})), TotalStakingsRecord: getStructure(TotalStakingsRecord.fromPartial({})), HistoricalRewardsRecord: getStructure(HistoricalRewardsRecord.fromPartial({})), OutstandingRewardsRecord: getStructure(OutstandingRewardsRecord.fromPartial({})), CurrentEpochRecord: getStructure(CurrentEpochRecord.fromPartial({})), PublicPlanProposal: getStructure(PublicPlanProposal.fromPartial({})), AddPlanRequest: getStructure(AddPlanRequest.fromPartial({})), ModifyPlanRequest: getStructure(ModifyPlanRequest.fromPartial({})), DeletePlanRequest: getStructure(DeletePlanRequest.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)] ?? {} }, getPlans: (state) => (params = { params: {}}) => { if (!( params).query) { ( params).query=null } return state.Plans[JSON.stringify(params)] ?? {} }, getPlan: (state) => (params = { params: {}}) => { if (!( params).query) { ( params).query=null } return state.Plan[JSON.stringify(params)] ?? {} }, getStakings: (state) => (params = { params: {}}) => { if (!( params).query) { ( params).query=null } return state.Stakings[JSON.stringify(params)] ?? {} }, getTotalStakings: (state) => (params = { params: {}}) => { if (!( params).query) { ( params).query=null } return state.TotalStakings[JSON.stringify(params)] ?? {} }, getRewards: (state) => (params = { params: {}}) => { if (!( params).query) { ( params).query=null } return state.Rewards[JSON.stringify(params)] ?? {} }, getCurrentEpochDays: (state) => (params = { params: {}}) => { if (!( params).query) { ( params).query=null } return state.CurrentEpochDays[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.farming.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 QueryPlans({ 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.queryPlans(query)).data while (all && ( value).pagination && ( value).pagination.next_key!=null) { let next_values=(await queryClient.queryPlans({...query, 'pagination.key':( value).pagination.next_key})).data value = mergeResults(value, next_values); } commit('QUERY', { query: 'Plans', key: { params: {...key}, query}, value }) if (subscribe) commit('SUBSCRIBE', { action: 'QueryPlans', payload: { options: { all }, params: {...key},query }}) return getters['getPlans']( { params: {...key}, query}) ?? {} } catch (e) { throw new SpVuexError('QueryClient:QueryPlans', 'API Node Unavailable. Could not perform query: ' + e.message) } }, async QueryPlan({ 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.queryPlan( key.plan_id)).data commit('QUERY', { query: 'Plan', key: { params: {...key}, query}, value }) if (subscribe) commit('SUBSCRIBE', { action: 'QueryPlan', payload: { options: { all }, params: {...key},query }}) return getters['getPlan']( { params: {...key}, query}) ?? {} } catch (e) { throw new SpVuexError('QueryClient:QueryPlan', 'API Node Unavailable. Could not perform query: ' + e.message) } }, async QueryStakings({ 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.queryStakings( key.farmer, query)).data while (all && ( value).pagination && ( value).pagination.next_key!=null) { let next_values=(await queryClient.queryStakings( key.farmer, {...query, 'pagination.key':( value).pagination.next_key})).data value = mergeResults(value, next_values); } commit('QUERY', { query: 'Stakings', key: { params: {...key}, query}, value }) if (subscribe) commit('SUBSCRIBE', { action: 'QueryStakings', payload: { options: { all }, params: {...key},query }}) return getters['getStakings']( { params: {...key}, query}) ?? {} } catch (e) { throw new SpVuexError('QueryClient:QueryStakings', 'API Node Unavailable. Could not perform query: ' + e.message) } }, async QueryTotalStakings({ 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.queryTotalStakings( key.staking_coin_denom)).data commit('QUERY', { query: 'TotalStakings', key: { params: {...key}, query}, value }) if (subscribe) commit('SUBSCRIBE', { action: 'QueryTotalStakings', payload: { options: { all }, params: {...key},query }}) return getters['getTotalStakings']( { params: {...key}, query}) ?? {} } catch (e) { throw new SpVuexError('QueryClient:QueryTotalStakings', 'API Node Unavailable. Could not perform query: ' + e.message) } }, async QueryRewards({ 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.queryRewards( key.farmer, query)).data while (all && ( value).pagination && ( value).pagination.next_key!=null) { let next_values=(await queryClient.queryRewards( key.farmer, {...query, 'pagination.key':( value).pagination.next_key})).data value = mergeResults(value, next_values); } commit('QUERY', { query: 'Rewards', key: { params: {...key}, query}, value }) if (subscribe) commit('SUBSCRIBE', { action: 'QueryRewards', payload: { options: { all }, params: {...key},query }}) return getters['getRewards']( { params: {...key}, query}) ?? {} } catch (e) { throw new SpVuexError('QueryClient:QueryRewards', 'API Node Unavailable. Could not perform query: ' + e.message) } }, async QueryCurrentEpochDays({ 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.queryCurrentEpochDays()).data commit('QUERY', { query: 'CurrentEpochDays', key: { params: {...key}, query}, value }) if (subscribe) commit('SUBSCRIBE', { action: 'QueryCurrentEpochDays', payload: { options: { all }, params: {...key},query }}) return getters['getCurrentEpochDays']( { params: {...key}, query}) ?? {} } catch (e) { throw new SpVuexError('QueryClient:QueryCurrentEpochDays', 'API Node Unavailable. Could not perform query: ' + e.message) } }, async sendMsgUnstake({ rootGetters }, { value, fee = [], memo = '' }) { try { const txClient=await initTxClient(rootGetters) const msg = await txClient.msgUnstake(value) const result = await txClient.signAndBroadcast([msg], {fee: { amount: fee, gas: "200000" }, memo}) return result } catch (e) { if (e == MissingWalletError) { throw new SpVuexError('TxClient:MsgUnstake:Init', 'Could not initialize signing client. Wallet is required.') }else{ throw new SpVuexError('TxClient:MsgUnstake:Send', 'Could not broadcast Tx: '+ e.message) } } }, async sendMsgCreateRatioPlan({ rootGetters }, { value, fee = [], memo = '' }) { try { const txClient=await initTxClient(rootGetters) const msg = await txClient.msgCreateRatioPlan(value) const result = await txClient.signAndBroadcast([msg], {fee: { amount: fee, gas: "200000" }, memo}) return result } catch (e) { if (e == MissingWalletError) { throw new SpVuexError('TxClient:MsgCreateRatioPlan:Init', 'Could not initialize signing client. Wallet is required.') }else{ throw new SpVuexError('TxClient:MsgCreateRatioPlan:Send', 'Could not broadcast Tx: '+ e.message) } } }, async sendMsgHarvest({ rootGetters }, { value, fee = [], memo = '' }) { try { const txClient=await initTxClient(rootGetters) const msg = await txClient.msgHarvest(value) const result = await txClient.signAndBroadcast([msg], {fee: { amount: fee, gas: "200000" }, memo}) return result } catch (e) { if (e == MissingWalletError) { throw new SpVuexError('TxClient:MsgHarvest:Init', 'Could not initialize signing client. Wallet is required.') }else{ throw new SpVuexError('TxClient:MsgHarvest:Send', 'Could not broadcast Tx: '+ e.message) } } }, async sendMsgRemovePlan({ rootGetters }, { value, fee = [], memo = '' }) { try { const txClient=await initTxClient(rootGetters) const msg = await txClient.msgRemovePlan(value) const result = await txClient.signAndBroadcast([msg], {fee: { amount: fee, gas: "200000" }, memo}) return result } catch (e) { if (e == MissingWalletError) { throw new SpVuexError('TxClient:MsgRemovePlan:Init', 'Could not initialize signing client. Wallet is required.') }else{ throw new SpVuexError('TxClient:MsgRemovePlan:Send', 'Could not broadcast Tx: '+ e.message) } } }, async sendMsgAdvanceEpoch({ rootGetters }, { value, fee = [], memo = '' }) { try { const txClient=await initTxClient(rootGetters) const msg = await txClient.msgAdvanceEpoch(value) const result = await txClient.signAndBroadcast([msg], {fee: { amount: fee, gas: "200000" }, memo}) return result } catch (e) { if (e == MissingWalletError) { throw new SpVuexError('TxClient:MsgAdvanceEpoch:Init', 'Could not initialize signing client. Wallet is required.') }else{ throw new SpVuexError('TxClient:MsgAdvanceEpoch:Send', 'Could not broadcast Tx: '+ e.message) } } }, async sendMsgCreateFixedAmountPlan({ rootGetters }, { value, fee = [], memo = '' }) { try { const txClient=await initTxClient(rootGetters) const msg = await txClient.msgCreateFixedAmountPlan(value) const result = await txClient.signAndBroadcast([msg], {fee: { amount: fee, gas: "200000" }, memo}) return result } catch (e) { if (e == MissingWalletError) { throw new SpVuexError('TxClient:MsgCreateFixedAmountPlan:Init', 'Could not initialize signing client. Wallet is required.') }else{ throw new SpVuexError('TxClient:MsgCreateFixedAmountPlan:Send', 'Could not broadcast Tx: '+ e.message) } } }, async sendMsgStake({ rootGetters }, { value, fee = [], memo = '' }) { try { const txClient=await initTxClient(rootGetters) const msg = await txClient.msgStake(value) const result = await txClient.signAndBroadcast([msg], {fee: { amount: fee, gas: "200000" }, memo}) return result } catch (e) { if (e == MissingWalletError) { throw new SpVuexError('TxClient:MsgStake:Init', 'Could not initialize signing client. Wallet is required.') }else{ throw new SpVuexError('TxClient:MsgStake:Send', 'Could not broadcast Tx: '+ e.message) } } }, async MsgUnstake({ rootGetters }, { value }) { try { const txClient=await initTxClient(rootGetters) const msg = await txClient.msgUnstake(value) return msg } catch (e) { if (e == MissingWalletError) { throw new SpVuexError('TxClient:MsgUnstake:Init', 'Could not initialize signing client. Wallet is required.') }else{ throw new SpVuexError('TxClient:MsgUnstake:Create', 'Could not create message: ' + e.message) } } }, async MsgCreateRatioPlan({ rootGetters }, { value }) { try { const txClient=await initTxClient(rootGetters) const msg = await txClient.msgCreateRatioPlan(value) return msg } catch (e) { if (e == MissingWalletError) { throw new SpVuexError('TxClient:MsgCreateRatioPlan:Init', 'Could not initialize signing client. Wallet is required.') }else{ throw new SpVuexError('TxClient:MsgCreateRatioPlan:Create', 'Could not create message: ' + e.message) } } }, async MsgHarvest({ rootGetters }, { value }) { try { const txClient=await initTxClient(rootGetters) const msg = await txClient.msgHarvest(value) return msg } catch (e) { if (e == MissingWalletError) { throw new SpVuexError('TxClient:MsgHarvest:Init', 'Could not initialize signing client. Wallet is required.') }else{ throw new SpVuexError('TxClient:MsgHarvest:Create', 'Could not create message: ' + e.message) } } }, async MsgRemovePlan({ rootGetters }, { value }) { try { const txClient=await initTxClient(rootGetters) const msg = await txClient.msgRemovePlan(value) return msg } catch (e) { if (e == MissingWalletError) { throw new SpVuexError('TxClient:MsgRemovePlan:Init', 'Could not initialize signing client. Wallet is required.') }else{ throw new SpVuexError('TxClient:MsgRemovePlan:Create', 'Could not create message: ' + e.message) } } }, async MsgAdvanceEpoch({ rootGetters }, { value }) { try { const txClient=await initTxClient(rootGetters) const msg = await txClient.msgAdvanceEpoch(value) return msg } catch (e) { if (e == MissingWalletError) { throw new SpVuexError('TxClient:MsgAdvanceEpoch:Init', 'Could not initialize signing client. Wallet is required.') }else{ throw new SpVuexError('TxClient:MsgAdvanceEpoch:Create', 'Could not create message: ' + e.message) } } }, async MsgCreateFixedAmountPlan({ rootGetters }, { value }) { try { const txClient=await initTxClient(rootGetters) const msg = await txClient.msgCreateFixedAmountPlan(value) return msg } catch (e) { if (e == MissingWalletError) { throw new SpVuexError('TxClient:MsgCreateFixedAmountPlan:Init', 'Could not initialize signing client. Wallet is required.') }else{ throw new SpVuexError('TxClient:MsgCreateFixedAmountPlan:Create', 'Could not create message: ' + e.message) } } }, async MsgStake({ rootGetters }, { value }) { try { const txClient=await initTxClient(rootGetters) const msg = await txClient.msgStake(value) return msg } catch (e) { if (e == MissingWalletError) { throw new SpVuexError('TxClient:MsgStake:Init', 'Could not initialize signing client. Wallet is required.') }else{ throw new SpVuexError('TxClient:MsgStake:Create', 'Could not create message: ' + e.message) } } }, } }