-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement basic Staking/unstaking (#611)
- Loading branch information
Showing
23 changed files
with
1,351 additions
and
7 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,84 @@ | ||
import { useState } from 'react'; | ||
|
||
import { Validator } from '@dydxprotocol/v4-client-js/build/node_modules/@dydxprotocol/v4-proto/src/codegen/cosmos/staking/v1beta1/staking'; | ||
import styled from 'styled-components'; | ||
|
||
import { Link } from './Link'; | ||
import { Output, OutputType } from './Output'; | ||
|
||
export type ValidatorNameProps = { | ||
validator?: Validator; | ||
}; | ||
|
||
const FaviconIcon = ({ url, fallbackText }: { url?: string; fallbackText?: string }) => { | ||
const [iconFail, setIconFail] = useState<boolean>(false); | ||
|
||
if (url && !iconFail) { | ||
const parsedUrl = new URL(url); | ||
const baseUrl = `${parsedUrl.protocol}//${parsedUrl.hostname}`; | ||
return ( | ||
<$Img | ||
src={`${baseUrl}/favicon.ico`} | ||
alt="validator favicon" | ||
onError={() => setIconFail(true)} | ||
/> | ||
); | ||
} | ||
if (fallbackText) { | ||
return <$IconContainer>{fallbackText.charAt(0)}</$IconContainer>; | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
export const ValidatorName = ({ validator }: ValidatorNameProps) => { | ||
if (!validator) { | ||
return null; | ||
} | ||
const output = ( | ||
<$Output | ||
type={OutputType.Text} | ||
value={validator?.description?.moniker} | ||
slotLeft={ | ||
<FaviconIcon | ||
url={validator?.description?.website} | ||
fallbackText={validator?.description?.moniker} | ||
/> | ||
} | ||
/> | ||
); | ||
|
||
if (validator?.description?.website) { | ||
return ( | ||
<Link href={validator?.description?.website} withIcon> | ||
{output} | ||
</Link> | ||
); | ||
} | ||
return output; | ||
}; | ||
|
||
const $IconContainer = styled.div` | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
width: 1.5em; | ||
height: 1.5em; | ||
background-color: var(--color-layer-6); | ||
border-radius: 50%; | ||
font-weight: bold; | ||
color: var(--color-text-1); | ||
margin-right: 0.25em; | ||
`; | ||
|
||
const $Img = styled.img` | ||
width: 1.5em; | ||
height: 1.5em; | ||
border-radius: 50%; | ||
object-fit: cover; | ||
margin-right: 0.25em; | ||
`; | ||
|
||
const $Output = styled(Output)` | ||
color: var(--color-text-1); | ||
`; |
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
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,28 @@ | ||
import { useCallback } from 'react'; | ||
|
||
import { useQuery } from '@tanstack/react-query'; | ||
|
||
// TODO: This api doesn't work due to cors, need to contact protocolstaking.info | ||
export const useStakingAPY = () => { | ||
const queryFn = useCallback(async () => { | ||
const response = await fetch('https://api.protocolstaking.info/v0/protocols/dydx', { | ||
headers: { | ||
accept: 'application/json', | ||
'x-access-key': import.meta.env.VITE_PROTOCOL_STAKING_API_KEY, | ||
}, | ||
}); | ||
|
||
const data = await response.json(); | ||
return data; | ||
}, []); | ||
|
||
const { data } = useQuery({ | ||
queryKey: ['stakingAPY'], | ||
queryFn, | ||
enabled: true, | ||
refetchOnWindowFocus: false, | ||
refetchOnReconnect: false, | ||
}); | ||
|
||
return data; | ||
}; |
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 { useCallback } from 'react'; | ||
|
||
import { useQuery } from '@tanstack/react-query'; | ||
import { groupBy } from 'lodash'; | ||
import { shallowEqual, useSelector } from 'react-redux'; | ||
|
||
import { ENVIRONMENT_CONFIG_MAP } from '@/constants/networks'; | ||
|
||
import { getStakingDelegations } from '@/state/accountSelectors'; | ||
import { getSelectedNetwork } from '@/state/appSelectors'; | ||
|
||
import { useDydxClient } from './useDydxClient'; | ||
|
||
export const useStakingValidator = () => { | ||
const { getValidators, isCompositeClientConnected } = useDydxClient(); | ||
const selectedNetwork = useSelector(getSelectedNetwork); | ||
const currentDelegations = useSelector(getStakingDelegations, shallowEqual)?.map((delegation) => { | ||
return { | ||
validator: delegation.validator.toLowerCase(), | ||
amount: delegation.amount, | ||
}; | ||
}); | ||
const validatorWhitelist = ENVIRONMENT_CONFIG_MAP[selectedNetwork].stakingValidators?.map( | ||
(delegation) => { | ||
return delegation.toLowerCase(); | ||
} | ||
); | ||
|
||
const queryFn = useCallback(async () => { | ||
const validatorOptions: string[] = []; | ||
const intersection = validatorWhitelist.filter((delegation) => | ||
currentDelegations?.map((d) => d.validator).includes(delegation) | ||
); | ||
|
||
if (intersection.length > 0) { | ||
validatorOptions.push(...intersection); | ||
} else { | ||
validatorOptions.push(...validatorWhitelist); | ||
} | ||
|
||
const response = await getValidators(); | ||
|
||
const filteredValidators = response?.validators.filter((validator) => | ||
validatorOptions.includes(validator.operatorAddress.toLowerCase()) | ||
); | ||
|
||
const stakingValidators = | ||
response?.validators.filter((validator) => | ||
currentDelegations | ||
?.map((d) => d.validator) | ||
.includes(validator.operatorAddress.toLowerCase()) | ||
) ?? []; | ||
|
||
if (!filteredValidators || filteredValidators.length === 0) { | ||
return undefined; | ||
} | ||
|
||
// Find the validator with the fewest tokens | ||
const validatorWithFewestTokens = filteredValidators.reduce((prev, curr) => { | ||
return BigInt(curr.tokens) < BigInt(prev.tokens) ? curr : prev; | ||
}); | ||
|
||
return { | ||
selectedValidator: validatorWithFewestTokens, | ||
stakingValidators: groupBy(stakingValidators, ({ operatorAddress }) => operatorAddress), | ||
currentDelegations, | ||
}; | ||
}, [validatorWhitelist, getValidators, currentDelegations]); | ||
|
||
const { data } = useQuery({ | ||
queryKey: ['stakingValidator', selectedNetwork], | ||
queryFn, | ||
enabled: Boolean(isCompositeClientConnected && validatorWhitelist?.length > 0), | ||
refetchOnWindowFocus: false, | ||
refetchOnReconnect: false, | ||
}); | ||
|
||
return data; | ||
}; |
Oops, something went wrong.