Skip to content

Commit

Permalink
resolved conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
jvonasek committed Jun 12, 2024
2 parents 9f564a0 + f5a052f commit 42383e1
Show file tree
Hide file tree
Showing 39 changed files with 304 additions and 210 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@
"@emotion/styled": "^11.10.4",
"@ethersproject/address": "^5.7.0",
"@ethersproject/providers": "^5.7.2",
"@galacticcouncil/apps": "^4.0.0",
"@galacticcouncil/apps": "^4.1.0",
"@galacticcouncil/math-lbp": "^1.0.0",
"@galacticcouncil/math-liquidity-mining": "^1.0.0",
"@galacticcouncil/math-omnipool": "^1.0.0",
"@galacticcouncil/math-stableswap": "^1.0.0",
"@galacticcouncil/math-staking": "^1.0.0",
"@galacticcouncil/math-xyk": "^1.0.0",
"@galacticcouncil/sdk": "^3.0.1",
"@galacticcouncil/ui": "^4.0.0",
"@galacticcouncil/ui": "^4.0.3",
"@galacticcouncil/xcm-cfg": "^2.5.0",
"@galacticcouncil/xcm-core": "^1.4.0",
"@galacticcouncil/xcm-sdk": "^3.3.0",
Expand Down
41 changes: 28 additions & 13 deletions src/api/era.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { ExtrinsicEra } from "@polkadot/types/interfaces/extrinsics"
import { bnToBn } from "@polkadot/util"
import { useTimestamp } from "./timestamp"
import { useMemo } from "react"
import { BLOCK_TIME } from "utils/constants"
import { PARACHAIN_BLOCK_TIME } from "utils/constants"

const DEFAULT_PERIOD = 900

export const useEra = (
era: ExtrinsicEra,
Expand All @@ -14,21 +16,27 @@ export const useEra = (
const blockNumber = bnToBn(hexBlockNumber)

const mortal = useMemo(() => {
if (blockNumber.isZero()) return null
if (era.isMortalEra) {
const mortal = era.asMortalEra
const period = new BN(mortal.period.toHex()) // Blocks validity

if (!blockNumber.isZero()) {
const birth = new BN(mortal.birth(blockNumber))
const death = new BN(mortal.death(blockNumber))
return {
birth,
death,
period,
}
const birth = new BN(mortal.birth(blockNumber))
const death = new BN(mortal.death(blockNumber))
return {
birth,
death,
period,
}
}
return null

const birth = new BN(blockNumber.toString())
const period = new BN(DEFAULT_PERIOD)
return {
birth,
death: birth.plus(period),
period,
}
}, [era, blockNumber])

const timestamp = useTimestamp(mortal?.birth, enabled)
Expand All @@ -38,14 +46,21 @@ export const useEra = (
const birthDate = new Date(timestamp.data)
const deathDate = addSeconds(
birthDate,
mortal.period.times(BLOCK_TIME).toNumber(),
mortal.period.times(PARACHAIN_BLOCK_TIME).toNumber(),
)
return {
birthDate,
deathDate,
period: mortal.period,
isLoading: false,
}
}

return null
}, [timestamp.data, mortal?.period])
return {
birthDate: null,
deathDate: null,
period: null,
isLoading: timestamp.isLoading,
}
}, [timestamp.data, timestamp.isLoading, mortal?.period])
}
74 changes: 60 additions & 14 deletions src/api/farms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import request, { gql } from "graphql-request"
import { AccountId32 } from "@polkadot/types/interfaces"
import { useMemo } from "react"
import { scale } from "utils/balance"
import { getAccountResolver } from "utils/farms/claiming/accountResolver"
import { useAccountBalances, useAccountsBalances } from "./accountBalances"

const NEW_YIELD_FARMS_BLOCKS = (48 * 60 * 60) / PARACHAIN_BLOCK_TIME.toNumber() // 48 hours

Expand All @@ -34,6 +36,7 @@ type FarmAprs = ReturnType<typeof useFarmAprs>
export interface Farm {
globalFarm: PalletLiquidityMiningGlobalFarmData
yieldFarm: PalletLiquidityMiningYieldFarmData
globalFarmPotAddress: string
poolId: string
}

Expand Down Expand Up @@ -161,6 +164,7 @@ const getGlobalFarm =
}

export const useFarms = (poolIds: Array<string>) => {
const { api } = useRpcProvider()
const activeYieldFarmsQuery = useActiveYieldFarms(poolIds)

const farmIds = activeYieldFarmsQuery
Expand All @@ -170,6 +174,15 @@ export const useFarms = (poolIds: Array<string>) => {
}, [])
.flat(2)

const accountResolver = getAccountResolver(api.registry)
const globalFarmPotAddresses = farmIds?.map((farm) => {
const potAddresss = accountResolver(Number(farm.globalFarmId)).toString()
return {
globalFarmId: farm.globalFarmId.toString(),
potAddresss,
}
})

