From b365e15d96bce8e277a15595dd8bcb70b596ec9a Mon Sep 17 00:00:00 2001 From: fabiolalombardim <37227394+fabiolalombardim@users.noreply.github.com> Date: Wed, 9 Aug 2023 20:15:21 +0200 Subject: [PATCH] Delegation options & status updates (#628) * new options & refetch * Fix Voting Weight Logic Signed-off-by: Manank Patni --------- Signed-off-by: Manank Patni Co-authored-by: Manank Patni --- .../User/components/DelegationBanner.tsx | 2 +- src/services/bakingBad/delegations/index.ts | 109 +++++++++++++----- .../token/hooks/useDelegationVoteWeight.ts | 4 +- 3 files changed, 86 insertions(+), 29 deletions(-) diff --git a/src/modules/explorer/pages/User/components/DelegationBanner.tsx b/src/modules/explorer/pages/User/components/DelegationBanner.tsx index ac782db0..e8a50c75 100644 --- a/src/modules/explorer/pages/User/components/DelegationBanner.tsx +++ b/src/modules/explorer/pages/User/components/DelegationBanner.tsx @@ -54,7 +54,7 @@ export const Delegation: React.FC<{ daoId: string }> = ({ daoId }) => { const { data: delegatedTo, isLoading, refetch } = useDelegationStatus(dao?.data.token.contract) const [delegationStatus, setDelegationStatus] = useState(DelegationsType.NOT_DELEGATING) const [openModal, setOpenModal] = useState(false) - const { data: delegateVoteBalances } = useDelegationVoteWeight(dao?.data.token.contract) + const { data: delegateVoteBalances } = useDelegationVoteWeight(dao?.data.token.contract, dao?.data.address) const [voteWeight, setVoteWeight] = useState(new BigNumber(0)) // console.log("voteWeight: ", voteWeight.toString()) diff --git a/src/services/bakingBad/delegations/index.ts b/src/services/bakingBad/delegations/index.ts index a2a4a7eb..b6262ebb 100644 --- a/src/services/bakingBad/delegations/index.ts +++ b/src/services/bakingBad/delegations/index.ts @@ -2,6 +2,7 @@ import { Network } from "services/beacon" import { networkNameMap } from ".." import { DelegationDTO, TokenDelegationDTO, UserDelegateBalance } from "./types" import { getUserTokenBalance } from "../tokenBalances" +import BigNumber from "bignumber.js" export const getLatestDelegation = async (daoAddress: string, network: Network) => { const url = `https://api.${networkNameMap[network]}.tzkt.io/v1/operations/delegations?sender=${daoAddress}&status=applied` @@ -21,6 +22,7 @@ export const getLatestDelegation = async (daoAddress: string, network: Network) export const getTokenDelegation = async (tokenAddress: string, account: string, network: Network) => { const url = `https://api.${networkNameMap[network]}.tzkt.io/v1/contracts/${tokenAddress}/bigmaps/delegates/keys?key.eq=${account}&active=true` + console.log("urlssdasd: ", url) const response = await fetch(url) if (!response.ok) { @@ -38,44 +40,99 @@ export const getTokenDelegation = async (tokenAddress: string, account: string, return delegatedTo } -export const getTokenDelegationVoteWeight = async (tokenAddress: string, account: string, network: Network) => { - const selfBalance = await getUserTokenBalance(account, network, tokenAddress) +export const getUserDAODepositBalance = async (account: string, network: Network, daoAddress: string) => { + const url = `https://api.${network}.tzkt.io/v1/contracts/${daoAddress}/bigmaps/freeze_history/keys?key.eq=${account}` - if (!selfBalance) { - throw new Error("Could not fetch delegate token balance from the TZKT API") - } - - const url = `https://api.${networkNameMap[network]}.tzkt.io/v1/contracts/${tokenAddress}/bigmaps/delegates/keys?value.eq=${account}&active=true` const response = await fetch(url) if (!response.ok) { throw new Error("Failed to fetch token delegations from TZKT API") } - const resultingDelegations: TokenDelegationDTO[] = await response.json() + const userStakedBalances = await response.json() - const delegateBalance: UserDelegateBalance = { - address: account, - balance: selfBalance - } + let userDAODepositBalance = new BigNumber(0) - if (resultingDelegations.length === 0) { - return [delegateBalance] + if (userStakedBalances && userStakedBalances[0]) { + const userStakedBalance = new BigNumber(userStakedBalances[0].value.staked) + const userCurrentUnstakedBalance = new BigNumber(userStakedBalances[0].value.current_unstaked) + const userPastUnstakedBalance = new BigNumber(userStakedBalances[0].value.past_unstaked) + + userDAODepositBalance = userStakedBalance.plus(userCurrentUnstakedBalance).plus(userPastUnstakedBalance) } - const delegatedAddressBalances: UserDelegateBalance[] = [] + return userDAODepositBalance.toString() +} - await Promise.all( - resultingDelegations.map(async del => { - const balance = await getUserTokenBalance(del.key, network, tokenAddress) - if (balance) { - delegatedAddressBalances.push({ - address: del.key, - balance: balance - }) +export const getTokenDelegationVoteWeight = async ( + tokenAddress: string, + account: string, + network: Network, + daoContract?: string +) => { + const tokenDelegationStatus = await getTokenDelegation(tokenAddress, account, network) + console.log("tokenDelegationStatus: ", tokenDelegationStatus) + if (tokenDelegationStatus && tokenDelegationStatus !== account) { + return [] + } else { + const selfBalance = await getUserTokenBalance(account, network, tokenAddress) + if (!selfBalance) { + throw new Error("Could not fetch delegate token balance from the TZKT API") + } + console.log("selfBalance: ", selfBalance) + + let selfDAOBalance + + console.log("daoContract: ", daoContract) + if (daoContract) { + selfDAOBalance = await getUserDAODepositBalance(account, network, daoContract) + console.log("selfDAOBalance: ", selfDAOBalance) + if (!selfDAOBalance) { + throw new Error("Could not fetch delegate dao balance from the TZKT API") + } + + const url = `https://api.${networkNameMap[network]}.tzkt.io/v1/contracts/${tokenAddress}/bigmaps/delegates/keys?value.eq=${account}&active=true` + console.log("ursssssl: ", url) + const response = await fetch(url) + + if (!response.ok) { + throw new Error("Failed to fetch token delegations from TZKT API") + } + + const resultingDelegations: TokenDelegationDTO[] = await response.json() + + const delegateBalance: UserDelegateBalance = { + address: account, + balance: selfDAOBalance + ? new BigNumber(selfBalance).plus(new BigNumber(selfDAOBalance)).toString() + : selfBalance + } + + if (resultingDelegations.length === 0) { + return [delegateBalance] } - }) - ) - return delegatedAddressBalances + const delegatedAddressBalances: UserDelegateBalance[] = [delegateBalance] + + await Promise.all( + resultingDelegations.map(async del => { + if (del.key === del.value) { + return + } + const balance = await getUserTokenBalance(del.key, network, tokenAddress) + const userDAOBalance = await getUserDAODepositBalance(del.key, network, daoContract) + if (balance) { + delegatedAddressBalances.push({ + address: del.key, + balance: userDAOBalance ? new BigNumber(userDAOBalance).plus(new BigNumber(balance)).toString() : balance + }) + } + }) + ) + + console.log("delegatedAddressBalances: ", delegatedAddressBalances) + + return delegatedAddressBalances + } + } } diff --git a/src/services/contracts/token/hooks/useDelegationVoteWeight.ts b/src/services/contracts/token/hooks/useDelegationVoteWeight.ts index 93a26dff..b5048842 100644 --- a/src/services/contracts/token/hooks/useDelegationVoteWeight.ts +++ b/src/services/contracts/token/hooks/useDelegationVoteWeight.ts @@ -3,14 +3,14 @@ import { getTokenDelegationVoteWeight } from "services/bakingBad/delegations" import { UserDelegateBalance } from "services/bakingBad/delegations/types" import { useTezos } from "services/beacon/hooks/useTezos" -export const useDelegationVoteWeight = (tokenAddress: string | undefined) => { +export const useDelegationVoteWeight = (tokenAddress: string | undefined, daoContract?: string | undefined) => { const { network, account } = useTezos() const { data, ...rest } = useQuery( ["delegationVoteWeight", tokenAddress], async () => { if (tokenAddress) { - return await getTokenDelegationVoteWeight(tokenAddress, account, network) + return await getTokenDelegationVoteWeight(tokenAddress, account, network, daoContract) } }, {