@rabby-wallet/rabby-api 0.7.0-alpha.0 → 0.7.0-alpha.2

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/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { AxiosAdapter, AxiosRequestConfig } from 'axios';
2
2
  import { RateLimitedAxiosInstance } from 'axios-rate-limit';
3
3
  import { AddrDescResponse, ApprovalStatus, AssetItem, CEXQuote, Cex, ChainWithPendingCount, Collection, CollectionList, CollectionWithFloorPrice, ComplexProtocol, ContractCredit, ExplainTxResponse, ExplainTypedDataResponse, GasLevel, GetTxResponse, LatestExplainTxResponse, MempoolCheckDetail, NFTApprovalResponse, NFTItem, ParseTextResponse, ParseTxResponse, ParseTypedDataResponse, PendingTxItem, Protocol, SecurityCheckResponse, ServerChain, SlippageStatus, Summary, SwapTradeList, TokenApproval, TokenItem, TotalBalanceResponse, Tx, TxAllHistoryResult, TxHistoryResult, TxPushType, TxRequest, UsedChain } from './types';
4
- import { RabbyApiPlugin } from './plugins/intf';
4
+ import { InitOptions, RabbyApiPlugin } from './plugins/intf';
5
5
  interface OpenApiStore {
6
6
  host: string;
7
7
  testnetHost?: string;
@@ -10,12 +10,17 @@ interface Options {
10
10
  store: OpenApiStore | Promise<OpenApiStore>;
11
11
  plugin: RabbyApiPlugin;
12
12
  adapter?: AxiosAdapter;
13
+ clientName?: string;
14
+ clientVersion?: string;
15
+ clientWebHf?: string;
13
16
  }
14
17
  export declare class OpenApiService {
15
18
  #private;
16
19
  store: OpenApiStore;
17
20
  request: RateLimitedAxiosInstance;
21
+ constructor({ store, plugin, adapter, clientName, clientVersion, clientWebHf, }: Options);
18
22
  setHost: (host: string) => Promise<void>;
23
+ setHostSync: (host: string) => void;
19
24
  getHost: () => string;
20
25
  setTestnetHost: (host: string) => Promise<void>;
21
26
  getTestnetHost: () => string | undefined;
@@ -24,9 +29,8 @@ export declare class OpenApiService {
24
29
  params: Array<any>;
25
30
  origin?: string;
26
31
  }) => Promise<any>) | (() => Promise<never>);
27
- adapter?: AxiosAdapter;
28
- constructor({ store, plugin, adapter }: Options);
29
- init: (hf?: string) => Promise<void>;
32
+ init: (options?: string | InitOptions) => Promise<void>;
33
+ initSync(options?: InitOptions): void;
30
34
  asyncJob: <T = any>(url: string, options?: AxiosRequestConfig & {
31
35
  retryDelay?: number;
32
36
  }) => Promise<T>;
package/dist/index.js CHANGED
@@ -18,7 +18,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
18
18
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
19
19
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
20
20
  };
21
- var _OpenApiService_plugin;
21
+ var _OpenApiService_adapter, _OpenApiService_plugin, _OpenApiService_clientName, _OpenApiService_clientVersion, _OpenApiService_clientWebHf;
22
22
  import axios from 'axios';
23
23
  import rateLimit from 'axios-rate-limit';
24
24
  import { ethErrors } from 'eth-rpc-errors';
@@ -26,21 +26,37 @@ import { CHAINS, genSignParams, getChain, getChainByNetwork, sleep, } from './ut
26
26
  import { ASYNC_JOB_RETRY_DELAY, ASYNC_JOB_TIMEOUT } from './const';
27
27
  import { omit } from 'lodash';
28
28
  const maxRPS = 500;