const globalFarms = useGlobalFarms(farmIds)
const yieldFarms = useYieldFarms(farmIds)

Expand All @@ -191,6 +204,10 @@ export const useFarms = (poolIds: Array<string>) => {
)
})?.data?.farm

const globalFarmPotAddress = globalFarmPotAddresses.find(
(farm) => farm.globalFarmId === globalFarm?.id.toString(),
)?.potAddresss

const yieldFarm = yieldFarms.find((yieldFarm) => {
const data = yieldFarm.data

Expand All @@ -202,10 +219,15 @@ export const useFarms = (poolIds: Array<string>) => {

if (!globalFarm || !yieldFarm) return undefined

return { globalFarm, yieldFarm, poolId: farmId.poolId }
return {
globalFarm,
yieldFarm,
globalFarmPotAddress,
poolId: farmId.poolId,
}
})
.filter((x): x is Farm => x != null)
}, [farmIds, globalFarms, yieldFarms])
}, [farmIds, globalFarms, yieldFarms, globalFarmPotAddresses])

return { data, isLoading }
}
Expand Down Expand Up @@ -247,6 +269,7 @@ function getFarmApr(
},
farm: Farm,
priceAdjustment: BigNumber,
potBalance?: BigNumber,
) {
const { globalFarm, yieldFarm } = farm
const { rewardCurrency, incentivizedAsset } = globalFarm
Expand Down Expand Up @@ -330,11 +353,16 @@ function getFarmApr(

const minApr = loyaltyFactor ? apr.times(loyaltyFactor) : null

const potMaxRewards = potBalance
? distributedRewards.plus(potBalance)
: undefined

return {
apr,
minApr,
distributedRewards,
maxRewards,
potMaxRewards,
fullness,
estimatedEndBlock: estimatedEndBlock,
assetId: globalFarm.rewardCurrency,
Expand All @@ -347,43 +375,48 @@ function getFarmApr(
}
}

export const useFarmApr = (farm: {
globalFarm: PalletLiquidityMiningGlobalFarmData
yieldFarm: PalletLiquidityMiningYieldFarmData
poolId: string
}) => {
export const useFarmApr = (farm: Farm) => {
const { assets } = useRpcProvider()
const bestNumber = useBestNumber()
const rewardCurrency = farm.globalFarm.rewardCurrency.toString()
const incentivizedAsset = farm.globalFarm.incentivizedAsset.toString()

const accountBalance = useAccountBalances(farm.globalFarmPotAddress)
const oraclePrice = useOraclePrice(rewardCurrency, incentivizedAsset)

return useQueryReduce(
[bestNumber, oraclePrice] as const,
(bestNumber, oraclePrice) => {
const rewardCurrency = farm.globalFarm.rewardCurrency.toString()
const potBalance =
rewardCurrency === assets.native.id
? accountBalance.data?.native.freeBalance
: accountBalance.data?.balances.find(
(balance) => balance.id.toString() === rewardCurrency,
)?.freeBalance

return getFarmApr(
bestNumber,
farm,
oraclePrice?.oraclePrice ??
farm.globalFarm.priceAdjustment.toBigNumber(),
potBalance,
)
},
)
}

export const useFarmAprs = (
farms: {
globalFarm: PalletLiquidityMiningGlobalFarmData
yieldFarm: PalletLiquidityMiningYieldFarmData
poolId: string
}[],
) => {
export const useFarmAprs = (farms: Farm[]) => {
const { assets } = useRpcProvider()
const bestNumber = useBestNumber()
const ids = farms.map((farm) => ({
rewardCurrency: farm.globalFarm.rewardCurrency.toString(),
incentivizedAsset: farm.globalFarm.incentivizedAsset.toString(),
}))
const oraclePrices = useOraclePrices(ids)
const accountsBalances = useAccountsBalances(
farms.map((farm) => farm.globalFarmPotAddress),
)

return useQueryReduce([bestNumber] as const, (bestNumber) => {
return farms.map((farm) => {
Expand All @@ -394,11 +427,24 @@ export const useFarmAprs = (
oraclePrice.data?.id.incentivizedAsset === incentivizedAsset &&
oraclePrice.data?.id.rewardCurrency === rewardCurrency,
)
const accountBalance = accountsBalances.data?.find(
(balance) => balance.accountId.toString() === farm.globalFarmPotAddress,
)

const potBalance = accountBalance
? rewardCurrency === assets.native.id
? accountBalance.native.freeBalance
: accountBalance.balances.find(
(balance) => balance.id.toString() === rewardCurrency,
)?.freeBalance
: undefined

return getFarmApr(
bestNumber,
farm,
oraclePrice?.data?.oraclePrice ??
farm.globalFarm.priceAdjustment.toBigNumber(),
potBalance,
)
})
})
Expand Down
9 changes: 5 additions & 4 deletions src/api/timestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ export function useTimestamp(
enabled = true,
) {
const { api } = useRpcProvider()
const queryEnabled = !!blockNumber && enabled

return useQuery(
QUERY_KEYS.timestamp(blockNumber),
() =>
blockNumber !== null ? getTimestamp(api, blockNumber) : undefinedNoop(),
() => (queryEnabled ? getTimestamp(api, blockNumber) : undefinedNoop()),
{
enabled: !!blockNumber && enabled,
enabled: queryEnabled,
},
)
}
Expand All @@ -25,7 +26,7 @@ export async function getTimestamp(
api: ApiPromise,
blockNumber?: u32 | BigNumber,
) {
if (blockNumber != null) {
if (blockNumber) {
const blockHash = await api.rpc.chain.getBlockHash(blockNumber.toString())
const apiAt = await api.at(blockHash)
const now = await apiAt.query.timestamp.now()
Expand Down
4 changes: 2 additions & 2 deletions src/assets/icons/HydraLogo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions src/components/AppProviders/AppProviders.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import * as Apps from "@galacticcouncil/apps"
import { createComponent } from "@lit-labs/react"
import { MigrationProvider } from "sections/migration/MigrationProvider"

const AppsPersistenceProvider = createComponent({
tagName: "gc-database-provider",
elementClass: Apps.DatabaseProvider,
const AppsContextProvider = createComponent({
tagName: "gc-context-provider",
elementClass: Apps.ContextProvider,
react: React,
})

Expand All @@ -28,7 +28,7 @@ export const AppProviders: FC<PropsWithChildren> = ({ children }) => {
highlightColor={`rgba(${theme.rgbColors.white}, 0.24)`}
borderRadius={4}
>
<AppsPersistenceProvider>{children}</AppsPersistenceProvider>
<AppsContextProvider>{children}</AppsContextProvider>
</SkeletonTheme>
</ToastProvider>
</InvalidateOnBlock>
Expand Down
12 changes: 7 additions & 5 deletions src/components/AssetIcon/AssetIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ import { AnyParachain } from "@galacticcouncil/xcm-core"
import { isAnyParachain } from "utils/helpers"
import { MetadataStore } from "@galacticcouncil/ui"

const EXTERNAL_ASSETS_WHITELIST =
MetadataStore.getInstance().externalWhitelist()

const chains = Array.from(chainsMap.values())

export const UigcAssetPlaceholder = createComponent({
Expand Down Expand Up @@ -48,6 +45,11 @@ export const AssetLogo = ({ id }: { id?: string }) => {
const { t } = useTranslation()
const { assets } = useRpcProvider()

const externalAssetsWhitelist = useMemo(
() => MetadataStore.getInstance().externalWhitelist(),
[],
)

const asset = useMemo(() => {
const assetDetails = id ? assets.getAsset(id) : undefined

Expand All @@ -58,7 +60,7 @@ export const AssetLogo = ({ id }: { id?: string }) => {
) as AnyParachain

const isWhitelisted = assetDetails
? EXTERNAL_ASSETS_WHITELIST.includes(assetDetails.id)
? externalAssetsWhitelist.includes(assetDetails.id)
: false

const badgeVariant: "warning" | "danger" | "" = assetDetails?.isExternal
Expand All @@ -72,7 +74,7 @@ export const AssetLogo = ({ id }: { id?: string }) => {
symbol: assetDetails?.symbol,
badgeVariant,
}
}, [assets, id])
}, [assets, externalAssetsWhitelist, id])

if (asset.chain || asset.symbol)
return (
Expand Down
8 changes: 7 additions & 1 deletion src/components/Layout/Header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ export const Header = () => {
<Link to={LINKS.swap} search={resetSearchParams(search)}>
<Icon
sx={{ color: "white" }}
icon={!isMediumMedia ? <HydraLogoFull /> : <HydraLogo />}
icon={
!isMediumMedia ? (
<HydraLogoFull />
) : (
<Icon size={24} icon={<HydraLogo />} />
)
}
/>
</Link>
<Suspense>
Expand Down
2 changes: 1 addition & 1 deletion src/components/Modal/Modal.styled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const SContainer = styled(Content)`
--modal-header-padding-x: 12px;
--modal-header-btn-size: 34px;
--modal-header-height: calc(
var(--modal-header-btn-size) + var(--modal-header-padding-y) * 2
var(--modal-header-btn-size) + var(--modal-header-padding-y) * 1.5
);
--modal-content-padding: 12px;
Expand Down
2 changes: 1 addition & 1 deletion src/i18n/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ i18n
if (num == null) return null
return formatNum(
num.toFixed(),
{ notation: "compact" },
{ notation: "compact", maximumSignificantDigits: 2 },
lng,
)?.replaceAll(
getFormatSeparators(lng).group ?? ",",
Expand Down
Loading

0 comments on commit 42383e1

Please sign in to comment.