Skip to content

Commit

Permalink
Implement balance for non wrapped tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
selankon committed Feb 7, 2024
1 parent 4aa5e61 commit 8df175f
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 4 deletions.
81 changes: 80 additions & 1 deletion src/hooks/useCensus3.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import {useClient} from '@vocdoni/react-providers';
import {useCallback, useEffect, useState} from 'react';
import {GaselessPluginName, usePluginClient} from './usePluginClient';
import {
GaselessPluginName,
PluginTypes,
usePluginClient,
} from './usePluginClient';
import {ErrTokenAlreadyExists} from '@vocdoni/sdk';
import {useParams} from 'react-router-dom';
import {useProposal} from '../services/aragon-sdk/queries/use-proposal';
import {GaslessVotingProposal} from '@vocdoni/gasless-voting';
import {DaoMember, TokenDaoMember} from './useDaoMembers';

const CENSUS3_URL = 'https://census3-stg.vocdoni.net/api';

Expand Down Expand Up @@ -59,3 +67,74 @@ export const useCensus3CreateToken = ({chainId}: {chainId: number}) => {

return {createToken};
};

// Hook that return census3 census id if is gasless plugin
export const useGaslessCensusId = ({
pluginType,
enable = true,
}: {
pluginType?: PluginTypes;
enable?: boolean;
}) => {
const {dao, id: proposalId} = useParams();

const isGasless = pluginType === GaselessPluginName;
const _enable: boolean = enable && !!dao && !!proposalId && isGasless;

const {data: proposalData} = useProposal(
{
pluginType: pluginType,
id: proposalId ?? '',
},
{
enabled: _enable,
}
);

let censusId: string | null = null;
let censusSize: number | null = null;
if (_enable && proposalData) {
const census = (proposalData as GaslessVotingProposal).vochain.metadata
.census;
censusId = census.censusId;
censusSize = census.size;
}

return {censusId, censusSize};
};

export const useNonWrappedDaoMemberBalance = ({
isGovernanceEnabled,
censusId,
subgraphMembers,
}: {
isGovernanceEnabled: boolean;
censusId: string | null;
subgraphMembers: TokenDaoMember[];
}) => {
// State to store DaoMembers[]
const [members, setMembers] = useState<DaoMember[]>(subgraphMembers);
const {client: vocdoniClient} = useClient();

// UseEffect to calculate the vocdoni client fetchProof function
useEffect(() => {
if (vocdoniClient && isGovernanceEnabled && censusId) {
(async () => {
const members = await Promise.all(
subgraphMembers.map(async member => {
const proof = await vocdoniClient.fetchProof(
censusId,
member.address
);
member.balance = Number(proof.weight);
member.votingPower = Number(proof.weight);
return member;
})
);
setMembers(members);
})();
}
}, [censusId, isGovernanceEnabled, subgraphMembers, vocdoniClient]);

return {members};
};
29 changes: 26 additions & 3 deletions src/hooks/useDaoMembers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {useMembers} from 'services/aragon-sdk/queries/use-members';
import {Address, useBalance} from 'wagmi';
import {useDaoToken} from './useDaoToken';
import {useWallet} from './useWallet';
import {useGaslessGovernanceEnabled} from './useGaslessGovernanceEnabled';
import {useGaslessCensusId, useNonWrappedDaoMemberBalance} from './useCensus3';

export type MultisigDaoMember = {
address: string;
Expand Down Expand Up @@ -122,6 +124,15 @@ export const useDaoMembers = (
const {address} = useWallet();
const {data: daoToken} = useDaoToken(pluginAddress);

const {isGovernanceEnabled} = useGaslessGovernanceEnabled({
pluginAddress,
pluginType,
});
const {censusId, censusSize: nonWrappedCensusSize} = useGaslessCensusId({
pluginType,
enable: !isGovernanceEnabled,
});

const isTokenBased =
pluginType === 'token-voting.plugin.dao.eth' ||
pluginType === GaselessPluginName;
Expand Down Expand Up @@ -154,6 +165,12 @@ export const useDaoMembers = (
sdkToDaoMember(member, daoToken?.decimals)
);

const {members: nonGovernanceMemebers} = useNonWrappedDaoMemberBalance({
isGovernanceEnabled,
subgraphMembers: parsedSubgraphData as TokenDaoMember[],
censusId,
});

const {data: userBalance} = useBalance({
address: address as Address,
token: daoToken?.address as Address,
Expand Down Expand Up @@ -245,6 +262,9 @@ export const useDaoMembers = (
},
];
} else {
if (!isGovernanceEnabled) {
return nonGovernanceMemebers;
}
return parsedSubgraphData;
}
} else {
Expand All @@ -255,9 +275,12 @@ export const useDaoMembers = (
const sortedData = opts?.sort
? [...getCombinedData()].sort(sortDaoMembers(opts.sort, address))
: getCombinedData();
memberCount = useSubgraph
? sortedData.length
: graphqlData?.holders.totalHolders || sortedData.length;
memberCount =
nonWrappedCensusSize !== null
? nonWrappedCensusSize
: useSubgraph
? sortedData.length
: graphqlData?.holders.totalHolders || sortedData.length;
const searchTerm = opts?.searchTerm;
const filteredData = !searchTerm
? sortedData
Expand Down
2 changes: 2 additions & 0 deletions src/pages/community.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export const Community: React.FC = () => {
}
);

console.log('AAAAAAAAAAAAAA', members);

const {isDAOTokenWrapped, isTokenMintable} = useExistingToken({
daoToken,
daoDetails,
Expand Down

0 comments on commit 8df175f

Please sign in to comment.