Skip to content

Commit

Permalink
Merge pull request #181 from lidofinance/develop
Browse files Browse the repository at this point in the history
Merge main into develop
  • Loading branch information
AnnaSila authored Aug 26, 2024
2 parents 0c0ad63 + e6b487f commit bcb73e3
Show file tree
Hide file tree
Showing 81 changed files with 3,162 additions and 181 deletions.
846 changes: 749 additions & 97 deletions abi/AragonVoting.abi.json

Large diffs are not rendered by default.

79 changes: 79 additions & 0 deletions abi/Snapshot.abi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
[
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "delegator",
"type": "address"
},
{
"indexed": true,
"internalType": "bytes32",
"name": "id",
"type": "bytes32"
},
{
"indexed": true,
"internalType": "address",
"name": "delegate",
"type": "address"
}
],
"name": "ClearDelegate",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "delegator",
"type": "address"
},
{
"indexed": true,
"internalType": "bytes32",
"name": "id",
"type": "bytes32"
},
{
"indexed": true,
"internalType": "address",
"name": "delegate",
"type": "address"
}
],
"name": "SetDelegate",
"type": "event"
},
{
"inputs": [{ "internalType": "bytes32", "name": "id", "type": "bytes32" }],
"name": "clearDelegate",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{ "internalType": "address", "name": "", "type": "address" },
{ "internalType": "bytes32", "name": "", "type": "bytes32" }
],
"name": "delegation",
"outputs": [{ "internalType": "address", "name": "", "type": "address" }],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{ "internalType": "bytes32", "name": "id", "type": "bytes32" },
{ "internalType": "address", "name": "delegate", "type": "address" }
],
"name": "setDelegate",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]
5 changes: 5 additions & 0 deletions assets/aragon.com.svg.react
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<svg width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M8.81606 4.76565L8.28182 4.19862C9.20547 4.07957 11.6426 4.23223 14.0018 5.79519C14.0325 6.01106 14.0259 6.22625 14.0259 6.22625L14.1998 6.32038L14.2008 6.32187L14.2011 6.31966L14.2046 6.32181C14.3022 6.48932 14.4007 6.73908 14.4637 7.03295C14.5399 7.38852 14.5616 7.79789 14.4728 8.18935C14.3833 8.58377 14.2134 8.9093 14.047 9.1474C13.9241 9.32334 13.8069 9.44634 13.7299 9.51368C13.4285 9.52006 12.8675 9.72227 12.7071 10.4233C12.2238 10.7 11.4651 10.9765 10.4629 10.9765C9.50555 10.9765 8.52902 10.6992 7.79491 10.1621C7.06377 9.62717 6.57275 8.83533 6.57275 7.79283C6.57275 6.96355 6.94427 6.31576 7.41544 5.83935C7.88819 5.36138 8.5344 4.97413 8.90919 4.8456L8.81606 4.76565ZM12.956 6.12596C12.7703 6.32848 12.5966 6.42564 12.547 6.45199C11.4624 6.21049 11.0716 5.62315 11.0716 5.62315C12.0947 5.61655 13.0181 5.87126 13.6936 6.30213C13.6936 6.30213 13.328 6.17808 12.956 6.12596Z" fill="#3164FA"/>
<path d="M8.26679 4.92285C5.94132 5.39135 4.1665 7.48608 4.1665 10.0191C4.1665 13.2216 6.86923 15.8677 10.2032 15.8677C12.6928 15.8677 14.7651 14.3432 15.8487 12.3034C14.4076 12.6457 12.687 12.0371 12.676 10.7068C12.1573 10.9739 11.4104 11.211 10.4631 11.211C9.46461 11.211 8.43767 10.9225 7.65723 10.3515C6.87383 9.77832 6.33926 8.91981 6.33926 7.79324C6.33926 6.88731 6.74688 6.18338 7.24962 5.67508C7.54046 5.38376 7.93924 5.1073 8.26679 4.92285Z" fill="#3164FA"/>
<path d="M14.6923 6.98379C14.6608 6.83674 14.6205 6.69768 14.5747 6.57129C15.1999 7.03918 15.6867 7.67771 15.6461 8.56743C15.5877 9.84887 14.8149 10.3531 14.3201 10.4371C14.5576 10.038 14.3596 9.78587 14.2805 9.70183C14.242 9.66094 14.1624 9.59208 14.0259 9.54914C14.0934 9.47499 14.1663 9.3851 14.2386 9.28153C14.4189 9.02353 14.6033 8.67045 14.7008 8.24116C14.7989 7.80893 14.7738 7.36417 14.6923 6.98379Z" fill="#3164FA"/>
</svg>
1 change: 0 additions & 1 deletion assets/danger.com.svg.react

