<template>
    <span>{{fmtAmount}} {{ raw ? '' : (token.s || '') }}</span>
</template>

<script setup>
import {getToken} from "@/blockchain/token.js";
import {FixedNumber} from "ethers";
import {computed} from "vue";

const props = defineProps(['chainId', 'addr', 'amount', 'raw'])
const token = await getToken(props.chainId, props.addr)
const fmtAmount = computed(() => {
  if( props.amount === null || props.amount === undefined )
    return ''
  const fixed = FixedNumber.fromValue(props.amount, token.d, {
    width: 256,
    decimals: token.d
  })
  let p = fixed.toUnsafeFloat().toPrecision(5);
  if (p.includes('e'))
    p = parseFloat(p)
  return p
})
</script>

<style scoped lang="scss">
@use "src/styles/vars" as *;

</style>
