From 1911485ebf7c9c335ee4ee5bb4d4fc8e058a0396 Mon Sep 17 00:00:00 2001 From: Pablo Pettinari Date: Mon, 24 Jan 2022 12:38:27 -0300 Subject: [PATCH] Refactor: maxRecipients getter (#505) (#506) * fetch maci factory data and save it in the store * Refactor last maxRecipients getter Co-authored-by: Sam Richards Co-authored-by: Sam Richards --- vue-app/src/App.vue | 2 ++ vue-app/src/api/abi.ts | 2 ++ vue-app/src/api/maci-factory.ts | 20 ++++++++++++++++++++ vue-app/src/store/action-types.ts | 1 + vue-app/src/store/actions.ts | 7 +++++++ vue-app/src/store/getters.ts | 13 +++++++++++++ vue-app/src/store/index.ts | 1 + vue-app/src/store/mutation-types.ts | 1 + vue-app/src/store/mutations.ts | 5 +++++ vue-app/src/views/AboutHowItWorks.vue | 4 ++-- 10 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 vue-app/src/api/maci-factory.ts diff --git a/vue-app/src/App.vue b/vue-app/src/App.vue index ac66f9799..f4cb525b4 100644 --- a/vue-app/src/App.vue +++ b/vue-app/src/App.vue @@ -57,6 +57,7 @@ import { LOAD_CONTRIBUTOR_DATA, LOGIN_USER, LOAD_FACTORY_INFO, + LOAD_MACI_FACTORY_INFO, } from '@/store/action-types' import { SET_CURRENT_USER } from '@/store/mutation-types' @@ -109,6 +110,7 @@ export default class App extends Vue { await this.$store.dispatch(SELECT_ROUND, roundAddress) this.$store.dispatch(LOAD_ROUND_INFO) this.$store.dispatch(LOAD_FACTORY_INFO) + this.$store.dispatch(LOAD_MACI_FACTORY_INFO) await this.$store.dispatch(LOAD_RECIPIENT_REGISTRY_INFO) } diff --git a/vue-app/src/api/abi.ts b/vue-app/src/api/abi.ts index 4a511c12d..f8ffc89d8 100644 --- a/vue-app/src/api/abi.ts +++ b/vue-app/src/api/abi.ts @@ -1,6 +1,7 @@ import { abi as ERC20 } from '../../../contracts/build/contracts/@openzeppelin/contracts/token/ERC20/ERC20.sol/ERC20.json' import { abi as FundingRoundFactory } from '../../../contracts/build/contracts/contracts/FundingRoundFactory.sol/FundingRoundFactory.json' import { abi as FundingRound } from '../../../contracts/build/contracts/contracts/FundingRound.sol/FundingRound.json' +import { abi as MACIFactory } from '../../../contracts/build/contracts/contracts/MACIFactory.sol/MACIFactory.json' import { abi as MACI } from '../../../contracts/build/contracts/maci-contracts/sol/MACI.sol/MACI.json' import { abi as UserRegistry } from '../../../contracts/build/contracts/contracts/userRegistry/IUserRegistry.sol/IUserRegistry.json' import { abi as BrightIdUserRegistry } from '../../../contracts/build/contracts/contracts/userRegistry/BrightIdUserRegistry.sol/BrightIdUserRegistry.json' @@ -13,6 +14,7 @@ export { ERC20, FundingRoundFactory, FundingRound, + MACIFactory, MACI, UserRegistry, BrightIdUserRegistry, diff --git a/vue-app/src/api/maci-factory.ts b/vue-app/src/api/maci-factory.ts new file mode 100644 index 000000000..3cc909dbd --- /dev/null +++ b/vue-app/src/api/maci-factory.ts @@ -0,0 +1,20 @@ +import { Contract } from 'ethers' +import { MACIFactory as MACIFactoryABI } from './abi' +import { factory, provider } from './core' + +export interface MACIFactory { + maciFactoryAddress: string + maxRecipients: number +} + +export async function getMACIFactoryInfo(): Promise { + const maciFactoryAddress = await factory.maciFactory() + + const maciFactory = new Contract(maciFactoryAddress, MACIFactoryABI, provider) + const treeDepths = await maciFactory.treeDepths() + + return { + maciFactoryAddress, + maxRecipients: 5 ** treeDepths.voteOptionTreeDepth - 1, + } +} diff --git a/vue-app/src/store/action-types.ts b/vue-app/src/store/action-types.ts index 59bf82cb7..c2f5656b1 100644 --- a/vue-app/src/store/action-types.ts +++ b/vue-app/src/store/action-types.ts @@ -1,6 +1,7 @@ export const SELECT_ROUND = 'SELECT_ROUND' export const LOAD_TALLY = 'LOAD_TALLY' export const LOAD_FACTORY_INFO = 'LOAD_FACTORY_INFO' +export const LOAD_MACI_FACTORY_INFO = 'LOAD_MACI_FACTORY_INFO' export const LOAD_ROUND_INFO = 'LOAD_ROUND_INFO' export const LOAD_RECIPIENT_REGISTRY_INFO = 'LOAD_RECIPIENT_REGISTRY_INFO' export const LOAD_USER_INFO = 'LOAD_USER_INFO' diff --git a/vue-app/src/store/actions.ts b/vue-app/src/store/actions.ts index f37878748..9270318e0 100644 --- a/vue-app/src/store/actions.ts +++ b/vue-app/src/store/actions.ts @@ -28,6 +28,7 @@ import { LOAD_COMMITTED_CART, LOAD_CONTRIBUTOR_DATA, LOAD_FACTORY_INFO, + LOAD_MACI_FACTORY_INFO, LOAD_RECIPIENT_REGISTRY_INFO, LOAD_ROUND_INFO, LOAD_TALLY, @@ -56,6 +57,7 @@ import { SET_RECIPIENT_REGISTRY_INFO, SET_HAS_VOTED, SET_FACTORY, + SET_MACI_FACTORY, } from './mutation-types' // Utils @@ -63,6 +65,7 @@ import { ensLookup } from '@/utils/accounts' import { UserRegistryType, userRegistryType } from '@/api/core' import { BrightId, getBrightId } from '@/api/bright-id' import { getFactoryInfo } from '@/api/factory' +import { getMACIFactoryInfo } from '@/api/maci-factory' const actions = { //TODO: also commit SET_CURRENT_FACTORY_ADDRESS on this action, should be passed optionally and default to env variable @@ -84,6 +87,10 @@ const actions = { const factory = await getFactoryInfo() commit(SET_FACTORY, factory) }, + async [LOAD_MACI_FACTORY_INFO]({ commit }) { + const factory = await getMACIFactoryInfo() + commit(SET_MACI_FACTORY, factory) + }, async [LOAD_ROUND_INFO]({ commit, state }) { const roundAddress = state.currentRoundAddress if (roundAddress === null) { diff --git a/vue-app/src/store/getters.ts b/vue-app/src/store/getters.ts index 4a3d04d60..8dada0016 100644 --- a/vue-app/src/store/getters.ts +++ b/vue-app/src/store/getters.ts @@ -9,6 +9,7 @@ import { RoundInfo, RoundStatus } from '@/api/round' import { Tally } from '@/api/tally' import { User } from '@/api/user' import { Factory } from '@/api/factory' +import { MACIFactory } from '@/api/maci-factory' import { RecipientApplicationData, RegistryInfo, @@ -34,6 +35,7 @@ export interface RootState { showCartPanel: boolean tally: Tally | null factory: Factory | null + maciFactory: MACIFactory | null } const getters = { @@ -218,6 +220,17 @@ const getters = { return nativeTokenDecimals }, + maxRecipients: (state: RootState): number | undefined => { + const { currentRound, maciFactory } = state + + if (currentRound) { + return currentRound.maxRecipients + } + + if (maciFactory) { + return maciFactory.maxRecipients + } + }, } export default getters diff --git a/vue-app/src/store/index.ts b/vue-app/src/store/index.ts index d06627bf6..064d21de3 100644 --- a/vue-app/src/store/index.ts +++ b/vue-app/src/store/index.ts @@ -32,6 +32,7 @@ const state: RootState = { showCartPanel: false, tally: null, factory: null, + maciFactory: null, } const store: StoreOptions = { diff --git a/vue-app/src/store/mutation-types.ts b/vue-app/src/store/mutation-types.ts index 565c551c2..038030b39 100644 --- a/vue-app/src/store/mutation-types.ts +++ b/vue-app/src/store/mutation-types.ts @@ -2,6 +2,7 @@ export const SET_RECIPIENT_REGISTRY_ADDRESS = 'SET_RECIPIENT_REGISTRY_ADDRESS' export const SET_RECIPIENT_REGISTRY_INFO = 'SET_RECIPIENT_REGISTRY_INFO' export const SET_CURRENT_USER = 'SET_CURRENT_USER' export const SET_FACTORY = 'SET_FACTORY' +export const SET_MACI_FACTORY = 'SET_MACI_FACTORY' export const SET_CURRENT_ROUND_ADDRESS = 'SET_CURRENT_ROUND_ADDRESS' export const SET_CURRENT_ROUND = 'SET_CURRENT_ROUND' export const SET_TALLY = 'SET_TALLY' diff --git a/vue-app/src/store/mutations.ts b/vue-app/src/store/mutations.ts index 8faaf7ab6..0869cfb17 100644 --- a/vue-app/src/store/mutations.ts +++ b/vue-app/src/store/mutations.ts @@ -8,6 +8,7 @@ import { RoundInfo } from '@/api/round' import { Tally } from '@/api/tally' import { User } from '@/api/user' import { Factory } from '@/api/factory' +import { MACIFactory } from '@/api/maci-factory' import { RecipientApplicationData, RegistryInfo, @@ -35,6 +36,7 @@ import { TOGGLE_EDIT_SELECTION, SET_HAS_VOTED, SET_FACTORY, + SET_MACI_FACTORY, } from './mutation-types' const mutations = { @@ -58,6 +60,9 @@ const mutations = { [SET_FACTORY](state, factory: Factory) { state.factory = factory }, + [SET_MACI_FACTORY](state, factory: MACIFactory) { + state.maciFactory = factory + }, //TODO: also dispatch SET_CURRENT_FACTORY_ADDRESS mutation when ever this fires [SET_CURRENT_ROUND_ADDRESS](state, address: string) { state.currentRoundAddress = address diff --git a/vue-app/src/views/AboutHowItWorks.vue b/vue-app/src/views/AboutHowItWorks.vue index 6973f4ae4..b4a1f8b9c 100644 --- a/vue-app/src/views/AboutHowItWorks.vue +++ b/vue-app/src/views/AboutHowItWorks.vue @@ -161,8 +161,8 @@ export default class AboutHowItWorks extends Vue { return MAX_CONTRIBUTION_AMOUNT } - get maxRecipients(): number | string { - return this.$store.state?.currentRound?.maxRecipients || 'TBD' + get maxRecipients(): number | undefined { + return this.$store.getters.maxRecipients } get nativeTokenSymbol(): string {