Skip to content

Commit

Permalink
feat: wrap provider with multicall provider + add passSigner paramete…
Browse files Browse the repository at this point in the history
…r to contrat hooks
  • Loading branch information
therealemjy committed Sep 1, 2023
1 parent 062b864 commit 5ec35c0
Show file tree
Hide file tree
Showing 38 changed files with 242 additions and 35 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"regression": "reg-suit run"
},
"dependencies": {
"@0xsequence/multicall": "^0.43.34",
"@binance-chain/bsc-connector": "^1.0.0",
"@emotion/react": "^11.11.1",
"@ethersproject/abstract-signer": "^5.7.0",
Expand Down
2 changes: 1 addition & 1 deletion src/clients/api/mutations/approveToken/useApproveToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type TrimmedApproveTokenInput = Omit<ApproveTokenInput, 'tokenContract'>;
type Options = MutationObserverOptions<ApproveTokenOutput, Error, TrimmedApproveTokenInput>;

const useApproveToken = ({ token }: { token: Token }, options?: Options) => {
const tokenContract = useGetTokenContract(token);
const tokenContract = useGetTokenContract({ token, passSigner: true });

return useMutation(
[FunctionKey.APPROVE_TOKEN, { token }],
Expand Down
2 changes: 1 addition & 1 deletion src/clients/api/mutations/borrow/useBorrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const useBorrow = (
{ vToken, poolName }: { vToken: VToken; poolName: string },
options?: Options,
) => {
const vTokenContract = useGetVTokenContract(vToken);
const vTokenContract = useGetVTokenContract({ vToken, passSigner: true });
const { captureAnalyticEvent } = useAnalytics();

return useMutation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Options = MutationObserverOptions<CancelProposalOutput, Error, TrimmedCance
const useCancelProposal = (options?: Options) => {
const governorBravoDelegateContract = useGetUniqueContract({
name: 'governorBravoDelegate',
passSigner: true,
});

return useMutation(
Expand Down
1 change: 1 addition & 0 deletions src/clients/api/mutations/claimRewards/useClaimRewards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Options = MutationObserverOptions<ClaimRewardsOutput, Error, TrimmedClaimRe
const useClaimRewards = (options?: Options) => {
const multicallContract = useGetUniqueContract({
name: 'multicall',
passSigner: true,
});

const mainPoolComptrollerContractAddress = useGetUniqueContractAddress({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Options = MutationObserverOptions<CreateProposalOutput, Error, CreatePropos
const useCreateProposal = (options?: Options) => {
const governorBravoDelegateContract = useGetUniqueContract({
name: 'governorBravoDelegate',
passSigner: true,
});

return useMutation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Options = MutationObserverOptions<ExecuteProposalOutput, Error, TrimmedExec
const useExecuteProposal = (options?: Options) => {
const governorBravoDelegateContract = useGetUniqueContract({
name: 'governorBravoDelegate',
passSigner: true,
});

return useMutation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ const useExecuteWithdrawalFromXvsVault = (
) => {
const xvsVaultContract = useGetUniqueContract({
name: 'xvsVault',
passSigner: true,
});

const { captureAnalyticEvent } = useAnalytics();

return useMutation(
Expand Down
1 change: 1 addition & 0 deletions src/clients/api/mutations/mintVai/useMintVai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Options = MutationObserverOptions<MintVaiOutput, Error, TrimmedClaimRewards
const useMintVai = (options?: Options) => {
const vaiControllerContract = useGetUniqueContract({
name: 'vaiController',
passSigner: true,
});

return useMutation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Options = MutationObserverOptions<QueueProposalOutput, Error, TrimmedQueueP
const useQueueProposal = (options?: Options) => {
const governorBravoDelegateContract = useGetUniqueContract({
name: 'governorBravoDelegate',
passSigner: true,
});

return useMutation(
Expand Down
6 changes: 5 additions & 1 deletion src/clients/api/mutations/redeem/useRedeem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ const useRedeem = (
{ vToken, poolName }: { vToken: VToken; poolName: string },
options?: Options,
) => {
const tokenContract = useGetVTokenContract(vToken);
const tokenContract = useGetVTokenContract({
vToken,
passSigner: true,
});

const { captureAnalyticEvent } = useAnalytics();

return useMutation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const useRedeemUnderlying = (
{ vToken, poolName }: { vToken: VToken; poolName: string },
options?: Options,
) => {
const vTokenContract = useGetVTokenContract(vToken);
const vTokenContract = useGetVTokenContract({ vToken, passSigner: true });

const { captureAnalyticEvent } = useAnalytics();

return useMutation(
Expand Down
1 change: 1 addition & 0 deletions src/clients/api/mutations/repay/useRepay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const useRepay = (
const { captureAnalyticEvent } = useAnalytics();
const maximillionContract = useGetUniqueContract({
name: 'maximillion',
passSigner: true,
});

return useMutation(
Expand Down
1 change: 1 addition & 0 deletions src/clients/api/mutations/repayVai/useRepayVai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Options = MutationObserverOptions<IRepayVaiOutput, Error, TrimmedRepayVai>;
const useRepayVai = (options?: Options) => {
const vaiControllerContract = useGetUniqueContract({
name: 'vaiController',
passSigner: true,
});

return useMutation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Options = MutationObserverOptions<
const useRequestWithdrawalFromXvsVault = (options?: Options) => {
const xvsVaultContract = useGetUniqueContract({
name: 'xvsVault',
passSigner: true,
});
const { captureAnalyticEvent } = useAnalytics();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ type Options = MutationObserverOptions<
>;

const useRevokeSpendingLimit = ({ token }: { token: Token }, options?: Options) => {
const tokenContract = useGetTokenContract(token);
const tokenContract = useGetTokenContract({
token,
passSigner: true,
});

return useMutation(
[FunctionKey.REVOKE_SPENDING_LIMIT, { token }],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Options = MutationObserverOptions<SetVoteDelegateOutput, Error, TrimmedSetV
const useSetVoteDelegate = (options?: Options) => {
const xvsVaultContract = useGetUniqueContract({
name: 'xvsVault',
passSigner: true,
});

return useMutation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Options = MutationObserverOptions<StakeInVaiVaultOutput, Error, TrimmedStak
const useStakeInVaiVault = (options?: Options) => {
const vaiVaultContract = useGetUniqueContract({
name: 'vaiVault',
passSigner: true,
});
const { captureAnalyticEvent } = useAnalytics();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const useStakeInXvsVault = (
) => {
const xvsVaultContract = useGetUniqueContract({
name: 'xvsVault',
passSigner: true,
});
const { captureAnalyticEvent } = useAnalytics();

Expand Down
1 change: 1 addition & 0 deletions src/clients/api/mutations/swapTokens/useSwapTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const useSwapTokens = (
) => {
const swapRouterContract = useGetSwapRouterContract({
comptrollerAddress: poolComptrollerAddress,
passSigner: true,
});
const { captureAnalyticEvent } = useAnalytics();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const useSwapTokensAndRepayAndRepay = (
) => {
const swapRouterContract = useGetSwapRouterContract({
comptrollerAddress: poolComptrollerAddress,
passSigner: true,
});
const { captureAnalyticEvent } = useAnalytics();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const useSwapTokensAndSupply = (
) => {
const swapRouterContract = useGetSwapRouterContract({
comptrollerAddress: poolComptrollerAddress,
passSigner: true,
});
const { captureAnalyticEvent } = useAnalytics();

Expand Down
1 change: 1 addition & 0 deletions src/clients/api/mutations/vote/useCastVote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Options = MutationObserverOptions<CastVoteOutput, Error, TrimmedCastVoteInp
const useCastVote = (options?: Options) => {
const governorBravoDelegateContract = useGetUniqueContract({
name: 'governorBravoDelegate',
passSigner: true,
});
const { captureAnalyticEvent } = useAnalytics();

Expand Down
1 change: 1 addition & 0 deletions src/clients/api/mutations/vote/useCastVoteWithReason.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Options = MutationObserverOptions<CastVoteWithReasonOutput, Error, CastVote
const useCastVoteWithReason = (options?: Options) => {
const governorBravoDelegateContract = useGetUniqueContract({
name: 'governorBravoDelegate',
passSigner: true,
});
const { captureAnalyticEvent } = useAnalytics();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Options = MutationObserverOptions<
const useWithdrawFromVaiVault = (options?: Options) => {
const vaiVaultContract = useGetUniqueContract({
name: 'vaiVault',
passSigner: true,
});
const { captureAnalyticEvent } = useAnalytics();

Expand Down
1 change: 1 addition & 0 deletions src/clients/api/mutations/withdrawXvs/useWithdrawXvs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import useGetUniqueContract from 'hooks/useGetUniqueContract';
const useWithdrawXvs = (options?: MutationObserverOptions<WithdrawXvsOutput, Error>) => {
const xvsVestingContract = useGetUniqueContract({
name: 'xvsVesting',
passSigner: true,
});

return useMutation(
Expand Down
2 changes: 1 addition & 1 deletion src/clients/api/queries/getAllowance/useGetAllowance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const useGetAllowance = (
{ token, spenderAddress, accountAddress }: TrimmedGetAllowanceInput,
options?: Options,
) => {
const tokenContract = useGetTokenContract(token);
const tokenContract = useGetTokenContract({ token });
const queryKey: UseGetAllowanceQueryKey = [
FunctionKey.GET_TOKEN_ALLOWANCE,
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const useGetVTokenBalanceOf = (
{ accountAddress, vToken }: TrimmedGetVTokenBalanceOfInput,
options?: Options,
) => {
const vTokenContract = useGetVTokenContract(vToken);
const vTokenContract = useGetVTokenContract({ vToken });

return useQuery(
[FunctionKey.GET_V_TOKEN_BALANCE, { accountAddress, vTokenAddress: vToken.address }],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Options = QueryObserverOptions<
>;

const useGetVTokenInterestRateModel = ({ vToken }: { vToken: VToken }, options?: Options) => {
const vTokenContract = useGetVTokenContract(vToken);
const vTokenContract = useGetVTokenContract({ vToken });

return useQuery(
[FunctionKey.GET_V_TOKEN_INTEREST_RATE_MODEL, { vTokenAddress: vToken.address }],
Expand Down
26 changes: 14 additions & 12 deletions src/context/AuthContext/useProvider/getProvider.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
import { getPublicClient } from '@wagmi/core';
import { providers } from 'ethers';
import { providers } from '@0xsequence/multicall';
import { PublicClient } from '@wagmi/core';
import { providers as ethersProviders } from 'ethers';
import { type HttpTransport } from 'viem';

// Convert a viem Public Client to an ethers.js Provider
const getProvider = ({ chainId }: { chainId?: number } = {}) => {
const publicClient = getPublicClient({ chainId });
const getProvider = ({ publicClient }: { publicClient: PublicClient }) => {
const { chain, transport } = publicClient;
const network = {
chainId: chain.id,
name: chain.name,
ensAddress: chain.contracts?.ensRegistry?.address,
};

if (transport.type === 'fallback') {
return new providers.FallbackProvider(
(transport.transports as ReturnType<HttpTransport>[]).map(
({ value }) => new providers.JsonRpcProvider(value?.url, network),
),
);
}
const ethersProvider =
transport.type === 'fallback'
? new ethersProviders.FallbackProvider(
(transport.transports as ReturnType<HttpTransport>[]).map(
({ value }) => new ethersProviders.JsonRpcProvider(value?.url, network),
),
)
: new ethersProviders.JsonRpcProvider(transport.url, network);

return new providers.JsonRpcProvider(transport.url, network);
// Wrap with multicall provider
return new providers.MulticallProvider(ethersProvider);
};

export default getProvider;
8 changes: 6 additions & 2 deletions src/context/AuthContext/useProvider/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { ChainId } from 'packages/contracts';
import { useMemo } from 'react';
import { usePublicClient } from 'wagmi';

import getProvider from './getProvider';

export interface UseProviderInput {
chainId?: ChainId;
}

const useProvider = ({ chainId }: UseProviderInput) =>
useMemo(() => getProvider({ chainId }), [chainId]);
const useProvider = ({ chainId }: UseProviderInput) => {
const publicClient = usePublicClient();

return useMemo(() => getProvider({ publicClient }), [chainId, publicClient]);
};

export default useProvider;
1 change: 1 addition & 0 deletions src/hooks/useCollateral/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const useCollateral = () => {

const mainPoolComptrollerContract = useGetUniqueContract({
name: 'mainPoolComptroller',
passSigner: true,
});

const contractToggleCollateral = async ({
Expand Down
7 changes: 5 additions & 2 deletions src/hooks/useGetGenericContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@ import { useAuth } from 'context/AuthContext';
export interface UseGetGenericContractInput<TContractName extends GenericContractName> {
name: TContractName;
address: string;
passSigner?: boolean;
}

function useGetGenericContract<TContractName extends GenericContractName>({
name,
address,
passSigner = false,
}: UseGetGenericContractInput<TContractName>) {
const { signer, provider, chainId } = useAuth();
const signerOrProvider = signer || provider;
const signerOrProvider = passSigner ? signer : provider;

return useMemo(
() =>
chainId !== undefined // Although chainId isn't used, we don't want to fetch any data unless it exists
chainId !== undefined && // Although chainId isn't used, we don't want to fetch any data unless it exists
!!signerOrProvider
? getGenericContract({
name,
address,
Expand Down
10 changes: 7 additions & 3 deletions src/hooks/useGetSwapRouterContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ import { useAuth } from 'context/AuthContext';

export interface UseGetSwapRouterContractInput {
comptrollerAddress: string;
passSigner?: boolean;
}

function useGetSwapRouterContract({ comptrollerAddress }: UseGetSwapRouterContractInput) {
function useGetSwapRouterContract({
comptrollerAddress,
passSigner = false,
}: UseGetSwapRouterContractInput) {
const { signer, provider, chainId } = useAuth();
const signerOrProvider = signer || provider;
const signerOrProvider = passSigner ? signer : provider;

return useMemo(
() =>
chainId !== undefined
chainId !== undefined && !!signerOrProvider
? getSwapRouterContract({
comptrollerAddress,
chainId,
Expand Down
12 changes: 9 additions & 3 deletions src/hooks/useGetTokenContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ import { getTokenContract } from 'utilities';

import { useAuth } from 'context/AuthContext';

const useGetTokenContract = (token: Token) => {
export interface UseGetTokenContractInput {
token: Token;
passSigner?: boolean;
}

const useGetTokenContract = ({ token, passSigner = false }: UseGetTokenContractInput) => {
const { signer, provider, chainId } = useAuth();
const signerOrProvider = signer || provider;
const signerOrProvider = passSigner ? signer : provider;

return useMemo(
() =>
chainId !== undefined // Although chainId isn't used, we don't want to fetch any data unless it exists
chainId !== undefined && // Although chainId isn't used, we don't want to fetch any data unless it exists
!!signerOrProvider
? getTokenContract({ token, signerOrProvider })
: undefined,
[signerOrProvider, chainId, token],
Expand Down
Loading

0 comments on commit 5ec35c0

Please sign in to comment.