Skip to content

Commit

Permalink
Delegation options & status updates (#628)
Browse files Browse the repository at this point in the history
* new options & refetch

* Fix Voting Weight Logic

Signed-off-by: Manank Patni <[email protected]>

---------

Signed-off-by: Manank Patni <[email protected]>
Co-authored-by: Manank Patni <[email protected]>
  • Loading branch information
fabiolalombardim and Man-Jain authored Aug 9, 2023
1 parent 1e3a849 commit b365e15
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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>(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())

Expand Down
109 changes: 83 additions & 26 deletions src/services/bakingBad/delegations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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) {
Expand All @@ -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
}
}
}
4 changes: 2 additions & 2 deletions src/services/contracts/token/hooks/useDelegationVoteWeight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<UserDelegateBalance[] | undefined, Error>(
["delegationVoteWeight", tokenAddress],
async () => {
if (tokenAddress) {
return await getTokenDelegationVoteWeight(tokenAddress, account, network)
return await getTokenDelegationVoteWeight(tokenAddress, account, network, daoContract)
}
},
{
Expand Down

0 comments on commit b365e15

Please sign in to comment.