Skip to content

Commit f8143ff

Browse files
committed
style(addresses): [refactor] Remove extra methods in lieu of single getBalance multipurpose method
1 parent 302889b commit f8143ff

File tree

10 files changed

+1250
-2134
lines changed

10 files changed

+1250
-2134
lines changed

dist/web3data.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/api.md

Lines changed: 1 addition & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ Returns null if no address is found.
412412
| ---- | ---- | ----------- | -------- |
413413
| hash | `string` | The address of the account. |   |
414414
| filterOptions | `object` | The filter options associated with the request. See [docs](https://docs.amberdata.io/reference#getcurrentaccountbalance) for more details. | *Optional* |
415+
| filterOptions.includeTokens=false | `object` | Return the token balances that the address is holding. | *Optional* |
415416

416417

417418

@@ -432,177 +433,6 @@ const balance = await web3data.address.getBalance('0x06012c8cf97bead5deae237070f
432433

433434

434435

435-
#### getLatestBalance(hash[, filterOptions])
436-
437-
438-
Retrieves the latest balance data of the given address. Returns null if no address is found.
439-
440-
441-
442-
443-
##### Parameters
444-
445-
| Name | Type | Description | |
446-
| ---- | ---- | ----------- | -------- |
447-
| hash | `string` | The address of the account. |   |
448-
| filterOptions | `object` | The filter options associated with the request. See [docs](https://docs.amberdata.io/reference#getcurrentaccountbalance) for more details. | *Optional* |
449-
450-
451-
452-
453-
##### Examples
454-
455-
```javascript
456-
const latestBalance = await web3data.address.getLatestBalance('0x06012c8cf97bead5deae237070f9587f8e7a266d')
457-
```
458-
459-
460-
##### Returns
461-
462-
463-
- `Promise.<object>` The balance data of the account or if no address is found.
464-
465-
466-
467-
468-
469-
#### getHistoricalBalance(hash[, filterOptions])
470-
471-
472-
Retrieves the historical balance data of the given address. Returns null if no address is found.
473-
474-
475-
476-
477-
##### Parameters
478-
479-
| Name | Type | Description | |
480-
| ---- | ---- | ----------- | -------- |
481-
| hash | `string` | The address of the account. | &nbsp; |
482-
| filterOptions | `object` | The filter options associated with the request. See [docs](https://docs.amberdata.io/reference#gethistoricalaccountbalance) for more details. | *Optional* |
483-
484-
485-
486-
487-
##### Examples
488-
489-
```javascript
490-
const historicalBalance = await web3data.address.getHistoricalBalance('0x06012c8cf97bead5deae237070f9587f8e7a266d')
491-
```
492-
493-
494-
##### Returns
495-
496-
497-
- `Promise.<object>` The historical balance data of the account or if no address is found.
498-
499-
500-
501-
502-
503-
#### getMultipleBalances(hashes[, filterOptions])
504-
505-
506-
Retrieves the latest account and token balances for the specified address(es).
507-
508-
509-
510-
511-
##### Parameters
512-
513-
| Name | Type | Description | |
514-
| ---- | ---- | ----------- | -------- |
515-
| hashes | `Array` | The array or string containing the address(es) of the account. | &nbsp; |
516-
| filterOptions | `object` | The filter options associated with the request. See [docs](https://docs.amberdata.io/reference#accountbalances) for more details. | *Optional* |
517-
518-
519-
520-
521-
##### Examples
522-
523-
```javascript
524-
const balances = await web3data.address.getMultipleBalances(['0x06012c8cf97bead5deae237070f9587f8e7a266d', '0x3f5ce5fbfe3e9af3971dd833d26ba9b5c936f0be'])
525-
```
526-
527-
528-
##### Returns
529-
530-
531-
- `Promise.<object>` The balance data of the account(s).
532-
533-
534-
535-
536-
537-
#### getBalances(hash[, filterOptions])
538-
539-
540-
Retrieves the latest account and token balances for the specified address.
541-
542-
543-
544-
545-
##### Parameters
546-
547-
| Name | Type | Description | |
548-
| ---- | ---- | ----------- | -------- |
549-
| hash | `string` | The address of the account. | &nbsp; |
550-
| filterOptions | `object` | The filter options associated with the request. See [docs](https://docs.amberdata.io/reference#accountbalances) for more details. | *Optional* |
551-
552-
553-
554-
555-
##### Examples
556-
557-
```javascript
558-
const balances = await web3data.address.getBalances('0x06012c8cf97bead5deae237070f9587f8e7a266d')
559-
```
560-
561-
562-
##### Returns
563-
564-
565-
- `Promise.<object>` The balance data of the account.
566-
567-
568-
569-
570-
571-
#### getBalancesBatch(hashes[, filterOptions])
572-
573-
574-
Retrieves the latest account and token balances for the specified addresses.
575-
576-
577-
578-
579-
##### Parameters
580-
581-
| Name | Type | Description | |
582-
| ---- | ---- | ----------- | -------- |
583-
| hashes | `string` | The array containing the address(es) of the account. | &nbsp; |
584-
| filterOptions | `object` | The filter options associated with the request. See [docs](https://docs.amberdata.io/reference#accountbalancesbatch) for more details. | *Optional* |
585-
586-
587-
588-
589-
##### Examples
590-
591-
```javascript
592-
const balanceBatch = await web3data.addresses.getBalancesBatch(['0x3f5ce5fbfe3e9af3971dd833d26ba9b5c936f0be', '0x06012c8cf97bead5deae237070f9587f8e7a266d'], { includePrice: true
593-
})
594-
```
595-
596-
597-
##### Returns
598-
599-
600-
- `Promise.<object>` The balance data of the account(s).
601-
602-
603-
604-
605-
606436
#### getTokens(hash[, filterOptions])
607437

608438

src/address.js

Lines changed: 11 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -214,110 +214,27 @@ Returns null if no address is found.
214214
*
215215
* @param {string} hash - The address of the account.
216216
* @param {object} [filterOptions] - The filter options associated with the request. See [docs](https://docs.amberdata.io/reference#get-current-account-balance) for more details.
217+
* @param {object} [filterOptions.includeTokens=false] - Return the token balances that the address is holding.
217218
* @returns {Promise<object>} The balance data of the account or if no address is found.
218219
* @example
219220
* const balance = await web3data.address.getBalance('0x06012c8cf97bead5deae237070f9587f8e7a266d')
220221
*/
221222
getBalance(hash, filterOptions = {}) {
222223
throwIf(is.notHash(hash), NO_ADDRESS)
223-
return filterOptions.startDate || filterOptions.endDate
224-
? this.web3data.address
225-
.getHistoricalBalance(hash, filterOptions)
226-
.then(data => data)
227-
: this.web3data.address
228-
.getLatestBalance(hash, filterOptions)
229-
.then(data => data)
230-
}
224+
let subendpoint = 'information'
231225

232-
/**
233-
* Retrieves the latest balance data of the given address. Returns null if no address is found.
234-
*
235-
* @param {string} hash - The address of the account.
236-
* @param {object} [filterOptions] - The filter options associated with the request. See [docs](https://docs.amberdata.io/reference#get-current-account-balance) for more details.
237-
* @returns {Promise<object>} The balance data of the account or if no address is found.
238-
* @example
239-
* const latestBalance = await web3data.address.getLatestBalance('0x06012c8cf97bead5deae237070f9587f8e7a266d')
240-
*/
241-
getLatestBalance(hash, filterOptions = {}) {
242-
throwIf(is.notHash(hash), NO_ADDRESS)
243-
return get(this.web3data, {
244-
hash,
245-
endpoint: ENDPOINT,
246-
subendpoint: 'account-balances/latest',
247-
filterOptions
248-
}).then(onFulfilled, onError)
249-
}
226+
if (Array.isArray(hash)) {
227+
subendpoint = 'balances'
228+
filterOptions.addresses = hash
229+
} else if (filterOptions.includeTokens) subendpoint = 'balances'
230+
else if (filterOptions.startDate || filterOptions.endDate)
231+
subendpoint = 'account-balances/historical'
250232

251-
/**
252-
* Retrieves the historical balance data of the given address. Returns null if no address is found.
253-
*
254-
* @param {string} hash - The address of the account.
255-
* @param {object} [filterOptions] - The filter options associated with the request. See [docs](https://docs.amberdata.io/reference#get-historical-account-balance) for more details.
256-
* @returns {Promise<object>} The historical balance data of the account or if no address is found.
257-
* @example
258-
* const historicalBalance = await web3data.address.getHistoricalBalance('0x06012c8cf97bead5deae237070f9587f8e7a266d')
259-
*/
260-
getHistoricalBalance(hash, filterOptions = {}) {
261-
throwIf(is.notHash(hash), NO_ADDRESS)
262-
return get(this.web3data, {
263-
hash,
264-
endpoint: ENDPOINT,
265-
subendpoint: 'account-balances/historical',
266-
filterOptions
267-
}).then(onFulfilled, onError)
268-
}
269-
270-
/**
271-
* Retrieves the latest account and token balances for the specified address(es).
272-
*
273-
* @param {Array} hashes - The array or string containing the address(es) of the account.
274-
* @param {object} [filterOptions] - The filter options associated with the request. See [docs](https://docs.amberdata.io/reference#account-balances) for more details.
275-
* @returns {Promise<object>} The balance data of the account(s).
276-
* @example
277-
* const balances = await web3data.address.getMultipleBalances(['0x06012c8cf97bead5deae237070f9587f8e7a266d', '0x3f5ce5fbfe3e9af3971dd833d26ba9b5c936f0be'])
278-
*/
279-
getMultipleBalances(hashes, filterOptions = {}) {
280-
return Array.isArray(hashes)
281-
? this.web3data.address.getBalancesBatch(hashes, filterOptions)
282-
: this.web3data.address.getBalances(hashes, filterOptions)
283-
}
284-
285-
/**
286-
* Retrieves the latest account and token balances for the specified address.
287-
*
288-
* @param {string} hash - The address of the account.
289-
* @param {object} [filterOptions] - The filter options associated with the request. See [docs](https://docs.amberdata.io/reference#account-balances) for more details.
290-
* @returns {Promise<object>} The balance data of the account.
291-
* @example
292-
* const balances = await web3data.address.getBalances('0x06012c8cf97bead5deae237070f9587f8e7a266d')
293-
*/
294-
getBalances(hash, filterOptions = {}) {
295-
throwIf(is.notHash(hash), NO_ADDRESS)
296-
return get(this.web3data, {
297-
hash,
298-
endpoint: ENDPOINT,
299-
subendpoint: 'balances',
300-
filterOptions
301-
}).then(onFulfilled, onError)
302-
}
303-
304-
/**
305-
* Retrieves the latest account and token balances for the specified addresses.
306-
*
307-
* @param {string} hashes - The array containing the address(es) of the account.
308-
* @param {object} [filterOptions] - The filter options associated with the request. See [docs](https://docs.amberdata.io/reference#account-balances-batch) for more details.
309-
* @returns {Promise<object>} The balance data of the account(s).
310-
* @example const balanceBatch = await web3data.addresses.getBalancesBatch(['0x3f5ce5fbfe3e9af3971dd833d26ba9b5c936f0be', '0x06012c8cf97bead5deae237070f9587f8e7a266d'], {
311-
* includePrice: true
312-
* })
313-
*/
314-
getBalancesBatch(hashes, filterOptions = {}) {
315-
throwIf(!Array.isArray(hashes), 'Must be array of valid address hashes')
316-
hashes.forEach(hash => throwIf(is.notHash(hash), NO_ADDRESS))
317-
filterOptions.addresses = hashes
318233
return get(this.web3data, {
234+
// Include 'hash' only if filterOptions.addresses not defined
235+
...(filterOptions.addresses ? {} : {hash}),
319236
endpoint: ENDPOINT,
320-
subendpoint: 'balances',
237+
subendpoint,
321238
filterOptions
322239
}).then(onFulfilled, onError)
323240
}

src/constants.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ module.exports.ETH_METHODS = {
6060
getTransactions: 'block',
6161
getTransaction: 'transaction',
6262
getPendingTransactions: 'transaction',
63-
getEtherPrice: 'market'
63+
getEtherPrice: 'market',
64+
getBalance: 'address'
6465
}
6566

6667
module.exports.BTC_METHODS = {
@@ -71,10 +72,6 @@ module.exports.BTC_METHODS = {
7172
'getTransactions',
7273
'getPendingTransactions',
7374
'getBalance',
74-
'getLatestBalance',
75-
'getHistoricalBalance',
76-
'getMultipleBalances',
77-
'getBalancesBatch',
7875
'getMetrics'
7976
],
8077
block: [
@@ -96,10 +93,6 @@ module.exports.LTC_METHODS = {
9693
'getTransactions',
9794
'getPendingTransactions',
9895
'getBalance',
99-
'getLatestBalance',
100-
'getHistoricalBalance',
101-
'getMultipleBalances',
102-
'getBalancesBatch',
10396
'getMetrics'
10497
],
10598
block: [

src/eth.js

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const {throwIf, ethFactory} = require('./utils')
1+
const {ethFactory} = require('./utils')
22
const {BLOCKCHAIN_ID_ETHEREUM_MAINNET: BLOCKCHAIN_ID} = require('./constants')
33

44
/**
@@ -22,25 +22,6 @@ class Eth {
2222
}
2323
}
2424

25-
/**
26-
* Returns the balance of the account in wei or null if the account doesn't
27-
exist.
28-
*
29-
* @param hash - The address of the account.
30-
* @returns Promise<object> - The balance or null if it doesn't exist.
31-
* @example
32-
*/
33-
async getBalance(hash) {
34-
let balance
35-
try {
36-
balance = await this.web3data.address.getBalance(hash)
37-
} catch (error) {
38-
throwIf(true, error.message)
39-
}
40-
41-
return balance && balance.value ? balance.value : null
42-
}
43-
4425
/* See Web3Data class for details on rpc method */
4526
rpc(method, params) {
4627
return this.web3data.rpc(method, params)

src/web3data.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const WebSocketClient = require('./websocket')
2424

2525
/**
2626
* Contains common methods used in.
27+
*
2728
* @private
2829
*/
2930
class Web3DataFactory {

0 commit comments

Comments
 (0)