-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(wallet): implement fee granter as a global setting
closes #219
- Loading branch information
1 parent
82d385c
commit d8da6a3
Showing
8 changed files
with
151 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
apps/deploy-web/src/components/authorizations/AllowanceWatcher.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { FC } from "react"; | ||
|
||
import { useAllowance } from "@src/hooks/useAllowance"; | ||
|
||
export const AllowanceWatcher: FC = () => { | ||
useAllowance(); | ||
return null; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import React, { FC, useMemo } from "react"; | ||
import isAfter from "date-fns/isAfter"; | ||
import parseISO from "date-fns/parseISO"; | ||
import { OpenNewWindow } from "iconoir-react"; | ||
import difference from "lodash/difference"; | ||
import Link from "next/link"; | ||
import { useSnackbar } from "notistack"; | ||
import { useLocalStorage } from "usehooks-ts"; | ||
|
||
import { Snackbar } from "@src/components/shared/Snackbar"; | ||
import { useWallet } from "@src/context/WalletProvider"; | ||
import { useWhen } from "@src/hooks/useWhen"; | ||
import { useAllowancesGranted } from "@src/queries/useGrantsQuery"; | ||
|
||
const persisted: Record<string, string[]> = typeof window !== "undefined" ? JSON.parse(localStorage.getItem("fee-granters") || "{}") : {}; | ||
|
||
const AllowanceNotificationMessage: FC = () => ( | ||
<> | ||
You can update default fee granter in | ||
<Link href="/settings/authorizations" className="inline-flex items-center space-x-2 !text-white"> | ||
<span>Authorizations Settings</span> | ||
<OpenNewWindow className="text-xs" /> | ||
</Link> | ||
</> | ||
); | ||
|
||
export const useAllowance = () => { | ||
const { address } = useWallet(); | ||
const [defaultFeeGranter, setDefaultFeeGranter] = useLocalStorage<string | undefined>("default-fee-granter", undefined); | ||
const { data: allFeeGranters, isLoading, isFetched } = useAllowancesGranted(address); | ||
const { enqueueSnackbar } = useSnackbar(); | ||
|
||
const actualAddresses = useMemo(() => { | ||
if (!address || !allFeeGranters) { | ||
return []; | ||
} | ||
|
||
return allFeeGranters.reduce((acc, grant) => { | ||
if (isAfter(parseISO(grant.allowance.expiration), new Date())) { | ||
acc.push(grant.granter); | ||
} | ||
|
||
return acc; | ||
}, [] as string[]); | ||
}, [allFeeGranters, address]); | ||
|
||
useWhen( | ||
isFetched && address, | ||
() => { | ||
const persistedAddresses = persisted[address] || []; | ||
const added = difference(actualAddresses, persistedAddresses); | ||
const removed = difference(persistedAddresses, actualAddresses); | ||
|
||
if (added.length || removed.length) { | ||
persisted[address] = actualAddresses; | ||
localStorage.setItem(`fee-granters`, JSON.stringify(persisted)); | ||
} | ||
|
||
if (added.length) { | ||
enqueueSnackbar(<Snackbar iconVariant="info" title="New fee allowance granted" subTitle={<AllowanceNotificationMessage />} />, { | ||
variant: "info" | ||
}); | ||
} | ||
|
||
if (removed.length) { | ||
enqueueSnackbar(<Snackbar iconVariant="warning" title="Some fee allowance is revoked or expired" subTitle={<AllowanceNotificationMessage />} />, { | ||
variant: "warning" | ||
}); | ||
} | ||
|
||
if (defaultFeeGranter && removed.includes(defaultFeeGranter)) { | ||
setDefaultFeeGranter(undefined); | ||
} | ||
}, | ||
[actualAddresses, persisted] | ||
); | ||
|
||
return useMemo( | ||
() => ({ | ||
fee: { | ||
all: allFeeGranters, | ||
default: defaultFeeGranter, | ||
setDefault: setDefaultFeeGranter, | ||
isLoading | ||
} | ||
}), | ||
[defaultFeeGranter, setDefaultFeeGranter, allFeeGranters, isLoading] | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { useEffect } from "react"; | ||
|
||
export function useWhen<T>(condition: T, run: () => void, deps: unknown[] = []): void { | ||
return useEffect(() => { | ||
if (condition) { | ||
run(); | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [condition, ...deps]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters