@stellar-expert/ui-framework 1.16.4 → 1.16.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,13 +1,15 @@
1
1
  import {toStroops, fromStroops} from '@stellar-expert/formatter'
2
+ import {AssetDescriptor} from '@stellar-expert/asset-descriptor'
2
3
 
3
4
  /**
4
5
  * Calculate available balance for a given account balance trustline
5
6
  * @param {AccountResponse} account
6
7
  * @param {Horizon.BalanceLine} balance
7
8
  * @param {Number} [additionalReserves]
9
+ * @param {Number} [decimals=7]
8
10
  * @return {String}
9
11
  */
10
- export function calculateAvailableBalance(account, balance, additionalReserves = null) {
12
+ export function calculateAvailableBalance(account, balance, additionalReserves = null, decimals = 7) {
11
13
  let available = toStroops(balance.balance) - toStroops(balance.selling_liabilities || 0)
12
14
  if (balance.asset_type === 'native') {
13
15
  const reserves = 2 + account.subentry_count + account.num_sponsoring - account.num_sponsored
@@ -15,7 +17,7 @@ export function calculateAvailableBalance(account, balance, additionalReserves =
15
17
  //TODO: fetch base_reserve from the Horizon
16
18
  }
17
19
  if (additionalReserves !== null) {
18
- available = available - toStroops(additionalReserves)
20
+ available = available - toStroops(additionalReserves, decimals)
19
21
  }
20
- return fromStroops(available)
22
+ return fromStroops(available, decimals)
21
23
  }
package/asset/amount.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import React from 'react'
2
- import {formatWithPrecision, formatWithAutoPrecision, stripTrailingZeros, fromStroops} from '@stellar-expert/formatter'
2
+ import {formatWithPrecision, formatWithAutoPrecision, fromStroops} from '@stellar-expert/formatter'
3
3
  import {AssetDescriptor, isAssetValid, isValidPoolId} from '@stellar-expert/asset-descriptor'
4
+ import {useAssetMeta} from './asset-meta-hooks'
4
5
  import {AssetLink} from './asset-link'
5
6
 
6
7
  /**
@@ -15,11 +16,12 @@ import {AssetLink} from './asset-link'
15
16
  * @constructor
16
17
  */
17
18
  export const Amount = React.memo(function Amount({amount, asset, decimals, adjust, round, issuer, icon}) {
19
+ const meta = useAssetMeta(asset)
18
20
  if (amount === undefined || amount === null)
19
21
  return null
20
22
  if (adjust === true) {
21
23
  try {
22
- amount = fromStroops(amount)
24
+ amount = fromStroops(amount, meta?.decimals ?? 7)
23
25
  } catch (e) {
24
26
  console.error(e)
25
27
  return null
@@ -47,7 +49,9 @@ export const Amount = React.memo(function Amount({amount, asset, decimals, adjus
47
49
  return <span className="amount nowrap condensed">
48
50
  {amount}
49
51
  {!!asset && <>
50
- {' '}{isAssetValid(asset) || isValidPoolId(asset) ? <AssetLink asset={asset} icon={icon} issuer={issuer}/> : asset.toString()}
52
+ {' '}{isAssetValid(asset) || isValidPoolId(asset) ?
53
+ <AssetLink asset={asset} icon={icon} issuer={issuer}/> :
54
+ asset.toString()}
51
55
  </>}
52
56
  </span>
53
57
  })
@@ -1,5 +1,6 @@
1
1
  import React from 'react'
2
2
  import {AssetDescriptor} from '@stellar-expert/asset-descriptor'
3
+ import {drawIdenticon} from '../account/identicon'
3
4
  import {useAssetMeta} from './asset-meta-hooks'
4
5
 
5
6
  /**
@@ -12,6 +13,9 @@ import {useAssetMeta} from './asset-meta-hooks'
12
13
  */
13
14
  export const AssetIcon = React.memo(function AssetIcon({asset, className, style, children}) {
14
15
  const meta = useAssetMeta(asset)
16
+ if (typeof asset === 'string') {
17
+ asset = AssetDescriptor.parse(asset)
18
+ }
15
19
  const icon = meta?.toml_info?.image || meta?.toml_info?.orgLogo
16
20
  const classes = ['asset-icon']
17
21
  if (!style) {
@@ -22,10 +26,13 @@ export const AssetIcon = React.memo(function AssetIcon({asset, className, style,
22
26
  classes.push(className)
23
27
  }
24
28
 
25
- if (asset.toString() === 'XLM') { // native asset
29
+ if (asset.isNative) { // native asset
26
30
  classes.push('icon icon-stellar')
27
31
  } else if (icon) { // asset with an icon
28
32
  style.backgroundImage = `url('${icon}')`
33
+ } else if (asset.isContract) {
34
+ style.backgroundImage = `url('${'data:image/svg+xml;charset=utf-8,' + drawIdenticon(asset.toString())}')`
35
+ style.borderRadius = '50%'
29
36
  } else { // asset without metadata
30
37
  classes.push('icon icon-empty-circle')
31
38
  }
@@ -65,10 +65,10 @@ export const AssetLink = React.memo(function AssetLink({
65
65
  } else if (asset.isContract) {
66
66
  children = <>
67
67
  <AssetWarningStatus meta={meta}/>
68
- {!!meta?.code && <>{meta.code} </>}
68
+ {icon !== false && <AssetIcon asset={asset}/>}
69
+ {!!meta?.code && <span title={meta.tokenName}>{meta.code} </span>}
69
70
  {(issuer !== false || !meta?.code) &&
70
- <AccountAddress account={asset.contract} chars={8} link={false} icon={!!icon}
71
- title={meta?.tokenName}/>}
71
+ <AccountAddress account={asset.contract} chars={8} link={false} icon={false}/>}
72
72
  </>
73
73
  } else {
74
74
  children = <>
@@ -24,7 +24,10 @@ import {getCurrentStellarNetwork} from '../state/stellar-network-hooks'
24
24
  const cache = new InMemoryClientCache()
25
25
 
26
26
  const loader = new ExplorerBatchInfoLoader(batch => {
27
- return fetchExplorerApi(getCurrentStellarNetwork() + '/asset/meta' + stringifyQuery({asset: batch, origin: window.location.origin}))
27
+ return fetchExplorerApi(getCurrentStellarNetwork() + '/asset/meta' + stringifyQuery({
28
+ asset: batch,
29
+ origin: window.location.origin
30
+ }))
28
31
  }, entry => {
29
32
  cache.set(entry.name, entry)
30
33
  return {
@@ -56,7 +59,11 @@ function normalizeAssetName(asset) {
56
59
  * @return {AssetMeta}
57
60
  */
58
61
  export function useAssetMeta(asset) {
59
- asset = normalizeAssetName(asset)
62
+ try {
63
+ asset = normalizeAssetName(asset)
64
+ } catch (e) {
65
+ asset = null
66
+ }
60
67
  const [assetInfo, setAssetInfo] = useState(retrieveFromCache(asset))
61
68
  useEffect(() => {
62
69
  if (!asset)
@@ -68,7 +75,11 @@ export function useAssetMeta(asset) {
68
75
  let unloaded = false
69
76
  //load from the server
70
77
  loader.loadEntry(asset)
71
- .then(a => !unloaded && setAssetInfo(a))
78
+ .then(a => {
79
+ if (unloaded)
80
+ return
81
+ setAssetInfo(a)
82
+ })
72
83
  return () => {
73
84
  unloaded = true
74
85
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stellar-expert/ui-framework",
3
- "version": "1.16.4",
3
+ "version": "1.16.6",
4
4
  "description": "StellarExpert shared UI components library",
5
5
  "main": "index.js",
6
6
  "module": "./index.js",
@@ -16,11 +16,11 @@
16
16
  "peerDependencies": {
17
17
  "@stellar/stellar-base": "^14.0.0",
18
18
  "@stellar/stellar-sdk": "^14.0.0",
19
- "@stellar-expert/asset-descriptor": "^1.4.0",
19
+ "@stellar-expert/asset-descriptor": "^1.5.0",
20
20
  "@stellar-expert/claimable-balance-utils": "^1.4.1",
21
21
  "@stellar-expert/client-cache": "^1.1.0",
22
22
  "@stellar-expert/contract-wasm-interface-parser": "^3.2.0",
23
- "@stellar-expert/formatter": "^2.5.0",
23
+ "@stellar-expert/formatter": "^3.0.0",
24
24
  "@stellar-expert/navigation": "^1.1.0",
25
25
  "@stellar-expert/tx-meta-effects-parser": "^8.3.3",
26
26
  "classnames": ">=2",