@exodus/solana-api 1.4.0 → 1.4.1

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 +3 -3
  2. package/src/index.js +25 -24
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/solana-api",
3
- "version": "1.4.0",
3
+ "version": "1.4.1",
4
4
  "description": "Exodus internal Solana asset API wrapper",
5
5
  "main": "src/index.js",
6
6
  "files": [
@@ -14,7 +14,7 @@
14
14
  },
15
15
  "dependencies": {
16
16
  "@exodus/asset-json-rpc": "^1.0.0",
17
- "@exodus/solana-lib": "^1.3.0",
17
+ "@exodus/solana-lib": "^1.3.1",
18
18
  "lodash": "^4.17.11",
19
19
  "url-join": "4.0.0",
20
20
  "wretch": "^1.5.2"
@@ -22,5 +22,5 @@
22
22
  "devDependencies": {
23
23
  "node-fetch": "~1.6.3"
24
24
  },
25
- "gitHead": "158aae6b4aeb871b1566b2500456a5f475ef9d5f"
25
+ "gitHead": "49f3f78ac41acaf405e2849cccb2f9ecd0060424"
26
26
  }
package/src/index.js CHANGED
@@ -5,13 +5,13 @@ import {
5
5
  deserializeMetaplexMetadata,
6
6
  getTransactionSimulationParams,
7
7
  filterAccountsByOwner,
8
- tokens,
9
8
  SYSTEM_PROGRAM_ID,
10
9
  STAKE_PROGRAM_ID,
11
10
  TOKEN_PROGRAM_ID,
12
11
  SOL_DECIMAL,
13
12
  computeBalance,
14
13
  } from '@exodus/solana-lib'
14
+ import assets from '@exodus/assets'
15
15
  import assert from 'assert'
16
16
  import lodash from 'lodash'
17
17
  import urljoin from 'url-join'
@@ -25,6 +25,7 @@ const RPC_URL = 'https://solana.a.exodus.io' // https://vip-api.mainnet-beta.sol
25
25
  class Api {
26
26
  constructor(rpcUrl) {
27
27
  this.setServer(rpcUrl)
28
+ this.setTokens(assets)
28
29
  }
29
30
 
30
31
  setServer(rpcUrl) {
@@ -32,12 +33,21 @@ class Api {
32
33
  this.api = createApi(this.rpcUrl)
33
34
  }
34
35
 
36
+ setTokens(assets = {}) {
37
+ const solTokens = lodash.pickBy(assets, (asset) => asset.assetType === 'SOLANA_TOKEN')
38
+ this.tokens = lodash.mapKeys(solTokens, (v) => v.mintAddress )
39
+ }
40
+
35
41
  request(path, contentType = 'application/json'): Wretcher {
36
42
  return wretch(urljoin(this.rpcUrl, path)).headers({
37
43
  'Content-type': contentType,
38
44
  })
39
45
  }
40
46
 
47
+ isTokenSupported(mint: string) {
48
+ return !!this.tokens[mint]
49
+ }
50
+
41
51
  async getEpochInfo(): number {
42
52
  const { epoch } = await this.api.post({
43
53
  method: 'getEpochInfo',
@@ -140,7 +150,7 @@ class Api {
140
150
  if (txDetail === null) return
141
151
 
142
152
  const timestamp = txDetail.blockTime * 1000
143
- const parsedTx = Api.parseTransaction(address, txDetail, tokenAccountsByOwner)
153
+ const parsedTx = this.parseTransaction(address, txDetail, tokenAccountsByOwner)
144
154
  if (!parsedTx.from) return // cannot parse it
145
155
 
146
156
  transactions.push({
@@ -161,16 +171,7 @@ class Api {
161
171
  return { transactions, newCursor }
162
172
  }
163
173
 
164
- parseTransaction(...args) {
165
- // alias
166
- return Api.parseTransaction(...args)
167
- }
168
-
169
- static parseTransaction(
170
- ownerAddress: string,
171
- txDetails: Object,
172
- tokenAccountsByOwner: ?Array
173
- ): Object {
174
+ parseTransaction(ownerAddress: string, txDetails: Object, tokenAccountsByOwner: ?Array): Object {
174
175
  const { fee, preTokenBalances, postTokenBalances } = txDetails.meta
175
176
  let { instructions, accountKeys } = txDetails.transaction.message
176
177
  instructions = instructions
@@ -305,10 +306,14 @@ class Api {
305
306
 
306
307
  // group by owner and supported token
307
308
  const preBalances = preTokenBalances.filter((t) => {
308
- return accountIndexes[t.accountIndex].owner === ownerAddress && isTokenSupported(t.mint)
309
+ return (
310
+ accountIndexes[t.accountIndex].owner === ownerAddress && this.isTokenSupported(t.mint)
311
+ )
309
312
  })
310
313
  const postBalances = postTokenBalances.filter((t) => {
311
- return accountIndexes[t.accountIndex].owner === ownerAddress && isTokenSupported(t.mint)
314
+ return (
315
+ accountIndexes[t.accountIndex].owner === ownerAddress && this.isTokenSupported(t.mint)
316
+ )
312
317
  })
313
318
 
314
319
  if (preBalances.length && preBalances.length === postBalances.length) {
@@ -319,8 +324,8 @@ class Api {
319
324
  const isSending = Math.sign(amount) <= 0
320
325
 
321
326
  const mint = lodash.get(postBalances, '[0].mint', '')
322
- const { tokenName, ticker } = tokens.find(({ mintAddress }) => mintAddress === mint) || {
323
- tokenName: 'unknown',
327
+ const { name, ticker } = this.tokens[mint] || {
328
+ name: 'unknown',
324
329
  ticker: 'UNKNOWN',
325
330
  }
326
331
 
@@ -328,7 +333,7 @@ class Api {
328
333
  owner: isSending ? ownerAddress : TOKEN_PROGRAM_ID.toBase58(),
329
334
  token: {
330
335
  tokenAccountAddress: '', // token account is closed after the swap
331
- tokenName,
336
+ tokenName: name,
332
337
  ticker,
333
338
  },
334
339
  from: isSending ? ownerAddress : TOKEN_PROGRAM_ID.toBase58(),
@@ -365,15 +370,15 @@ class Api {
365
370
  const { pubkey, account } = entry
366
371
 
367
372
  const mint = lodash.get(account, 'data.parsed.info.mint')
368
- const token = tokens.find(({ mintAddress }) => mintAddress === mint) || {
369
- tokenName: 'unknown',
373
+ const token = this.tokens[mint] || {
374
+ name: 'unknown',
370
375
  ticker: 'UNKNOWN',
371
376
  }
372
377
  const balance = lodash.get(account, 'data.parsed.info.tokenAmount.amount', '0')
373
378
  tokenAccounts.push({
374
379
  tokenAccountAddress: pubkey,
375
380
  owner: address,
376
- tokenName: token.tokenName,
381
+ tokenName: token.name,
377
382
  ticker: token.ticker,
378
383
  balance,
379
384
  mintAddress: mint,
@@ -663,8 +668,4 @@ class Api {
663
668
  }
664
669
  }
665
670
 
666
- function isTokenSupported(mint: string) {
667
- return !!tokens.find(({ mintAddress }) => mintAddress === mint)
668
- }
669
-
670
671
  export default new Api()