@exodus/solana-api 1.2.3 → 1.2.4

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 +9 -5
  2. package/src/index.js +54 -28
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/solana-api",
3
- "version": "1.2.3",
3
+ "version": "1.2.4",
4
4
  "description": "Exodus internal Solana asset API wrapper",
5
5
  "main": "src/index.js",
6
6
  "files": [
@@ -13,9 +13,13 @@
13
13
  "access": "restricted"
14
14
  },
15
15
  "dependencies": {
16
- "@exodus/solana-lib": "^1.2.3",
17
- "@exodus/solana-web3.js": "^0.87.1-exodus4",
18
- "lodash": "^4.17.11"
16
+ "@exodus/asset-json-rpc": "^1.0.0",
17
+ "@exodus/solana-lib": "^1.2.4",
18
+ "lodash": "^4.17.11",
19
+ "wretch": "^1.5.2"
19
20
  },
20
- "gitHead": "5be453e5b6c47cf82b8c80a83e8a4bebbb487a2e"
21
+ "devDependencies": {
22
+ "node-fetch": "~1.6.3"
23
+ },
24
+ "gitHead": "f5a2d444a2789ca118671566f32805dba061ce5a"
21
25
  }
package/src/index.js CHANGED
@@ -1,10 +1,10 @@
1
1
  // @flow
2
- import { Connection, PublicKey } from '@exodus/solana-web3.js'
2
+ import createApi from '@exodus/asset-json-rpc'
3
3
  import { tokens, SYSTEM_PROGRAM_ID, TOKEN_PROGRAM_ID } from '@exodus/solana-lib'
4
4
  import assert from 'assert'
5
5
  import lodash from 'lodash'
6
6
 
7
- // Doc: https://solana-labs.github.io/solana-web3.js/
7
+ // Doc: https://docs.solana.com/apps/jsonrpc-api
8
8
 
9
9
  const RPC_URL = 'https://api.mainnet-beta.solana.com' // https://solana-api.projectserum.com
10
10
 
@@ -16,45 +16,59 @@ class Api {
16
16
 
17
17
  setServer(rpcUrl) {
18
18
  this.rpcUrl = rpcUrl || RPC_URL
19
- this.connection = new Connection(this.rpcUrl)
19
+ this.api = createApi(this.rpcUrl)
20
20
  }
21
21
 
22
22
  async getRecentBlockHash(): string {
23
- const { blockhash } = await this.connection.getRecentBlockhash()
23
+ const {
24
+ value: { blockhash },
25
+ } = await this.api.post({
26
+ method: 'getRecentBlockhash',
27
+ })
24
28
  return blockhash
25
29
  }
26
30
 
27
31
  // Transaction structure: https://docs.solana.com/apps/jsonrpc-api#transaction-structure
28
32
  async getTransactionById(id: string) {
29
- const { result } = await this.connection._rpcRequest('getConfirmedTransaction', [
30
- id,
31
- 'jsonParsed',
32
- ])
33
+ const result = await this.api.post({
34
+ method: 'getConfirmedTransaction',
35
+ params: [id, 'jsonParsed'],
36
+ })
33
37
  return result
34
38
  }
35
39
 
36
40
  async getFee(): number {
37
41
  const {
38
- feeCalculator: { lamportsPerSignature },
39
- } = await this.connection.getRecentBlockhash()
42
+ value: {
43
+ feeCalculator: { lamportsPerSignature },
44
+ },
45
+ } = await this.api.post({
46
+ method: 'getRecentBlockhash',
47
+ })
40
48
  return lamportsPerSignature
41
49
  }
42
50
 
43
51
  async getBalance(address: string): number {
44
- return this.connection.getBalance(new PublicKey(address))
52
+ const res = await this.api.post({
53
+ method: 'getBalance',
54
+ params: [address],
55
+ })
56
+ return res.value || 0
45
57
  }
46
58
 
47
59
  async getBlockTime(slot: number) {
48
60
  // might result in error if executed on a validator with partial ledger (https://github.com/solana-labs/solana/issues/12413)
49
- return this.connection.getBlockTime(slot)
61
+ return this.api.post({
62
+ method: 'getBlockTime',
63
+ params: [slot],
64
+ })
50
65
  }
51
66
 
52
67
  async getConfirmedSignaturesForAddress(address: string, { until, before, limit } = {}): any {
53
68
  until = until || undefined
54
- return this.connection.getConfirmedSignaturesForAddress2(new PublicKey(address), {
55
- until,
56
- before,
57
- limit,
69
+ return this.api.post({
70
+ method: 'getConfirmedSignaturesForAddress2',
71
+ params: [address, { until, before, limit }],
58
72
  })
59
73
  }
60
74
 
@@ -191,13 +205,10 @@ class Api {
191
205
  }
192
206
 
193
207
  async getTokenAccountsByOwner(address: string, tokenTicker: ?string): Array {
194
- const {
195
- result: { value: accountsList },
196
- } = await this.connection._rpcRequest('getTokenAccountsByOwner', [
197
- address,
198
- { programId: TOKEN_PROGRAM_ID.toBase58() },
199
- { encoding: 'jsonParsed' },
200
- ])
208
+ const { value: accountsList } = await this.api.post({
209
+ method: 'getTokenAccountsByOwner',
210
+ params: [address, { programId: TOKEN_PROGRAM_ID.toBase58() }, { encoding: 'jsonParsed' }],
211
+ })
201
212
 
202
213
  const tokenAccounts = []
203
214
  for (let entry of accountsList) {
@@ -225,11 +236,22 @@ class Api {
225
236
 
226
237
  async getAddressType(address: string) {
227
238
  // solana, token or null (unknown), meaning address has never been initialized
228
- const account = await this.connection.getAccountInfo(new PublicKey(address))
229
- if (account === null) return null
230
- return account.owner.equals(SYSTEM_PROGRAM_ID)
239
+ const { value } = await this.api.post({
240
+ method: 'getAccountInfo',
241
+ params: [address, { encoding: 'base64' }],
242
+ })
243
+ if (value === null) return null
244
+
245
+ const account = {
246
+ executable: value.executable,
247
+ owner: value.owner,
248
+ lamports: value.lamports,
249
+ data: value.data,
250
+ }
251
+
252
+ return account.owner === SYSTEM_PROGRAM_ID.toBase58()
231
253
  ? 'solana'
232
- : account.owner.equals(TOKEN_PROGRAM_ID)
254
+ : account.owner === TOKEN_PROGRAM_ID.toBase58()
233
255
  ? 'token'
234
256
  : null
235
257
  }
@@ -250,7 +272,11 @@ class Api {
250
272
  broadcastTransaction = async (signedTx: string): string => {
251
273
  console.log('Solana broadcasting TX:', signedTx) // base64
252
274
 
253
- const { message, ...result } = await this.connection.sendEncodedTransaction(signedTx)
275
+ const { message, ...result } = await this.api.post({
276
+ method: 'sendTransaction',
277
+ params: [signedTx, { encoding: 'base64' }],
278
+ })
279
+
254
280
  if (message) throw new Error(message)
255
281
 
256
282
  console.log(`tx ${result} sent!`)