29
- export class OpenApiService {
30
- constructor({ store, plugin, adapter }) {
31
- _OpenApiService_plugin.set(this, void 0);
32
- this.setHost = (host) => __awaiter(this, void 0, void 0, function* () {
33
- var _a, _b, _c, _d;
34
- this.store.host = host;
35
- let hf =
36
- // @ts-expect-error
29
+ function getWebHf() {
30
+ var _a, _b, _c, _d;
31
+ const hf =
32
+ // @ts-expect-error
33
+ typeof chrome === 'undefined'
34
+ ? ''
35
+ : // @ts-expect-error
37
36
  ((_b = (_a = chrome === null || chrome === void 0 ? void 0 : chrome.runtime) === null || _a === void 0 ? void 0 : _a.getURL) === null || _b === void 0 ? void 0 : _b.call(_a, 'bridge.html')) ||
38
37
  (
39
38
  // @ts-expect-error
40
39
  (_d = (_c = chrome === null || chrome === void 0 ? void 0 : chrome.extension) === null || _c === void 0 ? void 0 : _c.getURL) === null || _d === void 0 ? void 0 : _d.call(_c, 'bridge.html')) ||
41
40
  '';
42
- yield this.init(hf);
41
+ return hf;
42
+ }
43
+ export class OpenApiService {
44
+ constructor(_a) {
45
+ var _b;
46
+ var { store, plugin, adapter, clientName = 'Rabby', clientVersion = (_b = process.env.release) !== null && _b !== void 0 ? _b : '0.0.0', clientWebHf = getWebHf(), } = _a;
47
+ _OpenApiService_adapter.set(this, void 0);
48
+ _OpenApiService_plugin.set(this, void 0);
49
+ _OpenApiService_clientName.set(this, void 0);
50
+ _OpenApiService_clientVersion.set(this, void 0);
51
+ _OpenApiService_clientWebHf.set(this, void 0);
52
+ this.setHost = (host) => __awaiter(this, void 0, void 0, function* () {
53
+ this.store.host = host;
54
+ yield this.init({ webHf: __classPrivateFieldGet(this, _OpenApiService_clientWebHf, "f") });
43
55
  });
56
+ this.setHostSync = (host) => {
57
+ this.store.host = host;
58
+ this.initSync({ webHf: __classPrivateFieldGet(this, _OpenApiService_clientWebHf, "f") });
59
+ };
44
60
  this.getHost = () => {
45
61
  return this.store.host;
46
62
  };
@@ -53,49 +69,11 @@ export class OpenApiService {
53
69
  this.ethRpc = () => __awaiter(this, void 0, void 0, function* () {
54
70
  throw ethErrors.provider.disconnected();
55
71
  });
56
- this.init = (hf) => __awaiter(this, void 0, void 0, function* () {
57
- var _e;
58
- yield __classPrivateFieldGet(this, _OpenApiService_plugin, "f").onInitiate({
59
- webHr: hf,
60
- });
61
- const request = axios.create({
62
- baseURL: this.store.host,
63
- adapter: this.adapter,
64
- headers: {
65
- 'X-Client': 'Rabby',
66
- 'X-Version': (_e = process.env.release) !== null && _e !== void 0 ? _e : '0.0.0',
67
- },
68
- });
69
- // sign after rateLimit, timestamp is the latest
70
- request.interceptors.request.use((config) => __awaiter(this, void 0, void 0, function* () {
71
- const { method, url, params } = genSignParams(config);
72
- yield __classPrivateFieldGet(this, _OpenApiService_plugin, "f").onSignRequest({
73
- axiosRequestConfig: config,
74
- parsed: { method, url, params },
75
- });
76
- return config;
77
- }));
78
- this.request = rateLimit(request, { maxRPS });
79
- this.request.interceptors.response.use((response) => {
80
- var _a, _b, _c, _d;
81
- const code = ((_a = response.data) === null || _a === void 0 ? void 0 : _a.err_code) || ((_b = response.data) === null || _b === void 0 ? void 0 : _b.error_code);
82
- const msg = ((_c = response.data) === null || _c === void 0 ? void 0 : _c.err_msg) || ((_d = response.data) === null || _d === void 0 ? void 0 : _d.error_msg);
83
- if (code && code !== 200) {
84
- if (msg) {
85
- let err;
86
- try {
87
- err = new Error(JSON.parse(msg));
88
- }
89
- catch (e) {
90
- err = new Error(msg);
91
- }
92
- throw err;
93
- }
94
- throw new Error(response.data);
95
- }
96
- return response;
97
- });
98
- this._mountMethods();
72
+ this.init = (options) => __awaiter(this, void 0, void 0, function* () {
73
+ var _c, _d;
74
+ options = typeof options === 'string' ? { webHf: options } : options;
75
+ yield ((_d = (_c = __classPrivateFieldGet(this, _OpenApiService_plugin, "f")).onInitiateAsync) === null || _d === void 0 ? void 0 : _d.call(_c, Object.assign({}, options)));
76
+ this.initSync(Object.assign({}, options));
99
77
  });
100
78
  this.asyncJob = (url, options) => {
101
79
  const _option = Object.assign({ timeout: ASYNC_JOB_TIMEOUT, retryDelay: ASYNC_JOB_RETRY_DELAY }, options);
@@ -183,39 +161,39 @@ export class OpenApiService {
183
161
  return data;
184
162
  });
185
163
  this.checkTx = (tx, origin, address, update_nonce = false) => __awaiter(this, void 0, void 0, function* () {
186
- var _f;
164
+ var _e;
187
165
  const { data } = yield this.request.post('/v1/wallet/check_tx', {
188
166
  user_addr: address,
189
167
  origin,
190
168
  tx,
191
169
  update_nonce,
192
- }, this._getRequestOptions((_f = getChainByNetwork(tx.chainId)) === null || _f === void 0 ? void 0 : _f.serverId));
170
+ }, this._getRequestOptions((_e = getChainByNetwork(tx.chainId)) === null || _e === void 0 ? void 0 : _e.serverId));
193
171
  return data;
194
172
  });
195
173
  this.preExecTx = ({ tx, origin, address, updateNonce = false, pending_tx_list = [], }) => __awaiter(this, void 0, void 0, function* () {
196
- var _g;
174
+ var _f;
197
175
  const { data } = yield this.request.post('/v1/wallet/pre_exec_tx', {
198
176
  tx,
199
177
  user_addr: address,
200
178
  origin,
201
179
  update_nonce: updateNonce,
202
180
  pending_tx_list,
203
- }, this._getRequestOptions((_g = getChainByNetwork(tx.chainId)) === null || _g === void 0 ? void 0 : _g.serverId));
181
+ }, this._getRequestOptions((_f = getChainByNetwork(tx.chainId)) === null || _f === void 0 ? void 0 : _f.serverId));
204
182
  return data;
205
183
  });
206
184
  this.historyGasUsed = (params) => __awaiter(this, void 0, void 0, function* () {
207
- var _h, _j;
208
- const { data } = yield this.request.post('/v1/wallet/history_tx_used_gas', Object.assign({}, params), this._getRequestOptions((_j = getChainByNetwork((_h = params === null || params === void 0 ? void 0 : params.tx) === null || _h === void 0 ? void 0 : _h.chainId)) === null || _j === void 0 ? void 0 : _j.serverId));
185
+ var _g, _h;
186
+ const { data } = yield this.request.post('/v1/wallet/history_tx_used_gas', Object.assign({}, params), this._getRequestOptions((_h = getChainByNetwork((_g = params === null || params === void 0 ? void 0 : params.tx) === null || _g === void 0 ? void 0 : _g.chainId)) === null || _h === void 0 ? void 0 : _h.serverId));
209
187
  return data;
210
188
  });
211
189
  this.pendingTxList = (tx, origin, address, update_nonce = false) => __awaiter(this, void 0, void 0, function* () {
212
- var _k;
190
+ var _j;
213
191
  const { data } = yield this.request.post('/v1/wallet/pending_tx_list', {
214
192
  tx,
215
193
  user_addr: address,
216
194
  origin,
217
195
  update_nonce,
218
- }, this._getRequestOptions((_k = getChainByNetwork(tx.chainId)) === null || _k === void 0 ? void 0 : _k.serverId));
196
+ }, this._getRequestOptions((_j = getChainByNetwork(tx.chainId)) === null || _j === void 0 ? void 0 : _j.serverId));
219
197
  return data;
220
198
  });
221
199
  this.traceTx = (txId, traceId, chainId) => __awaiter(this, void 0, void 0, function* () {
@@ -227,11 +205,11 @@ export class OpenApiService {
227
205
  return data;
228
206
  });
229
207
  this.pushTx = (tx, traceId) => __awaiter(this, void 0, void 0, function* () {
230
- var _l;
208
+ var _k;
231
209
  const { data } = yield this.request.post('/v1/wallet/push_tx', {
232
210
  tx,
233
211
  trace_id: traceId,
234
- }, this._getRequestOptions((_l = getChainByNetwork(tx.chainId)) === null || _l === void 0 ? void 0 : _l.serverId));
212
+ }, this._getRequestOptions((_k = getChainByNetwork(tx.chainId)) === null || _k === void 0 ? void 0 : _k.serverId));
235
213
  return data;
236
214
  });
237
215
  this.explainText = (origin, address, text) => __awaiter(this, void 0, void 0, function* () {
@@ -380,10 +358,10 @@ export class OpenApiService {
380
358
  return data;
381
359
  });
382
360
  this.tokenPrice = (tokenName) => __awaiter(this, void 0, void 0, function* () {
383
- var _m;
361
+ var _l;
384
362
  const { data } = yield this.request.get('/v1/token/price_change', Object.assign({ params: {
385
363
  token: tokenName,
386
- } }, this._getRequestOptions((_m = tokenName === null || tokenName === void 0 ? void 0 : tokenName.split(':')) === null || _m === void 0 ? void 0 : _m[0])));
364
+ } }, this._getRequestOptions((_l = tokenName === null || tokenName === void 0 ? void 0 : tokenName.split(':')) === null || _l === void 0 ? void 0 : _l[0])));
387
365
  return data;
388
366
  });
