Skip to content

Commit

Permalink
Refactor: maxRecipients getter (#505) (#506)
Browse files Browse the repository at this point in the history
* fetch maci factory data and save it in the store

* Refactor last maxRecipients getter

Co-authored-by: Sam Richards <[email protected]>

Co-authored-by: Sam Richards <[email protected]>
  • Loading branch information
pettinarip and samajammin authored Jan 24, 2022
1 parent 31f09d9 commit 1911485
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 2 deletions.
2 changes: 2 additions & 0 deletions vue-app/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 2 additions & 0 deletions vue-app/src/api/abi.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -13,6 +14,7 @@ export {
ERC20,
FundingRoundFactory,
FundingRound,
MACIFactory,
MACI,
UserRegistry,
BrightIdUserRegistry,
Expand Down
20 changes: 20 additions & 0 deletions vue-app/src/api/maci-factory.ts
Original file line number Diff line number Diff line change
@@ -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<MACIFactory> {
const maciFactoryAddress = await factory.maciFactory()

const maciFactory = new Contract(maciFactoryAddress, MACIFactoryABI, provider)
const treeDepths = await maciFactory.treeDepths()

return {
maciFactoryAddress,
maxRecipients: 5 ** treeDepths.voteOptionTreeDepth - 1,
}
}
1 change: 1 addition & 0 deletions vue-app/src/store/action-types.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
7 changes: 7 additions & 0 deletions vue-app/src/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -56,13 +57,15 @@ import {
SET_RECIPIENT_REGISTRY_INFO,
SET_HAS_VOTED,
SET_FACTORY,
SET_MACI_FACTORY,
} from './mutation-types'

// Utils
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
Expand All @@ -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) {
Expand Down
13 changes: 13 additions & 0 deletions vue-app/src/store/getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -34,6 +35,7 @@ export interface RootState {
showCartPanel: boolean
tally: Tally | null
factory: Factory | null
maciFactory: MACIFactory | null
}

const getters = {
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions vue-app/src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const state: RootState = {
showCartPanel: false,
tally: null,
factory: null,
maciFactory: null,
}

const store: StoreOptions<RootState> = {
Expand Down
1 change: 1 addition & 0 deletions vue-app/src/store/mutation-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
5 changes: 5 additions & 0 deletions vue-app/src/store/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -35,6 +36,7 @@ import {
TOGGLE_EDIT_SELECTION,
SET_HAS_VOTED,
SET_FACTORY,
SET_MACI_FACTORY,
} from './mutation-types'

const mutations = {
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions vue-app/src/views/AboutHowItWorks.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 1911485

Please sign in to comment.