Skip to content

Commit

Permalink
Merge pull request #10857 from hassnian/issue-10852
Browse files Browse the repository at this point in the history
feat: ETH rounding
  • Loading branch information
vikiival authored Aug 22, 2024
2 parents 16f8080 + b2e894d commit 56a57bb
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
1 change: 1 addition & 0 deletions components/collection/drop/GenerativePreview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ const { formatted: formattedPrice } = useAmount(
computed(() => drop.value.price),
decimals,
chainSymbol,
computed(() => drop.value.chain),
)
const emit = defineEmits(['generation:start', 'generation:end', 'mint'])
Expand Down
2 changes: 1 addition & 1 deletion components/collection/drop/MintButton.vue
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ watch(drop, async () => {
const mintForLabel = computed(() =>
$i18n.t('drops.mintForPaid', [
`${formatAmountWithRound(drop.value?.price ? Number(drop.value?.price) * amountToMint.value : '', decimals.value)} ${
`${formatAmountWithRound(drop.value?.price ? Number(drop.value?.price) * amountToMint.value : '', decimals.value, drop.value.chain)} ${
chainSymbol.value
} ${priceUsd.value ? '/ ' + (priceUsd.value * amountToMint.value).toFixed(2) + ' ' + $i18n.t('general.usd') : ''}`,
]),
Expand Down
5 changes: 3 additions & 2 deletions composables/useAmount.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import type { Prefix } from '@kodadot1/static'
import { formatAmountWithRound } from '@/utils/format/balance'

export default function (
tokenAmount: ComputedRef<number | string | undefined>,
tokenDecimals: ComputedRef<number>,
chainSymbol: ComputedRef<string>,
round: undefined | number = undefined,
roundBy?: ComputedRef<Prefix | number>,
) {
const { getCurrentTokenValue } = useFiatStore()

const amountFormatted = computed(() => {
const amount = tokenAmount.value
? formatAmountWithRound(tokenAmount.value, tokenDecimals.value, round)
? formatAmountWithRound(tokenAmount.value, tokenDecimals.value, roundBy?.value)
: 0
return `${amount} ${chainSymbol.value}`
})
Expand Down
25 changes: 20 additions & 5 deletions utils/format/balance.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { trimAll } from '@kodadot1/minimark/utils'
import type BN from 'bn.js'
import { formatBalance } from '@polkadot/util'
import type { Prefix } from '@kodadot1/static'

function format(
balance: number | string | BN | bigint,
Expand Down Expand Up @@ -83,7 +84,6 @@ export function roundTo(value: number | string, limit = 2) {
const hasDecimals = number % 1 !== 0
const fractionDigits = hasDecimals ? limit : 0
return number.toLocaleString(undefined, {
minimumFractionDigits: fractionDigits,
maximumFractionDigits: fractionDigits,
})
}
Expand Down Expand Up @@ -113,12 +113,27 @@ const roundAmount = (
export const formatAmountWithRound = (
value: string | number | bigint,
tokenDecimals: number,
round?: number,
) =>
roundAmount(
roundBy?: number | Prefix,
) => {
let round: number | undefined
let roundByPrefix = false

if (typeof roundBy === 'string') {
const prefix = roundBy as Prefix
if (prefix && isEvm(prefix)) {
roundByPrefix = true
round = chainToPrecisionMap[prefixToChainMap[prefix]!]
}
}
else {
round = roundBy
}

return roundAmount(
format(checkInvalidBalanceFilter(value), tokenDecimals, ''),
round === 0 ? round : round || 4,
round === undefined,
roundByPrefix ? false : round === undefined,
)
}

export default format

0 comments on commit 56a57bb

Please sign in to comment.