Skip to content

Commit 4ef5266

Browse files
committed
style(transactions): [refactor] standardize methods implementation + responses
1 parent 624edf2 commit 4ef5266

File tree

7 files changed

+90
-122
lines changed

7 files changed

+90
-122
lines changed

dist/web3data.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/web3data.min.js.map

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: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ const orders = await web3data.market.getOrders('eth_usd', ['bitfinex', 'bitstamp
729729

730730

731731

732-
#### getOrderBooks(pair[, filterOptions])
732+
#### getOrderBooks(pair, filterOptions)
733733

734734

735735
Retrieves order book update events.
@@ -741,8 +741,8 @@ Retrieves order book update events.
741741

742742
| Name | Type | Description | |
743743
| ---- | ---- | ----------- | -------- |
744-
| pair | `string` | The market pair for which to retrieve the historical best bid and offer data. |   |
745-
| filterOptions | `object` | See [docs](https://docs.amberdata.io/reference#orderbookupdates) for complete list of filters. | *Optional* |
744+
| pair | | The market pair for which to retrieve the historical best bid and offer data. |   |
745+
| filterOptions | | See [docs](https://docs.amberdata.io/reference#orderbookupdates) for complete list of filters. |   |
746746

747747

748748

@@ -982,6 +982,49 @@ const batTokenAddress = web3data.market.getAssetAddresses('bat') const assetAddr
982982

983983

984984

985+
### src/utils.js
986+
987+
988+
989+
#### get(web3data, subendpoint, endpoint, hash, pathParam, filterOptions)
990+
991+
992+
Builds the endpoint url to pass to .rawQuery(). Checks for non empties and appends
993+
the appropriate parameter(s) where applicable.
994+
995+
996+
997+
998+
##### Parameters
999+
1000+
| Name | Type | Description | |
1001+
| ---- | ---- | ----------- | -------- |
1002+
| web3data | | Instance on which to call .rawQuery(). |   |
1003+
| subendpoint | | The subendpoint. |   |
1004+
| endpoint | | The endpoint. |   |
1005+
| hash | | The address hash. |   |
1006+
| pathParam | | The path parameter. |   |
1007+
| filterOptions | | The filters associated with a given endpoint. |   |
1008+
1009+
1010+
1011+
1012+
##### Examples
1013+
1014+
```javascript
1015+
1016+
```
1017+
1018+
1019+
##### Returns
1020+
1021+
1022+
- Returns a Promise of the rawQuery request from web3data.
1023+
1024+
1025+
1026+
1027+
9851028
### src/web3data.js
9861029

9871030

@@ -1118,49 +1161,6 @@ API endpoints.
11181161

11191162

11201163

1121-
### src/utils.js
1122-
1123-
1124-
1125-
#### get(web3data, subendpoint, endpoint, hash, pathParam, filterOptions)
1126-
1127-
1128-
Builds the endpoint url to pass to .rawQuery(). Checks for non empties and appends
1129-
the appropriate parameter(s) where applicable.
1130-
1131-
1132-
1133-
1134-
##### Parameters
1135-
1136-
| Name | Type | Description | |
1137-
| ---- | ---- | ----------- | -------- |
1138-
| web3data | | Instance on which to call .rawQuery(). |   |
1139-
| subendpoint | | The subendpoint. |   |
1140-
| endpoint | | The endpoint. |   |
1141-
| hash | | The address hash. |   |
1142-
| pathParam | | The path parameter. |   |
1143-
| filterOptions | | The filters associated with a given endpoint. |   |
1144-
1145-
1146-
1147-
1148-
##### Examples
1149-
1150-
```javascript
1151-
1152-
```
1153-
1154-
1155-
##### Returns
1156-
1157-
1158-
- Returns a Promise of the rawQuery request from web3data.
1159-
1160-
1161-
1162-
1163-
11641164
### src/websocket.js
11651165

11661166

src/market.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ class Market {
159159
/**
160160
* Retrieves order book update events.
161161
*
162-
* @param {string} pair - The market pair for which to retrieve the historical best bid and offer data.
163-
* @param {object} [filterOptions] - See [docs](https://docs.amberdata.io/reference#order-book-updates) for complete list of filters.
162+
* @param pair - The market pair for which to retrieve the historical best bid and offer data.
163+
* @param [filterOptions] - See [docs](https://docs.amberdata.io/reference#order-book-updates) for complete list of filters.
164164
* @returns The order book update data.
165165
* @example const orderBooks = await web3data.market.getOrderBooks('btc_usd')
166166
*/

src/transaction.js

Lines changed: 27 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,80 +3,56 @@ const {
33
ERROR_MESSAGE_TRANSACTION_NO_HASH: NO_HASH
44
} = require('./constants')
55

6-
const {is, get, onFulfilled, onError} = require('./utils')
6+
const {is, get, onFulfilled, onError, throwIf} = require('./utils')
77

88
class Transaction {
99
constructor(web3data) {
1010
this.web3data = web3data
1111
}
1212

13-
async getTransactions(filterOptions) {
14-
const response = await get(this.web3data, {
13+
getTransactions(filterOptions) {
14+
return get(this.web3data, {
1515
endpoint: ENDPOINT,
1616
filterOptions
17-
})
18-
return new Promise((resolve, reject) => {
19-
if (
20-
!response ||
21-
response.status !== 200 ||
22-
!response.payload ||
23-
!response.payload.records
24-
) {
25-
reject(new Error('Failed to retrieve transactions.'))
26-
} else {
27-
resolve(response.payload.records)
28-
}
29-
})
17+
}).then(onFulfilled, onError)
3018
}
3119

32-
async getTransaction(hash, filterOptions) {
33-
if (!hash) return Promise.reject(new Error(NO_HASH))
34-
const response = await get(this.web3data, {
20+
getTransaction(hash, filterOptions) {
21+
throwIf(is.notHash(hash), NO_HASH)
22+
return get(this.web3data, {
3523
pathParam: hash,
3624
endpoint: ENDPOINT,
3725
filterOptions
38-
})
39-
return new Promise((resolve, reject) => {
40-
if (!response || response.status !== 200 || !response.payload) {
41-
reject(new Error('Failed to retrieve transaction.'))
42-
} else {
43-
resolve(response.payload)
44-
}
45-
})
26+
}).then(onFulfilled, onError)
4627
}
4728

48-
async getPendingTransactions() {
49-
const pendingTransactions = await this.getTransactions({status: 'pending'})
50-
return new Promise((resolve, reject) => {
51-
if (is.undefined(pendingTransactions) || is.null(pendingTransactions)) {
52-
reject(new Error('Failed to retrieve pending transactions.'))
53-
} else {
54-
resolve(pendingTransactions)
55-
}
56-
})
29+
getPendingTransactions() {
30+
return this.getTransactions({status: 'pending'}).then(
31+
pendingTransactions => {
32+
throwIf(
33+
is.undefined(pendingTransactions) || is.null(pendingTransactions),
34+
'Failed to retrieve pending transactions.'
35+
)
36+
return pendingTransactions
37+
},
38+
console.error
39+
)
5740
}
5841

5942
getGasPrediction() {
6043
return get(this.web3data, {
6144
endpoint: ENDPOINT,
6245
subendpoint: 'gas/predictions'
63-
})
46+
}).then(onFulfilled, onError)
6447
}
6548

66-
async getGasPrice() {
67-
const response = await this.getGasPrediction()
68-
return new Promise((resolve, reject) => {
69-
if (
70-
is.null(response) ||
71-
is.undefined(response) ||
72-
response.status !== 200
73-
) {
74-
reject(new Error('Failed to retrieve gas price.'))
75-
} else if (!response.payload || !response.payload.average) {
76-
reject(new Error('Failed to retrieve gas price.'))
77-
} else {
78-
resolve(`${response.payload.average.gasPrice}`)
79-
}
49+
getGasPrice() {
50+
return this.getGasPrediction().then(gasPrediction => {
51+
throwIf(
52+
!gasPrediction.average || !gasPrediction.average.gasPrice,
53+
'Failed to retrieve gas price.'
54+
)
55+
return `${gasPrediction.average.gasPrice}`
8056
})
8157
}
8258

0 commit comments

Comments
 (0)