@exodus/solana-api 2.0.2 → 2.0.3

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.
Files changed (2) hide show
  1. package/package.json +4 -3
  2. package/src/index.js +68 -20
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/solana-api",
3
- "version": "2.0.2",
3
+ "version": "2.0.3",
4
4
  "description": "Exodus internal Solana asset API wrapper",
5
5
  "main": "src/index.js",
6
6
  "files": [
@@ -14,7 +14,8 @@
14
14
  },
15
15
  "dependencies": {
16
16
  "@exodus/asset-json-rpc": "^1.0.0",
17
- "@exodus/solana-lib": "^1.3.7",
17
+ "@exodus/nfts-core": "^0.5.0",
18
+ "@exodus/solana-lib": "^1.3.8",
18
19
  "lodash": "^4.17.11",
19
20
  "url-join": "4.0.0",
20
21
  "wretch": "^1.5.2"
@@ -22,5 +23,5 @@
22
23
  "devDependencies": {
23
24
  "node-fetch": "~1.6.3"
24
25
  },
25
- "gitHead": "342e5908cd9a4b4806acbb2b7286549e2ed3d80b"
26
+ "gitHead": "a0fdfde1511f31373bc94c8cd12c8bee22757876"
26
27
  }
package/src/index.js CHANGED
@@ -18,6 +18,7 @@ import assert from 'assert'
18
18
  import lodash from 'lodash'
19
19
  import urljoin from 'url-join'
20
20
  import wretch, { Wretcher } from 'wretch'
21
+ import { magicEden } from '@exodus/nfts-core'
21
22
 
22
23
  // Doc: https://docs.solana.com/apps/jsonrpc-api
23
24
 
@@ -744,37 +745,84 @@ class Api {
744
745
  type: 'SOL',
745
746
  }
746
747
  })
747
- const _getTokenBalance = async (address) => {
748
- try {
749
- return await this.getTokenBalance(address)
750
- } catch (error) {
751
- if (error.message && error.message.includes('could not find account')) {
752
- return '0'
748
+
749
+ const _wrapAndHandleAccountNotFound = (fn, defaultValue) => {
750
+ return async (...params) => {
751
+ try {
752
+ return await fn.apply(this, params)
753
+ } catch (error) {
754
+ if (error.message && error.message.includes('could not find account')) {
755
+ return defaultValue
756
+ }
757
+ throw error
753
758
  }
754
- throw error
755
759
  }
756
760
  }
757
761
 
762
+ const _getTokenBalance = _wrapAndHandleAccountNotFound(this.getTokenBalance, '0')
763
+ const _getDecimals = _wrapAndHandleAccountNotFound(this.getDecimals, 0)
764
+ const _getSupply = _wrapAndHandleAccountNotFound(this.getSupply, '0')
765
+
758
766
  const resolveTokens = tokenAccounts.map(async (account) => {
759
- const [_tokenMetaPlex, currentAmount, decimal] = await Promise.all([
760
- this.getMetaplexMetadata(account.mint),
761
- _getTokenBalance(account.address),
762
- this.getDecimals(account.mint),
763
- ])
767
+ try {
768
+ const [_tokenMetaPlex, currentAmount, decimal] = await Promise.all([
769
+ this.getMetaplexMetadata(account.mint),
770
+ _getTokenBalance(account.address),
771
+ _getDecimals(account.mint),
772
+ ])
773
+
774
+ const tokenMetaPlex = _tokenMetaPlex || { name: null, symbol: null }
775
+ let nft = {
776
+ collectionId: null,
777
+ collectionName: null,
778
+ collectionTitle: null,
779
+ }
764
780
 
765
- const balance = computeBalance(account.amount, currentAmount)
766
- const tokenMetaPlex = _tokenMetaPlex || { name: null, symbol: null }
767
- return {
768
- name: tokenMetaPlex.name,
769
- symbol: tokenMetaPlex.symbol,
770
- balance,
771
- decimal,
772
- type: 'TOKEN',
781
+ // Only perform an NFT check (getSupply) if decimal is zero
782
+ if (decimal === 0 && (await _getSupply(account.mint)) === '1') {
783
+ try {
784
+ const {
785
+ id: collectionId,
786
+ collectionName,
787
+ collectionTitle,
788
+ } = await magicEden.api.getNFTByMintAddress(account.mint)
789
+ nft = {
790
+ collectionId,
791
+ collectionTitle,
792
+ collectionName,
793
+ }
794
+ tokenMetaPlex.name = tokenMetaPlex.name || collectionTitle
795
+ tokenMetaPlex.symbol = tokenMetaPlex.symbol || collectionName
796
+ } catch (error) {
797
+ console.warn(error)
798
+ }
799
+ }
800
+
801
+ const balance = computeBalance(account.amount, currentAmount)
802
+ return {
803
+ balance,
804
+ decimal,
805
+ nft,
806
+ address: account.address,
807
+ mint: account.mint,
808
+ name: tokenMetaPlex.name,
809
+ symbol: tokenMetaPlex.symbol,
810
+ type: 'TOKEN',
811
+ }
812
+ } catch (error) {
813
+ console.warn(error)
814
+ return {
815
+ balance: null,
816
+ }
773
817
  }
774
818
  })
775
819
 
776
820
  const accounts = await Promise.all([...resolveSols, ...resolveTokens])
777
821
  accounts.forEach((account) => {
822
+ if (account.balance === null) {
823
+ return
824
+ }
825
+
778
826
  if (account.balance > 0) {
779
827
  willReceive.push(account)
780
828
  } else {