389
367
  this.tokenAuthorizedList = (id, chain_id) => __awaiter(this, void 0, void 0, function* () {
@@ -563,8 +541,8 @@ export class OpenApiService {
563
541
  return data;
564
542
  });
565
543
  this.postSwap = (params) => __awaiter(this, void 0, void 0, function* () {
566
- var _o, _p;
567
- const { data } = yield this.request.post('/v1/wallet/swap_trade', params, this._getRequestOptions((_p = getChainByNetwork((_o = params === null || params === void 0 ? void 0 : params.tx) === null || _o === void 0 ? void 0 : _o.chainId)) === null || _p === void 0 ? void 0 : _p.serverId));
544
+ var _m, _o;
545
+ const { data } = yield this.request.post('/v1/wallet/swap_trade', params, this._getRequestOptions((_o = getChainByNetwork((_m = params === null || params === void 0 ? void 0 : params.tx) === null || _m === void 0 ? void 0 : _m.chainId)) === null || _o === void 0 ? void 0 : _o.serverId));
568
546
  return data;
569
547
  });
570
548
  this.checkSlippage = (params) => __awaiter(this, void 0, void 0, function* () {
@@ -779,8 +757,8 @@ export class OpenApiService {
779
757
  return data;
780
758
  });
781
759
  this.submitTx = (postData) => __awaiter(this, void 0, void 0, function* () {
782
- var _q;
783
- const { data } = yield this.request.post('/v1/wallet/submit_tx', Object.assign({}, postData), this._getRequestOptions((_q = getChainByNetwork(postData.tx.chainId)) === null || _q === void 0 ? void 0 : _q.serverId));
760
+ var _p;
761
+ const { data } = yield this.request.post('/v1/wallet/submit_tx', Object.assign({}, postData), this._getRequestOptions((_p = getChainByNetwork(postData.tx.chainId)) === null || _p === void 0 ? void 0 : _p.serverId));
784
762
  return data;
785
763
  });
786
764
  this.getTxRequests = (ids) => __awaiter(this, void 0, void 0, function* () {
@@ -812,17 +790,17 @@ export class OpenApiService {
812
790
  return data;
813
791
  });
814
792
  this.mempoolChecks = (txId, chainId, node_info) => __awaiter(this, void 0, void 0, function* () {
815
- var _r;
793
+ var _q;
816
794
  const { data } = yield this.request.get('/v1/wallet/mempool_checks', Object.assign({ params: {
817
795
  tx_id: txId,
818
796
  chain_id: chainId,
819
797
  node_info: node_info ? 1 : 0,
820
- } }, this._getRequestOptions((_r = getChainByNetwork(chainId)) === null || _r === void 0 ? void 0 : _r.serverId)));
798
+ } }, this._getRequestOptions((_q = getChainByNetwork(chainId)) === null || _q === void 0 ? void 0 : _q.serverId)));
821
799
  return data;
822
800
  });
