Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fetch delegation amount from on chain if using a mock wallet, or the … #938

Merged
merged 4 commits into from
Oct 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions modules/mkr/hooks/useMkrDelegatedByUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import { useWeb3 } from 'modules/web3/hooks/useWeb3';
import { BigNumber, ethers } from 'ethers';
import { chainIdToNetworkName } from 'modules/web3/helpers/chain';
import { fetchDelegationEventsByAddresses } from 'modules/delegates/api/fetchDelegationEventsByAddresses';
import abi from 'modules/contracts/ethers/voteDelegate.json';
import { getEthersContracts } from 'modules/web3/helpers/getEthersContracts';
import { VoteDelegate } from 'types/ethers-contracts';
import { config } from 'lib/config';

type DelegatedByUserResponse = {
data: {
Expand All @@ -28,7 +32,7 @@ export const useMkrDelegatedByUser = (
userAddress?: string,
voteDelegateAddress?: string
): DelegatedByUserResponse => {
const { chainId } = useWeb3();
const { chainId, provider, account } = useWeb3();
if (!voteDelegateAddress) {
return {
data: undefined,
Expand All @@ -38,9 +42,32 @@ export const useMkrDelegatedByUser = (
};
}
const network = chainIdToNetworkName(chainId);

const fetchFromChain = async(userAddress: string | undefined, voteDelegateAddress: string | undefined) => {
const contract = getEthersContracts<VoteDelegate>(
voteDelegateAddress as string,
abi,
chainId,
provider,
account,
true
);

const directDelegated = await contract.stake(userAddress as string);

return {
directDelegationAmount: directDelegated,
sealDelegationAmount: undefined,
totalDelegationAmount: directDelegated
};
};

const { data, error, mutate } = useSWR(
userAddress && voteDelegateAddress ? ['/user/mkr-delegated', voteDelegateAddress, userAddress] : null,
async () => {
if (config.USE_MOCK_WALLET) {
return fetchFromChain(userAddress, voteDelegateAddress);
}
try {
const data = await fetchDelegationEventsByAddresses([voteDelegateAddress], network);
console.log('data', data);
Expand All @@ -66,8 +93,8 @@ export const useMkrDelegatedByUser = (
totalDelegationAmount: sealDelegated.add(directDelegated)
};
} catch (outerError) {
console.error('Error in useMkrDelegatedByUser:', outerError);
throw outerError;
console.error('Error in useMkrDelegatedByUser. Fetching from chain instead. Error:', outerError);
return fetchFromChain(userAddress, voteDelegateAddress);
}
},
{
Expand Down
Loading