-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Account permission management (#276)
* feat: account permission management * ref: deps * feat(account-settings): hooks are taking in now arrays * ref(deps) * feat(AddPermissionModal) * feat(transferownership): added modal * feat: remove all permission button * feat(Modals): passing in the refetch function to modals * ref(deps) * fix: account management issues and improvements * fix: deps --------- Co-authored-by: fritzschoff <[email protected]> Co-authored-by: max <[email protected]>
- Loading branch information
1 parent
3285fb9
commit f9f7a8b
Showing
25 changed files
with
997 additions
and
5 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
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 @@ | ||
export * from './useAccountPermissions'; |
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,13 @@ | ||
{ | ||
"name": "@snx-v3/useAccountPermissions", | ||
"private": true, | ||
"main": "index.ts", | ||
"version": "0.0.1", | ||
"dependencies": { | ||
"@snx-v3/useAccountProxy": "workspace:*", | ||
"@snx-v3/useBlockchain": "workspace:*", | ||
"@snx-v3/useCoreProxy": "workspace:*", | ||
"@tanstack/react-query": "^5.8.3", | ||
"ethers": "^5.7.2" | ||
} | ||
} |
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,43 @@ | ||
import { useAccountProxy } from '@snx-v3/useAccountProxy'; | ||
import { useNetwork } from '@snx-v3/useBlockchain'; | ||
import { useCoreProxy } from '@snx-v3/useCoreProxy'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { utils } from 'ethers'; | ||
|
||
export function useAccountPermissions(accountId: string | undefined) { | ||
const { data: CoreProxy } = useCoreProxy(); | ||
const { network } = useNetwork(); | ||
|
||
return useQuery({ | ||
queryKey: [`${network?.id}-${network?.preset}`, 'account-permissions', accountId], | ||
queryFn: async function () { | ||
if (!CoreProxy || !accountId) throw new Error('Should be disabled'); | ||
const permissions = await CoreProxy.getAccountPermissions(accountId); | ||
|
||
return permissions.reduce( | ||
(acc, { user, permissions }) => ({ | ||
...acc, | ||
[user.toLowerCase()]: permissions.map((r: string) => utils.parseBytes32String(r)), | ||
}), | ||
{} | ||
) as { | ||
[key: string]: string[]; | ||
}; | ||
}, | ||
enabled: Boolean(CoreProxy?.address), | ||
}); | ||
} | ||
|
||
export function useAccountOwner(accountId: string | undefined) { | ||
const { data: AccountProxy } = useAccountProxy(); | ||
const { network } = useNetwork(); | ||
|
||
return useQuery({ | ||
queryKey: [`${network?.id}-${network?.preset}`, 'account-owner', accountId], | ||
queryFn: async function () { | ||
if (!AccountProxy || !accountId) throw new Error('Should be disabled'); | ||
return await AccountProxy.ownerOf(accountId); | ||
}, | ||
enabled: Boolean(AccountProxy?.address), | ||
}); | ||
} |
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 @@ | ||
export * from './useManagePermissions'; |
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,12 @@ | ||
{ | ||
"name": "@snx-v3/useManagePermissions", | ||
"private": true, | ||
"main": "index.ts", | ||
"version": "0.0.1", | ||
"dependencies": { | ||
"@snx-v3/useCoreProxy": "workspace:*", | ||
"@snx-v3/useMulticall3": "workspace:*", | ||
"@tanstack/react-query": "^5.8.3", | ||
"ethers": "^5.7.2" | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
liquidity/lib/useManagePermissions/useManagePermissions.ts
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,79 @@ | ||
import { utils } from 'ethers'; | ||
import { useCoreProxy } from '@snx-v3/useCoreProxy'; | ||
import { useMutation } from '@tanstack/react-query'; | ||
import { useMulticall3 } from '@snx-v3/useMulticall3'; | ||
|
||
type Permissions = Array<string>; | ||
const getPermissionDiff = ( | ||
existing: Permissions, | ||
selected: Permissions | ||
): { | ||
grants: Permissions; | ||
revokes: Permissions; | ||
} => { | ||
let grants: Permissions = [], | ||
revokes: Permissions = []; | ||
existing.concat(selected).forEach((permission) => { | ||
if (!existing.includes(permission)) { | ||
grants = [...grants, permission]; | ||
} | ||
if (!selected.includes(permission)) { | ||
revokes = [...revokes, permission]; | ||
} | ||
}); | ||
return { grants, revokes }; | ||
}; | ||
|
||
export const useManagePermissions = ({ | ||
accountId, | ||
target, | ||
existing = [], | ||
selected = [], | ||
}: { | ||
accountId: string; | ||
target: string; | ||
existing: Permissions; | ||
selected: Permissions; | ||
}) => { | ||
const { data: CoreProxy } = useCoreProxy(); | ||
const { data: multicall } = useMulticall3(); | ||
|
||
return useMutation({ | ||
mutationFn: async () => { | ||
if (!CoreProxy || !multicall) { | ||
return; | ||
} | ||
|
||
const { grants, revokes } = getPermissionDiff(existing, selected); | ||
|
||
try { | ||
const grantCalls = grants.map((permission) => ({ | ||
target: CoreProxy.address, | ||
callData: CoreProxy.interface.encodeFunctionData('grantPermission', [ | ||
accountId, | ||
utils.formatBytes32String(permission), | ||
target, | ||
]), | ||
allowFailure: false, | ||
requireSuccess: true, | ||
})); | ||
|
||
const revokeCalls = revokes.map((permission) => ({ | ||
target: CoreProxy.address, | ||
callData: CoreProxy.interface.encodeFunctionData('revokePermission', [ | ||
accountId, | ||
utils.formatBytes32String(permission), | ||
target, | ||
]), | ||
allowFailure: false, | ||
requireSuccess: true, | ||
})); | ||
|
||
const tx = await multicall.aggregate3([...grantCalls, ...revokeCalls]); | ||
await tx.wait(); | ||
} catch (error: any) { | ||
throw error; | ||
} | ||
}, | ||
}); | ||
}; |
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 @@ | ||
export * from './useTransferAccountId'; |
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,11 @@ | ||
{ | ||
"name": "@snx-v3/useTransferAccountId", | ||
"private": true, | ||
"main": "index.ts", | ||
"version": "0.0.1", | ||
"dependencies": { | ||
"@snx-v3/useAccountProxy": "workspace:*", | ||
"@snx-v3/useBlockchain": "workspace:*", | ||
"@tanstack/react-query": "^5.8.3" | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
liquidity/lib/useTransferAccountId/useTransferAccountId.ts
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,18 @@ | ||
import { useAccountProxy } from '@snx-v3/useAccountProxy'; | ||
import { useWallet } from '@snx-v3/useBlockchain'; | ||
import { useMutation } from '@tanstack/react-query'; | ||
|
||
export function useTransferAccountId(to: string, accountId: string) { | ||
const { data: AccountProxy } = useAccountProxy(); | ||
const { activeWallet } = useWallet(); | ||
|
||
return useMutation({ | ||
mutationFn: async () => { | ||
if (!AccountProxy) throw new Error('CoreProxy or Multicall not defined'); | ||
if (!activeWallet?.address) throw new Error('Wallet is not connected'); | ||
const tx = await AccountProxy.transferFrom(activeWallet.address, to, accountId); | ||
const response = await tx.wait(); | ||
return response; | ||
}, | ||
}); | ||
} |
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,43 @@ | ||
import { CopyIcon, ExternalLinkIcon } from '@chakra-ui/icons'; | ||
import { Flex, Tooltip } from '@chakra-ui/react'; | ||
import { etherscanLink } from '@snx-v3/etherscanLink'; | ||
import { prettyString } from '@snx-v3/format'; | ||
import { useNetwork } from '@snx-v3/useBlockchain'; | ||
import { FC, useMemo } from 'react'; | ||
|
||
interface AddressProps { | ||
address: string; | ||
} | ||
|
||
export const Address: FC<AddressProps> = ({ address }) => { | ||
const { network } = useNetwork(); | ||
const link = useMemo( | ||
() => | ||
etherscanLink({ | ||
chain: network?.name || '', | ||
address, | ||
}), | ||
[address, network?.name] | ||
); | ||
return ( | ||
<Flex alignItems="center" gap={2}> | ||
<Tooltip label={address}>{prettyString(address)}</Tooltip> | ||
<CopyIcon | ||
onClick={() => { | ||
navigator.clipboard.writeText(address); | ||
}} | ||
cursor="pointer" | ||
_hover={{ | ||
color: 'cyan', | ||
}} | ||
/> | ||
<a target="_blank" href={link} rel="noreferrer"> | ||
<ExternalLinkIcon | ||
_hover={{ | ||
color: 'cyan', | ||
}} | ||
/> | ||
</a> | ||
</Flex> | ||
); | ||
}; |
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 @@ | ||
export * from './Address'; |
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 @@ | ||
export const permissionsList = ['ADMIN', 'BURN', 'DELEGATE', 'MINT', 'REWARDS', 'WITHDRAW']; |
Oops, something went wrong.