823
801
  this.getPendingTxList = (params, options) => __awaiter(this, void 0, void 0, function* () {
824
- var _s;
825
- const data = yield this.asyncJob('/v1/wallet/get_pending_tx_list', Object.assign(Object.assign({ params }, this._getRequestOptions((_s = getChainByNetwork(params.chain_id)) === null || _s === void 0 ? void 0 : _s.serverId)), options));
802
+ var _r;
803
+ const data = yield this.asyncJob('/v1/wallet/get_pending_tx_list', Object.assign(Object.assign({ params }, this._getRequestOptions((_r = getChainByNetwork(params.chain_id)) === null || _r === void 0 ? void 0 : _r.serverId)), options));
826
804
  return data;
827
805
  });
828
806
  this.getLatestPreExec = (params) => __awaiter(this, void 0, void 0, function* () {
@@ -852,7 +830,52 @@ export class OpenApiService {
852
830
  this.store = store;
853
831
  }
854
832
  __classPrivateFieldSet(this, _OpenApiService_plugin, plugin, "f");
855
- this.adapter = adapter;
833
+ __classPrivateFieldSet(this, _OpenApiService_adapter, adapter, "f");
834
+ __classPrivateFieldSet(this, _OpenApiService_clientName, clientName, "f");
835
+ __classPrivateFieldSet(this, _OpenApiService_clientVersion, clientVersion, "f");
836
+ __classPrivateFieldSet(this, _OpenApiService_clientWebHf, clientWebHf, "f");
837
+ }
838
+ initSync(options) {
839
+ var _a, _b;
840
+ (_b = (_a = __classPrivateFieldGet(this, _OpenApiService_plugin, "f")).onInitiate) === null || _b === void 0 ? void 0 : _b.call(_a, Object.assign({}, options));
841
+ const request = axios.create({
842
+ baseURL: this.store.host,
843
+ adapter: __classPrivateFieldGet(this, _OpenApiService_adapter, "f"),
844
+ headers: {
845
+ 'X-Client': __classPrivateFieldGet(this, _OpenApiService_clientName, "f"),
846
+ 'X-Version': __classPrivateFieldGet(this, _OpenApiService_clientVersion, "f"),
847
+ },
848
+ });
849
+ // sign after rateLimit, timestamp is the latest
850
+ request.interceptors.request.use((config) => __awaiter(this, void 0, void 0, function* () {
851
+ const { method, url, params } = genSignParams(config);
852
+ yield __classPrivateFieldGet(this, _OpenApiService_plugin, "f").onSignRequest({
853
+ axiosRequestConfig: config,
854
+ parsed: { method, url, params },
855
+ });
856
+ return config;
857
+ }));
858
+ this.request = rateLimit(request, { maxRPS });
859
+ this.request.interceptors.response.use((response) => {
860
+ var _a, _b, _c, _d;
861
+ const code = ((_a = response.data) === null || _a === void 0 ? void 0 : _a.err_code) || ((_b = response.data) === null || _b === void 0 ? void 0 : _b.error_code);
862
+ const msg = ((_c = response.data) === null || _c === void 0 ? void 0 : _c.err_msg) || ((_d = response.data) === null || _d === void 0 ? void 0 : _d.error_msg);
863
+ if (code && code !== 200) {
864
+ if (msg) {
865
+ let err;
866
+ try {
867
+ err = new Error(JSON.parse(msg));
868
+ }
869
+ catch (e) {
870
+ err = new Error(msg);
871
+ }
872
+ throw err;
873
+ }
874
+ throw new Error(response.data);
875
+ }
876
+ return response;
877
+ });
878
+ this._mountMethods();
856
879
  }
857
880
  }
