@exodus/solana-api 2.0.1 → 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 +78 -23
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/solana-api",
3
- "version": "2.0.1",
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.6",
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": "90859f94717cf2b928cb93b030ecfba523b8179e"
26
+ "gitHead": "a0fdfde1511f31373bc94c8cd12c8bee22757876"
26
27
  }
package/src/index.js CHANGED
@@ -10,6 +10,7 @@ import {
10
10
  TOKEN_PROGRAM_ID,
11
11
  SOL_DECIMAL,
12
12
  computeBalance,
13
+ SolanaWeb3Message,
13
14
  buildRawTransaction,
14
15
  } from '@exodus/solana-lib'
15
16
  import assets from '@exodus/assets'
@@ -17,6 +18,7 @@ import assert from 'assert'
17
18
  import lodash from 'lodash'
18
19
  import urljoin from 'url-join'
19
20
  import wretch, { Wretcher } from 'wretch'
21
+ import { magicEden } from '@exodus/nfts-core'
20
22
 
21
23
  // Doc: https://docs.solana.com/apps/jsonrpc-api
22
24
 
@@ -458,7 +460,9 @@ class Api {
458
460
  }
459
461
 
460
462
  async getSupply(mintAddress: string): string {
461
- const { value: { amount } } = await this.api.post({
463
+ const {
464
+ value: { amount },
465
+ } = await this.api.post({
462
466
  method: 'getTokenSupply',
463
467
  params: [mintAddress],
464
468
  })
@@ -741,37 +745,84 @@ class Api {
741
745
  type: 'SOL',
742
746
  }
743
747
  })
744
- const _getTokenBalance = async (address) => {
745
- try {
746
- return await this.getTokenBalance(address)
747
- } catch (error) {
748
- if (error.message && error.message.includes('could not find account')) {
749
- 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
750
758
  }
751
- throw error
752
759
  }
753
760
  }
754
761
 
762
+ const _getTokenBalance = _wrapAndHandleAccountNotFound(this.getTokenBalance, '0')
763
+ const _getDecimals = _wrapAndHandleAccountNotFound(this.getDecimals, 0)
764
+ const _getSupply = _wrapAndHandleAccountNotFound(this.getSupply, '0')
765
+
755
766
  const resolveTokens = tokenAccounts.map(async (account) => {
756
- const [_tokenMetaPlex, currentAmount, decimal] = await Promise.all([
757
- this.getMetaplexMetadata(account.mint),
758
- _getTokenBalance(account.address),
759
- this.getDecimals(account.mint),
760
- ])
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
+ }
761
780
 
762
- const balance = computeBalance(account.amount, currentAmount)
763
- const tokenMetaPlex = _tokenMetaPlex || { name: null, symbol: null }
764
- return {
765
- name: tokenMetaPlex.name,
766
- symbol: tokenMetaPlex.symbol,
767
- balance,
768
- decimal,
769
- 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
+ }
770
817
  }
771
818
  })
772
819
 
773
820
  const accounts = await Promise.all([...resolveSols, ...resolveTokens])
774
821
  accounts.forEach((account) => {
822
+ if (account.balance === null) {
823
+ return
824
+ }
825
+
775
826
  if (account.balance > 0) {
776
827
  willReceive.push(account)
777
828
  } else {
@@ -788,11 +839,15 @@ class Api {
788
839
  /**
789
840
  * Simulate transaction and return side effects
790
841
  */
791
- simulateAndRetrieveSideEffects = async (transactionMessage, publicKey: string) => {
842
+ simulateAndRetrieveSideEffects = async (
843
+ transactionMessage: SolanaWeb3Message,
844
+ publicKey: string
845
+ ) => {
792
846
  const { config, accountAddresses } = getTransactionSimulationParams(transactionMessage)
847
+ const signatures = new Array(transactionMessage.header.numRequiredSignatures || 1).fill(null)
793
848
  const encodedTransaction = buildRawTransaction(
794
849
  transactionMessage.serialize(),
795
- transactionMessage.header.numRequiredSignatures || 1
850
+ signatures
796
851
  ).toString('base64')
797
852
  const futureAccountsState = await this.simulateTransaction(encodedTransaction, config)
798
853
  const { solAccounts, tokenAccounts } = filterAccountsByOwner(