@dashevo/dapi-client 1.3.0-dev.5 → 1.3.0-dev.6

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.
@@ -15,6 +15,7 @@ const getIdentityNonceFactory = require('./getIdentityNonce/getIdentityNonceFact
15
15
  const getIdentityKeysFactory = require('./getIdentityKeys/getIdentityKeysFactory');
16
16
  const getTotalCreditsInPlatformFactory = require('./getTotalCreditsInPlatform/getTotalCreditsInPlatformFactory');
17
17
  const getStatusFactory = require('./getStatus/getStatusFactory');
18
+ const getIdentityBalanceFactory = require('./getIdentityBalance/getIdentityBalanceFactory');
18
19
 
19
20
  class PlatformMethodsFacade {
20
21
  /**
@@ -40,6 +41,7 @@ class PlatformMethodsFacade {
40
41
  this.getIdentityKeys = getIdentityKeysFactory(grpcTransport);
41
42
  this.getTotalCreditsInPlatform = getTotalCreditsInPlatformFactory(grpcTransport);
42
43
  this.getStatus = getStatusFactory(grpcTransport);
44
+ this.getIdentityBalance = getIdentityBalanceFactory(grpcTransport);
43
45
  }
44
46
  }
45
47
 
@@ -28,7 +28,7 @@ function getEpochsInfoFactory(grpcTransport) {
28
28
  const getEpochInfosRequest = new GetEpochsInfoRequest();
29
29
  getEpochInfosRequest.setV0(
30
30
  new GetEpochsInfoRequestV0()
31
- .setStartEpoch(new UInt32Value([startEpoch]))
31
+ .setStartEpoch(typeof startEpoch === 'number' ? new UInt32Value([startEpoch]) : undefined)
32
32
  .setCount(count)
33
33
  .setAscending(!!options.ascending)
34
34
  .setProve(!!options.prove),
@@ -0,0 +1,43 @@
1
+ const AbstractResponse = require('../response/AbstractResponse');
2
+ const InvalidResponseError = require('../response/errors/InvalidResponseError');
3
+
4
+ class GetIdentityBalanceResponse extends AbstractResponse {
5
+ /**
6
+ * @param {number} balance
7
+ * @param {Metadata} metadata
8
+ * @param {Proof} [proof]
9
+ */
10
+ constructor(balance, metadata, proof = undefined) {
11
+ super(metadata, proof);
12
+
13
+ this.balance = balance;
14
+ }
15
+
16
+ /**
17
+ * @returns {number}
18
+ */
19
+ getBalance() {
20
+ return this.balance;
21
+ }
22
+
23
+ /**
24
+ * @param proto
25
+ * @returns {GetIdentityBalanceResponse}
26
+ */
27
+ static createFromProto(proto) {
28
+ const balance = proto.getV0().getBalance();
29
+ const { metadata, proof } = AbstractResponse.createMetadataAndProofFromProto(proto);
30
+
31
+ if ((balance === null || balance === undefined) && !proof) {
32
+ throw new InvalidResponseError('Balance is not defined');
33
+ }
34
+
35
+ return new GetIdentityBalanceResponse(
36
+ balance,
37
+ metadata,
38
+ proof,
39
+ );
40
+ }
41
+ }
42
+
43
+ module.exports = GetIdentityBalanceResponse;
@@ -0,0 +1,71 @@
1
+ const {
2
+ v0: {
3
+ PlatformPromiseClient,
4
+ GetIdentityBalanceRequest,
5
+ },
6
+ } = require('@dashevo/dapi-grpc');
7
+
8
+ const GetIdentityBalanceResponse = require('./GetIdentityBalanceResponse');
9
+ const InvalidResponseError = require('../response/errors/InvalidResponseError');
10
+
11
+ /**
12
+ * @param {GrpcTransport} grpcTransport
13
+ * @returns {getIdentityBalance}
14
+ */
15
+ function getIdentityBalanceFactory(grpcTransport) {
16
+ /**
17
+ * Fetch the identity balance by id
18
+ * @typedef {getIdentityBalance}
19
+ * @param {Buffer} id
20
+ * @param {DAPIClientOptions & {prove: boolean}} [options]
21
+ * @returns {Promise<GetIdentityBalanceResponse>}
22
+ */
23
+ async function getIdentityBalance(id, options = {}) {
24
+ const { GetIdentityBalanceRequestV0 } = GetIdentityBalanceRequest;
25
+ const getIdentityBalanceRequest = new GetIdentityBalanceRequest();
26
+ // need to convert objects inherited from Buffer to pure buffer as google protobuf
27
+ // doesn't support extended buffers
28
+ // https://github.com/protocolbuffers/protobuf/blob/master/js/binary/utils.js#L1049
29
+ if (Buffer.isBuffer(id)) {
30
+ // eslint-disable-next-line no-param-reassign
31
+ id = Buffer.from(id);
32
+ }
33
+
34
+ getIdentityBalanceRequest.setV0(
35
+ new GetIdentityBalanceRequestV0()
36
+ .setId(id)
37
+ .setProve(!!options.prove),
38
+ );
39
+
40
+ let lastError;
41
+
42
+ // TODO: simple retry before the dapi versioning is properly implemented
43
+ for (let i = 0; i < 3; i += 1) {
44
+ try {
45
+ // eslint-disable-next-line no-await-in-loop
46
+ const getIdentityBalanceResponse = await grpcTransport.request(
47
+ PlatformPromiseClient,
48
+ 'getIdentityBalance',
49
+ getIdentityBalanceRequest,
50
+ options,
51
+ );
52
+
53
+ return GetIdentityBalanceResponse.createFromProto(getIdentityBalanceResponse);
54
+ } catch (e) {
55
+ if (e instanceof InvalidResponseError) {
56
+ lastError = e;
57
+ } else {
58
+ throw e;
59
+ }
60
+ }
61
+ }
62
+
63
+ // If we made it past the cycle it means that the retry didn't work,
64
+ // and we're throwing the last error encountered
65
+ throw lastError;
66
+ }
67
+
68
+ return getIdentityBalance;
69
+ }
70
+
71
+ module.exports = getIdentityBalanceFactory;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dashevo/dapi-client",
3
- "version": "1.3.0-dev.5",
3
+ "version": "1.3.0-dev.6",
4
4
  "description": "Client library used to access Dash DAPI endpoints",
5
5
  "main": "lib/index.js",
6
6
  "contributors": [
@@ -26,11 +26,11 @@
26
26
  }
27
27
  ],
28
28
  "dependencies": {
29
- "@dashevo/dapi-grpc": "1.3.0-dev.5",
30
- "@dashevo/dash-spv": "2.3.0-dev.5",
29
+ "@dashevo/dapi-grpc": "1.3.0-dev.6",
30
+ "@dashevo/dash-spv": "2.3.0-dev.6",
31
31
  "@dashevo/dashcore-lib": "~0.21.3",
32
- "@dashevo/grpc-common": "1.3.0-dev.5",
33
- "@dashevo/wasm-dpp": "1.3.0-dev.5",
32
+ "@dashevo/grpc-common": "1.3.0-dev.6",
33
+ "@dashevo/wasm-dpp": "1.3.0-dev.6",
34
34
  "bs58": "^4.0.1",
35
35
  "cbor": "^8.0.0",
36
36
  "google-protobuf": "^3.12.2",