858
- _OpenApiService_plugin = new WeakMap();
881
+ _OpenApiService_adapter = new WeakMap(), _OpenApiService_plugin = new WeakMap(), _OpenApiService_clientName = new WeakMap(), _OpenApiService_clientVersion = new WeakMap(), _OpenApiService_clientWebHf = new WeakMap();
@@ -1,8 +1,10 @@
1
1
  import type { AxiosRequestConfig } from 'axios';
2
+ export declare type InitOptions = {
3
+ webHf?: string;
4
+ };
2
5
  export interface RabbyApiPlugin {
3
- onInitiate: (options?: {
4
- webHr?: string;
5
- }) => Promise<void>;
6
+ onInitiateAsync?: (options?: InitOptions) => Promise<void>;
7
+ onInitiate?: (options?: InitOptions) => Promise<void>;
6
8
  onSignRequest: (ctx: {
7
9
  axiosRequestConfig: AxiosRequestConfig<any>;
8
10
  parsed: {
@@ -10,9 +10,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  import * as sign from '@rabby-wallet/rabby-sign/umd/sign-wasm-rabby';
11
11
  import { SIGN_HDS } from '../const';
12
12
  export const WebSignApiPlugin = {
13
- onInitiate(options) {
13
+ onInitiateAsync(options) {
14
14
  return __awaiter(this, void 0, void 0, function* () {
15
- yield sign.lW(options === null || options === void 0 ? void 0 : options.webHr);
15
+ yield sign.lW(options === null || options === void 0 ? void 0 : options.webHf);
16
16
  });
17
17
  },
18
18
  onSignRequest(ctx) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rabby-wallet/rabby-api",
3
- "version": "0.7.0-alpha.0",
3
+ "version": "0.7.0-alpha.2",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [