@exodus/ethereum-api 0.1.1 → 0.2.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.
@@ -1,59 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.filterTxsSent = filterTxsSent;
7
- exports.filterTxsReceived = filterTxsReceived;
8
- exports.ethCall = exports.ws = exports.setApiKey = exports.getLogs = exports.gasPrice = exports.getCode = exports.estimateGas = exports.getTransactionReceipt = exports.getTransactionCount = exports.sendRawTransaction = exports.tokenBalance = exports.fetchTxlistinternal = exports.fetchTxlist = exports.fetchBalance = exports.ETHERSCAN_WS_URL = void 0;
9
-
10
- var _account = require("./account");
11
-
12
- var _proxy = require("./proxy");
13
-
14
- var _request = require("./request");
15
-
16
- var _logs = require("./logs");
17
-
18
- var _ws = _interopRequireDefault(require("./ws"));
19
-
20
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21
-
22
- const ETHERSCAN_WS_URL = 'wss://socket.etherscan.io/wshandler';
23
- exports.ETHERSCAN_WS_URL = ETHERSCAN_WS_URL;
24
- const fetchBalance = _account.fetchBalance;
25
- exports.fetchBalance = fetchBalance;
26
- const fetchTxlist = _account.fetchTxlist;
27
- exports.fetchTxlist = fetchTxlist;
28
- const fetchTxlistinternal = _account.fetchTxlistinternal;
29
- exports.fetchTxlistinternal = fetchTxlistinternal;
30
- const tokenBalance = _account.tokenBalance;
31
- exports.tokenBalance = tokenBalance;
32
- const sendRawTransaction = _proxy.sendRawTransaction;
33
- exports.sendRawTransaction = sendRawTransaction;
34
- const getTransactionCount = _proxy.getTransactionCount;
35
- exports.getTransactionCount = getTransactionCount;
36
- const getTransactionReceipt = _proxy.getTransactionReceipt;
37
- exports.getTransactionReceipt = getTransactionReceipt;
38
- const estimateGas = _proxy.estimateGas;
39
- exports.estimateGas = estimateGas;
40
- const getCode = _proxy.getCode;
41
- exports.getCode = getCode;
42
- const gasPrice = _proxy.gasPrice;
43
- exports.gasPrice = gasPrice;
44
- const getLogs = _logs.getLogs;
45
- exports.getLogs = getLogs;
46
- const setApiKey = _request.setEtherscanApiKey;
47
- exports.setApiKey = setApiKey;
48
- const ws = (0, _ws.default)(ETHERSCAN_WS_URL);
49
- exports.ws = ws;
50
- const ethCall = _proxy.ethCall;
51
- exports.ethCall = ethCall;
52
-
53
- function filterTxsSent(addr, etherscanTxs) {
54
- return etherscanTxs.filter(tx => tx.from.toLowerCase() === addr.toLowerCase());
55
- }
56
-
57
- function filterTxsReceived(addr, etherscanTxs) {
58
- return etherscanTxs.filter(tx => tx.to.toLowerCase() === addr.toLowerCase());
59
- }
@@ -1,28 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.getLogs = getLogs;
7
-
8
- var _assert = _interopRequireDefault(require("assert"));
9
-
10
- var _request2 = _interopRequireDefault(require("./request"));
11
-
12
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
-
14
- const isValidResponseCheck = x => x.status === '1' && x.message === 'OK' || x.message === 'No records found';
15
-
16
- const _request = async (...args) => (0, _request2.default)(isValidResponseCheck, 'logs', ...args);
17
-
18
- async function getLogs(address, fromBlock, toBlock, options) {
19
- const params = { ...options,
20
- address,
21
- fromBlock,
22
- toBlock
23
- };
24
- const events = await _request('getLogs', params); // simple check
25
-
26
- (0, _assert.default)(Array.isArray(events), `Invalid transactions: ${events}`);
27
- return events;
28
- }
@@ -1,65 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.sendRawTransaction = sendRawTransaction;
7
- exports.getTransactionCount = getTransactionCount;
8
- exports.getTransactionReceipt = getTransactionReceipt;
9
- exports.estimateGas = estimateGas;
10
- exports.getCode = getCode;
11
- exports.gasPrice = gasPrice;
12
- exports.ethCall = ethCall;
13
-
14
- var _request2 = _interopRequireDefault(require("./request"));
15
-
16
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
-
18
- const isValidResponseCheck = x => x.result !== undefined;
19
-
20
- const _request = async (...args) => (0, _request2.default)(isValidResponseCheck, 'proxy', ...args);
21
-
22
- async function sendRawTransaction(data) {
23
- const txhash = await _request('eth_sendRawTransaction', {
24
- hex: '0x' + data
25
- });
26
- const isValidTxHash = /^0x[0-9a-fA-F]{64}$/.test(txhash);
27
- if (!isValidTxHash) throw new Error(`Invalid tx hash: ${txhash}`);
28
- return txhash.slice(2);
29
- }
30
-
31
- async function getTransactionCount(address) {
32
- return _request('eth_getTransactionCount', {
33
- address
34
- });
35
- }
36
-
37
- async function getTransactionReceipt(txhash) {
38
- return _request('eth_getTransactionReceipt', {
39
- txhash
40
- });
41
- }
42
-
43
- async function estimateGas(data) {
44
- return _request('eth_estimateGas', data);
45
- }
46
-
47
- async function getCode(address) {
48
- const code = await _request('eth_getCode', {
49
- address
50
- });
51
- const isValidCode = /^0x[0-9a-fA-F]*$/.test(code) && code.length % 2 === 0;
52
- if (!isValidCode) throw new Error(`Invalid address code: ${code}`);
53
- return code;
54
- }
55
-
56
- async function gasPrice() {
57
- const price = await _request('eth_gasPrice');
58
- const isValidPrice = /^0x[0-9a-fA-F]+$/.test(price);
59
- if (!isValidPrice) throw new Error(`Invalid price: ${price}`);
60
- return price;
61
- }
62
-
63
- async function ethCall(data) {
64
- return _request('eth_call', data);
65
- }
@@ -1,43 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.setEtherscanApiKey = setEtherscanApiKey;
7
- exports.default = void 0;
8
-
9
- var _ms = _interopRequireDefault(require("ms"));
10
-
11
- var _makeConcurrent = _interopRequireDefault(require("make-concurrent"));
12
-
13
- var _fetchival = _interopRequireDefault(require("fetchival"));
14
-
15
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
16
-
17
- // The module in desktop explicitly sets node-fetch. Do we need this?
18
- // import fetch from '../fetch'
19
- // fetchival.fetch = fetch
20
- const ETHERSCAN_API_URL = 'https://api.etherscan.io/api';
21
- const DEFAULT_ETHERSCAN_API_KEY = 'XM3VGRSNW1TMSIR14I9MVFP15X74GNHTRI';
22
- let etherscanApiKey = DEFAULT_ETHERSCAN_API_KEY;
23
-
24
- function setEtherscanApiKey(apiKey) {
25
- etherscanApiKey = apiKey || DEFAULT_ETHERSCAN_API_KEY;
26
- }
27
-
28
- var _default = (0, _makeConcurrent.default)(async function (isValidResponseCheck, module, action, params = {}) {
29
- const queryParams = { ...params,
30
- module,
31
- action,
32
- apiKey: etherscanApiKey
33
- };
34
- const data = await (0, _fetchival.default)(ETHERSCAN_API_URL, {
35
- timeout: (0, _ms.default)('15s')
36
- }).get(queryParams);
37
- if (!isValidResponseCheck(data)) throw new Error(`Invalid response: ${JSON.stringify(data)}`);
38
- return data.result;
39
- }, {
40
- concurrency: 3
41
- });
42
-
43
- exports.default = _default;
@@ -1,107 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = createWebSocket;
7
-
8
- var _events = require("events");
9
-
10
- var _ms = _interopRequireDefault(require("ms"));
11
-
12
- var _websocket = _interopRequireDefault(require("../websocket"));
13
-
14
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
-
16
- const RECONNECT_INTERVAL = (0, _ms.default)('10s');
17
- const PING_INTERVAL = (0, _ms.default)('20s');
18
-
19
- function createWebSocket(url) {
20
- const addresses = new Set();
21
- const events = new _events.EventEmitter().setMaxListeners(20);
22
- const pingMessage = JSON.stringify({
23
- event: 'ping'
24
- });
25
- let ws;
26
- let wsOpened = false;
27
- let opened = false;
28
- let openTimeoutId;
29
- let pingIntervalId;
30
-
31
- function subscribeAddress(address) {
32
- const data = JSON.stringify({
33
- event: 'txlist',
34
- address
35
- });
36
- ws.send(data);
37
- }
38
-
39
- function onMessage(data) {
40
- data = JSON.parse(data);
41
-
42
- switch (data.event) {
43
- case 'txlist':
44
- for (const tx of data.result) events.emit(`address-${data.address}`, tx);
45
-
46
- break;
47
-
48
- case 'subscribe-txlist':
49
- const match = data.message.toLowerCase().match(/0x[0-9a-f]{40}/);
50
- if (match && data.status === '1') events.emit(`address-${match[0]}-subscribed`);else ws.close();
51
- break;
52
- }
53
- }
54
-
55
- function isOpened() {
56
- return wsOpened;
57
- }
58
-
59
- function open() {
60
- opened = true;
61
- clearTimeout(openTimeoutId);
62
- if (ws) return;
63
- ws = new _websocket.default(url);
64
- ws.on('message', data => {
65
- try {
66
- onMessage(data);
67
- } catch (err) {}
68
- });
69
- ws.once('open', () => {
70
- for (const address of addresses.values()) subscribeAddress(address);
71
-
72
- pingIntervalId = setInterval(() => ws && ws.send(pingMessage), PING_INTERVAL);
73
- wsOpened = true;
74
- events.emit('open');
75
- });
76
- ws.once('close', () => {
77
- ws = null;
78
- clearInterval(pingIntervalId);
79
- if (opened) openTimeoutId = setTimeout(open, RECONNECT_INTERVAL);
80
- wsOpened = false;
81
- events.emit('close');
82
- });
83
- }
84
-
85
- function close() {
86
- opened = false;
87
- clearTimeout(openTimeoutId);
88
- if (!ws) return;
89
- ws.close();
90
- ws = null;
91
- }
92
-
93
- function watch(address) {
94
- address = address.toLowerCase();
95
- if (addresses.has(address)) return;
96
- addresses.add(address);
97
- if (wsOpened) subscribeAddress(address);
98
- }
99
-
100
- return {
101
- events,
102
- isOpened,
103
- open,
104
- close,
105
- watch
106
- };
107
- }
@@ -1,170 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.create = create;
7
-
8
- var _urlJoin = _interopRequireDefault(require("url-join"));
9
-
10
- var _url = _interopRequireDefault(require("url"));
11
-
12
- var _fetchival = _interopRequireDefault(require("fetchival"));
13
-
14
- var _ms = _interopRequireDefault(require("ms"));
15
-
16
- var _ws = _interopRequireDefault(require("./ws"));
17
-
18
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
-
20
- function create(defaultURL) {
21
- let API_URL = defaultURL;
22
- const ws = (0, _ws.default)(() => {
23
- // eslint-disable-next-line
24
- const obj = _url.default.parse(API_URL);
25
-
26
- obj.protocol = 'wss:';
27
- return _url.default.format(obj);
28
- });
29
-
30
- async function request(module, params = {}) {
31
- const url = (0, _urlJoin.default)(API_URL, module);
32
-
33
- try {
34
- return await (0, _fetchival.default)(url, {
35
- timeout: (0, _ms.default)('15s')
36
- }).get(params);
37
- } catch (err) {
38
- let nerr = err;
39
-
40
- if (err.response && err.response.status === 500) {
41
- try {
42
- const data = await err.response.json();
43
- const msg = data.error.replace(/^RPC error \(code: -\d+\): /, '');
44
- nerr = new Error(msg);
45
- } catch (err) {}
46
- }
47
-
48
- throw nerr;
49
- }
50
- }
51
-
52
- return {
53
- getURL() {
54
- return API_URL;
55
- },
56
-
57
- setURL(newURL) {
58
- API_URL = newURL || defaultURL;
59
-
60
- if (ws.isCreated()) {
61
- ws.close();
62
- ws.open();
63
- }
64
- },
65
-
66
- ws,
67
-
68
- async getBalance(address, opts) {
69
- opts = {
70
- startblock: 'earliest',
71
- endblock: 'pending',
72
- ...opts
73
- };
74
- return request('balance', {
75
- address,
76
- from: opts.startblock,
77
- to: opts.endblock
78
- });
79
- },
80
-
81
- async getHistory(address, opts) {
82
- opts = {
83
- startblock: 'earliest',
84
- endblock: 'pending',
85
- limit: 1000,
86
- ...opts
87
- };
88
- return request('history', {
89
- address,
90
- from: opts.startblock,
91
- to: opts.endblock,
92
- limit: opts.limit
93
- });
94
- },
95
-
96
- async gasPrice() {
97
- return request('proxy', {
98
- method: 'eth_gasPrice'
99
- });
100
- },
101
-
102
- async getTransactionCount(address, tag = 'latest') {
103
- return request('proxy', {
104
- method: 'eth_getTransactionCount',
105
- address,
106
- tag
107
- });
108
- },
109
-
110
- async getTransactionByHash(hash) {
111
- return request('proxy', {
112
- method: 'eth_getTransactionByHash',
113
- hash
114
- });
115
- },
116
-
117
- async getTransactionReceipt(txhash) {
118
- return request('proxy', {
119
- method: 'eth_getTransactionReceipt',
120
- txhash
121
- });
122
- },
123
-
124
- async getCode(address, tag = 'latest') {
125
- return request('proxy', {
126
- method: 'eth_getCode',
127
- address,
128
- tag
129
- });
130
- },
131
-
132
- async sendRawTransaction(data) {
133
- return request('proxy', {
134
- method: 'eth_sendRawTransaction',
135
- hex: '0x' + data
136
- });
137
- },
138
-
139
- async estimateGas(data, tag = 'latest') {
140
- return request('proxy', {
141
- method: 'eth_estimateGas',
142
- ...data,
143
- tag
144
- });
145
- },
146
-
147
- async ethCall(data, tag = 'latest') {
148
- return request('proxy', {
149
- method: 'eth_call',
150
- ...data,
151
- tag
152
- });
153
- },
154
-
155
- async blockNumber() {
156
- return request('proxy', {
157
- method: 'eth_blockNumber'
158
- });
159
- },
160
-
161
- async getBlockByNumber(number, isFullTransactions = false) {
162
- return request('proxy', {
163
- method: 'eth_getBlockByNumber',
164
- number,
165
- isFullTransactions
166
- });
167
- }
168
-
169
- };
170
- }
@@ -1,17 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.etc = exports.eth = void 0;
7
-
8
- var _api = require("./api");
9
-
10
- const EXODUS_ETH_SERVER_URL = 'https://eth.a.exodus.io/wallet/v1/';
11
- const EXODUS_ETC_SERVER_URL = 'https://etc.a.exodus.io/wallet/v1/'; // allow self-signed certs
12
- // process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
13
-
14
- const eth = (0, _api.create)(EXODUS_ETH_SERVER_URL);
15
- exports.eth = eth;
16
- const etc = (0, _api.create)(EXODUS_ETC_SERVER_URL);
17
- exports.etc = etc;
@@ -1,116 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = createWebSocket;
7
-
8
- var _ms = _interopRequireDefault(require("ms"));
9
-
10
- var _events = require("events");
11
-
12
- var _websocket = _interopRequireDefault(require("../websocket"));
13
-
14
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
-
16
- const RECONNECT_INTERVAL = (0, _ms.default)('10s');
17
- const PING_INTERVAL = (0, _ms.default)('10s');
18
-
19
- function createWebSocket(getURL) {
20
- const addresses = new Set();
21
- const events = new _events.EventEmitter();
22
- let ws;
23
- let pingIntervalId = null;
24
- let opened = false;
25
- let openTimeoutId;
26
-
27
- function subscribeAddress(address) {
28
- const data = JSON.stringify({
29
- type: 'subscribe-address',
30
- address
31
- });
32
- ws.send(data);
33
- }
34
-
35
- function subscribeGasPrice() {
36
- const data = JSON.stringify({
37
- type: 'subscribe-gasprice'
38
- });
39
- ws.send(data);
40
- }
41
-
42
- function onMessage(data) {
43
- data = JSON.parse(data);
44
-
45
- switch (data.type) {
46
- case 'address':
47
- events.emit(`address-${data.address}`);
48
- break;
49
-
50
- case 'gasprice':
51
- events.emit('gasprice', data.gasprice);
52
- break;
53
- }
54
- }
55
-
56
- function open() {
57
- opened = true;
58
- clearTimeout(openTimeoutId);
59
- if (ws) return;
60
- ws = new _websocket.default(getURL());
61
-
62
- ws.onerror = () => {};
63
-
64
- ws.onmessage = e => {
65
- try {
66
- onMessage(e.data);
67
- } catch (err) {}
68
- };
69
-
70
- ws.onopen = () => {
71
- for (const address of addresses.values()) subscribeAddress(address);
72
-
73
- subscribeGasPrice();
74
- pingIntervalId = setInterval(() => ws.ping(), PING_INTERVAL);
75
- events.emit('open');
76
- };
77
-
78
- ws.onclose = () => {
79
- ws = null;
80
- clearInterval(pingIntervalId);
81
- pingIntervalId = null;
82
- if (opened) openTimeoutId = setTimeout(open, RECONNECT_INTERVAL);
83
- events.emit('close');
84
- };
85
- }
86
-
87
- function close() {
88
- opened = false;
89
- clearTimeout(openTimeoutId);
90
- if (!ws) return;
91
- ws.onerror = null;
92
- ws.onmessage = null;
93
- ws.onopen = null;
94
- ws.onclose = null;
95
- ws.close();
96
- ws = null;
97
- clearInterval(pingIntervalId);
98
- pingIntervalId = null;
99
- events.emit('close');
100
- }
101
-
102
- function watch(address) {
103
- if (addresses.has(address)) return;
104
- addresses.add(address);
105
- if (ws) subscribeAddress(address);
106
- }
107
-
108
- return {
109
- events,
110
- open,
111
- close,
112
- watch,
113
- isCreated: () => !!ws,
114
- isOpened: () => !!pingIntervalId
115
- };
116
- }
@@ -1,11 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- /* global fetch */
9
- var _default = fetch; // fetch already exists in react-native
10
-
11
- exports.default = _default;
@@ -1,11 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- /* global fetch */
9
- var _default = fetch; // fetch already exists in react-native
10
-
11
- exports.default = _default;
@@ -1,13 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- var _nodeFetch = _interopRequireDefault(require("node-fetch"));
9
-
10
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
-
12
- var _default = _nodeFetch.default;
13
- exports.default = _default;
package/lib/index.js DELETED
@@ -1,43 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- var _exportNames = {
7
- etherscan: true
8
- };
9
- exports.etherscan = void 0;
10
-
11
- var etherscan = _interopRequireWildcard(require("./etherscan"));
12
-
13
- exports.etherscan = etherscan;
14
-
15
- var _withFallback = require("./with-fallback");
16
-
17
- Object.keys(_withFallback).forEach(function (key) {
18
- if (key === "default" || key === "__esModule") return;
19
- if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
20
- Object.defineProperty(exports, key, {
21
- enumerable: true,
22
- get: function () {
23
- return _withFallback[key];
24
- }
25
- });
26
- });
27
-
28
- var _exodusEthServer = require("./exodus-eth-server");
29
-
30
- Object.keys(_exodusEthServer).forEach(function (key) {
31
- if (key === "default" || key === "__esModule") return;
32
- if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
33
- Object.defineProperty(exports, key, {
34
- enumerable: true,
35
- get: function () {
36
- return _exodusEthServer[key];
37
- }
38
- });
39
- });
40
-
41
- function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
42
-
43
- function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
@@ -1,10 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- /* global WebSocket */
9
- var _default = WebSocket;
10
- exports.default = _default;