-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
128 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...ages/js-dapi-client/lib/methods/platform/getIdentityBalance/GetIdentityBalanceResponse.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
const AbstractResponse = require('../response/AbstractResponse'); | ||
const InvalidResponseError = require('../response/errors/InvalidResponseError'); | ||
|
||
class GetIdentityBalanceResponse extends AbstractResponse { | ||
/** | ||
* @param {Buffer} identity | ||
* @param {Metadata} metadata | ||
* @param {Proof} [proof] | ||
*/ | ||
constructor(balance, metadata, proof = undefined) { | ||
super(metadata, proof); | ||
|
||
this.balance = balance; | ||
} | ||
|
||
/** | ||
* @returns {Buffer} | ||
*/ | ||
getIdentity() { | ||
return this.balance; | ||
} | ||
|
||
/** | ||
* @param proto | ||
* @returns {GetIdentityBalanceResponse} | ||
*/ | ||
static createFromProto(proto) { | ||
const balance = proto.getV0().getBalance(); | ||
const { metadata, proof } = AbstractResponse.createMetadataAndProofFromProto(proto); | ||
|
||
if (!balance && !proof) { | ||
throw new InvalidResponseError('Balance is not defined'); | ||
} | ||
|
||
return new GetIdentityBalanceResponse( | ||
balance, | ||
metadata, | ||
proof, | ||
); | ||
} | ||
} | ||
|
||
module.exports = GetIdentityBalanceResponse; |
71 changes: 71 additions & 0 deletions
71
packages/js-dapi-client/lib/methods/platform/getIdentityBalance/getIdentityBalanceFactory.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
const { | ||
v0: { | ||
PlatformPromiseClient, | ||
GetIdentityBalanceRequest, | ||
}, | ||
} = require('@dashevo/dapi-grpc'); | ||
|
||
const GetIdentityBalanceResponse = require('./GetIdentityBalanceResponse'); | ||
const InvalidResponseError = require('../response/errors/InvalidResponseError'); | ||
|
||
/** | ||
* @param {GrpcTransport} grpcTransport | ||
* @returns {getIdentityBalance} | ||
*/ | ||
function getIdentityBalanceFactory(grpcTransport) { | ||
/** | ||
* Fetch the identity balance by id | ||
* @typedef {getIdentityBalance} | ||
* @param {Buffer} id | ||
* @param {DAPIClientOptions & {prove: boolean}} [options] | ||
* @returns {Promise<GetIdentityBalanceResponse>} | ||
*/ | ||
async function getIdentityBalance(id, options = {}) { | ||
const { GetIdentityBalanceRequestV0 } = GetIdentityBalanceRequest; | ||
const getIdentityBalanceRequest = new GetIdentityBalanceRequest(); | ||
// need to convert objects inherited from Buffer to pure buffer as google protobuf | ||
// doesn't support extended buffers | ||
// https://github.com/protocolbuffers/protobuf/blob/master/js/binary/utils.js#L1049 | ||
if (Buffer.isBuffer(id)) { | ||
// eslint-disable-next-line no-param-reassign | ||
id = Buffer.from(id); | ||
} | ||
|
||
getIdentityBalanceRequest.setV0( | ||
new GetIdentityBalanceRequestV0() | ||
.setId(id) | ||
.setProve(!!options.prove), | ||
); | ||
|
||
let lastError; | ||
|
||
// TODO: simple retry before the dapi versioning is properly implemented | ||
for (let i = 0; i < 3; i += 1) { | ||
try { | ||
// eslint-disable-next-line no-await-in-loop | ||
const getIdentityBalanceResponse = await grpcTransport.request( | ||
PlatformPromiseClient, | ||
'getIdentityBalance', | ||
getIdentityBalanceRequest, | ||
options, | ||
); | ||
|
||
return GetIdentityBalanceResponse.createFromProto(getIdentityBalanceResponse); | ||
} catch (e) { | ||
if (e instanceof InvalidResponseError) { | ||
lastError = e; | ||
} else { | ||
throw e; | ||
} | ||
} | ||
} | ||
|
||
// If we made it past the cycle it means that the retry didn't work, | ||
// and we're throwing the last error encountered | ||
throw lastError; | ||
} | ||
|
||
return getIdentityBalance; | ||
} | ||
|
||
module.exports = getIdentityBalanceFactory; |