This file was deleted.

3 changes: 3 additions & 0 deletions assets/delegation.com.svg.react
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<svg width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M1.2251 10C1.2251 6.9948 3.6613 4.55859 6.66651 4.55859C7.93174 4.55859 9.09595 4.99032 10.02 5.71449C10.9357 5.00565 12.0852 4.58336 13.3331 4.58336C16.3247 4.58336 18.7498 7.00849 18.7498 10C18.7498 12.9916 16.3247 15.4167 13.3331 15.4167C12.0851 15.4167 10.9357 14.9944 10.02 14.2855C9.09592 15.0097 7.93172 15.4414 6.66651 15.4414C3.6613 15.4414 1.2251 13.0052 1.2251 10ZM12.0335 10.902C11.8537 11.9787 11.3579 12.9485 10.6447 13.7126C11.4 14.2606 12.3288 14.5834 13.3331 14.5834C15.8644 14.5834 17.9164 12.5313 17.9164 10C17.9164 7.46873 15.8644 5.4167 13.3331 5.4167C12.3288 5.4167 11.4001 5.73948 10.6447 6.28749C11.358 7.05155 11.8537 8.02131 12.0335 9.0981L12.1005 9.50005H14.6042C14.6117 9.50005 14.6191 9.50021 14.6265 9.50054L13.3962 8.27027C13.201 8.07501 13.201 7.75843 13.3962 7.56316C13.5915 7.3679 13.9081 7.3679 14.1033 7.56316L16.5402 10.0001L14.1033 12.4369C13.9081 12.6322 13.5915 12.6322 13.3962 12.4369C13.201 12.2417 13.201 11.9251 13.3962 11.7298L14.6265 10.4996C14.6191 10.4999 14.6117 10.5001 14.6042 10.5001H12.1005L12.0335 10.902ZM12.0896 10.4167C12.0993 10.2791 12.1042 10.1401 12.1042 10C12.1042 9.85993 12.0993 9.72099 12.0896 9.58336H6.6118C6.43803 9.58336 6.24978 9.74284 6.24978 10C6.24978 10.2572 6.43803 10.4167 6.6118 10.4167H12.0896ZM15.1038 10.0223L15.126 10.0001L15.1038 9.9778C15.1041 9.98518 15.1042 9.9926 15.1042 10.0001C15.1042 10.0075 15.1041 10.0149 15.1038 10.0223Z" fill="#273852"/>
</svg>
10 changes: 10 additions & 0 deletions assets/fail.com.svg.react
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<svg width="12" height="12" viewBox="0 0 12 12" fill="none" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0_2153_2478)">
<path fill-rule="evenodd" clip-rule="evenodd" d="M12 6C12 9.31371 9.31371 12 6 12C2.68629 12 0 9.31371 0 6C0 2.68629 2.68629 0 6 0C9.31371 0 12 2.68629 12 6ZM3.77181 3.77264C4.0647 3.47974 4.53958 3.47974 4.83247 3.77264L5.99878 4.93895L7.16592 3.77181C7.45881 3.47892 7.93369 3.47892 8.22658 3.77181C8.51947 4.0647 8.51947 4.53958 8.22658 4.83247L7.05944 5.99961L8.22658 7.16675C8.51947 7.45964 8.51947 7.93452 8.22658 8.22741C7.93369 8.5203 7.45881 8.5203 7.16592 8.22741L5.99878 7.06027L4.83247 8.22658C4.53958 8.51947 4.0647 8.51947 3.77181 8.22658C3.47892 7.93369 3.47892 7.45881 3.77181 7.16592L4.93812 5.99961L3.77181 4.8333C3.47892 4.5404 3.47892 4.06553 3.77181 3.77264Z" fill="#E14D4D"/>
</g>
<defs>
<clipPath id="clip0_2153_2478">
<rect width="12" height="12" fill="white"/>
</clipPath>
</defs>
</svg>
3 changes: 3 additions & 0 deletions assets/retry.com.svg.react
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<svg width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M4.84 4.5775C6.23011 3.24919 8.07729 2.50548 10 2.5C11.9891 2.5 13.8968 3.29018 15.3033 4.6967C16.7098 6.10322 17.5 8.01088 17.5 10C17.5 10.4142 17.1642 10.75 16.75 10.75C16.3358 10.75 16 10.4142 16 10C15.9905 7.5534 14.4965 5.35759 12.2242 4.45065C9.95189 3.5437 7.3565 4.1073 5.665 5.875H7.465C7.87921 5.875 8.215 6.21079 8.215 6.625C8.215 7.03921 7.87921 7.375 7.465 7.375H4.09C3.67578 7.375 3.34 7.03921 3.34 6.625V3.25C3.34 2.83579 3.67578 2.5 4.09 2.5C4.50421 2.5 4.84 2.83579 4.84 3.25V4.5775ZM12.535 12.6325H15.9325C16.335 12.6446 16.6562 12.9723 16.66 13.375V16.75C16.66 17.1642 16.3242 17.5 15.91 17.5C15.4958 17.5 15.16 17.1642 15.16 16.75V15.4225C12.9877 17.482 9.7987 18.0525 7.04706 16.8739C4.29542 15.6953 2.5081 12.9934 2.5 10C2.5 9.58579 2.83579 9.25 3.25 9.25C3.66421 9.25 4 9.58579 4 10C4.00641 12.448 5.49933 14.6466 7.77233 15.5554C10.0453 16.4643 12.6425 15.9012 14.335 14.1325H12.535C12.1208 14.1325 11.785 13.7967 11.785 13.3825C11.785 12.9683 12.1208 12.6325 12.535 12.6325Z" fill="#00A3FF"/>
</svg>
3 changes: 3 additions & 0 deletions assets/snapshot.com.svg.react
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<svg width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M5.32389 10.9956L9.28249 10.9932L7.01361 15.3719C6.85273 15.6824 7.25151 15.9807 7.52257 15.753L14.8996 9.56628C15.1019 9.39644 14.9781 9.07511 14.7095 9.07464L10.6358 9.0704L13.0003 4.60563C13.1558 4.31159 12.7775 4.02602 12.5196 4.24243L5.11182 10.4484C4.8861 10.6375 5.02504 10.9956 5.32389 10.9951V10.9956Z" fill="#FFC773"/>
</svg>
3 changes: 3 additions & 0 deletions assets/warning.com.svg.react
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<svg width="14" height="12" viewBox="0 0 14 12" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M1.49084 12C1.22697 12 0.967837 11.9299 0.7399 11.7971C0.511964 11.6643 0.323386 11.4734 0.193418 11.2439C0.0634499 11.0144 -0.00325232 10.7546 0.000121951 10.4909C0.00349622 10.2273 0.0768253 9.96925 0.212623 9.74318L5.72681 0.723155C5.85927 0.502592 6.04664 0.320061 6.27065 0.193334C6.49467 0.0666063 6.74771 0 7.00513 0C7.26256 0 7.51559 0.0666063 7.73961 0.193334C7.96363 0.320061 8.15099 0.502592 8.28346 0.723155L13.7874 9.74318C13.9232 9.96925 13.9965 10.2273 13.9999 10.4909C14.0033 10.7546 13.9366 11.0144 13.8066 11.2439C13.6766 11.4734 13.488 11.6643 13.2601 11.7971C13.0322 11.9299 12.773 12 12.5092 12H1.49084ZM6.0701 3.99764C6.03453 3.45759 6.46299 3 7.0042 3C7.5461 3 7.9748 3.45871 7.93818 3.99938L7.64002 6.40193C7.61722 6.73858 7.3375 7 7.00008 7C6.6622 7 6.38226 6.73789 6.36005 6.40074L6.0701 3.99764ZM7.00513 8C6.45285 8 6.0044 8.44771 6.0044 9C6.0044 9.55229 6.45285 10 7.00513 10C7.55742 10 8.00587 9.55229 8.00587 9C8.00587 8.44771 7.55742 8 7.00513 8Z" fill="#EC8600"/>
</svg>
6 changes: 6 additions & 0 deletions modules/blockChain/contractAddresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,9 @@ export const SimpleDVT: ChainAddressMap = {
[CHAINS.Mainnet]: '0xaE7B191A31f627b4eB1d4DaC64eaB9976995b433',
[CHAINS.Holesky]: '0x11a93807078f8BB880c1BD0ee4C387537de4b4b6',
}

