@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.
- package/package.json +9 -5
- 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
|
+
"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/
|
|
17
|
-
"@exodus/solana-
|
|
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
|
-
"
|
|
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
|
|
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
|
|
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.
|
|
19
|
+
this.api = createApi(this.rpcUrl)
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
async getRecentBlockHash(): string {
|
|
23
|
-
const {
|
|
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
|
|
30
|
-
|
|
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
|
-
|
|
39
|
-
|
|
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
|
-
|
|
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.
|
|
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.
|
|
55
|
-
|
|
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
|
-
|
|
196
|
-
|
|
197
|
-
|
|
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
|
|
229
|
-
|
|
230
|
-
|
|
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.
|
|
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.
|
|
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!`)
|