Skip to content

Commit

Permalink
Add preset to query cache (#11)
Browse files Browse the repository at this point in the history
* Add preset to the query cache

* Log preset

* Add preset to all query guards
  • Loading branch information
noisekit authored Oct 8, 2024
1 parent 8dcadd1 commit ee7727d
Show file tree
Hide file tree
Showing 40 changed files with 254 additions and 161 deletions.
9 changes: 5 additions & 4 deletions src/useAccountAvailableCollateral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,26 @@ export function useAccountAvailableCollateral({
accountId?: ethers.BigNumberish;
collateralTypeTokenAddress?: string;
}) {
const { chainId } = useSynthetix();
const { chainId, preset } = useSynthetix();
const errorParser = useErrorParser();

const { data: CoreProxyContract } = useImportContract('CoreProxy');

return useQuery<ethers.BigNumber>({
enabled: Boolean(chainId && provider && CoreProxyContract?.address && accountId && collateralTypeTokenAddress),
enabled: Boolean(chainId && preset && provider && CoreProxyContract?.address && accountId && collateralTypeTokenAddress),
queryKey: [
chainId,
preset,
'AccountAvailableCollateral',
{ CoreProxy: CoreProxyContract?.address },
{ accountId: accountId ? ethers.BigNumber.from(accountId).toHexString() : undefined, collateralTypeTokenAddress },
],
queryFn: async () => {
if (!(chainId && provider && CoreProxyContract?.address && accountId && collateralTypeTokenAddress)) {
if (!(chainId && preset && provider && CoreProxyContract?.address && accountId && collateralTypeTokenAddress)) {
throw 'OMFG';
}

log({ chainId, provider, CoreProxyContract, accountId, collateralTypeTokenAddress });
log({ chainId, preset, CoreProxyContract, accountId, collateralTypeTokenAddress });

const accountAvailableCollateral = fetchAccountAvailableCollateral({
provider,
Expand Down
7 changes: 5 additions & 2 deletions src/useAccountCollateral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function useAccountCollateral({
accountId?: ethers.BigNumberish;
collateralTypeTokenAddress?: string;
}) {
const { chainId } = useSynthetix();
const { chainId, preset } = useSynthetix();
const errorParser = useErrorParser();

const { data: CoreProxyContract } = useImportContract('CoreProxy');
Expand All @@ -30,6 +30,7 @@ export function useAccountCollateral({
return useQuery({
enabled: Boolean(
chainId &&
preset &&
provider &&
CoreProxyContract?.address &&
MulticallContract?.address &&
Expand All @@ -39,6 +40,7 @@ export function useAccountCollateral({
),
queryKey: [
chainId,
preset,
'AccountCollateral',
{ CoreProxy: CoreProxyContract?.address, Multicall: MulticallContract?.address },
{ accountId: accountId ? ethers.BigNumber.from(accountId).toHexString() : undefined, collateralTypeTokenAddress },
Expand All @@ -47,6 +49,7 @@ export function useAccountCollateral({
if (
!(
chainId &&
preset &&
provider &&
CoreProxyContract?.address &&
MulticallContract?.address &&
Expand All @@ -60,7 +63,7 @@ export function useAccountCollateral({

log({
chainId,
provider,
preset,
CoreProxyContract,
MulticallContract,
accountId,
Expand Down
9 changes: 5 additions & 4 deletions src/useAccountLastInteraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,26 @@ export function useAccountLastInteraction({
provider?: ethers.providers.BaseProvider;
accountId?: ethers.BigNumberish;
}) {
const { chainId } = useSynthetix();
const { chainId, preset } = useSynthetix();
const errorParser = useErrorParser();

const { data: CoreProxyContract } = useImportContract('CoreProxy');

return useQuery({
enabled: Boolean(chainId && provider && CoreProxyContract?.address && accountId),
enabled: Boolean(chainId && preset && provider && CoreProxyContract?.address && accountId),
queryKey: [
chainId,
preset,
'AccountLastInteraction',
{ CoreProxy: CoreProxyContract?.address },
{ accountId: accountId ? ethers.BigNumber.from(accountId).toHexString() : undefined },
],
queryFn: async () => {
if (!(chainId && provider && CoreProxyContract?.address && accountId)) {
if (!(chainId && preset && provider && CoreProxyContract?.address && accountId)) {
throw 'OMFG';
}

log({ chainId, provider, CoreProxyContract, accountId });
log({ chainId, preset, CoreProxyContract, accountId });

const CoreProxy = new ethers.Contract(CoreProxyContract.address, CoreProxyContract.abi, provider);

Expand Down
10 changes: 5 additions & 5 deletions src/useAccountTimeoutWithdraw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ import { useSynthetix } from './useSynthetix';
const log = debug('snx:useAccountTimeoutWithdraw');

export function useAccountTimeoutWithdraw({ provider }: { provider?: ethers.providers.BaseProvider }) {
const { chainId } = useSynthetix();
const { chainId, preset } = useSynthetix();
const errorParser = useErrorParser();

const { data: CoreProxyContract } = useImportContract('CoreProxy');

return useQuery<ethers.BigNumber>({
enabled: Boolean(chainId && provider && CoreProxyContract?.address),
queryKey: [chainId, 'ConfigUint accountTimeoutWithdraw', { CoreProxy: CoreProxyContract?.address }],
enabled: Boolean(chainId && preset && provider && CoreProxyContract?.address),
queryKey: [chainId, preset, 'ConfigUint accountTimeoutWithdraw', { CoreProxy: CoreProxyContract?.address }],
queryFn: async () => {
if (!(chainId && provider && CoreProxyContract?.address)) {
if (!(chainId && preset && provider && CoreProxyContract?.address)) {
throw 'OMFG';
}

log({ chainId, provider, CoreProxyContract });
log({ chainId, preset, CoreProxyContract });

const CoreProxy = new ethers.Contract(CoreProxyContract.address, CoreProxyContract.abi, provider);

Expand Down
10 changes: 5 additions & 5 deletions src/useAccounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ import { useSynthetix } from './useSynthetix';
const log = debug('snx:useAccounts');

export function useAccounts({ provider, walletAddress }: { walletAddress?: string; provider?: ethers.providers.BaseProvider }) {
const { chainId } = useSynthetix();
const { chainId, preset } = useSynthetix();
const errorParser = useErrorParser();
const { data: AccountProxyContract } = useImportContract('AccountProxy');

return useQuery<ethers.BigNumber[]>({
enabled: Boolean(chainId && AccountProxyContract?.address && walletAddress && provider),
queryKey: [chainId, 'Accounts', { AccountProxy: AccountProxyContract?.address }, { ownerAddress: walletAddress }],
enabled: Boolean(chainId && preset && provider && AccountProxyContract?.address && walletAddress),
queryKey: [chainId, preset, 'Accounts', { AccountProxy: AccountProxyContract?.address }, { ownerAddress: walletAddress }],
queryFn: async () => {
if (!(chainId && AccountProxyContract?.address && walletAddress && provider)) throw 'OMFG';
if (!(chainId && preset && provider && AccountProxyContract?.address && walletAddress)) throw 'OMFG';

log({ chainId, AccountProxyContract, walletAddress, provider });
log({ chainId, preset, AccountProxyContract, walletAddress });

const AccountProxy = new ethers.Contract(AccountProxyContract.address, AccountProxyContract.abi, provider);
const numberOfAccountTokens = await AccountProxy.balanceOf(walletAddress);
Expand Down
6 changes: 3 additions & 3 deletions src/useAllPriceFeeds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import { useSynthetix } from './useSynthetix';
const log = debug('snx:useAllPriceFeeds');

export function useAllPriceFeeds() {
const { chainId } = useSynthetix();
const { chainId, preset } = useSynthetix();
const { data: extras } = useImportExtras();
const errorParser = useErrorParser();

return useQuery<string[]>({
enabled: Boolean(chainId && extras),
queryKey: [chainId, 'AllPriceFeeds'],
queryKey: [chainId, preset, 'AllPriceFeeds'],
queryFn: async () => {
if (!(chainId && extras)) {
throw 'OMFG';
}

log({ chainId, extras });
log({ chainId, preset, extras });

const result = Object.entries(extras)
.filter(
Expand Down
10 changes: 7 additions & 3 deletions src/useBurnUsd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function useBurnUsd({
poolId?: ethers.BigNumberish;
onSuccess: () => void;
}) {
const { chainId, queryClient } = useSynthetix();
const { chainId, preset, queryClient } = useSynthetix();
const errorParser = useErrorParser();

const { data: systemToken } = useImportSystemToken();
Expand All @@ -44,6 +44,7 @@ export function useBurnUsd({
if (
!(
chainId &&
preset &&
provider &&
walletAddress &&
accountId &&
Expand All @@ -61,7 +62,7 @@ export function useBurnUsd({

log({
chainId,
provider,
preset,
walletAddress,
accountId,
poolId,
Expand Down Expand Up @@ -136,14 +137,15 @@ export function useBurnUsd({

if (priceUpdated) {
queryClient.invalidateQueries({
queryKey: [chainId, 'PriceUpdateTxn'],
queryKey: [chainId, preset, 'PriceUpdateTxn'],
});
}

// Intentionally do not await
queryClient.invalidateQueries({
queryKey: [
chainId,
preset,
'PositionDebt',
{ CoreProxy: CoreProxyContract?.address, Multicall: MulticallContract?.address },
{
Expand All @@ -155,6 +157,7 @@ export function useBurnUsd({
queryClient.invalidateQueries({
queryKey: [
chainId,
preset,
'AccountAvailableCollateral',
{ CoreProxy: CoreProxyContract?.address },
{
Expand All @@ -166,6 +169,7 @@ export function useBurnUsd({
queryClient.invalidateQueries({
queryKey: [
chainId,
preset,
'AccountLastInteraction',
{ CoreProxy: CoreProxyContract?.address },
{ accountId: accountId ? ethers.BigNumber.from(accountId).toHexString() : undefined },
Expand Down
10 changes: 7 additions & 3 deletions src/useClaimReward.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function useClaimReward({
accountId?: ethers.BigNumberish;
onSuccess: () => void;
}) {
const { chainId, queryClient } = useSynthetix();
const { chainId, preset, queryClient } = useSynthetix();
const errorParser = useErrorParser();

const { data: priceIds } = useAllPriceFeeds();
Expand All @@ -40,6 +40,7 @@ export function useClaimReward({
if (
!(
chainId &&
preset &&
provider &&
walletAddress &&
accountId &&
Expand All @@ -55,7 +56,7 @@ export function useClaimReward({

log({
chainId,
provider,
preset,
walletAddress,
accountId,
collateralTypeTokenAddress,
Expand Down Expand Up @@ -126,14 +127,15 @@ export function useClaimReward({

if (priceUpdated) {
queryClient.invalidateQueries({
queryKey: [chainId, 'PriceUpdateTxn'],
queryKey: [chainId, preset, 'PriceUpdateTxn'],
});
}

// Intentionally do not await
queryClient.invalidateQueries({
queryKey: [
chainId,
preset,
'AccountCollateral',
{ CoreProxy: CoreProxyContract?.address, Multicall: MulticallContract?.address },
{
Expand All @@ -145,6 +147,7 @@ export function useClaimReward({
queryClient.invalidateQueries({
queryKey: [
chainId,
preset,
'AccountAvailableCollateral',
{ CoreProxy: CoreProxyContract?.address },
{
Expand All @@ -156,6 +159,7 @@ export function useClaimReward({
queryClient.invalidateQueries({
queryKey: [
chainId,
preset,
'Balance',
{
collateralTypeTokenAddress,
Expand Down
23 changes: 19 additions & 4 deletions src/useCollateralPrice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function useCollateralPrice({
provider?: ethers.providers.BaseProvider;
collateralTypeTokenAddress?: string;
}) {
const { chainId } = useSynthetix();
const { chainId, preset } = useSynthetix();
const errorParser = useErrorParser();

const { data: priceUpdateTxn } = usePriceUpdateTxn({ provider });
Expand All @@ -27,24 +27,39 @@ export function useCollateralPrice({

return useQuery<ethers.BigNumber>({
enabled: Boolean(
chainId && provider && CoreProxyContract?.address && MulticallContract?.address && collateralTypeTokenAddress && priceUpdateTxn
chainId &&
preset &&
provider &&
CoreProxyContract?.address &&
MulticallContract?.address &&
collateralTypeTokenAddress &&
priceUpdateTxn
),
queryKey: [
chainId,
preset,
'CollateralPrice',
{ CoreProxy: CoreProxyContract?.address, Multicall: MulticallContract?.address },
{ collateralTypeTokenAddress },
],
queryFn: async () => {
if (
!(chainId && provider && CoreProxyContract?.address && MulticallContract?.address && collateralTypeTokenAddress && priceUpdateTxn)
!(
chainId &&
preset &&
provider &&
CoreProxyContract?.address &&
MulticallContract?.address &&
collateralTypeTokenAddress &&
priceUpdateTxn
)
) {
throw 'OMFG';
}

log({
chainId,
provider,
preset,
CoreProxyContract,
MulticallContract,
collateralTypeTokenAddress,
Expand Down
6 changes: 3 additions & 3 deletions src/useCreateAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ export function useCreateAccount({
walletAddress,
handleAccountCreated,
}: { provider?: ethers.providers.Web3Provider; walletAddress?: string; handleAccountCreated: (accountId: string) => void }) {
const { chainId, queryClient } = useSynthetix();
const { chainId, preset, queryClient } = useSynthetix();

const { data: CoreProxyContract } = useImportContract('CoreProxy');
const { data: AccountProxyContract } = useImportContract('AccountProxy');

return useMutation({
mutationFn: async () => {
if (!(chainId && provider && CoreProxyContract && AccountProxyContract && walletAddress && queryClient)) throw 'OMFG';
if (!(chainId && preset && provider && CoreProxyContract && AccountProxyContract && walletAddress && queryClient)) throw 'OMFG';

log({ chainId, provider, CoreProxyContract, AccountProxyContract, walletAddress, queryClient });
log({ chainId, preset, CoreProxyContract, AccountProxyContract, walletAddress, queryClient });

const signer = provider.getSigner(walletAddress);
const CoreProxy = new ethers.Contract(CoreProxyContract.address, CoreProxyContract.abi, signer);
Expand Down
Loading

0 comments on commit ee7727d

Please sign in to comment.