Skip to content

Instantly share code, notes, and snippets.

@sbauch
Created January 18, 2023 16:20
Show Gist options
  • Save sbauch/8d8121f906a28f7e88df262bb6d4517a to your computer and use it in GitHub Desktop.
Save sbauch/8d8121f906a28f7e88df262bb6d4517a to your computer and use it in GitHub Desktop.
Example hook for using delegate cash with wagmi hooks.
import { useAccount, useContractRead } from 'wagmi';
import { abi, address as delegateCashAddress } from '~abis/delegateCash';
import { env } from '~env/client.mjs';
export function useDelegatedAccount() {
const { address, isConnected, ...account } = useAccount();
const txConfig = {
address: delegateCashAddress,
abi,
chainId: env.NEXT_PUBLIC_MATIC_CHAIN_ID,
enabled: isConnected,
};
const { data: delegationsByDelegate } = useContractRead({
...txConfig,
functionName: 'getDelegationsByDelegate',
args: [address as `0x${string}`],
});
const { data: delegatesForAll } = useContractRead({
...txConfig,
functionName: 'getDelegatesForAll',
args: [address as `0x${string}`],
});
return {
address,
...account,
isConnected,
vaultAddress: delegationsByDelegate?.[0]?.vault,
delegatedAddress: delegatesForAll?.[0],
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment