-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: asset requested issue while currency is something other than USD
closes #1588
- Loading branch information
Showing
5 changed files
with
91 additions
and
33 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
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
52 changes: 52 additions & 0 deletions
52
packages/extension-polkagate/src/fullscreen/governance/post/useReferendaRequested.tsx
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,52 @@ | ||
// Copyright 2019-2024 @polkadot/extension-polkagate authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import type { Referendum } from '../utils/types'; | ||
|
||
import { useMemo } from 'react'; | ||
|
||
import { useCurrency, useInfo, useTokenPrice } from '../../../hooks'; | ||
|
||
interface ReferendaRequested{ | ||
rAssetInCurrency: number, | ||
rCurrencySign: string, | ||
rDecimal: number, | ||
rToken: string | ||
} | ||
|
||
const DEFAULT_OUTPUT = { | ||
rAssetInCurrency: undefined, | ||
rCurrencySign: undefined, | ||
rDecimal: undefined, | ||
rToken: undefined | ||
}; | ||
|
||
export default function useReferendaRequested (address: string | undefined, referendum: Referendum | undefined): ReferendaRequested | typeof DEFAULT_OUTPUT { | ||
const { chainName, decimal, token } = useInfo(address); | ||
const currency = useCurrency(); | ||
|
||
const maybeAssetIdInNumber = referendum?.assetId ? Number(referendum.assetId) : undefined; | ||
const maybeAssetHubs = referendum?.assetId | ||
? chainName?.includes('Polkadot') | ||
? 'polkadot asset hub' | ||
: 'kusama asset hub' | ||
: undefined; | ||
const priceInfo = useTokenPrice(address, maybeAssetIdInNumber, maybeAssetHubs); | ||
const _decimal = priceInfo?.decimal || referendum?.decimal || decimal; | ||
const _token = priceInfo?.token || referendum?.token || token; | ||
|
||
return useMemo(() => { | ||
if (!referendum?.requested || !currency || !_decimal || !_token || !priceInfo.price || !priceInfo.decimal) { | ||
return DEFAULT_OUTPUT; | ||
} | ||
|
||
const requestedAssetInCurrency = (Number(referendum.requested) / 10 ** priceInfo.decimal) * priceInfo.price; | ||
|
||
return { | ||
rAssetInCurrency: requestedAssetInCurrency, | ||
rCurrencySign: currency.sign, | ||
rDecimal: _decimal, | ||
rToken: _token | ||
}; | ||
}, [_decimal, _token, currency, priceInfo, referendum?.requested]); | ||
} |
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