// https://docs.snapshot.org/user-guides/delegation
export const Snapshot: ChainAddressMap = {
[CHAINS.Mainnet]: '0x469788fE6E9E9681C6ebF3bF78e7Fd26Fc015446',
[CHAINS.Holesky]: '0x575e8c7DD6422e574A0E3e8cf6B23db1F01584CD',
}
11 changes: 10 additions & 1 deletion modules/blockChain/contracts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { createContractHelpers } from './utils/createContractHelpers'
import { MiniMeTokenAbi__factory, AragonVotingAbi__factory } from 'generated'
import {
MiniMeTokenAbi__factory,
AragonVotingAbi__factory,
SnapshotAbi__factory,
} from 'generated'
import * as CONTRACT_ADDRESSES from './contractAddresses'

export const ContractVoting = createContractHelpers({
Expand All @@ -11,3 +15,8 @@ export const ContractGovernanceToken = createContractHelpers({
factory: MiniMeTokenAbi__factory,
address: CONTRACT_ADDRESSES.GovernanceToken,
})

export const ContractSnapshot = createContractHelpers({
factory: SnapshotAbi__factory,
address: CONTRACT_ADDRESSES.Snapshot,
})
8 changes: 6 additions & 2 deletions modules/blockChain/hooks/useTransactionSender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ export function useTransactionSender<A extends unknown[]>(

setResultTx(res)
if (res.type === 'safe') finish('success', res)
return res
} catch (error: any) {
onError?.()
setStatus('empty')
console.error(error)
ToastError(getErrorMessage(error), {})
return null
}
},
[finish, populateTx, sendTransactionGnosisWorkaround, onError],
Expand All @@ -70,9 +72,9 @@ export function useTransactionSender<A extends unknown[]>(
const checkTransaction = (e: any) => {
if (!e) {
setStatus('pending')
} else if (e.status === 1) {
} else if (e.status === 1 && status !== 'success') {
finish('success', resultTx)
} else if (e.status === 0) {
} else if (e.status === 0 && status !== 'failed') {
finish('failed', resultTx)
}
}
Expand All @@ -83,6 +85,8 @@ export function useTransactionSender<A extends unknown[]>(
return () => {
library.off(tx.hash)
}
// disable to avoid re-subscribing on `status` change
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [library, resultTx, onFinish, finish])

const open = useCallback(() => {
Expand Down
23 changes: 8 additions & 15 deletions modules/blockChain/utils/formatBalance.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import { BigNumber } from '@ethersproject/bignumber'
import { formatEther } from '@ethersproject/units'
import { Zero } from '@ethersproject/constants'
import { weiToNum } from './parseWei'
import { BigNumberish } from 'ethers'

export const formatBalance = (
balance: BigNumber = Zero,
maxDecimalDigits = 4,
) => {
const balanceString = formatEther(balance)

if (balanceString.includes('.')) {
const parts = balanceString.split('.')
return parts[0] + '.' + parts[1].slice(0, maxDecimalDigits)
}

return balanceString
const formatter = Intl.NumberFormat('en', {
notation: 'compact',
maximumSignificantDigits: 3,
})
export const formatBalance = (amount: BigNumberish) => {
return formatter.format(weiToNum(amount))
}
11 changes: 8 additions & 3 deletions modules/blockChain/utils/getGnosisSafeLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ import { CHAINS } from '@lido-sdk/constants'
const PREFIXES = {
[CHAINS.Mainnet]: 'eth',
[CHAINS.Goerli]: 'gor',
[CHAINS.Holesky]: 'hol',
[CHAINS.Holesky]: 'holesky',
} as const

export const getGnosisSafeLink = (chainId: CHAINS, address: string) =>
`https://app.safe.global/transactions/history?safe=${get(
export const getGnosisSafeLink = (chainId: CHAINS, address: string) => {
if (chainId === CHAINS.Holesky) {
return `https://holesky-safe.protofire.io/transactions/queue?safe=holesky:${address}`
}

return `https://app.safe.global/transactions/queue?safe=${get(
PREFIXES,
chainId,
'?',
)}:${address}`
}
9 changes: 9 additions & 0 deletions modules/delegation/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { BigNumber } from 'ethers'

export const SNAPSHOT_LIDO_SPACE_NAME =
'0x6c69646f2d736e617073686f742e657468000000000000000000000000000000' // lido-snapshot.eth

export const DELEGATORS_PAGE_SIZE = 10
export const DELEGATORS_FETCH_SIZE = 50
export const DELEGATORS_FETCH_TOTAL = 200
export const VP_MIN_TO_SHOW = BigNumber.from(10).pow(16)
149 changes: 149 additions & 0 deletions modules/delegation/hooks/useDelegationFormSubmit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import { useCallback } from 'react'
import invariant from 'tiny-invariant'
import { ContractSnapshot, ContractVoting } from 'modules/blockChain/contracts'
import { useTransactionSender } from 'modules/blockChain/hooks/useTransactionSender'
import {
DelegationFormInput,
DelegationFormMode,
DelegationFormNetworkData,
} from '../types'
import { NonNullableMembers } from 'modules/shared/utils/utilTypes'
import { estimateDelegationGasLimit } from '../utils/estimateDelegationGasLimit'
import { SNAPSHOT_LIDO_SPACE_NAME } from '../constants'
import type { ResultTx } from '../../blockChain/types'

type Args = {
networkData: DelegationFormNetworkData
mode: DelegationFormMode
onSubmitClick?: () => void
onError?: () => void
onFinish?: (hasError?: boolean) => Promise<void>
}

type TxDelegate = {
send: (
args: NonNullableMembers<DelegationFormInput>,
) => Promise<ResultTx | null>
}

type FormData = NonNullableMembers<DelegationFormInput>

export function useDelegationFormSubmit({
networkData,
mode,
onSubmitClick,
onError,
onFinish,
}: Args) {
const voting = ContractVoting.useWeb3()
const snapshot = ContractSnapshot.useWeb3()

const populateAragonDelegate = useCallback(
async (args: FormData) => {
const gasLimit = await estimateDelegationGasLimit(
voting.estimateGas.assignDelegate(args.delegateAddress),
)

const tx = await voting.populateTransaction.assignDelegate(
args.delegateAddress,
{ gasLimit },
)
return tx
},
[voting.estimateGas, voting.populateTransaction],
)

const txAragonDelegate = useTransactionSender(populateAragonDelegate, {
onError,
})

const populateSnapshotDelegate = useCallback(
async (args: FormData) => {
const gasLimit = await estimateDelegationGasLimit(
snapshot.estimateGas.setDelegate(
SNAPSHOT_LIDO_SPACE_NAME,
args.delegateAddress,
),
)

const tx = await snapshot.populateTransaction.setDelegate(
SNAPSHOT_LIDO_SPACE_NAME,
args.delegateAddress,
{ gasLimit },
)
return tx
},
[snapshot.estimateGas, snapshot.populateTransaction],
)

const txSnapshotDelegate = useTransactionSender(populateSnapshotDelegate, {
onError,
})

const delegate = useCallback(
async (txDelegate: TxDelegate, address: string) => {
const tx = await txDelegate.send({ delegateAddress: address })
if (tx === null) {
return false
}
if (tx.type === 'regular') {
await tx.tx.wait()
}
return true
},
[],
)

const submitDelegation = useCallback(
async ({ delegateAddress }: DelegationFormInput) => {
let hasError = false
try {
invariant(delegateAddress, 'Delegate address is required')
const loweredDelegateAddress = delegateAddress.toLowerCase()
onSubmitClick?.()
if (mode === 'simple') {
if (loweredDelegateAddress !== networkData.aragonDelegateAddress) {
const success = await delegate(txAragonDelegate, delegateAddress)
if (!success) {
hasError = true
}
}
if (loweredDelegateAddress !== networkData.snapshotDelegateAddress) {
const success = await delegate(txSnapshotDelegate, delegateAddress)
if (!success) {
hasError = true
}
}
} else {
const txDelegate =
mode === 'aragon' ? txAragonDelegate : txSnapshotDelegate
const success = await delegate(txDelegate, delegateAddress)
if (!success) {
hasError = true
}
}
} catch (err) {
hasError = true
console.error(err)
} finally {
await onFinish?.(hasError)
}
},
[
mode,
networkData.aragonDelegateAddress,
networkData.snapshotDelegateAddress,
txAragonDelegate,
txSnapshotDelegate,
onSubmitClick,
onFinish,
delegate,
],
)

return {
txAragonDelegate,
txSnapshotDelegate,
submitDelegation,
}
}
Loading

0 comments on commit bcb73e3

Please sign in to comment.